How to verify JWT id_token produced by MS Azure AD?

FOR :

I have an angularjs SPA web app which uses ADAL-JS (and adal-angular). It's set up to authenticate vs our corporate AD in MS Azure. The log-in flow seems to work correctly, and the SPA receives an id_token.

Next, when the user clicks a button, the SPA makes a request to a REST API I am hosting on AWS API Gateway. I am passing the id_token on the Authorization: Bearer <id_token> header. The API Gateway receives the header as intended, and now has to determine if the given token is good or not to either allow or deny access.

I have a sample token, and it parses correctly on https://jwt.io/ but I have so far failed to find the Public Key or Certificate I should use to verify the signature. I have looked in:

I think I should use the value of the x5c property of the key in https://login.microsoftonline.com/common/discovery/keys matching the kid and x5t properties from the JWT id_token (currently a3QN0BZS7s4nN-BdrjbF0Y_LdMM, which leads to an x5c value starting with "MIIDBTCCAe2gAwIBAgIQY..." ). However, the https://jwt.io/ page reports "Invalid Signature" (I also tried wrapping the key value with "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----").

Also, is there a (possibly python) library that can facilitate the verification of a given id_token as in the case above (so that I won't have to go grab the signing key on the fly myself?)... The best I could find (ADAL for python) doesn't seem to provide this feature?

FOR :

The best solution I could put together so far:

Grab the certificate (the first value in the x5c property array) from either https://login.microsoftonline.com/common/discovery/keys or https://login.microsoftonline.com/common/discovery/v2.0/keys, matching kid and x5t from the id_token.

Wrap the certificate in -----BEGIN CERTIFICATE-----\n and \n-----END CERTIFICATE----- (the newlines seem to matter), and use the result as Public Key (in conjunction with the id_token, on https://jwt.io/ ).

Of course, your actual use case will likely be to have some program validate the incoming JWT id_tokens, so your goal won't be to simply get the token to validate through the web UI on https://jwt.io/.

For instance, in python, I need something like this:

#!/usr/bin/env python

import jwt
from cryptography.x509 import load_pem_x509_certificate
from cryptography.hazmat.backends import default_backend

PEMSTART = "-----BEGIN CERTIFICATE-----\n"
PEMEND = "\n-----END CERTIFICATE-----\n"

mspubkey = "The value from the x5c property"
IDTOKEN = "the id_token to be validated"
tenant_id = "your tenant id"

cert_str = PEMSTART + mspubkey + PEMEND
cert_obj = load_pem_x509_certificate(cert_str, default_backend())
public_key = cert_obj.public_key()

decoded = jwt.decode(IDTOKEN, public_key, algorithms=['RS256'], audience=tenant_id)
if decoded:
    print "Decoded!"
else:
    print "Could not decode token."

For a list of JWT libraries in various languages, see the JWT Site. I'm using pyjwt, and its cryptography dependency (which has binary dependencies, so needs to be built and packaged for the target OS).

And then, of course, you can verify additional details such as the claims as recommended here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to verify signature of refreshed id_token in Azure active directory

How to use Jsonwebtoken NPM package to verify JWT token issued by Azure AD?

Azure AD: id_token as bearer token

How to verify and renew a JWT id_token during my SPA load?

Azure AD - id_token not refreshing

Azure AD B2C - how is username/password exchanged for id_token?

How to verify firebase ID token with PHP(JWT)?

How jwt token get reissued in azure ad OuthImplicitFlow

How to access and filter groups in JWT token using Azure AD?

How to create Client Assertion JWT token when connecting to Azure AD?

How do I verify a JWT signature for an Azure B2C id token in Java?

Use express-jwt as middleware to verify Azure AD issued tokens

Unable to have Azure AD B2C issue a token and redirect it to https://jwt.ms

How to verify that a string is JWT token?

Azure AD Daemon App Application Permission and id_token

Google OpenID Connect: How to verify id_token?

Azure AD JWT Token Validation options

JWT Token based Authentication in Azure AD

Azure AD token service does't response refresh_token and id_token

Verify Signature with Azure AD

I added api permerssions in Azure AD, but when I get the token,and showed it in jwt.ms, the scopes field doesn't change

C# How to verify signature on JWT token?

how to verify jwt token in nodejs / never expire?

Angular and JWT - how does client verify token?

Azure AD - No application roles claims in id_token in /authorize login but there are in /token login

Azure AD exchange access_token with id_token v.1.0 endpoint

How can I retrieve the azure AD JWT access token from Spring?

How to get proper JWT token using IdentityModel and Azure AD to authenticate with my API?

Azure AD token endpoint doesn't return an access_token (just an id_token and a refresh_token)