令牌中不存在必需的声明 nbf(使用 Firebase Microsoft Sign In 尝试访问 Microsoft Graph)

2024-05-13

我目前有一个具有以下结构的应用程序: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 时做错了什么有什么建议吗?


我能够使用 vanilla js 在我的环境中重现此错误。为了开始复制,我使用了可用的快速入门代码here https://github.com/firebase/quickstart-js/tree/master/auth。基本上,我怀疑您将错误的 accessToken 传递给了图形 API。正如在auth/microsoft-popup.html,您需要使用以下访问令牌:

var provider = new firebase.auth.OAuthProvider('microsoft.com');
provider.addScope('User.Read');
firebase.auth().signInWithPopup(provider).then(function(result) {
    // This gives you a Microsoft Access Token. You can use it to access the Microsoft API.
    var token = result.credential.accessToken; //This one!!!
    console.log(token)
}).catch(function(error) {
    console.error(error);

});

然后将此令牌导出为:

export ACCESS_TOKEN=FOO_BAR

使用此令牌发出一个curl请求:

curl -i -X GET -H "Authorization: Bearer $ACCESS_TOKEN" https://graph.microsoft.com/v1.0/me

重现此错误Required claim nbf not present in token请执行下列操作:

var provider = new firebase.auth.OAuthProvider('microsoft.com');
provider.addScope('User.Read');
firebase.auth().signInWithPopup(provider).then(function(result) {
    var user = result.user;
    var token = user.stsTokenManager.accessToken;
    console.log(token)
}).catch(function(error) {
    console.error(error);
});
  • 然后获取令牌并点击请求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

令牌中不存在必需的声明 nbf(使用 Firebase Microsoft Sign In 尝试访问 Microsoft Graph) 的相关文章

随机推荐