Search code examples
node.jsazurepassport.jsrolespassport-azure-ad

How can I get the azure AD roles in my Backend?


I am developing a backend in node express where I use the passport-azure-ad library to protect the routes of my api, is there any way to access the roles defined in Azure Ad for the application and validate them in the routes?


Solution

  • To achieve the above requirement you may need to follow the below workaround.

    We can get our Azure AD log details by using MS GRAPH Programmatically

    SAMPLE CODE:-

    const options = {
        authProvider,
    };
    
    const client = Client.init(options);
    
    let directoryAudit = await client.api('/auditLogs/directoryAudits/{id}')
        .get();
    

    Also you can get roles which has assigned in Azure AD by using below MS GRAPH query in your code.

    GET /users/{id | userPrincipalName}/appRoleAssignments
    

    For complete setup please refer the below links: