打电话给AWS Cognito安全端点时获得401

发布于 2025-02-05 18:12:07 字数 1219 浏览 4 评论 0原文

我有一个Cognito用户池。 该池具有用于JavaScript的应用程序集成,该应用程序没有秘密。 我可以使用以下代码登录

  private static async signin(role: UserRole): Promise<string> {
    const user = getUser();

    const cognitoUser = new CognitoUser({
      Username: user.username,
      Pool: "myuserpool"
    });

    const authDetails = new AuthenticationDetails({
      Username: user.username,
      Password: user.password
    });

    return new Promise((resolve, reject): void => {
      cognitoUser.authenticateUser(authDetails, {
        onSuccess: result => {
          this.credentials[role] = result.getIdToken().getJwtToken();
          resolve(this.credentials[role]);
        },
        onFailure: err => {
          console.log(`Failed login to cognito with ${role}: `, err);
          reject(err);
        }
      });
    });
  }

当我使用AWS-API-GATEWAY-CLIENT拨打端点时,

,我可以看到附加的令牌,但它总是返回401个未经授权的返回。这是非常令人困惑的,因为我可以将这个令牌粘贴到Apigeway授权者中并获得200个好的。因此,似乎令牌似乎有效,只是无法正常工作。

编辑: 这是流程。...

  • 我有一个Cognito用户池
  • ,我可以使用用户名 /密码登录到该用户池并获得令牌,
  • 然后回到AWS-API-GATEWAY上的“授权”:“ Bearer {token}” - 关键请求标题。
  • 请求未经授权的401失败,它告诉我令牌是有效的。
  • 如果我将同样的令牌粘贴并粘贴到Apigateway授权器的测试部分,

I have a Cognito user pool.
The pool has an application integration for JavaScript that does not have a secret.
I am able to login using the following code

  private static async signin(role: UserRole): Promise<string> {
    const user = getUser();

    const cognitoUser = new CognitoUser({
      Username: user.username,
      Pool: "myuserpool"
    });

    const authDetails = new AuthenticationDetails({
      Username: user.username,
      Password: user.password
    });

    return new Promise((resolve, reject): void => {
      cognitoUser.authenticateUser(authDetails, {
        onSuccess: result => {
          this.credentials[role] = result.getIdToken().getJwtToken();
          resolve(this.credentials[role]);
        },
        onFailure: err => {
          console.log(`Failed login to cognito with ${role}: `, err);
          reject(err);
        }
      });
    });
  }

When I make a call to my endpoint with the aws-api-gateway-client, I can see the token attached, but it always returns a 401 unauthorized.

It's super confusing because I can take this token and paste it into the ApiGateway Authorizer and receive a 200 ok. So it seems the token is valid, just not working properly.

EDIT:
Here is the flow....

  • I have a Cognito user pool
  • I can login to that userpool with a username / password and get a token back
  • I then set "Authorization": "bearer {token}" on the aws-api-gateway-client request headers.
  • The request fails with a 401 Unauthorized
  • If I take that same token and paste it in the test section of the ApiGateway Authorizer, It tells me the token is valid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄司 2025-02-12 18:12:07

我认为您不需要“承载”部分才能获得标题值。 Cognito授权者只是在寻找密钥/令牌。尝试“授权”的标题:“&lt; token_goes_here&gt;”。

I don't think you need the "bearer" part for the header value. Cognito authorizers are just looking for the key/token. Try a header of "Authorization" : "<token_goes_here>".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文