Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate JWT token in C# using JWK

I have JsonWebKeys(JWK) for id_token and access_token. Then I got my id_token, from /token url. How to validate this JWT id_token using JWK in C#.

Needless to say I have tried almost everything but(IdenityModels.Jwt, etc) but JwtSecurityTokenHandler does not take JsonWebKey. I am using RS512 as signing algorithm.

like image 376
Manish Ranjan Avatar asked Mar 13 '23 03:03

Manish Ranjan


1 Answers

I just implemented this myself for Google IdToken validation as:

var parameters = new TokenValidationParameters
{
    ...
    IssuerSigningKeyResolver = await keyProvider.GetIssuerSigningKeyResolverAsync(cancellationToken),
};

SecurityToken token;
var handler = new JwtSecurityTokenHandler();
var principal = handler.ValidateToken(source, parameters, out token);

Where keyProvider is a:

public class GoogleOAuth2KeyProvider
{
    private readonly IGoogleAuthConfiguration configuration;
    private Jwk lastCertResponse;
    private DateTimeOffset lastCertExpires;

    public GoogleOAuth2KeyProvider(IGoogleAuthConfiguration configuration)
    {
        this.configuration = configuration;
    }

    public async Task<IssuerSigningKeyResolver> GetIssuerSigningKeyResolverAsync(CancellationToken cancellationToken)
    {
        await UpdateCert(cancellationToken);

        var keys = lastCertResponse.Keys
            .Where(key => key.Kty == "RSA" && key.Use == "sig")
            .ToDictionary(key => key.Kid, StringComparer.InvariantCultureIgnoreCase);
        return new IssuerSigningKeyResolver((token, securityToken, keyIdentifier, tokenValidationParameters) =>
        {
            foreach (var keyIdentifierClause in keyIdentifier)
            {
                Jwk.KeysData key;
                if (!keys.TryGetValue(keyIdentifierClause.Id, out key))
                {
                    continue;
                }
                var rsa = RSA.Create();
                rsa.ImportParameters(new RSAParameters
                {
                    Exponent = Base64UrlEncoder.DecodeBytes(key.E),
                    Modulus = Base64UrlEncoder.DecodeBytes(key.N),
                });
                return new RsaSecurityKey(rsa);
            }
            return null;
        });
    }

    private async Task UpdateCert(CancellationToken cancellationToken)
    {
        if (lastCertResponse != null && DateTimeOffset.UtcNow < lastCertExpires)
        {
            return;
        }
        var initializer = new BaseClientService.Initializer
        {
            ApiKey = configuration.ServerApiKey,
        };
        using (var service = new Oauth2Service(initializer))
        {
            lastCertResponse = await service.GetCertForOpenIdConnect().ExecuteAsync(cancellationToken);
            lastCertExpires = DateTimeOffset.UtcNow.AddHours(12);
        }
    }
}

RSA etc. is just System.Security.Cryptography, while Base64UrlEncoder is from System.IdentityModel (but easy enough to do yourself)

Unfortunately, it doesn't look like the other kty / alg values are as easy to support, e.g. there's no ECDsa.ImportParameters(), it wants a generic CngKey from byte[], so someone doing a generic .NET JWK library would have to pack the x, y params themselves, presumably.

like image 187
Simon Buchan Avatar answered Mar 25 '23 05:03

Simon Buchan