AWS CLI and SDK support for validating Cognito tokens

0

Even though manually validating Cognito JWT (e.g. for my case of authenticating WebSocket connections) can be done by extracting the key ID from the token, getting the respective JWKS from Cognito and verifying the token validity using various existing libraries I don’t understand why AWS doesn’t support this as part of the CLI/SDK - this would both reduce the load on Cognito (as developers wouldn’t need to download the JWKS anymore) and simplify clients (that wouldn’t need to implement the validation anymore). Is there any chance to address this as part of the Cognito roadmap?

질문됨 2달 전172회 조회
1개 답변
0

The AWS JWT Verify library available on npm and source on GitHub does this.

JavaScript library for verifying JWTs signed by Amazon Cognito, and any OIDC-compatible IDP that signs JWTs with RS256 / RS384 / RS512.

Below is a basic usage example from the README, but there are other supported parameters and configuration options.

import { CognitoJwtVerifier } from "aws-jwt-verify";

// Verifier that expects valid access tokens:
const verifier = CognitoJwtVerifier.create({
  userPoolId: "<user_pool_id>",
  tokenUse: "access",
  clientId: "<client_id>",
});

try {
  const payload = await verifier.verify(
    "eyJraWQeyJhdF9oYXNoIjoidk..." // the JWT as string
  );
  console.log("Token is valid. Payload:", payload);
} catch {
  console.log("Token not valid!");
}
profile pictureAWS
답변함 2달 전
profile picture
전문가
검토됨 2달 전
  • Thanks for that, I didn’t manage to find a similar library for Go though, I suspect it’s a custom implementation for Type/JavaScript only?

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인