Encrypted JWTs?
See original GitHub issueI’m looking to create an encrypted JWT and I’m a little lost.
Option 1: Articles like this one suggest just encrypting the string representation of a JWT: https://www.owasp.org/index.php/JSON_Web_Token_(JWT)_Cheat_Sheet_for_Java#Token_information_disclosure
Option 2: However, this article suggests nesting a signed JWT inside an encrypted JWT: https://connect2id.com/products/nimbus-jose-jwt/examples/signed-and-encrypted-jwt
My preference would be to use option 2 as I assume this is the correct way of encrypting a JWT. I can’t seem to find the right overloads and/or properties in the JwtSecurityToken
object though to make it work. On the JwtHeader
I see a property called Cty
which is great, because I need to use that to set the content type, and on the JwtSecurityToken
I see a property called InnerToken
which looks promising. I don’t, however, know how to set that property and/or create a JwtPayload
that wraps an existing JWT.
Has anybody else done this before and able to provide a sample?
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
@brentschmaltz, thanks for your response. I have some follow-up questions.
Funny you mention that… after posting I persisted for another couple hours and found the
JwtSecurityTokenHandler.CreateJwtSecurityToken
method – a parent to the one you mentioned. As far as I can tell, that is the only way to create a signed and encrypted token because all the other methods seem to wantSigningCredentials
orEncryptingCredentials
, but not both. Perhaps you could show me otherwise?So the code I currently have is like this:
The result is:
As you can see, the second part of the JWE – which as I understand is the encrypted key – is empty. Why is that?
Finally, I would love some clarification around the choice of encryption algorithms. I’m trying to create a JWE using AES-GCM and AAD, as mentioned in my first link, however, I’m not sure if .NET supports this algorithm. Do you know?
@jacobslusser I will close this now. Please feel free to re-open if you need additional info.