Skip to content

Instantly share code, notes, and snippets.

@charlires
Last active August 26, 2022 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlires/376c737fd28cb4ece9fc5d9566a50964 to your computer and use it in GitHub Desktop.
Save charlires/376c737fd28cb4ece9fc5d9566a50964 to your computer and use it in GitHub Desktop.
Go Snippets
privateKey := "YOU-PRIVATE-KEY"
// atClaims := jwt.MapClaims{}
// atClaims["exp"] = time.Now().Add(time.Minute*15).UnixMilli()
// atClaims["aud"] = "Audience" // OPTIONAL AUDIENCE
atClaims := jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(30 * time.Second).unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodES256, atClaims)
token.Header["kid"] = "KEY-IDENTIFIER"
block, _ := pem.Decode([]byte(privateKey))
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
fmt.Println("Error parsing private key ", err.Error())
return "", err
}
signedToken, err := token.SignedString(key)
if err != nil {
fmt.Println("Error signing token ", err.Error())
return "", err
}
fmt.Println("signedToken: ", signedToken)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment