简体   繁体   English

从passport-azure-ad.OIDCStrategy获取access_token用作承载令牌

[英]Get access_token from passport-azure-ad.OIDCStrategy for use as bearer token

** Disclaimer -- I'm new to the world of oAuth and OpenIDConnect -- please be patient if I'm asking a stupid question here. **免责声明-我是oAuth和OpenIDConnect领域的新手-如果在这里提出一个愚蠢的问题,请耐心等待。

I want to create a SPA that will request data from an API. 我想创建一个SPA,它将从API请求数据。 Both the SPA and API are hosted on the same nodejs server. SPA和API都托管在同一nodejs服务器上。 I want anyone accessing data and/or the app to be authenticated with our AzureAD tenant on Office365. 我希望任何访问数据和/或应用程序的人都可以通过Office365上的AzureAD租户进行身份验证。

Currently, I have the authentication piece working using passport-azure-ad.OIDCStrategy. 目前,我的身份验证文件正在使用Passport-azure-ad.OIDCStrategy。 However, in my app, I would also like to be able to access information from the Microsoft GRAPH api in the server side api code. 但是,在我的应用程序中,我还希望能够从服务器端api代码中的Microsoft GRAPH api访问信息。 However, the OIDC connection that I've already made does not seem to be enough to allow me access to the GRAPH api. 但是,我已经建立的OIDC连接似乎还不足以允许我访问GRAPH api。 It appears that maybe I need a jwt bearer token. 看来我可能需要一个jwt承载令牌。

My question is, do I need to use the access token from the OIDC response to get a bearer token? 我的问题是,我是否需要使用OIDC响应中的访问令牌来获得承载令牌? If so, how do I go about this (on the server side -- nodejs)? 如果是这样,我该如何处理(在服务器端-nodejs)?

I tried viewing the example listed in passport-auth-ad for BearerStrategy v2 endpoint. 我尝试查看BearerStrategy v2端点的Passport-auth-ad中列出的示例。 What confuses me though is that it uses OIDCStrategy! 令我困惑的是它使用了OIDCStrategy! Does that also return a bearer token? 那还会返回承载令牌吗? If so, am I already receiving everything I need in my first OIDCStrategy call? 如果是这样,我在我的第一个OIDCStrategy呼叫中已经收到了我需要的一切吗?

Thanks for whatever help you can offer! 感谢您提供的任何帮助!

Update 更新

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 Message: 错误信息:

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

I confirmed that the token is the same token that was passed as the id_token in the auth callback from Azure. 我确认该令牌与Azure中的auth回调中传递的id_token相同。 Any thoughts? 有什么想法吗?

Update 2 更新2

A few more code snippets to help in diagnosing where I may be going wrong. 其他一些代码段可帮助诊断我可能出了什么问题。

Strategy Config 策略设定

//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 });
}));

Route Definitions 路线定义

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")})

The OpenIDConnect work grand flow also will returns a JWT token for Authentication & Authorization. OpenIDConnect工作盛大流程还将返回用于身份验证和授权的JWT令牌。 You can use the id_token in Authentication header for the resources. 您可以在身份验证标头中使用id_token作为资源。 However, some operations in Graph APIs require an administrator permission. 但是,Graph API中的某些操作需要管理员权限。

You can try to run the following script in PowerShell to upgrade your Azure AD application's privilege. 您可以尝试在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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM