【问题标题】:Using instagram as an 'authorizer' for API Gateway - AWS Cognito使用 instagram 作为 API Gateway 的“授权人” - AWS Cognito
【发布时间】:2018-12-05 10:06:23
【问题描述】:

我正在使用 Congnito 用户池来执行 API 网关授权,它工作正常。现在,我正在尝试将 Instagram 添加为登录提供程序之一,为此我在 Federated Identities 中创建了 Custom Authentication Provider 并使用以下代码:

var cognitoidentity = new AWS.CognitoIdentity({ apiVersion: '2014-06-30' });

var params = {
    IdentityPoolId: 'us-east-1:7d99e750-.....',
    Logins: {
        'login.instagram': 'Access-Token-Returned-By-Instagram',
    },
    TokenDuration: 60
};

cognitoidentity.getOpenIdTokenForDeveloperIdentity(params, function (err, data) {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data);
        var idParams = {
            IdentityId: data['IdentityId'],
            Logins: {
                'cognito-identity.amazonaws.com': data['Token']
            }
        };

        cognitoidentity.getCredentialsForIdentity(idParams, function (err2, data2) {
            if (err2) console.log(err2, err2.stack); // an error occurred
            else console.log(data2);           // successful response
        });
    }
});

我能够获得 accessTokensessionToken,但是我仍然无法找到获得 idToken 的方法accessToken 是 API Gateway 对传入请求进行授权所需的。

我尝试查看 SDK 以及 AWS 论坛,但我仍然无法找到一种方法来使用自定义联合身份提供商来授权使用认知用户池的 API 网关。

【问题讨论】:

    标签: amazon-web-services amazon-cognito aws-cognito aws-sdk-js


    【解决方案1】:

    我要试一试……

    1. 你有认知用户吗?

      • 没有
    import { CognitoUser, CognitoUserPool, AuthenticationDetails } from "amazon-cognito-identity-js";
    
    let cognitoUser;
    
    const userPool = new CognitoUserPool({
       UserPoolId: config.USER_POOL.pool_Id, //your userpool id
       ClientId: config.appClientId, //your appClient 
    });
    
    const userData = {
           Username: 'user name',
           Pool: userPool
     };
    
    cognitoUser = new CognitoUser(userData);
    /* Should now have a cognitoUser */
    
    1. 验证您的 cognito 用户
    const authenticationData = {
        Username : payload.userName,
        Password : payload.password,
    };
    
    const authenticationDetails = new AuthenticationDetails(authenticationData);
    
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            const accessToken = result.getAccessToken().getJwtToken();
    
            /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer*/
             const idToken = result.idToken.jwtToken;
    
              /*
                Do something with 
                  idToken
                  accessToken
    
               I would write them to a file or encrypt them and store them on the localStorage OR encrypt and save in REDUX store.
              */
    
    
        },
    
        onFailure: function(err) {
             //handle error
       },
    
    });
    

    【讨论】:

    • 我可以将 instagram 与 aws cognito 一起使用
    猜你喜欢
    • 2021-08-05
    • 2020-12-23
    • 1970-01-01
    • 2020-09-23
    • 2019-12-22
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    相关资源
    最近更新 更多