带有PaginationToken的AWS Cognito ListUsers上的502

时间:2018-11-24 19:41:19

标签: aws-lambda aws-api-gateway amazon-cognito aws-amplify

使用AWSMibileHub设置我的后端。在一个管理页面上工作,该页面显示AWS Cognito中的所有用户。

我正在使用ListUsers来获取用户列表,并注意到带有PagenationToken的请求出现502错误,而且似乎是随机发生的。

在CloudWatch中,此错误显示如下。 InvalidParameterException: 1 validation error detected: Value 'CAISlAIIARLtAQgDEugBADSdLB5dZXQEaQjoL8y8CE1RGGT3PZ4FpCqxFwJkNuhIeyJAbiI6IlBhZ2luYXRpb25Db250aW51YXRpb25EVE8iLCJuZXh0S2V5IjoiQUFBQUFBQUFCZHM4QVFFQmJBZmU5OFgzUnJTM1BHcnYzVmRiVWNScVdIck82VXFZaTdlZklleVRCSEZsYm1ZN01UVTRPV1k0TVRNdE9UYzVZUzAwWTJKakxUazROekl0TkdRek9UYzROMlpoWmpVM093PT0iLCJwcmV2aW91c1JlcXVlc3RUaW1lIjoxNTQzMDkzODIyNDk5fRogNk7FThSKBOuwQGi DoZBnmNN85UY5oFiSAbHWfOzreY=' at 'paginationToken' failed to satisfy constraint: Member must satisfy regular expression pattern: [\S]+

有人有相同的问题或如何解决此问题的想法吗?

2 个答案:

答案 0 :(得分:0)

将分页令牌存储在变量中,然后将此“ .replace(/ \ / g,'+')”添加到变量中,以将空格更改回+。

例如:const pagination ='TOKEN FROM COGNITO'.replace(/ \ / g,'+')

答案 1 :(得分:0)

pagination_token 由非字母数字字符组成,因此最好在需要时加密和解密。

加密?解密实用程序文件

const crypto = require("crypto");
const algorithm = "aes-256-ctr";
const password = "sassed";

class Util {

  static encrypt_aes(text) {
    let cipher = crypto.createCipher(algorithm, password);
    let crypted = cipher.update(text, "utf8", "hex");
    crypted += cipher.final("hex");
    return crypted;
  }

  static decrypt_aes(text) {
    let decipher = crypto.createDecipher(algorithm, password);
    let dec = decipher.update(text, "hex", "utf8");
    dec += decipher.final("utf8");
    return dec;
  }
}

module.exports = Util;

PaginationToken 加密/解密

  const Util = require("./utils");
  class Users {
     static async getUsersAttributes(params) {
        let listparams = {
          UserPoolId: userPoolID /* required */,
          Limit: params.limit || 10,
        };
        if (params.paginationToken) {
          listparams.PaginationToken = Util.decrypt_aes(params.paginationToken);
        }
        let response = await cognitoISPClient.listUsers(listparams).promise();
        response.PaginationToken = Util.encrypt_aes(response.PaginationToken);
    
        return response;
      }
  }