Azure-Samples/active-directory-javascript-nodejs-webapi-v2

Authentication failed due to: In Strategy.prototype.jwtVerify: cannot verify token

Kilio22 opened this issue · 2 comments

Hi there,

After following theses tutorials: https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-configure-app-expose-web-apis & https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-configure-app-access-web-apis, I'm getting this error message when trying to GET /hello using react:
authentication failed due to: In Strategy.prototype.jwtVerify: cannot verify token

Here's the full trace:

{"name":"AzureAD: Bearer Strategy","hostname":"Kylian","pid":18560,"level":30,"msg":"In Strategy.prototype.authenticate: received metadata","time":"2020-06-24T16:30:49.636Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Kylian","pid":18560,"level":30,"msg":"In Strategy.prototype.authenticate: we will validate the options","time":"2020-06-24T16:30:49.655Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Kylian","pid":18560,"level":30,"msg":"In Strategy.prototype.authenticate: access_token is received from request header","time":"2020-06-24T16:30:49.655Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Kylian","pid":18560,"level":30,"msg":"In Strategy.prototype.jwtVerify: token is decoded","time":"2020-06-24T16:30:49.656Z","v":0}
{"name":"AzureAD: Metadata Parser","hostname":"Kylian","pid":18560,"level":30,"msg":"working on key","time":"2020-06-24T16:30:49.656Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Kylian","pid":18560,"level":30,"msg":"PEMkey generated","time":"2020-06-24T16:30:49.657Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Kylian","pid":18560,"level":30,"msg":"authentication failed due to: In Strategy.prototype.jwtVerify: cannot verify token","time":"2020-06-24T16:30:49.675Z","v":0}

Here's my code from react side:

try {
            const accessToken = await this.userAgentApplication.acquireTokenSilent({
                scopes: scopes
            });

            if (accessToken) {
                console.log(accessToken);
                ....
                fetch("http://localhost:5000/hello", {
                    method: 'GET',
                    headers: {
                        "Authorization": `Bearer ${accessToken.accessToken}`,
                        "Content-Type": 'application/json'
                    }
                })
            }
        } catch (err) {
            ...
        }
    }

Here's the config.js file:

const config = {
    identityMetadata: "https://login.microsoftonline.com/<tenant-id>/v2.0/.well-known/openid-configuration",
    clientID: <client-id>,
    validateIssuer: false,
    loggingLevel: 'info',
    passReqToCallback: false
};

module.exports = config

Am I doing something wrong ?
I did a lot of search on google and I still can't find where is the problem :/

Regards,

Kilio22

I've fixed it, there was a problem with scopes, sry for notification

It would be great if you can post where did you make the changes.