用户身份验证使用STS令牌而不是用户/通行证检查



我是AWS Cognito的新手。这是我想做的:

我有一些用户,我希望他们登录我的网站,然后就他们的会话有效,我想保留它们而不强迫他们登录。因此,据我了解,我需要生成STS使用Cognito的令牌并将其发送给用户,然后在下一个呼叫中,用户将将STS令牌发送为标头,我使用Cognito检查STS令牌,如果有效,我将为用户服务。

为此,我在以下链接中使用了指令:

https://github.com/aws/amazon-cognito-indistity-js/

因此,我特别使用此部分来验证用户并建立会话:

var authenticationData = {
    Username : 'username',
    Password : 'password',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var poolData = {
    UserPoolId : '...', // Your user pool id here
    ClientId : '...' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var userData = {
    Username : 'username',
    Pool : userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        console.log('access token + ' + result.getAccessToken().getJwtToken());
        //POTENTIAL: Region needs to be set if not already set previously elsewhere.
        AWS.config.region = '<region>';
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
            IdentityPoolId : '...', // your identity pool id here
            Logins : {
                // Change the key below according to the specific region your user pool is in.
                'cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>' : result.getIdToken().getJwtToken()
            }
        });
        // Instantiate aws sdk service objects now that the credentials have been updated.
        // example: var s3 = new AWS.S3();
    },
    onFailure: function(err) {
        alert(err);
    },
});

到目前为止一切都很好。但是,现在,在任何后续的电话中,我都不想发送

var authenticationData = {
Username : 'username',
Password : 'password',
};

而相反,我想使用STS令牌来验证用户:

var authenticationData = {
    sts: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

我看不到这种情况的任何例子或用法。Cognito是适合这种情况的正确服务吗?如果是,我该如何使用STS进行身份验证检查?

我看到您在身份验证回调中使用了AWS.CognitoIdentityCredentials。如果您的应用需要AWS凭据来访问各种AWS服务,则可以使用。

如果您的用户仅与您自己的网站进行交互,则默认情况下,成功用户身份验证后获得的会话最多可达30天。

您可以通过在Cognitouser上调用GetSession检查会话是否活动

https://github.com/aws/amazon-cognito-indistity-js/blob/9e949c08188855b8087106564e7b5964e7b5966ec588117ab/src/src/src/cognitouser.js#l826

您可以通过更改Cognito用户池控制台的客户的刷新令牌有效性来配置该持续时间。

最新更新