AWS-amplify 与 React Native Token 集成 - 未经授权

发布于 2025-01-09 11:59:15 字数 2224 浏览 1 评论 0原文

我已将我的 React Native 应用程序配置为使用 Cognito 用户池,该用户池用于使用 AWS Amplify 进行用户身份管理和身份验证。我正在使用自定义身份验证进行用户管理。我能够注册、登录并执行其他与用户管理相关的任务。我还有一个 API 网关 和一些 Lambda 函数,我想通过我的 React Native 应用程序访问它们。当我登录时,我收到一个 JWT 令牌,我想将其发送到 API 网关以访问我的 Lambda,但无论我做什么,我都会从我的 API 收到“未经授权”403 或 401 消息网关。

我的问题是:如何向 Cognito 用户池用户公开 API 网关/Lambda 以及为什么 Cognito 本身生成的令牌未经授权访问我的 api 网关。

PS - 我使用具有正确身份验证 URL 和设置的邮递员,邮递员令牌本身被授权访问 API 网关和 lambda。 (用户凭据与我在 React Native 应用程序中使用的用户凭据相同)

我已经花了几天时间,任何指向正确方向的指针都会非常有帮助。

提前致谢。

NPN

enter图片描述在这里

    Amplify Config:

    const awsmobile = {
      aws_project_region: 'us-XXXX-X',
      aws_cognito_region: 'us-XXXX-X',
      aws_user_pools_id: 'us-XXXX-XXXXXX',
      aws_user_pools_web_client_id: 'XXXXh1i5nXXXX',
      //aws_user_pools_web_client_secret: 'XXXXXoofuu0lXXXX',
      oauth: {
        domain: 'XXXXXXXX.us-XXXX-X.amazoncognito.com',
        scope: ["email", "openid", "aws.cognito.signin.user.admin"]
      },
      aws_cognito_username_attributes: ['EMAIL'],
      aws_cognito_social_providers: ['GOOGLE'],
      aws_cognito_signup_attributes: ['XXXXX', 'XXXXX', 'EMAIL', 'XXXXXX'],
      aws_cognito_mfa_configuration: 'OFF',
      aws_cognito_mfa_types: [],
      aws_cognito_password_protection_settings: {
        passwordPolicyMinLength: 8,
        passwordPolicyCharacters: ['REQUIRES_LOWERCASE', 'REQUIRES_UPPERCASE', 'REQUIRES_NUMBERS', 'REQUIRES_SYMBOLS'],
      },
      aws_cognito_verification_mechanisms: ['EMAIL'],
    };

    export default awsmobile;

  import { Auth } from 'aws-amplify';

  const login = async (username: string, password: string) => {
    const response = await Auth.signIn(username, password);
  console.log(response.data.signInUserSession.accessToken.jwtToken);
    return response;
  };

I have configured my React Native application to use a Cognito user pool that is used for user identity management and authentication using AWS Amplify. I am using Custom Authentication for user management. I am able to register, log in and perform other user management-related tasks. I also have an API gateway and a few Lambda Functions which I want to access through my React Native app. When I sign in, I receive a JWT Token which I want to send to the API gateway to access my Lambdas, but no matter what I do I get an 'unauthorized' 403 or 401 message from my API Gateway.

My question is: How can I expose the API gateway/ Lambdas to the Cognito user pool users and Why the token generated by Cognito itself is unauthorized to access my api gateway.

P.S. - I used postman with the right Auth URL and settings, the postman token itself is authorized to access the API gateway and lambdas. (The user credentials are the same as I use with the React Native app)

I have spent a few days, any pointers in the right direction would be very helpful.

Thanks in advance.

NPN

enter image description here

    Amplify Config:

    const awsmobile = {
      aws_project_region: 'us-XXXX-X',
      aws_cognito_region: 'us-XXXX-X',
      aws_user_pools_id: 'us-XXXX-XXXXXX',
      aws_user_pools_web_client_id: 'XXXXh1i5nXXXX',
      //aws_user_pools_web_client_secret: 'XXXXXoofuu0lXXXX',
      oauth: {
        domain: 'XXXXXXXX.us-XXXX-X.amazoncognito.com',
        scope: ["email", "openid", "aws.cognito.signin.user.admin"]
      },
      aws_cognito_username_attributes: ['EMAIL'],
      aws_cognito_social_providers: ['GOOGLE'],
      aws_cognito_signup_attributes: ['XXXXX', 'XXXXX', 'EMAIL', 'XXXXXX'],
      aws_cognito_mfa_configuration: 'OFF',
      aws_cognito_mfa_types: [],
      aws_cognito_password_protection_settings: {
        passwordPolicyMinLength: 8,
        passwordPolicyCharacters: ['REQUIRES_LOWERCASE', 'REQUIRES_UPPERCASE', 'REQUIRES_NUMBERS', 'REQUIRES_SYMBOLS'],
      },
      aws_cognito_verification_mechanisms: ['EMAIL'],
    };

    export default awsmobile;

  import { Auth } from 'aws-amplify';

  const login = async (username: string, password: string) => {
    const response = await Auth.signIn(username, password);
  console.log(response.data.signInUserSession.accessToken.jwtToken);
    return response;
  };

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文