node.js - 无法从 Azure 应用程序的授权代码检索访问 token

标签 node.js access-token adal passport-azure-ad

我正在使用 Passport-azure-ad 库对我的 Azure 应用程序执行 SSO 到 Azure AD。以下是选项对象:

const options = {
    identityMetadata: process.env.AZUREAD_IDENTITY_METADATA,
    clientID: process.env.AZUREAD_AUTH_CLIENT_ID,
    responseType: 'code id_token',
    responseMode: 'form_post',
    redirectUrl: process.env.AZUREAD_REDIRECT_URL,
    allowHttpForRedirectUrl: true,
    clientSecret: process.env.AZUREAD_AUTH_CLIENT_SECRET,
    validateIssuer: false,
    issuer: null,
    passReqToCallback: true,
    useCookieInsteadOfSession: true,
    cookieEncryptionKeys: [
        { key: '********************************', iv: '************' },
        { key: '********************************', iv: '************' },
    ],
    scope: ['profile', 'offline_access', 'https://graph.microsoft.com/mail.read'],
    loggingLevel: 'info',
    nonceLifetime: null,
    nonceMaxAmount: 5,
    clockSkew: null,
};

从 AD 回调获取授权代码 (req.body.code) 后,我使用它通过 adal-node 库检索我的应用程序的访问 token 。相关代码片段 -

const authenticationContext = new AuthenticationContext(
            `https://login.microsoftonline.com/<tenant>.onmicrosoft.com`,
        );
        authenticationContext.acquireTokenWithAuthorizationCode(
            req.body.code,
            process.env.AZUREAD_REDIRECT_URL,
            process.env.AZUREAD_AUTH_CLIENT_ID,
            process.env.AZUREAD_AUTH_CLIENT_ID,
            process.env.AZUREAD_AUTH_CLIENT_SECRET,
            function(err, response) {
                let message = '';
                if (err) {
                    message = 'error: ' + err.message + '\n';
                }
                message += 'response: ' + JSON.stringify(response);

                if (err) {
                    console.log(message);
                    return;
                }
...

但是此方法会导致以下错误:

"error": "invalid_grant",
    "error_description": "AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token.\r\nTrace ID: 539cdaf3-460d-43fa-b4bb-6f23a8058800\r\nCorrelation ID: 507aafd9-5a4a-4417-b1f8-9fe4a5bc4cd3\r\nTimestamp: 2020-02-28 13:10:27Z",
    "error_codes": [
        54005
    ],

还尝试使用 Postman 根据文档 ( https://learn.microsoft.com/en-us/azure/active-directory/azuread-dev/v1-protocols-oauth-code#use-the-authorization-code-to-request-an-access-token ) 检索访问 token ,但这也会导致相同的错误。

请帮我找出这里的错误。

最佳答案

Azure AD 将不再接受授权代码来颁发已使用的 token 。

您每次都需要获取新的授权码。此外,您还可以更改代码以请求刷新 token ,该刷新 token 将传递给其他资源/由其他资源使用,因为刷新 token 仍然可以重复使用。

引用:

https://social.msdn.microsoft.com/Forums/en-US/4192e141-309a-4dd6-a5c9-f1a8ce32f4ca/aadsts54005-oauth2-authorization-code-was-already-redeemed

关于node.js - 无法从 Azure 应用程序的授权代码检索访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60452781/

相关文章:

node.js - fuse .js : How to return an exact match when there are two words to search for?

mysql - 如何获得数据库列中的最大值?

java - 验证 Azure AD token 签名失败 JAVA

ios - 在 Objective C 中注销用户 MSAL azure 库

javascript - Yodlee Interactive - 用于身份验证的 coblogin API - 错误代码 415

javascript - Multer 不接受数组格式的文件给出 'Unexpected File Error'

c# - JWT 和 Web API(JwtAuthForWebAPI?)——寻找示例

android - 无法在 Android 上设置范围 Google 登录

angularjs - CORS 预检请求在 Azure 托管 Web API 中通过 302 重定向进行响应

azure - ADAL JS - 不支持response_type ="token"