当前位置: 首页 > 知识库问答 >
问题:

amazoncognito:如何验证访问令牌的签名?

尉迟清野
2023-03-14

文件上说https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html

必须验证访问令牌。如何验证令牌?

共有1个答案

赖渊
2023-03-14

如果您使用的是Java,那么您就很幸运了。我编程的方式是,

    // Parse the Cognito Keys and get the key by kid
    // Key is just a class that is used for parsing JSON to POJO
    Key key = this.keyService.getKeyByKeyId(JWT.decode(token).getKeyId());

    // Use Key's N and E
    BigInteger modulus = new BigInteger(1, Base64.decodeBase64(key.getN()));
    BigInteger exponent = new BigInteger(1, Base64.decodeBase64(key.getE()));

    // Create a publick key
    PublicKey publicKey = null;
    try {
        publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, exponent));
    } catch (InvalidKeySpecException e) {
        // Throw error
    } catch (NoSuchAlgorithmException e) {
        // Throw error
    }

    // get an algorithm instance
    Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) publicKey, null);

    // I verify ISS field of the token to make sure it's from the Cognito source
    String iss = String.format("https://cognito-idp.%s.amazonaws.com/%s", REGION, POOL_ID);

    JWTVerifier verifier = JWT.require(algorithm)
            .withIssuer(iss)
            .withClaim("token_use", "id") // make sure you're verifying id token
            .build();

    // Verify the token
    DecodedJWT jwt = verifier.verify(token);

    // Parse various fields
    String username = jwt.getClaim("sub").asString();
    String email = jwt.getClaim("email").asString();
    String phone = jwt.getClaim("phone_number").asString();
    String[] groups = jwt.getClaim("cognito:groups").asArray(String.class);

我正在使用这个repo来验证和解析令牌,

    <dependency>
        <groupId>com.auth0</groupId>
        <artifactId>java-jwt</artifactId>
        <version>3.4.1</version>
    </dependency>

请确保您正在导入以下内容,

    import com.auth0.jwt.JWT;
    import com.auth0.jwt.JWTVerifier;
    import com.auth0.jwt.algorithms.Algorithm;
    import com.auth0.jwt.exceptions.JWTDecodeException;
    import com.auth0.jwt.interfaces.DecodedJWT;

    import java.math.BigInteger;
    import java.security.KeyFactory;
    import java.security.NoSuchAlgorithmException;
    import java.security.PublicKey;
    import java.security.interfaces.RSAPublicKey;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.RSAPublicKeySpec;
    import org.apache.commons.codec.binary.Base64;
 类似资料:
  • 我在使用jwt.io验证我的azure广告访问令牌时获得无效签名(在手动检查后将转移到scala代码)。 我正在使用 curl 生成访问令牌: 虽然它为我提供了访问令牌,但响应不包含“Id_token”。不知道为什么。 我正在使用 BEGIN 和 END 证书包装 https://login.microsoftonline.com/common/discovery/keys 中的公钥。(如 htt

  • 我正在使用以下示例来玩Spring Cloud OAuth2实现: https://github.com/spring-cloud-samples/authserver https://github.com/spring-cloud-samples/sso 第一个是OAuth服务器,它在对用户进行身份验证时生成JWT令牌。第二个是正在被消耗的资源。根据OAuth规范,资源将用户的身份验证转发给au

  • 我目前正在使用Vapor开发Swift后端。我的iOS客户端使用新的iOS 13功能“使用Apple登录”。当用户登录时,我会得到一个身份令牌(访问令牌),这是一个由Apple签名的有效JWT令牌。这将在所有正在进行的通信中发送到服务器,以验证服务器提供的某些路由。 在服务器上,我想通过验证令牌签名来验证发送的令牌是否确实由Apple签名,并且不是由某些恶意用户专门创建的。Apple提供了一个HT

  • 我使用IdentityServer4验证angular 4客户端以保护WebApi。但我在授权的情况下进入了401。我需要一种调试访问令牌验证过程的方法来帮助我解决问题。

  • 当客户端请求资源服务器使用OAuth2.0访问令牌获取受保护的资源时,该服务器如何验证该令牌?OAuth 2.0刷新令牌协议?

  • 当且仅当用户在发出OAuth2请求时登录到LinkedIn时,它才起作用。 如果用户未登录,则会遇到错误。 我们的行动顺序: 成功获取新的访问令牌 使用访问令牌,发布到apiendpoint 之后,我们将收到一份401,内容如下: 有时,经过一段时间后,使用相同的访问令牌重试会得到200。有时不会。 如果用户在“401期间”登录LinkedIn,那么之前获取的访问令牌就会神奇地开始工作。 我不知道