-
|
Hello, I have been trying to use JWE tokens for a while now but it's not working, thought it was easy since JWS worked perfectly but I keep getting the same error no matter what I do.
I would really appreciate any help or clarification on what I'm doing wrong here. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
|
Hi there! I'm not sure what is causing your exception to be honest. I just ran this test locally and it passed without throwing any exceptions: @Test
public void testIssue1050() {
final SecretKey generated = Jwts.ENC.A256GCM.key().build();
String encoded = Encoders.BASE64.encode(generated.getEncoded());
final SecretKey encryptionKey = new SecretKeySpec(Decoders.BASE64.decode(encoded), "AES");
String subject = "testUser";
long emailDurationMillis = 1000 * 60 * 10; // 10 minutes
String otp = "123456";
String jwe = Jwts.builder()
.issuer("My App")
.subject(subject)
.issuedAt(new Date(System.currentTimeMillis()))
.expiration(new Date(System.currentTimeMillis() + emailDurationMillis))
.claim("otp", otp)
.encryptWith(encryptionKey, Jwts.ENC.A256GCM)
.compact();
Claims claims = Jwts.parser()
.decryptWith(encryptionKey)
.requireIssuer("My App")
.require("otp", otp)
.build()
.parseEncryptedClaims(jwe)
.getPayload();
assert claims.getSubject().equals(subject);
}The only thing I noticed when copy-pasting your code into my IDE is that it warned that it couldn't compile due to this line:
since there shouldn't be a second/trailing closing parenthesis. But I just assumed that was a typo and removed it, and then the above code worked. The only thing I can think of is something might be off with how the Base64 string is created/exported and then read into the program. It's just a guess though. Ensure it's not Base64Url, no trailing characters have been removed, etc. Hopefully this helps! |
Beta Was this translation helpful? Give feedback.
Hi, thank you for the quick answer, I read it a while ago but tried to find the issue before I reply here.
Turns out, my custom authentication filter was trying to validate the encrypted token using the signed token method, and when the exception was thrown, instead of handling it and letting other filters take control, I just caught it and threw it again, messing up everything.
This is quite embarrassing, sorry 🥲