Support JWT for Client Authentication
This feature will partially implement JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and Authorization Grants.
Section 2.2. Using JWTs for Client Authentication will be the focus for this feature implementation.
NOTE: This ticket addresses client-side support only.
Related #6881 #6053
Cool. Authenticating with Azure AD using a certificate uses this flow, afaics. This feature will help us executing that flow cleanly. Thx.
I believe that this would also provide better support for "Sign In With Apple", as Apple uses a non-static client secret, which in their case must be the string representation of a JWT (seemingly compliant with the JWT profile covered in the issue) with an expiration date of at most 6 months in the future (see https://developer.apple.com/documentation/sign_in_with_apple/generate_and_validate_tokens).
@thomas-corte Thanks for pointing that out. Indeed, Apple does support private_key_jwt client authentication so this feature will be needed.
FYI, we are targeting this for 5.5 but we're dependent on merging #9208 first, which we are getting close.
Not sure whether this helps (as #9208 seems to aim at using the Nimbus libraries), but for comparison, I was able to successfully create a working secret for Apple using this code (which utilizes BouncyCastle and the io.jsonwebtoken library):
import java.util.Date; import java.util.HashMap; import java.util.Map;
import java.io.Reader; import java.io.StringReader;
import java.security.Key; import java.security.PrivateKey;
import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
...
JwtBuilder jwtBuilder = Jwts.builder ();
String keyBase64 = "-----BEGIN PRIVATE KEY-----\n" +
"<base64 encoded private key data from Apple>" +
"-----END PRIVATE KEY-----";
final Reader pemReader = new StringReader (keyBase64);
final PEMParser pemParser = new PEMParser (pemReader);
final JcaPEMKeyConverter converter = new JcaPEMKeyConverter ();
final PrivateKeyInfo object = (PrivateKeyInfo) pemParser.readObject ();
final PrivateKey pKey = converter.getPrivateKey (object);
Map<String, Object> headers = new HashMap<> ();
headers.put ("kid", "WTXXXXXX"); // Key ID from Apple
headers.put ("alg", "ES256");
jwtBuilder.setHeader (headers);
String clientSecret = jwtBuilder.
setIssuer ("KVXXXXXXXX"). // Team ID from Apple
setIssuedAt (new Date (System.currentTimeMillis ())).
setExpiration (new Date (System.currentTimeMillis () + 180l * 86_400_000l)). // 6 months max
setAudience ("https://appleid.apple.com").
setSubject ("com.my.app.name").
signWith (SignatureAlgorithm.ES256, pKey).compact ();
Thanks for the sample @thomas-corte. We will be using Nimbus for implementation.
Any updates on this?
@willladislaw I'm starting work on this next week.
@roelal @thomas-corte @willladislaw Please see #9520.
It would be greatly appreciated if you can test out the initial implementation with the provider(s) you are using. Please let me know if the new API provides the flexibility you require for your use case(s).
FYI, I need to get this merged before April 12 for 5.5.0-RC1, so your prompt response would be a huge help.
Thank you.
@roelal @thomas-corte @willladislaw Please see #9520.
It would be greatly appreciated if you can test out the initial implementation with the provider(s) you are using.
I tried to download the sources from your branch and build it with Gradle, but I'm not having sufficient access to the required dependencies (Received status code 401 from server: Unauthorized), probably related to this issue: https://stackoverflow.com/questions/64839144/got-an-error-when-building-spring-security-from-source
@jgrandja
I would like to help out with testing support for 'Private Key JWT Client Authentication'.
The Australian Government's Trusted Digital Identity Framework requires Relying Party's to use Private Key JWT Client Authentication.
Ref:
- https://www.dta.gov.au/our-projects/digital-identity/trusted-digital-identity-framework
- https://relying-party-technical-integration-guide.apps.y.cld.gov.au/
Thanks for your offer @Robinyo. Feel free to test the implementation in #9520 with the Australian Government's Identity system.
Let me know how it goes. Thanks.
Maven repository unavailable. How do I add this dependency? (Spring Boot)
@willladislaw Spring Security 5.5.0-RC1 is only available in https://repo.spring.io. After 5.5.0 goes GA then it's available in Maven Central.
See the Spring Boot reference on how to setup the Spring Maven repo.
How long before 5.5.0 GA (approx)? No other way to add it?
I'm eager to try this out, but I didn't get any further after I added the following to my config class (and removed clientSecret):
.clientAuthenticationMethod(ClientAuthenticationMethod.PRIVATE_KEY_JWT)
Should I customize any Spring Security class and sign the JWT by using Nimbus? In that case, in which class? Or is it possible to add the certificate along with the private key to an existing Spring Security class?
@willladislaw See milestone dates
@erlendfg See the main comment in gh-9520 as it points to a couple of tests that demonstrate configuration / usage.
I tried out this, but did not get it to work, seems like x5t:base64EncodedThumprintHere and typ:jwt parameters inside the client_assertion are not sent to to the Authentication provider at all.
@thomas-corte do you have a working example of how to configure the application.yaml and how to use a non-static client secret using the private key?
No, just what I wrote here: https://github.com/spring-projects/spring-security/issues/8175#issuecomment-784957091