我目前有一个具有以下结构的应用程序:Angular 前端,Node.js 服务器。
我们已实施 Google Cloud 的身份提供商以使用 Google 和/或 Microsoft 进行登录。
Google 登录并访问 Google Cloud Admin SDK 工作正常,但尝试访问 Microsoft Graph 时出现以下错误:
UnhandledPromiseRejectionWarning: Error: Required claim nbf not present in token
根据Firebase 文档 https://firebase.google.com/docs/auth/web/microsoft-oauth您可以使用从登录收到的访问令牌来访问图表:
Firebase 文档屏幕截图 https://i.stack.imgur.com/lIMz6.png
成功完成后,可以从返回的 firebase.auth.UserCredential 对象中检索与提供程序关联的 OAuth 访问令牌。使用 OAuth 访问令牌,您可以调用 Microsoft Graph API。例如,要获取基本的个人资料信息,可以调用以下REST API:
curl -i -H "Authorization: Bearer ACCESS_TOKEN" https://graph.microsoft.com/v1.0/me
在 url 中运行上述内容时,出现相同的错误:
{"error":{"code":"InvalidAuthenticationToken","message":"Required claim nbf not present in token","innerError":{"date":"2022-05-26T12:51:11","request-id":"##########","client-request-id":"##########"}}}
我正在使用使用弹出窗口登录在我的身份验证服务(authentication.service.ts)中:
await signInWithPopup(this.auth, provider)
.then((result) => {
const credential = OAuthProvider.credentialFromResult(result);
const accessToken = credential.accessToken;
const idToken = credential.idToken;
this.setCurrentUser(result.user);
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
const email = error.email;
const credential = OAuthProvider.credentialFromError(error);
console.log(error);
});
我将 accessToken 发送到我的服务器(app.ts):
app.get(
'/api/microsoft-get-organisation',
async (req: express.Request, res: express.Response) => {
//https://graph.microsoft.com/v1.0/organization
const organisation = await ms.getOrganisation(req.headers, '/organization');
res.send(JSON.stringify(organisation));
}
);
export const getOrganisation = async (headers: any, graphEndpoint: string) => {
const client = await getAuthenticatedClient(headers);
return await client.api(graphEndpoint).get();
};
async function getAuthenticatedClient(headers: any) {
const client = await graph.Client.init({
authProvider: (done: any) => {
done(null, headers.authorization.split(' ')[1]);
},
});
return client;
}
验证令牌时,我可以看到没有 nbf 声明:
令牌屏幕截图 https://i.stack.imgur.com/E4ALR.png
对于我在访问 Microsoft Graph 时做错了什么有什么建议吗?