2017-06-06 59 views
0

我是aws认知的新手。这是我想要做的:用户认证检查使用sts令牌而不是用户/ pass

我有一些用户,我希望他们登录到我的网站,然后就他们的会话有效我想保持他们在没有强迫他们登录。所以据我所知,我需要使用cognito生成STS令牌并发送给用户,然后在接下来的调用中,用户将发送sts令牌作为头,并使用cognito检查sts令牌,如果有效,我将为用户。

对于我使用的指令在下面的链接:

https://github.com/aws/amazon-cognito-identity-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进行身份验证检查?

回答

1

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

如果您的用户只与您自己的网站进行交互,则默认情况下成功进行用户身份验证后获得的会话有效期最长为30天。

您可以检查会话处于活动状态或不通过改变刷新令牌有效性在Cognito用户群控制台客户端调用的getSession上CognitoUser

https://github.com/aws/amazon-cognito-identity-js/blob/9e949c08188f13b8087106564e7b596ec58117ab/src/CognitoUser.js#L826

您可以配置时间。