我已经阅读了这个问题 AWS Cognito 用于 API 令牌身份验证,但我的问题是关于 AWS Cognito 于 2016 年 5 月推出的用户池功能。
期望的用例:
我有一个 Node.js 后端和一个客户端 Web 应用程序 (Polymer),并希望使用 Cognito 来简化我的基于令牌的身份验证系统。
我想使用此库进行用户身份验证,方法是让他们在客户端上注册和身份验证,并存储 AWS 提供给我的访问令牌,然后对于将来对 Node.js后端的所有请求,在标头中发送访问令牌。我希望后端可以使用 Cognito 检查访问令牌是否与有效会话相关联,如果是,则获取用户 ID。但是,我没有看到这样的方法来检查针对 Cognito 的访问令牌。
有什么方法可以做到这一点吗?
以下是我在代码中设想的流程的一般概念:
// client
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
setCookie('access-token', result.getAccessToken().getJWT())
},
// client
function fetchData() {
ajax.request('/server/data',
headers: {
token: getCookie('access-token')
})
}
// node server
get('/data', function(req, res) {
AWSCognito.validateAccessToken(req.headers.token).then(function(userId) {
// if user id exists, this is a valid token. now have user id, can look them up in database
// and do stuff
}
})
如果要验证用户的身份,则只需验证后端中 Cognito 颁发的 id 令牌。