代码之家  ›  专栏  ›  技术社区  ›  Adam Jungen

如何用认知键生成rsapublicey

  •  -1
  • Adam Jungen  · 技术社区  · 6 年前

    我需要生成一个公钥来验证来自Cognito的JWT令牌。这是钥匙:

    {
      keys: [
        {
          alg: "RS256",
          e: "AQAB",
          kid: "7sbG73+G/8A+wPT4Vaf/+ttnBHuLZcjEmfiBXl3E8Fk=",
          kty: "RSA",
          n: "blablablablabla",
          use: "sig"
        },
        {
          alg: "RS256",
          e: "AQAB",
          kid: "Zbf1euSHNj4lXgXHIQwYZfERTdm1Rkeqrm1ppnpClRR=",
          kty: "RSA",
          n: "blablablablabla",
          use: "sig"
        }
      ]
    }
    

    我可以很容易地用来自Azure的X5C密钥创建它,但是我不能用Cognito来创建它。我应该使用哪个键来创建rsapublickey?

     CertificateFactory factory = CertificateFactory.getInstance("X.509");
    
    //Creating a cert from x5c key   
    X509Certificate cert = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(x5c)));
    
    RSAPublicKey publicKey = (RSAPublicKey) cert.getPublicKey();
    RSAPrivateKey privateKey = null;
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Adam Jungen    6 年前

    我在Auth0库中找到了解决方案。

     String keyUrl = "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxxx/.well-known/jwks.json";
     JwkProvider provider = new UrlJwkProvider(new URL(keyUrl));
     Jwk jwk = provider.get(kid);
     RSAPublicKey publicKey = (RSAPublicKey) jwk.getPublicKey();