spring-security icon indicating copy to clipboard operation
spring-security copied to clipboard

Support JWT for Client Authentication

Open jgrandja opened this issue 6 years ago • 33 comments

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

jgrandja avatar Mar 23 '20 19:03 jgrandja

Cool. Authenticating with Azure AD using a certificate uses this flow, afaics. This feature will help us executing that flow cleanly. Thx.

ghost avatar Apr 15 '20 11:04 ghost

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 avatar Feb 23 '21 17:02 thomas-corte

@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.

jgrandja avatar Feb 23 '21 18:02 jgrandja

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 ();

thomas-corte avatar Feb 24 '21 09:02 thomas-corte

Thanks for the sample @thomas-corte. We will be using Nimbus for implementation.

jgrandja avatar Feb 24 '21 10:02 jgrandja

Any updates on this?

willladislaw avatar Mar 17 '21 15:03 willladislaw

@willladislaw I'm starting work on this next week.

jgrandja avatar Mar 17 '21 17:03 jgrandja

@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.

jgrandja avatar Mar 24 '21 15:03 jgrandja

@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

erlendfg avatar Mar 30 '21 09:03 erlendfg

@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/

Robinyo avatar Apr 01 '21 21:04 Robinyo

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.

jgrandja avatar Apr 05 '21 11:04 jgrandja

Maven repository unavailable. How do I add this dependency? (Spring Boot)

willladislaw avatar Apr 17 '21 16:04 willladislaw

@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.

jgrandja avatar Apr 19 '21 16:04 jgrandja

How long before 5.5.0 GA (approx)? No other way to add it?

willladislaw avatar Apr 21 '21 04:04 willladislaw

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?

erlendfg avatar Apr 21 '21 09:04 erlendfg

@willladislaw See milestone dates

jgrandja avatar Apr 21 '21 10:04 jgrandja

@erlendfg See the main comment in gh-9520 as it points to a couple of tests that demonstrate configuration / usage.

jgrandja avatar Apr 21 '21 10:04 jgrandja

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.

marcerik avatar Jun 01 '21 10:06 marcerik

@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?

alan-czajkowski avatar Jan 06 '22 05:01 alan-czajkowski

No, just what I wrote here: https://github.com/spring-projects/spring-security/issues/8175#issuecomment-784957091

thomas-corte avatar Jan 06 '22 09:01 thomas-corte