![](/img/trans.png)
[英]AWS Cognito Developer Authenticated Identities using JavaScript SDK
[英]AWS Cognito - Developer Authenticated Identities in JavaScript(Browser)
我在瀏覽器腳本中獲取憑據時遇到問題。
驗證服務器返回cognito_identityId和cognito_token。
然后我設置了一個Cookie:
我嘗試在瀏覽器上以4種方式獲取憑據,並且所有失敗:
CognitoIdentityCredentials
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'us-east-1:xxxxxxxxxxxx' IdentityId: $.cookie('cognito_identityId'), Logins: { 'myauth': $.cookie('cognito_token') } });
// =>錯誤:在params中缺少必需的鍵'IdentityId'
assumeRoleWithWebIdentity
var params = { RoleArn: 'arn:aws:iam::xxxxxxxxxxxx:role/Cognito_xxxxxxxAuth_Role', RoleSessionName: 'xxxxxxxxxxx', WebIdentityToken: $.cookie('cognito_token'), DurationSeconds: 900, ProviderId: 'myauth' }; var sts = new AWS.STS({apiVersion: '2011-06-15'}); sts.assumeRoleWithWebIdentity(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response });
// => AccessDenied:未授權執行sts:AssumeRoleWithWebIdentity
PolicyDocument
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "us-east-1:xxxxxxxxxxxxx"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}
}
]
}
GetCredentialsForIdentity
var params = { IdentityId: $.cookie('cognito_identityId'), Logins: { "myauth": $.cookie('oauth.io_token') } }; var cognitoidentity = new AWS.CognitoIdentity({apiVersion: '2014-06-30'}); cognitoidentity.getCredentialsForIdentity(params, function(err, data) { if (err) { console.log(err, err.stack); // an error occurred } else { console.log(data); // successful response } });
// => InvalidParameterException:請提供有效的公共提供者
WebIdentityCredentials
AWS.config.credentials = new AWS.WebIdentityCredentials({ RoleArn: 'arn:aws:iam::xxxxxxxx:role/Cognito_xxxxxxxxxxAuth_Role', WebIdentityToken: $.cookie('cognito_token') });
// =>錯誤:有2個驗證錯誤:// * MissingRequiredParameter:在params中缺少必需的鍵'IdentityPoolId'// MissingRequiredParameter:在params中缺少必需的鍵'IdentityId'
問題:
我究竟做錯了什么?
使用它的正確方法是什么?
謝謝。
謝謝你的好意。
我提出了你的建議,但沒有改變。
錯誤消息。
POST https://cognito-identity.us-east-1.amazonaws.com/ 400 (Bad Request)
POST https://cognito-identity.us-east-1.amazonaws.com/ 400 (Bad Request)
Error: Missing required key 'IdentityId' in params
at fail (chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:2163:37)
at validateStructure (chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:2084:14)
at validateMember (chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:2110:21)
at validate (chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:2059:10)
at Request.VALIDATE_PARAMETERS (chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:800:32)
at Request.callListeners (chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:3913:20)
at callNextListener (chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:3903:12)
at chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:787:9
at finish (chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:126:7)
at chrome-extension://hmjdjbikinkmjbilihjibcihbkbjdgjf/bower_components/aws-sdk-js/dist/aws-sdk.js:142:9
鏈接下面有源代碼。
https://github.com/bisque33/my-custom-dictionary
和服務器端是AWS Lambda函數。
var aws = require('aws-sdk');
aws.config.region = 'us-east-1';
var cognitoidentity = new aws.CognitoIdentity();
var identityPoolId = 'us-east-1:0dccff0d-5fd7-4d14-b38f-d27204feaecc';
console.log('Loading function');
exports.handler = function(event, context) {
console.log('token: %s', event.token);
var params = {
IdentityPoolId: identityPoolId,
Logins: {
'oauth.io': event.token
}
};
cognitoidentity.getOpenIdTokenForDeveloperIdentity(params,function(err,data){
if(err){
console.log(err);
context.fail('Something went wrong');
}else{
context.succeed(data);
}
});
};
此程序是Google-Chrome-Extension。
我用錯了嗎?
感謝您提供更多信息。
錯誤出現在background.js上的104行
AWS.config.credentials.get(function(){
和background.js上的115行
dataset.synchronize(
而且,我的解釋還不夠。 Facebook身份驗證需要域名(例如http:// example.com)。 但是,Google-Chrome-Ext沒有域名。 它有一個域'chrome-extension:// xxxxxxxxxxxxxxxxxxxx'。 然后,我使用https://oauth.io 。 它代理任何身份驗證並接受chrome-extension域。
Popup.js通過oauth.io sdk進行Facebook身份驗證。 它獲取了一個facebook令牌,並提供給getOpenIdTokenForDeveloperIdentity。 我認為facebook token.substr(0,14)是獨一無二的。 但是,如果它是錯的,我使用另一個唯一標識符(例如電子郵件地址。)
對不起我錯了。 AWS.config.credentials.get給出錯誤:
Error: Invalid login token.
並且,dataset.synchronize顯示此錯誤:
Error: Missing required key 'IdentityId' in params
使用CognitoIdentityCredentials的第一種方法很可能是您采用的最佳方法。 我無法確切地發現導致錯誤的原因,但我們可以嘗試以下幾點:
'cognito-identity.amazonaws.com'
並確保該值是從后端調用getOpenIdTokenForDeveloperIdentity
返回的標記。 如果您仍然遇到使用CognitoIdentityCredentials方法的問題,請在此處回復一些更多信息,例如您在收到錯誤消息時正在調用的確切方法/代碼,以及跟蹤輸出(即使用console.log('%o) ',..))在調用CognitoIdentityCredentials構造函數之前輸入的params。
我仍然需要確切地知道您收到錯誤的代碼行,但根據提供的信息,我認為我仍然可以幫助...
根據我在background.js中看到的內容,您似乎正在嘗試使用Developer Authenticated Identities提供程序初始化CognitoIdentityCredentials。 這是我猜你收到錯誤的地方。
但是,在Popup.js中 ,您似乎正在嘗試使用Facebook對用戶進行身份驗證。 如果您使用Facebook對用戶進行身份驗證,則應在使用Cognito時將facebook訪問令牌傳遞到您的登錄地圖中。 只需使用graph.facebook.com
作為登錄地圖中的密鑰和來自Facebook的訪問令牌。 有關如何執行此操作的更多詳細信息,請參閱Amazon Cognito開發人員指南的Facebook集成主題 。
我們可以讓Developer Authenticated Identities為您工作,但在這種情況下,它看起來不適合您,因為您實際上沒有對Lambda函數中的標識進行任何其他身份驗證以及唯一的用戶標識符你傳遞給getOpenIdTokenForDeveloperIdentity
操作似乎是facebook令牌,這是不好的,因為令牌本身將在用戶會話之間改變,即使對於同一個用戶也是如此。 通常,良好的唯一標識符是內部系統使用的電子郵件地址或用戶ID。
由於您最終嘗試使用Facebook進行登錄而Amazon Cognito已經內置了Facebook集成 ,因此您最好的辦法是從Facebook獲取訪問令牌並直接將Facebook令牌傳遞給Cognito的登錄地圖。 我不確定這是否適用於Auth.io(我只是不熟悉它),但只要Auth.io為您的JavaScript代碼提供一個bonefide facebook令牌並且您添加相同的Facebook App ID Auth.io和Amazon Cognito的控制台都應該可以使用。 但是,您提到要使用Auth.io來避免Facebook重定向到登錄頁面。 我可能會弄錯,但我很確定如果您使用Facebook的JavaScript SDK ,則不需要重定向頁面。 如果您正在進行Facebook的手動構建登錄流程,則只需要重定向頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.