Node.js 从passport-azure-ad.OIDCStrategy获取访问令牌,用作承载令牌
**免责声明——我是oAuth和OpenIDConnect的新手——如果我在这里问了一个愚蠢的问题,请耐心等待 我想创建一个SPA,它将从API请求数据。SPA和API都托管在同一个nodejs服务器上。我希望任何访问数据和/或应用程序的人都能通过Office365上AzureAD租户的身份验证 目前,我使用passport-azure-ad.OIDCStrategy进行身份验证。但是,在我的应用程序中,我还希望能够在服务器端api代码中访问来自Microsoft GRAPH api的信息。然而,我已经建立的OIDC连接似乎不足以让我访问GRAPH api。看来我需要一个jwt承载令牌 我的问题是,我是否需要使用OIDC响应中的访问令牌来获取承载令牌?如果是这样的话,我该怎么做(在服务器端——nodejs) 我尝试查看BearStrategyV2端点的passport auth ad中列出的示例。但让我困惑的是,它使用了一种策略!这是否也返回不记名代币?如果是这样,我是否已经在第一次战略电话中收到了我所需要的一切 谢谢你能提供的一切帮助 更新Node.js 从passport-azure-ad.OIDCStrategy获取访问令牌,用作承载令牌,node.js,azure,adal,azure-ad-graph-api,passport-azure-ad,Node.js,Azure,Adal,Azure Ad Graph Api,Passport Azure Ad,**免责声明——我是oAuth和OpenIDConnect的新手——如果我在这里问了一个愚蠢的问题,请耐心等待 我想创建一个SPA,它将从API请求数据。SPA和API都托管在同一个nodejs服务器上。我希望任何访问数据和/或应用程序的人都能通过Office365上AzureAD租户的身份验证 目前,我使用passport-azure-ad.OIDCStrategy进行身份验证。但是,在我的应用程序中,我还希望能够在服务器端api代码中访问来自Microsoft GRAPH api的信息。然而
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_令牌相同。有什么想法吗
更新2
还有一些代码片段可以帮助我诊断哪里可能出错
策略配置
//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")})
OpenIDConnect工作流还将返回一个用于身份验证和授权的JWT令牌。您可以在资源的身份验证标头中使用
id\u令牌。但是,图形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,我找到了身份证代币。然而,我正试图用上面更新的代码连接到graph(请参阅更新我的附加问题)。我获得了401,但我的用户应该可以访问它,因为我可以通过Graph API门户访问我的邮件。@GaryLiuMSFT您看到我关于使用id_令牌的评论了吗?您启用了Mail.Read;Mail.ReadWrite
在Azure AD应用程序的范围内?@GaryLiuMSFT,我正在发送Mail.Read和User.Readapp.post(“/auth/azure/callback”)、azure.passport.authenticate(“azure”、{scope:['Mail.Read'、'User.Read']、failureRedirect:/error.html”}),(req、res)=>{res redirect(/User”)}
@TimReilly,实际上,id\u令牌可以用作访问令牌