0

How to verify HS256 ID Token using FusionAuth java Client libraries ?

Description: I have created an app in FusionAuth that has a Client ID and Client Secret generated, I have not touched any other section/tab like JWT etc, default JWT signing algo is OIDC standard HMAC SHA256.

I tried using following code approach to validate the token based on public key, but its not working for HS256 signed token, I searched over internet and found public key is not applicable for HS256

Could you please provide me a Java code to verify HS256 signed token using FusionAuth Java client libraries (https://github.com/FusionAuth/fusionauth-jwt). Please also let me know that do I need any additional configuration on FusionAuth Admin Console.

Code which I tried: It's giving blank public keys.

List<JSONWebKey> keys = JSONWebKeySetHelper.retrieveKeysFromJWKS("http://localhost:9011/.well-known/jwks.json");

Map<String, Verifier> publicKeyVerifiers = new HashMap<String, Verifier>();

JWT jwtDecoded = JWT.getDecoder().decode(idToken, publicKeyVerifiers);

2 Answers 2

2

Tokens received by apps should usually be signed with an asymmetric key. You can then verify the digital signature of the token with its signing public key, which is provided via the JWKS endpoint. The most mainstream algorithm for tokens is RS256. Perhaps you need to reconfigure as suggested here?. I think your code will then work also.

HS256 is a symmetric algorithm, which feels wrong, since clients would only be able to verify JWTs if they had the full signing key. This enables a malicious client to mint their own tokens, whereas only the Authorization Server should be able to do that. For this reason, if JWTs are ever signed with symmetric keys, the JWKS endpoint will not provide the token signing keys.

Meant to say Curity have a good related doc:

3
  • Hello Gary Archer - I have came across this topic earlier but I ignored it, thanks for pointing that and I will be starting some research on it with my team to know whether we should shift to RS256 or not. I have also asked this to @mooreds to know if he can help us to make that decision. Many thanks for your response. Feb 11, 2022 at 11:48
  • Great points Gary!
    – mooreds
    Feb 11, 2022 at 15:38
  • 1
    Cool - I added the above link in case useful to your team for future reference. Feb 11, 2022 at 17:59
1

It looks like there is an example in the README:

// Build an HMC verifier using the same secret that was used to sign the JWT
Verifier verifier = HMACVerifier.newVerifier("too many secrets");

// Verify and decode the encoded string JWT to a rich object
JWT jwt = JWT.getDecoder().decode(encodedJWT, verifier);

// Assert the subject of the JWT is as expected
assertEquals(jwt.subject, "f1e33ab3-027f-47c5-bb07-8dd8ab37a2d3");

https://github.com/FusionAuth/fusionauth-jwt#verify-and-decode-a-jwt-using-hmac

With HMAC signing you need to distribute the secret ("too many secrets" in the example above) to all locations which need to verify the JWT.

8
  • Many thanks for the reply, look likes I overlooked this section in readme. Could you please confirm that I need to put my client_secret in place of "too many secrets" ? Feb 10, 2022 at 16:55
  • Use whatever the default secret value is. You can find that in the admin ui in the key master section. Don't forget to accept the answer if it works for you.
    – mooreds
    Feb 10, 2022 at 17:25
  • Hello @mooreds - Thanks it worked & I accepted the answer. Some follow ups pls Question 1. if (jwtDecoded.audience.equals("<my client id>") && jwtDecoded.issuer.equals("www.acme.com") && (jwtDecoded.expiration.toEpochSecond() > (System.currentTimeMillis()/1000) )) { // valid id token } A] Value of Issuer coming without "www" jwtDecoded.issuer = acme.com B] Expiration time is a ZonedDateTime object, if FusionAuth and verification code is running on 2 different zoned servers then will it be problem by just using System time as it will be a local system time? Feb 11, 2022 at 11:37
  • 1
    It depends on your security profile and what you are doing with the tokens. In general I recommend using an asymmetric key as @gary does, because it does lower the risk of the signing key being stolen. However, if you have tight control over your token consumer too and speed of signing is really important, using HS256 might be better. I wrote more about this here: fusionauth.io/learn/expert-advice/tokens/building-a-secure-jwt
    – mooreds
    Feb 11, 2022 at 15:37
  • 1
    Hello @mooreds Thanks for the reply, I can do that. New to stackoverflow, still learning :-) Feb 16, 2022 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.