Validating NearAuth's JWT

If your own web app signs people in through NearAuth.ai, NearAuth.ai sends it a signed JSON Web Token (JWT) after each sign-in. Your app must check every one of these points before trusting it. If any check fails, reject the token.

1. Only fetch the key from NearAuth.ai

Read x5u in the JWT's header. Continue only if it starts with
https://secure.nearauth.ai/
Never fetch a key from any other address: anyone can make a JWT that points x5u at their own key.

2. Get the public key

Fetch the public key from that address. Cache it by address rather than fetching it for every sign-in.

3. Check the signature and claims

Verify the signature with that key (RS256), then check:
  • iss (issuer) is exactly
    https://secure.nearauth.ai/SAML2/SSO/YOUR_COMPANY_ID/EntityId
  • aud (audience) is your Login URL, shown on the Settings page.
  • The token isn't expired (exp) and isn't used before nbf.
Your Company ID is shown in the admin console. Sign in to see these values filled in.

4. Accept each token once

Remember each token's id (jti) until it expires, and refuse it if it comes again. Use it only for the person who signed in to your site in that same browser session.

5. Check it's for the person who just signed in

The email claim is the person's NearAuth.ai email address, and uid is your own id for them, when set. Compare one of them with the account that just entered its password, and reject the token if they differ. Otherwise another employee's token would pass for that account.

If you validate through NearAuth.ai's token endpoint instead, send that account's email or uid as the user parameter. A token for anyone else is refused.

Need help? We've done this before and are happy to help. Email help@nearauth.ai or call 607-238-3533.