Skip to content

Instantly share code, notes, and snippets.

@billinghamj
Created April 6, 2016 11:38
Show Gist options
  • Save billinghamj/57592583cb1a86fb5dd41e84a31e2906 to your computer and use it in GitHub Desktop.
Save billinghamj/57592583cb1a86fb5dd41e84a31e2906 to your computer and use it in GitHub Desktop.
Go JWT Generator - ES512 with OpenSSL PEM format keys
package main
import (
"crypto"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/dgrijalva/jwt-go"
"io/ioutil"
"time"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
data, err := ioutil.ReadFile("private-key.pem")
check(err)
privateKey, err := loadKey(data)
check(err)
now := time.Now()
claims := map[string]interface{}{
"iss": "https://example.com",
"jti": "this-is-definitely-a-uuid",
"sub": "some-user-id",
"aud": "some-client-id",
"iat": now.Unix(),
"nbf": now.Unix(),
"exp": now.Add(time.Hour).Unix(),
}
token, err := generateJwt(jwt.SigningMethodES512, privateKey, claims)
check(err)
fmt.Println(token)
}
func loadKey(pemData []byte) (crypto.PrivateKey, error) {
block, _ := pem.Decode(pemData)
if block == nil {
return nil, fmt.Errorf("unable to load key")
}
if block.Type != "EC PRIVATE KEY" {
return nil, fmt.Errorf("wrong type of key - %s", block.Type)
}
return x509.ParseECPrivateKey(block.Bytes)
}
func generateJwt(alg jwt.SigningMethod, key crypto.PrivateKey, claims map[string]interface{}) (string, error) {
token := jwt.New(alg)
token.Claims = claims
return token.SignedString(key)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment