AWS Cognito访问令牌Javascript
我正在使用与Alexa的帐户链接并获得accessToken。我正在使用AWS Cognito进行身份验证。我的假设是accessToken是AWS Cognito的标记——但是我如何使用它呢?我需要得到CognitoUser的信息。我见过使用Facebook SDK的例子,说Fb.setTokenaccessToken很简单,但我找不到Cognito的等价物。我错过了什么 这是我的身份验证流程,仅使用cognito,对我来说很好:AWS Cognito访问令牌Javascript,javascript,amazon-web-services,access-token,amazon-cognito,aws-cognito,Javascript,Amazon Web Services,Access Token,Amazon Cognito,Aws Cognito,我正在使用与Alexa的帐户链接并获得accessToken。我正在使用AWS Cognito进行身份验证。我的假设是accessToken是AWS Cognito的标记——但是我如何使用它呢?我需要得到CognitoUser的信息。我见过使用Facebook SDK的例子,说Fb.setTokenaccessToken很简单,但我找不到Cognito的等价物。我错过了什么 这是我的身份验证流程,仅使用cognito,对我来说很好: var authenticationData = {
var authenticationData = {
Username: document.getElementById("user").value,
Password: document.getElementById("password").value
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId: AWSConfiguration.UserPoolId,
ClientId: AWSConfiguration.ClientAppId
};
userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username: document.getElementById("user").value,
Pool: userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
// authenticate here
只需在Alexa skill Lambda函数中解码Cognito访问令牌 此外,您可以通过使用令牌生成前Lambda触发器在用户身份验证时向该jwt令牌添加属性:
AWS Cognito用户池为身份验证机制生成id令牌和访问令牌。它们都是jwt令牌,id令牌具有用户名、电子邮件和姓氏等用户属性。您可以使用id或访问令牌对用户进行身份验证
相关链接:,我有点晚了,但您可以从URL获取AWS Cognito JSON Web Token JWT响应,并对其进行解码以获得如下用户数据:
$( document ).ready(function() {
var pageURL = window.location.href;
pageURL = pageURL.toString();
// Gets url strings
var paramIndex = pageURL.indexOf("#"); // When page is hosted on the web, use '?'
if (paramIndex === -1) {
return;
}
// Gets url parameters from AWS Cognito response including the 'access token'
var parameters = pageURL.substring(paramIndex + 1);
console.log(" page url: " + pageURL);
console.log(" url parameters: " + parameters);
// Extracts the encoded tokens from url parameters
var idToken = getParameter(parameters, "id_token=");
var accessToken = getParameter(parameters, "access_token=");
console.log("id token: " + idToken);
console.log("access token: " + accessToken);
// Decodes the tokens
var idTokenDecoded = atob(idToken.split('.')[1]);
var accessTokenDecoded = atob(accessToken.split('.')[1]);
console.log("id token decoded: " + idTokenDecoded);
console.log("access token decoded: " + accessTokenDecoded);
// Converts string tokens to JSON
var idTokenJson = JSON.parse(idTokenDecoded);
var accessTokenJson = JSON.parse(accessTokenDecoded);
// Can now access the fields as such using the JSON.parse()
console.log("email: " + idTokenJson.email);
console.log("id: " + idTokenJson.sub);
});
/**
* Takes the url parameters and extracts the field that matches the "param"
* input.
* @param {type} url, contains URL parameters
* @param {type} param, field to look for in url
* @returns {unresolved} the param value.
*/
function getParameter(url, param) {
var urlVars = url.split('&');
var returnValue;
for (var i = 0; i < urlVars.length; i++) {
var urlParam = urlVars[i];
// get up to index.
var index = urlParam.toString().indexOf("=");
urlParam = urlParam.substring(0, index + 1);
if (param === urlParam) {
returnValue = urlVars[i].replace(param, "");
i = urlVars.length; // exits for loop
}
}
return returnValue;
}
我确实有这段视频解释了以上内容,希望能有所帮助。