简体   繁体   English

AWS Cognito 如何在收到授权码后查询 JWT 令牌

[英]AWS Cognito how to query for the JWT Token after receiving an authorization code

my application requires an authorization code grant flow integration with Cognito and the website responded to me that Auth-Code accordingly.我的应用程序需要授权码授权流程与 Cognito 集成,网站相应地回复了我该授权码。

https://<poolName>.auth.eu-central-1.amazoncognito.com/login?client_id=<clientID>&response_type=code&scope=email+openid+profile&redirect_uri=<redirectURI>

Now I want to know what email address has logged in. As fas as I understood, this is where I would need the JWT token.现在我想知道 email 地址登录了什么。据我所知,这是我需要 JWT 令牌的地方。

How can I query the email adress of the token I have just received?如何查询刚刚收到的token的地址email?

Any help is much appreciated!任何帮助深表感谢!

As I was going through that, actually, I still am.实际上,当我经历这些时,我仍然是。 I have talked a lot with Amazon Engineers for past weeks, have done a bunch of research on my own and let me clarify couple of things.在过去的几周里,我与 Amazon 工程师进行了很多交谈,自己进行了大量研究,并让我澄清几件事。

  1. Hosted UI is a way to go if you can accept the limitations.如果您可以接受限制,托管 UI 是一种通往 go 的方式。 This is after all hosted ui, you can only change that much.这毕竟是托管用户界面,您只能更改那么多。 BUT (:..,) and I can not stretch this enough.但是 (:..,) 而且我无法充分伸展它。 it works only for simple usage.它仅适用于简单使用。 If you start searching you'll see that it doesn't support CUSTOM_AUTH flows which is extremely useful if you want to implement even something as basic as MFA.如果你开始搜索,你会发现它不支持 CUSTOM_AUTH 流,如果你想实现像 MFA 这样基本的东西,这将非常有用。 So if you dream of Revolut-like login page with just phone number email verification - pity, hosted ui will not help.因此,如果您梦想仅通过电话号码 email 验证的类似 Revolut 的登录页面 - 遗憾的是,托管 ui 将无济于事。
  2. Hosted UI is more than just UI.托管 UI 不仅仅是 UI。 It's a whole server!这是一个完整的服务器! That's why you can't simply replace it.这就是为什么你不能简单地更换它。
  3. Now, as we established what hosted UI can't do.现在,我们确定了托管 UI 不能做什么。 What are the alternatives?有哪些选择? Of course, you can use other providers like Okta or Auth0, but I assume, you're here because you want to use AWS.当然,您可以使用 Okta 或 Auth0 等其他提供商,但我假设您来这里是因为您想使用 AWS。 But the recommended (by AWS) alternative is to actually implement your own authentication using Amplify SDK. It's quite simple to use, I must say that.但是(AWS)推荐的替代方法是使用 Amplify SDK 实际实施您自己的身份验证。它使用起来非常简单,我必须这么说。 But what they don't tell you explicitly, is that it's no longer OIDC flow.但他们没有明确告诉你的是,它不再是 OIDC 流程。 Instead, AWS suggests to use their custom flows, such as USER_PASSWORD flow or SRP (Secure Remote Password), where password doesn't fly over http(s) at all.相反,AWS 建议使用他们的自定义流程,例如 USER_PASSWORD 流程或 SRP(安全远程密码),其中密码根本不会通过 http(s)。 You might ask: can't I have OIDC with Cognito AND custom flows?您可能会问:我不能拥有带有 Cognito 和自定义流程的 OIDC 吗? Well... you can, but it's not that simple.好吧……你可以,但没那么简单。 Long story short, you can use both hosted ui and amplify and possibly create your own cool SSO.长话短说,您可以同时使用托管 ui 和 amplify,并可能创建自己的酷 SSO。 For details look at their github page where AWS Labs go through some details.有关详细信息,请查看他们的github 页面,其中 AWS Labs go 通过一些详细信息。
  4. If you can't afford spending next 2 months working on SSO, but you don't necessarily need OIDC flow and get settle for another solution, you can easily go for SRP or USERNAME_PASSWORD flows.如果您无力承担接下来 2 个月在 SSO 上的花费,但您不一定需要 OIDC 流程并接受其他解决方案,您可以轻松地通过 go 获得 SRP 或 USERNAME_PASSWORD 流程。
  5. If you're like me and you're migrating from the old legacy authentication system done... wherever (;)), go for USERNAME_PASSWORD and utilise user migration lambda trigger in Cognito, where you can automatically migrate users once they login with their old credentials!如果你像我一样,并且你正在从旧的遗留身份验证系统迁移完成......无论在哪里(;)),USERNAME_PASSWORD go 并在 Cognito 中使用用户迁移 lambda 触发器,你可以在用户使用他们的登录后自动迁移用户旧证件! Neat整洁的
  6. AWS Cognito is full of traps... Consider that as well AWS Cognito 充满了陷阱……也请考虑一下

First, make sure your Cognito client includes the email OAuth scope. User Pools > my-user-pool > App client settings > Allowed OAuth Scopes.首先,确保您的 Cognito 客户端包含email OAuth scope。用户池 > 我的用户池 > 应用程序客户端设置 > 允许的 OAuth 范围。

Then, decode the id token and you will have the email. You can use JWT.io to quickly decode tokens for testing and development.然后,解码id 令牌,您将获得 email。您可以使用JWT.io快速解码令牌以进行测试和开发。

UPDATE: You can use the POST /oauth2/token endpoint to fetch the tokens.更新:您可以使用POST /oauth2/token端点来获取令牌。 But in general, if you're creating a frontend for users, it's better to use someone else's UI.但一般来说,如果您要为用户创建前端,最好使用其他人的 UI。 The Cognito hosted UI works , although it looks a bit dated and it doesn't support MFA/TOTP . Cognito 托管 UI 有效,尽管它看起来有点过时并且不支持 MFA/TOTP The modern approach is to use the Amplify UI Authenticator component, which supports TOTP and all the flows you'd expect (sign-up, password reset, etc).现代方法是使用 Amplify UI Authenticator组件,它支持 TOTP 和您期望的所有流程(注册、密码重置等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM