AWS Cognito - 如何从令牌对象获取用户组
Posted
技术标签:
【中文标题】AWS Cognito - 如何从令牌对象获取用户组【英文标题】:AWS Cognito - How To Get User's Group From Token Object 【发布时间】:2019-03-23 12:09:12 【问题描述】:登录后,我可以在控制台返回的用户数据对象中看到用户分配的用户池组。我尝试将组分配给变量的所有操作都失败了。我错过了什么?我可以轻松获取 client_id、JWT 和属性。
对象路径:
let userGroup = user.signInUserSession.accessToken.payload.cognito.groups[0];
但是,这是可行的:
user.signInUserSession.accessToken.payload.client_id;
对象的这一部分看起来像这样。我可以检索令牌、用户名,几乎可以检索除组之外的任何内容。
signInUserSession: CognitoUserSession
accessToken: CognitoAccessToken
jwtToken: ...
payload:
auth_time: 1539824552
client_id: "5u7o1o1...."
cognito:groups: Array(1)
0: "admin-full"
length: 1
... more properties here...
token_use: "access"
username: "me"
我正在使用 Angular 6 和 AWS-Amplify,但我怀疑这是否重要。我的部分代码:
constructor(
private amplifyService: AmplifyService,
)
this.amplifyService.auth().currentAuthenticatedUser()
.then(user =>
console.log('user data in checkLogin: ', user); // The whole object.
console.log('user token in checkLogin: ', user.signInUserSession.accessToken.jwtToken); // Retrieves the token.
我的整个会话对象需要澄清:
CognitoUser username: "me", pool: CognitoUserPool, Session: null, client: Client, signInUserSession: CognitoUserSession, …
Session: null
attributes: phone_number: "########", locale: "Silicon Valley", given_name: "J", family_name: "Preston", email: "j@whatever.com"
authenticationFlowType: "USER_SRP_AUTH"
client: Client endpoint: "https://cognito-idp.us-west-2.amazonaws.com/", userAgent: "aws-amplify/0.1.x js"
pool: CognitoUserPool userPoolId: "us-west-2_UW9KsJm0o", clientId: "5u7o1o1v288e...", client: Client, advancedSecurityDataCollectionFlag: true, storage: Storage
preferredMFA: "NOMFA"
signInUserSession: CognitoUserSession
accessToken: CognitoAccessToken
jwtToken: "eyJraWQiOiJwaUdRSnc..."
payload:
auth_time: 1539881072
client_id: "5u7o1o1v2..."
cognito:groups: ["admin"]
event_id: "168d9cc3-d2f5-11e8-ae71-f734087f965a"
exp: 1539884672
iat: 1539881072
iss: "https://cognito-idp.us-west-2.amazonaws.com/us-west-2_UW9KsJm0o"
jti: "13c4c552-ee70-4f8a-b64d-f95e0bdc81cf"
scope: "aws.cognito.signin.user.admin openid"
sub: "96a047b2-ae9a-42ff-af09-fc1c0802b88f"
token_use: "access"
username: "me"
version: 2
__proto__: Object
__proto__: CognitoJwtToken
clockDrift: 0
idToken: CognitoIdToken jwtToken: "eyJraWQiOiI...", payload: …
refreshToken: CognitoRefreshToken token: ""
__proto__: Object
storage: Storage CognitoIdentityServiceProvider.5u7o1o1....LastAuthUser: "me", CognitoIdentityServiceProvider.5u7o1o1v....jimadmin.accessToken: "eyJraWQiOiJ...", CognitoIdentityServiceProvider.5u7o1o1....jimadmin.idToken: "eyJraWQiOiI3...", CognitoIdentityServiceProvider.5u7o1o1....jimadmin.refreshToken: "", CognitoIdentityServiceProvider.5u7o1o1....jimadmin.tokenScopesString: "email openid aws.cognito.signin.user.admin", …
username: "me"
【问题讨论】:
【参考方案1】:如果有人在使用 Amplify
Auth.currentAuthenticatedUser()
.then(data => console.log(data.signInUserSession.accessToken.payload['cognito:groups']));
【讨论】:
【参考方案2】:该组位于会话对象和 idToken Payload 中,如下所示。
如果您的用户不在组中,则该组不存在。
你应该可以像accessToken.payload['cognito:groups'];
一样访问它
我碰巧有一个 cognito session 对象供组中的用户使用,它显示了所有令牌及其所有有效负载。 以及获取用户属性时得到的结果。
会话:
"idToken":
"jwtToken":"eyJraWQiOiJQS1wvMHNNMlk...",
"payload":
"sub":"ceb234234-b0e0-4c3d-8abc-af08c002b4de",
"cognito:groups":[
"user"
],
"email_verified":true,
"iss":"https://cognito-idp.us-east-2.amazonaws.com/us-east-2_sinJIhGA8",
"phone_number_verified":false,
"cognito:username":"ceba8336-b0e0-4c3d-8abc-af08c002b4de",
"aud":"203e1rl2o1d8d5chhs9v6s1i79",
"event_id":"89502ffe-d2fe-11e8-8427-1b3482253d90",
"token_use":"id",
"auth_time":1539885130,
"exp":1539888730,
"iat":1539885130,
"email":"r32423423@icloud.com"
,
"refreshToken":
"token":"eyJjdHkiOiJKV1QiLCJlb..."
,
"accessToken":
"jwtToken":"eyJraWQiOiI4N2pRRnpqSm..",
"payload":
"sub":"ceba8336-b0e0-4c3d-8abc-af08c002b4de",
"device_key":"us-east-2_94234234234b-4cec-ae49-b1f90555d979",
"cognito:groups":[
"user"
],
"iss":"https://cognito-idp.us-east-2.amazonaws.com/us-east-2_sinJIhGA8",
"client_id":"203e1rl223423hhs9v6s1i79",
"event_id":"895234fe-d2fe-11e8-8427-1b3482253d90",
"token_use":"access",
"scope":"aws.cognito.signin.user.admin",
"auth_time":1539885130,
"exp":1539888730,
"iat":1539885130,
"jti":"936fd8f9-c091-4f642345f-ba9454f16b9c",
"username":"ceba83362342-4c3d-8abc-af08c002b4de"
,
"clockDrift":0
用户属性:
"details":[
"Name":"sub",
"Value":"ceba8336-4234-4c3d-8abc-af08c002b4de"
,
"Name":"email_verified",
"Value":"true"
,
"Name":"phone_number_verified",
"Value":"false"
,
"Name":"email",
"Value":"r23423423@icloud.com"
]
【讨论】:
分配的组在 Cognito 返回的对象中。我可以在那里看到它以及所有其他数据和 JWT。由于它存在于那里,我想用它来确定用户可以访问应用程序的哪些部分。我将通过编辑进一步构建返回的对象以使其清晰。 在我的答案中添加更多内容。 我的控制台中的属性几乎相同,但我也有 cognito:groups。这就是我在上面展示的。我不知道您为什么不包含这些组。为了清楚起见,我将编辑并添加一些代码。 答案是:this.userGroup = user.signInUserSession.accessToken.payload['cognito:groups']; var 包含一个包含我的用户组的数组。谢谢!我之前尝试过 [] 但不是''。他们有所作为。 好吧,我们把这个问题的所有内容都放在那里,希望它能帮助其他人。 :-)以上是关于AWS Cognito - 如何从令牌对象获取用户组的主要内容,如果未能解决你的问题,请参考以下文章
Swift - AWS Cognito 使用 Amplify - 如何在 swift 登录后获取令牌?