【问题标题】:AWS Amplify uses guest credentials, not authenticated creds, in API requestsAWS Amplify 在 API 请求中使用访客凭证,而不是经过身份验证的凭证
【发布时间】:2018-05-12 09:16:52
【问题描述】:

我正在使用带有 MobileHub 的 AWS Amplify 库。

我连接了一个 Cognito 用户池和一个 API 网关(与 Lambda 函数通信)。我希望我的用户在访问资源之前进行签名,因此我在 MobileHub 用户登录页面和云逻辑页面中启用了“强制登录”。

身份验证工作正常,但是当我向我的 API 发送 GET 请求时,我收到此错误:

"[WARN] 46:22.756 API - ensure credentials error": "cannot get guest credentials when mandatory signin enabled"

我了解 Amplify 会生成访客凭据,并将这些凭据放入我的 GET 请求中。由于我启用了“强制登录”,这不起作用。

但为什么要使用访客凭据?我已经登录——它不应该使用这些凭据吗?如何使用认证用户的信息?

干杯。

编辑:这是 Lambda 函数的代码:

lambda 函数:

import { success, failure } from '../lib/response';
import * as dynamoDb from '../lib/dynamodb';

export const main = async (event, context, callback) => {
    const params = {
        TableName: 'chatrooms',
        Key: {
            user_id: 'user-abc', //event.pathParameters.user_id,
            chatroom_id: 'chatroom-abc',
        }
    };

    try {
        const result = await dynamoDb.call('get', params);
        if (result.Item) { 
            return callback(null, success(result.Item, 'Item found'));
        } else {
            return callback(null, failure({ status: false }, 'Item not found.'));
        }
    } catch (err) {
        console.log(err);
        return callback(null, failure({ status: false }), err);
    }
}

还有这些小辅助函数:

response.js:

export const success = (body, message) => buildResponse(200, body, message)
export const failure = (body, message) => buildResponse(500, body, message)

const buildResponse = (statusCode, body, message=null) => ({
    statusCode: statusCode,
    headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true
    },
    body: JSON.stringify({
        ...body,
        message: message
    })
});

dynamodb.js:

import AWS from 'aws-sdk';

AWS.config.update({ region: 'ap-southeast-2' });

export const call = (action, params) => {
    const dynamoDb = new AWS.DynamoDB.DocumentClient();
    return dynamoDb[action](params).promise();
}

【问题讨论】:

  • 你能发布一些API网关调用的代码吗?
  • 我已经更新了主帖。一些细节是硬编码的,用于调试。 AWS 是否要求响应采用我不遵守的格式?
  • 我猜这之前没有经过身份验证(即使您认为自己是),当您打开强制身份验证时,您发现您的 API 调用从未经过身份验证。 github.com/aws/aws-amplify/blob/master/packages/aws-amplify/src/…
  • 如何确保我进行了身份验证?我运行Auth.signIn('username', 'password'),我似乎收到了正确的对象(没有错误或任何东西)。我应该执行其他登录方法吗?
  • 我明白了。这里似乎发布了一些选项github.com/aws/aws-amplify/issues/432

标签: amazon-web-services aws-mobilehub aws-amplify


【解决方案1】:

我正在遵循“serverless-stack”指南并提示相同的警告消息,我正确登录并正确注销,但不明白为什么会出现警告消息。 就我而言,在Amplify.configure 中我跳过添加identity pool id,这就是问题所在,User poolsfederated identities 不一样。

(英语不是我的母语)

【讨论】:

    【解决方案2】:

    您是否尝试过检查您的登录请求被拒绝/容易出错的原因?

    Auth.signIn(username, password)
        .then(user => console.log(user))
        .catch(err => console.log(err));
    
    // If MFA is enabled, confirm user signing 
    // `user` : Return object from Auth.signIn()
    // `code` : Confirmation code  
    // `mfaType` : MFA Type e.g. SMS, TOTP.
    Auth.confirmSignIn(user, code, mfaType)
        .then(data => console.log(data))
        .catch(err => console.log(err));
    

    你可以试试这个,调试起来会更方便。

    【讨论】:

    • 我的登录请求没有被拒绝,它通过了,我收到了正确的 User 对象。这就是我感到困惑的原因 - 这是失败的 API 调用,但 Cognito SignIn 工作正常。
    • 只有在您启动 SignOut 调用或 Aws amplify 库无法获取 currentSessionUser 的数据时才会出现您收到的错误。您可能会给我们一些认证代码和 GET API 调用的代码,只有我们才能给您更好的答案。
    【解决方案3】:

    根据 aws-amplify issues tracker 的建议,将匿名用户添加到您的 cognito 用户池并在您的应用程序中硬编码密码。似乎还有其他选择,但我认为这是最简单的。

    【讨论】:

    • 不过,我并不想实现匿名用户。我的用户拥有所有详细信息(用户名、密码、电子邮件),我正在尝试使用它登录。我是否(可能)遗漏了 Cognito 用户和 AWS 用户之间的区别?
    【解决方案4】:

    您必须在每次请求时使用您的凭证才能使用 AWS 服务: (示例代码角度)

    登录

    import Amplify from 'aws-amplify';
    import Auth from '@aws-amplify/auth';
    
     Amplify.configure({
          Auth: {
            region: ****,
            userPoolId: *****,
            userPoolWebClientId: ******,
          }
        });
    
    //sign in
    
    Auth.signIn(email, password)
    

    请求

    import Auth from '@aws-amplify/auth';
    
    from(Auth.currentCredentials())
       .pipe(
          map(credentials => {
            const documentClient = new AWS.DynamoDB.DocumentClient({
              apiVersion: '2012-08-10',
              region: *****,
              credentials: Auth.essentialCredentials(credentials)
           });
    
           return documentClient.query(params).promise()
    
          }),
          flatMap(data => {
              return data
           })
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 2021-07-03
      • 2019-02-09
      • 1970-01-01
      相关资源
      最近更新 更多