【问题标题】:Get access_token from passport-azure-ad.OIDCStrategy for use as bearer token从 passport-azure-ad.OIDCStrategy 获取 access_token 以用作不记名令牌
【发布时间】:2016-10-28 15:19:07
【问题描述】:

** 免责声明 -- 我是 oAuth 和 OpenIDConnect 领域的新手 -- 如果我在这里问一个愚蠢的问题,请耐心等待。

我想创建一个从 API 请求数据的 SPA。 SPA 和 API 都托管在同一个 nodejs 服务器上。我希望访问数据和/或应用程序的任何人都通过我们在 Office365 上的 AzureAD 租户进行身份验证。

目前,我正在使用 passport-azure-ad.OIDCStrategy 进行身份验证。但是,在我的应用程序中,我还希望能够从服务器端 api 代码中的 Microsoft GRAPH api 访问信息。但是,我已经建立的 OIDC 连接似乎不足以让我访问 GRAPH api。看来我可能需要一个 jwt 不记名令牌。

我的问题是,我是否需要使用 OIDC 响应中的访问令牌来获取不记名令牌?如果是这样,我该怎么做(在服务器端——nodejs)?

我尝试查看 Passport-auth-ad 中列出的 BearerStrategy v2 端点示例。令我困惑的是它使用了 OIDCStrategy!这是否也返回不记名令牌?如果是这样,我是否已经在我的第一次 OIDCStrategy 通话中收到了我需要的一切?

感谢您提供的任何帮助!

更新

https.request({
        hostname: "graph.microsoft.com",
        path: '/v1.0/me/messages',
        port: 443, 
        method: 'GET',
        headers: {Authorization: 'Bearer ' + req.user.token, Accept: "application/json"}
    },(rs) => {
        console.log("HTTPS Response Status: ", rs.statusCode);
        console.log("HTTPS Response Headers: ", rs.headers)
        rs.on('data', (d) => {
            res.send(d)
        })
    }).end();

错误信息:

{
  "error": {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure.", ...

我确认该令牌与在 Azure 的身份验证回调中作为 id_token 传递的令牌相同。有什么想法吗?

更新 2

还有一些代码 sn-ps 来帮助诊断我可能出错的地方。

策略配置

//Still test code so user management not fully implemented
passport.use("azure", new azureStrategy({
    identityMetadata: 'https://login.microsoftonline.com/common/.well-known/openid-configuration',
    clientID: "*********************",
    responseType: 'code id_token',
    issuer: "https://sts.windows.net/****************/",
    responseMode: 'form_post',
    redirectUrl: "https://localhost:5070/auth/azure/callback",
    allowHttpForRedirectUrl: true,
    clientSecret: "***************" ,
    state: "************"
},
(iss, sub, profile, claims, accessToken, refreshToken, params, done) => {
    process.nextTick(() => {
        var user = usvc.findUserByAltId(profile.oid, "azure");
        if(!user){

        }
    })
    done(null, {id: profile.oid, name: profile.displayName, email: profile.upn, photoURL: "", token: params.id_token });
}));

路线定义

app.get("/auth/azure", azure.passport.authenticate(
 'azure', {scope: ['Mail.Read','User.Read'], failureRedirect: '/'}))

app.post("/auth/azure/callback", azure.passport.authenticate(
  "azure", {scope: ['Mail.Read','User.Read'], failureRedirect: "/error.html"}),
(req, res) => {res.redirect("/user")})

【问题讨论】:

    标签: node.js azure adal azure-ad-graph-api passport-azure-ad


    【解决方案1】:

    OpenIDConnect 工作大流程还将返回一个用于身份验证和授权的 JWT 令牌。您可以在资源的 Authentication 标头中使用 id_token。但是,Graph API 中的某些操作需要管理员权限。

    您可以尝试在PowerShell 中运行以下脚本来升级您的 Azure AD 应用程序的权限。

    Connect-MsolService
    $ClientIdWebApp = '{your_AD_application_client_id}'
    $webApp = Get-MsolServicePrincipal –AppPrincipalId $ClientIdWebApp
    #use Add-MsolRoleMember to add it to "Company Administrator" role).
    Add-MsolRoleMember -RoleName "Company Administrator" -RoleMemberType ServicePrincipal -RoleMemberObjectId $webApp.ObjectId 
    

    【讨论】:

    • 感谢 Gary,我找到了 id_token。但是,我正在尝试使用上面更新的代码连接到图形(请参阅对我的附加问题的更新)。我收到了 401,但我的用户应该可以访问它,因为我可以通过 Graph API 门户访问我的消息。
    • @GaryLiuMSFT 你看到我关于使用 id_token 的评论了吗?
    • 您是否在 Azure AD 应用程序的范围内启用了Mail.Read; Mail.ReadWrite?
    • @GaryLiuMSFT,此时我正在发送 Mail.Read 和 User.Read。 app.post("/auth/azure/callback", azure.passport.authenticate("azure", {scope: ['Mail.Read','User.Read'], failureRedirect: "/error.html"}), (req, res) => {res.redirect("/user")} )
    • @TimReilly,其实id_token可以作为access_token使用
    猜你喜欢
    • 2018-04-23
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2021-09-28
    • 2017-03-10
    相关资源
    最近更新 更多