Fetching user data from access token received from NextAuth.

Aashutosh Aryal 45 Reputation points
2023-11-01T06:12:26.21+00:00

I am trying to use NextAuth for next.js with Azure Active directory for single-sign-on. I followed the guide on Microsoft's identity platform quickstart guide: https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app

I receive an id_token and an access_token. I want to use one of these tokens get information about the user. I am able to decode the id_token but I get a Signature verification failed error when trying to decode the access_token.

What would be the proper way to achieve what I am trying to do?
What I am trying to accomplish: Get an authorization token (access or id token) from the frontend (Nextauth) and then the backend uses the token to retrieve user information like first name, last name, etc.

I looked into MSAL but it seems to be only for acquiring tokens and this has already been handled by Nextauth.

Microsoft Security Microsoft Entra Microsoft Entra ID
{count} votes

Accepted answer
  1. Navya 19,795 Reputation points Microsoft External Staff Moderator
    2023-11-02T11:39:30.0066667+00:00

    Hi @Aashutosh Aryal , thanks for reaching us.
    I understand you trying to use NextAuth for next.js with Azure Active directory for single-sign-on. You will be able to decode the id_token but get a Signature verification failed error when trying to decode the access token.

    Register two applications in Azure AD for Front-end and Back-end.

    Sign into the Azure portal -> select Azure Active Directory in the tenant ->register the application.

    User's image

    Create second application as followed above step.

    To use OAuth 2.0 On-Behalf-Of flow (OBO) to get access tokens. Scope should be the Api://{client_id} so that no consent is required.

    Azure active directory ->App registration -> select your Application

    ->Expose an API ->Add an Application ID URI as api://{clientid}

    ->Add scope and Client Application.

    User's image

    Go to second application ->Expose an API and add Application1 API as client application.
    User's image

    Use www.jwt.io to validate your access token.

    • audience - Verifies that the ID token was intended to be given to your application. Access tokens are created based on the audience of the token,
    meaning the application that owns the scopes in the token.
    • not before and expiration time - Verifies that the ID token hasn't expired.
    • issuer - Verifies that the token was issued to your application by Azure AD.
    • nonce - A strategy for token replay attack mitigation.

    To get User information using access token, you can use Graph API and make sure to use authorization as Bearer Token.

    For your reference: https://microsoft.github.io/MicrosoftCloud/tutorials/docs/Authentication-App-With-NextJs-And-Microsoft-Graph/

    https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-on-behalf-of-flow

    Thanks,

    Navya.

    Hopes this helps. Do let us know if you have any queries.

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.