** 免责声明 - 我是 oAuth 和 OpenIDConnect 领域的新手 - 如果我在这里问一个愚蠢的问题,请耐心等待。
我想创建一个从 API 请求数据的 SPA。 SPA 和 API 都托管在同一个 Nodejs 服务器上。我希望访问数据和/或应用程序的任何人都能够通过 Office365 上的 AzureAD 租户进行身份验证。
目前,我使用 Passport-azure-ad.OIDCStrategy 进行身份验证。但是,在我的应用程序中,我还希望能够从服务器端 api 代码中的 Microsoft GRAPH api 访问信息。但是,我已经建立的 OIDC 连接似乎不足以允许我访问 GRAPH api。看来我可能需要一个 jwt 不记名 token 。
我的问题是,我是否需要使用 OIDC 响应中的访问 token 来获取不记名 token ?如果是这样,我该如何解决这个问题(在服务器端——nodejs)?
我尝试查看 Passport-auth-ad 中列出的 BearerStrategy v2 端点的示例。但令我困惑的是它使用 OIDCStrategy!这也返回不记名 token 吗?如果是这样,我是否已经在第一次 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.", ...
我确认该 token 与从 Azure 的身份验证回调中作为 id_token 传递的 token 相同。有什么想法吗?
更新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 token 。您可以在资源的身份验证 header 中使用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
关于node.js - 从 Passport-azure-ad.OIDCStrategy 获取 access_token 用作不记名 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40308208/