使用 JavaScript SDK 的 AWS Cognito 开发人员身份验证
AWS Cognito Developer Authenticated Identities using JavaScript SDK
我需要使用 JavaScript SDK 实施开发人员身份验证,但我遇到了问题。我已使用自定义身份验证提供程序配置身份池
在服务器上:
AWS.config = new AWS.Config({
region: 'ap-northeast-2',
credentials: new AWS.Credentials('XXXXXS7FJBAOO5IXXXXX', 'XXXXXYBo4jmfsu7K0qJSFvu3nlVvYOcVz4GXXXXX')
});
var params = {
IdentityPoolId: 'ap-northeast-2:a383cb2e-e302-4ff6-8d8f-70e3185XXXXX',
Logins: {
'com.abc.xyz': '9876543210' // different value for each user
}
};
var cognitoidentity = new AWS.CognitoIdentity();
cognitoidentity.getOpenIdTokenForDeveloperIdentity(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
}
});
服务器结果:
IdentityId: "ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX"
Token: "eyJra.....sL8bg"
在浏览器上:
AWS.config = new AWS.Config({
region: 'ap-northeast-2'
});
var params = {
IdentityId: 'ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX', //Received from server
CustomRoleArn: 'arn:aws:iam::356127965XXX:role/XXXXX_Customer',
Logins: {
'com.abc.xyz': '9876543210'
}
};
var cognitoidentity = new AWS.CognitoIdentity();
cognitoidentity.getCredentialsForIdentity(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
}
});
浏览器结果:
Please provide a valid public provider
身份池配置
基于this post,我在浏览器部分做了如下修改
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityId: 'ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX', //Received from server
IdentityPoolId: 'ap-northeast-2:a383cb2e-e302-4ff6-8d8f-70e3185XXXXX',
Logins: {
'cognito-identity.amazonaws.com': '9876543210'
}
});
AWS.config.credentials.get(function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
console.log(data); // successful response
}
});
AWS.config.credentials
现在我能够收到包含 accessKeyId、expireTime、secretAccessKey 和 sessionToken 的响应
我知道这是一个旧的 post,但万一有人遇到这个问题,我相信如果您进行更改,您的第一种方法会奏效:
Logins: {
'com.abc.xyz': '9876543210'
}
至
Logins: {
'cognito-identity.amazonaws.com': "eyJra.....sL8bg"
}
我觉得任何不使用您在步骤 1) 中生成的令牌的解决方案都是不完整的。
我需要使用 JavaScript SDK 实施开发人员身份验证,但我遇到了问题。我已使用自定义身份验证提供程序配置身份池
在服务器上:
AWS.config = new AWS.Config({
region: 'ap-northeast-2',
credentials: new AWS.Credentials('XXXXXS7FJBAOO5IXXXXX', 'XXXXXYBo4jmfsu7K0qJSFvu3nlVvYOcVz4GXXXXX')
});
var params = {
IdentityPoolId: 'ap-northeast-2:a383cb2e-e302-4ff6-8d8f-70e3185XXXXX',
Logins: {
'com.abc.xyz': '9876543210' // different value for each user
}
};
var cognitoidentity = new AWS.CognitoIdentity();
cognitoidentity.getOpenIdTokenForDeveloperIdentity(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
}
});
服务器结果:
IdentityId: "ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX"
Token: "eyJra.....sL8bg"
在浏览器上:
AWS.config = new AWS.Config({
region: 'ap-northeast-2'
});
var params = {
IdentityId: 'ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX', //Received from server
CustomRoleArn: 'arn:aws:iam::356127965XXX:role/XXXXX_Customer',
Logins: {
'com.abc.xyz': '9876543210'
}
};
var cognitoidentity = new AWS.CognitoIdentity();
cognitoidentity.getCredentialsForIdentity(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
}
});
浏览器结果:
Please provide a valid public provider
身份池配置
基于this post,我在浏览器部分做了如下修改
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityId: 'ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX', //Received from server
IdentityPoolId: 'ap-northeast-2:a383cb2e-e302-4ff6-8d8f-70e3185XXXXX',
Logins: {
'cognito-identity.amazonaws.com': '9876543210'
}
});
AWS.config.credentials.get(function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
console.log(data); // successful response
}
});
AWS.config.credentials
现在我能够收到包含 accessKeyId、expireTime、secretAccessKey 和 sessionToken 的响应
我知道这是一个旧的 post,但万一有人遇到这个问题,我相信如果您进行更改,您的第一种方法会奏效:
Logins: {
'com.abc.xyz': '9876543210'
}
至
Logins: {
'cognito-identity.amazonaws.com': "eyJra.....sL8bg"
}
我觉得任何不使用您在步骤 1) 中生成的令牌的解决方案都是不完整的。