简体   繁体   English

如何直接打开 Amazon Cognito 托管 UI 的注册页面?

[英]How to open the sign up page of an Amazon Cognito Hosted UI directly?

In an app I am trying to have separate buttons to link to the sign up and sign in pages respectively of an Amazon Cognito User Pools' Hosted UI but so far I can only link to the sign in page.在一个应用程序中,我尝试使用单独的按钮分别链接到 Amazon Cognito 用户池的托管 UI 的注册和登录页面,但到目前为止我只能链接到登录页面。

I am using the AWS Amplify Package from npm and my code may look somehow like the following:我正在使用来自 npm 的AWS Amplify Package ,我的代码可能类似于以下内容:

import { Auth } from "aws-amplify";

//...
function openSignIn() {
  Auth.federatedSignIn();
}

function openSignUp() {
  // ???
}

I have found no federatedSignUp() or a function that would accept options regarding it.我没有发现federatedSignUp()或 function 会接受有关它的选项。

The url of the sign up page is:注册页面的url是:

<domain>/signup?redirect_uri=<redirect_uri>&response_type=<response_typ>&client_id=<client_id>&identity_provider=<identity_provider>&scopes=<scopes>&state=<state>

and while I know all parameters' values I don't know the value of the state param which makes it difficult to use it immediately in an anchor tag even though I don't like this solution.虽然我知道所有参数的值,但我不知道state参数的值,这使得即使我不喜欢这个解决方案,也很难在锚标记中立即使用它。

Is there a proper/elegant solution at all?是否有适当/优雅的解决方案?

These values are ones you can generate yourself so long as they follow the proper patterns and you can make them available later in the OAuth process.这些值是您可以自己生成的值,只要它们遵循正确的模式,并且您可以稍后在 OAuth 过程中使用它们。 The values required and their format depend on your specific Cognito implementation for that hosted UI / user pool and how you are using it.所需的值及其格式取决于您对该托管 UI/用户池的特定 Cognito 实现以及您使用它的方式。

Here is some code (pseudo nodejs) to get you started:这是一些让您入门的代码(伪 nodejs):

    var crypto = require('crypto');

    function getRandomString () {
          const randomItems = new Uint32Array(28);
          var bytes = crypto.randomBytes(28);
          randomItems.set(bytes);
          const binaryStringItems = randomItems.map(dec => `0${dec.toString(16).substr(-2)}`)
          return binaryStringItems.reduce((acc, item) => `${acc}${item}`, '');
    }
    
    var state = getRandomString();
    var code_verifier = getRandomString();
    var code_challenge = crypto.createHash('sha256').update(code_verifier).digest('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
    
    var redirect_url  = "https://"+domain+".auth."+region+".amazoncognito.com/oauth2/authorize?response_type=code&state="+state+"&client_id="+appClientId+"&redirect_uri="+redirectURI+"&scope=openid&code_challenge_method=S256&code_challenge="+code_challenge;

This assumes a number of things about the Cognito setup and how you are leveraging it (eg scope is openid, challenge is S256) but hopefully it can guide you a little.这假设了许多关于 Cognito 设置的事情以及您如何利用它(例如,范围是 openid,挑战是 S256),但希望它可以对您有所指导。 You will need to store some of these things locally before passing them on:在传递它们之前,您需要在本地存储其中一些东西:

https://betterprogramming.pub/how-to-securely-implement-authentication-in-single-page-applications-670534da746f https://betterprogramming.pub/how-to-securely-implement-authentication-in-single-page-applications-670534da746f

Sorry this isn't more Amplify specific.抱歉,这不是更具体的 Amplify。

I'm not sure this counts as elegant, but here is a way:我不确定这算不算优雅,但这是一种方法:

  1. Override the Amplify Auth urlOpener configuration.覆盖 Amplify Auth urlOpener配置。
  2. Initiate Auth.federatedSignIn()启动Auth.federatedSignIn()
  3. Modify the captured URL of the form /oauth2/authorize?修改抓取的URL 形式为/oauth2/authorize? and replace with /signup?并替换为/signup? . .
  4. Open the URL.打开 URL。
  5. This will initiate Cognito Hosted UI sign-up and then proceed as normal.这将启动 Cognito 托管 UI 注册,然后照常进行。

Notes:笔记:

  1. Details on Cognito Hosted UI URLs are here .有关 Cognito 托管 UI URL 的详细信息,请参见此处
  2. Details on Amplify Auth config here and here . 此处此处有关 Amplify Auth 配置的详细信息。
  3. The default launchUri implementation just uses window.open() - see here .默认的launchUri实现仅使用window.open() - 请参见此处

More detail on implementation below.有关实施的更多详细信息,请参见下文。

Define your own urlOpener :定义您自己的urlOpener

const urlOpener = async (url: string, redirectUrl: string): Promise<any> => {
  const signupUrl = url.replace(/\/oauth2\/authorize\?/, '/signup?');
  return launchUri(signupUrl);
};

Configure Amplify:配置放大:

const config = {
    Auth: {
        ...
        oauth: {
            ...
            urlOpener,
        },
    },
};
Amplify.configure(config);

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

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