AWS Cognito访问令牌Javascript

时间:2017-10-17 18:17:46

标签: javascript amazon-web-services access-token amazon-cognito aws-cognito

我正在使用与Alexa的帐户关联并返回一个accessToken。我正在使用AWS Cognito进行身份验证。我的假设是accessToken是AWS Cognito的令牌 - 但我该如何使用它?我需要获取CognitoUser信息。我见过使用Facebook SDK的例子,说Fb.setToken(accessToken)很简单,但我找不到Cognito的等价物。我错过了什么?!

4 个答案:

答案 0 :(得分:1)

我来晚了一点,但是您可以从URL获取AWS Cognito JSON Web令牌(JWT)响应,并将其解码以获取用户数据,例如:

import java.awt.Frame;
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;

public class ResponsiveUI extends Frame
{

    public final Object lock = new Object();  // POINT #2 : signaling mechanism
    public final Button btn = new Button("abcde");

    public ResponsiveUI()
    {
        add(btn);

        btn.addActionListener((ActionEvent e) ->
        {
            // POINT #2 : signal the main() thread that button is clicked
            synchronized (lock)
            {
                lock.notify();
            } 
        });
    }

    public static void main(String[] args)
    {
        ResponsiveUI rui = new ResponsiveUI();

        // POINT #1: put UI into separate thread, so we can keep it responsive
        // POINT #1: I still do not know how to properly join() (it works OK though)
        Runnable r = () ->
        {
            rui.addWindowListener(new WindowAdapter()
            {
                @Override
                public void windowClosing(WindowEvent we)
                {
                    System.exit(0);
                }
            });
            rui.setSize(250, 150);
            rui.setResizable(false);
            rui.setVisible(true);
        };

        EventQueue.invokeLater(r);

        try
        {
            synchronized (rui.lock)    // POINT #2  
            {                          // POINT #2 
                rui.lock.wait();       // POINT #2 : wait for button press

                final Button b = new Button();  // POINT #4 : EventQueue uses final local variables
                                                // store text into temp button (ugly but works)
                EventQueue.invokeAndWait(() ->  // POINT #4
                {
                    b.setLabel(rui.btn.getLabel());  
                });
                // we could do all kind of things, but for illustrative purpose just transform text into upper case
                EventQueue.invokeAndWait(() ->    // POINT #3 :
                {
                    rui.btn.setLabel(b.getLabel().toUpperCase());

                });
            }
        }
        catch (InterruptedException | InvocationTargetException ex)
        {
            System.out.println("Error : " + ex);
        }
    }
}

答案 1 :(得分:0)

这是我的身份验证流程,仅使用cognito,对我来说很好用:

  var authenticationData = {
    Username: document.getElementById("user").value,
    Password: document.getElementById("password").value
  };

  var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);

  var poolData = {
    UserPoolId: AWSConfiguration.UserPoolId,
    ClientId: AWSConfiguration.ClientAppId
  };

  userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

  var userData = {
    Username: document.getElementById("user").value,
    Pool: userPool
  };

  var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);

  cognitoUser.authenticateUser(authenticationDetails, {

  // authenticate here

答案 2 :(得分:0)

只需解码Alexa技能Lambda函数中的Cognito访问令牌即可。

https://github.com/awslabs/aws-support-tools/tree/master/Cognito/decode-verify-jwt

此外,您可以通过使用预令牌生成Lambda触发器在用户身份验证时向该jwt令牌添加属性:

https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html

答案 3 :(得分:0)

AWS Cognito用户池为身份验证机制生成ID令牌和访问令牌。它们都是jwt令牌,而id令牌则具有用户属性,如username,email,family name。您可以使用id或访问令牌来认证用户。

相关链接:https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html

https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html