刷新令牌是由Cognito服务产生的JWT,该令牌本质上是一个加密的JSON字符串。为了提高安全性,刷新令牌采用了不同的加密算法,这使得其不像访问令牌那样容易地解码。因此,刷新令牌解码后会变成不可读的字符串。
以下是如何通过AWS SDK for JavaScript v3使用Cognito的示例代码:
import { CognitoIdentityProviderClient, DecodeAuthorizationMessageCommand, InitiateAuthCommand } from "@aws-sdk/client-cognito-identity-provider";
const cognitoClient = new CognitoIdentityProviderClient({ region: "your_region" });
const initiateAuthParams = {
ClientId: "your_client_id",
AuthFlow: "REFRESH_TOKEN_AUTH",
AuthParameters: {
REFRESH_TOKEN: "your_refresh_token",
},
};
const { AuthenticationResult: { AccessToken, RefreshToken } } = await cognitoClient.send(new InitiateAuthCommand(initiateAuthParams));
const decodeAccessTokenParams = {
AccessToken: AccessToken,
};
const { Payload: decodedAccessToken } = await cognitoClient.send(new DecodeAuthorizationMessageCommand(decodeAccessTokenParams));
console.log(decodedAccessToken);
const decodeRefreshTokenParams = {
RefreshToken: RefreshToken,
};
const { Payload: decodedRefreshToken } = await cognitoClient.send(new DecodeAuthorizationMessageCommand(decodeRefreshTokenParams));
console.log(decodedRefreshToken);
在上面的示例中,我们发起了一个REFRESH_TOKEN_AUTH身份验证流并获取了访问令牌和刷新令牌。然后我们使用DecodeAuthorizationMessageCommand来解码访问令牌和刷新令牌。你将会发现,访问令牌可以被解码,但刷新令牌解码后的结果是不可读的字符串。