将 AWS Cognito 与 google 集成以及使用 React 自定义登录的步骤
Steps to integrate AWS Cognito with both google and custom login with React
嗨,谁能告诉我为自定义 (username/password) 和 google 登录集成 AWS Cognito 的步骤?我能够让自定义登录正常工作。我能够毫无问题地配置用户池。我只是在徘徊客户端的变化。
我使用 amazon-cognito-identity-js 和 aws-sdk 包。
以下是我使用的一些代码片段。在这里,我使用一个参数来决定身份验证器。
如果你们中的任何人可以提供任何反馈,那就太好了。谢谢你。
export function getAwsCredentials(userToken, type) {
let authenticator = '';
if (type == 'CUSTOM') {
authenticator = `cognito-idp.${config.cognito
.REGION}.amazonaws.com/${config.cognito.USER_POOL_ID}`;
}
else if (type == 'GOOGLE') {
authenticator = 'accounts.google.com';
}
AWS.config.update({ region: config.cognito.REGION });
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: config.cognito.IDENTITY_POOL_ID,
Logins: {
[authenticator]: userToken
}
});
return AWS.config.credentials.getPromise();
}
当谈到联合身份池时,您确实可以对所有令牌一视同仁。您只需更改登录映射中的 [authenticator]
。
据推测,调用您的 getAwsCredentials
函数的代码会知道令牌的来源,并且它可以传入一个参数,就像您在上面所做的那样。
我想提一下,我认为这不一定是一种糟糕的方法。我可能会直接传递 [authenticator]
值,而不是 type
,但这是相对较小的事情。我的主要观点是,可能没问题让知道令牌来自何处的调用代码而不是被调用函数负责。
使用令牌确定身份提供者
当然,有一些同样有效的方法可能想要或需要将身份提供者从令牌本身中提取出来。
当您 integrate with Google directly 时,您会从 Google 获得一个令牌,然后将其传递给 Cognito(如上面的代码所示)。如果你解码令牌,你会发现
"iss": "accounts.google.com",
在某处。
同样,当您直接与用户池集成时,您从用户池中获得的令牌(您稍后将其提供给身份池)包含如下内容:
"iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxxxxxx",
这样您就可以通过检查用户的令牌来判断用户是如何登录的。
Cognito 托管注意事项UI
如果您使用 Cognito 的托管 UI 通过 Google 登录,您将从 Cognito 返回一个 access_token
和一个 id_token
(通过重定向)。在这种情况下,所有 令牌来自 same 地方 (Cognito),这意味着您不能依赖任何调用代码来了解谁是真正的身份提供者。
在这种情况下,如果您的应用程序关心 IDP 是谁,则您必须打开令牌。 id_token
的这一部分可能是您想要的:
"identities": [
{
"userId": "100000000000000000000",
"providerName": "Google",
"providerType": "Google",
"issuer": null,
"primary": "true",
"dateCreated": "1507749926267"
}
],
"token_use": "id",
"auth_time": 1509303233,
"name": "Your User",
"exp": 1509306833,
"iat": 1509303233,
"email": "youruser@gmail.com"
嗨,谁能告诉我为自定义 (username/password) 和 google 登录集成 AWS Cognito 的步骤?我能够让自定义登录正常工作。我能够毫无问题地配置用户池。我只是在徘徊客户端的变化。
我使用 amazon-cognito-identity-js 和 aws-sdk 包。
以下是我使用的一些代码片段。在这里,我使用一个参数来决定身份验证器。 如果你们中的任何人可以提供任何反馈,那就太好了。谢谢你。
export function getAwsCredentials(userToken, type) {
let authenticator = '';
if (type == 'CUSTOM') {
authenticator = `cognito-idp.${config.cognito
.REGION}.amazonaws.com/${config.cognito.USER_POOL_ID}`;
}
else if (type == 'GOOGLE') {
authenticator = 'accounts.google.com';
}
AWS.config.update({ region: config.cognito.REGION });
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: config.cognito.IDENTITY_POOL_ID,
Logins: {
[authenticator]: userToken
}
});
return AWS.config.credentials.getPromise();
}
当谈到联合身份池时,您确实可以对所有令牌一视同仁。您只需更改登录映射中的 [authenticator]
。
据推测,调用您的 getAwsCredentials
函数的代码会知道令牌的来源,并且它可以传入一个参数,就像您在上面所做的那样。
我想提一下,我认为这不一定是一种糟糕的方法。我可能会直接传递 [authenticator]
值,而不是 type
,但这是相对较小的事情。我的主要观点是,可能没问题让知道令牌来自何处的调用代码而不是被调用函数负责。
使用令牌确定身份提供者
当然,有一些同样有效的方法可能想要或需要将身份提供者从令牌本身中提取出来。
当您 integrate with Google directly 时,您会从 Google 获得一个令牌,然后将其传递给 Cognito(如上面的代码所示)。如果你解码令牌,你会发现
"iss": "accounts.google.com",
在某处。
同样,当您直接与用户池集成时,您从用户池中获得的令牌(您稍后将其提供给身份池)包含如下内容:
"iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxxxxxx",
这样您就可以通过检查用户的令牌来判断用户是如何登录的。
Cognito 托管注意事项UI
如果您使用 Cognito 的托管 UI 通过 Google 登录,您将从 Cognito 返回一个 access_token
和一个 id_token
(通过重定向)。在这种情况下,所有 令牌来自 same 地方 (Cognito),这意味着您不能依赖任何调用代码来了解谁是真正的身份提供者。
在这种情况下,如果您的应用程序关心 IDP 是谁,则您必须打开令牌。 id_token
的这一部分可能是您想要的:
"identities": [
{
"userId": "100000000000000000000",
"providerName": "Google",
"providerType": "Google",
"issuer": null,
"primary": "true",
"dateCreated": "1507749926267"
}
],
"token_use": "id",
"auth_time": 1509303233,
"name": "Your User",
"exp": 1509306833,
"iat": 1509303233,
"email": "youruser@gmail.com"