Open2
jwtのpublic keyによる検証
https://jwt.io/ のDebuggerを使ってRS256でJWTとPublicKeyを生成
token = `https://jwt.io/で生成したJWT`
raw := "https://jwt.io/で生成したPublicKey"
pubKeyBytes, err := base64.StdEncoding.DecodeString(raw)
if err != nil {
log.Fatalf("Failed to decode public key: %v", err)
}
pubKey, err := x509.ParsePKIXPublicKey(pubKeyBytes)
if err != nil {
log.Fatalf("Failed to parse public key: %v", err)
}
publicKey, ok = pubKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("Not an ECDSA public key")
}
token, err := jwt.ParseWithClaims(token, &jwt.RegisteredClaims{}, func(token *jwt.Token) (interface{}, error) {
return publicKey, nil
})
if err != nil {
log.Fatalf("Failed to parse token: %v", err)
}
if !token.Valid {
log.Fatal("Token is invalid")
}
if claims, ok := token.Claims.(*jwt.RegisteredClaims); ok {
if claims.ExpiresAt != nil && claims.ExpiresAt.Time.Before(time.Now()) {
log.Fatal("Token is expired")
}
}