Creating id_token with Go
Sample Code to generate a id_token using Go
package main
import (
"fmt"
"os"
"time"
"github.com/golang-jwt/jwt/v4"
)
const PRIVATE_KEY = "<env><partner_name>privatekey.pem"
func main() {
// Read private key from file
privateKeyBuffer, err := os.ReadFile(PRIVATE_KEY)
if err != nil {
fmt.Println("error reading private key file:", err)
return
}
// Parse private key
key, err := jwt.ParseRSAPrivateKeyFromPEM(privateKeyBuffer)
if err != nil {
fmt.Println("error parsing private key: ", err)
return
}
// Create token with claims
token := jwt.NewWithClaims(jwt.SigningMethodRS512, jwt.MapClaims{
"stockal_username": "<stockal-customer-id>",
"exp": time.Now().Add(time.Minute * 1).Unix(),
"aud": "stockal.com",
"iss": "<YOUR_PARTNER_CODE>",
})
// Sign token
signedString, err := token.SignedString(key)
if err != nil {
fmt.Println("error signing token: ", err)
return
}
fmt.Println(signedString)
}
Updated almost 3 years ago