Created
June 5, 2021 21:59
-
-
Save rorycl/b8a093bdd3f2a4ccb7e12831b37a5154 to your computer and use it in GitHub Desktop.
Validate Header issue with ed25519 keys : github.com/gbrlsnchs/jwt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/ed25519" | |
"crypto/x509" | |
"fmt" | |
"time" | |
"encoding/pem" | |
"github.com/gbrlsnchs/jwt/v3" | |
) | |
// CustomPayload allows custom payload content | |
type CustomPayload struct { | |
jwt.Payload | |
Additional map[string]interface{} `json:"content,omitempty"` | |
} | |
var hs = `-----BEGIN PUBLIC KEY----- | |
MCowBQYDK2VwAyEAOkaHl0rUKvwKsKVon/G7binN1bZ42lQbhoaO1SUJ200= | |
-----END PUBLIC KEY-----` | |
// https://jwt.io#debugger-io?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6InI2NDcifQ.eyJpYXQiOjE2MjI5MjkwMzksIm5iZiI6MTYyMjkyNTQzOSwiZXhwIjoxNjIyOTQ3MDM5LCJpc3MiOiJ0ZXN0IiwiYXVkIjoiaGVscC50ZXN0LmNvbSIsImNvbnRlbnQiOnsic29tZSI6ImNvbnRlbnQifX0.GgNleA6RzI2PT0NRNtKDw9vBifpfp9nuj40jLgL31CN_Wzqb-Mlm3M4NOK5YMoRwNd_4eZSxgYkF6cVDX3g1CQ | |
var token = []byte(`eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6InI2NDcifQ.eyJpYXQiOjE2MjI5MjkwMzksIm5iZiI6MTYyMjkyNTQzOSwiZXhwIjoxNjIyOTQ3MDM5LCJpc3MiOiJ0ZXN0IiwiYXVkIjoiaGVscC50ZXN0LmNvbSIsImNvbnRlbnQiOnsic29tZSI6ImNvbnRlbnQifX0.GgNleA6RzI2PT0NRNtKDw9vBifpfp9nuj40jLgL31CN_Wzqb-Mlm3M4NOK5YMoRwNd_4eZSxgYkF6cVDX3g1CQ`) | |
func main() { | |
// load public key | |
block, _ := pem.Decode([]byte(hs)) | |
if block == nil { | |
panic("failed to parse PEM block containing the public key") | |
} | |
pub, err := x509.ParsePKIXPublicKey(block.Bytes) | |
if err != nil { | |
panic("failed to parse DER encoded public key: " + err.Error()) | |
} | |
k := jwt.NewEd25519(jwt.Ed25519PublicKey(pub.(ed25519.PublicKey))) | |
var ( | |
pl CustomPayload | |
now = time.Now() | |
aud = []string{"help.test.com"} | |
iatValidator = jwt.IssuedAtValidator(now) | |
audValidator = jwt.AudienceValidator(aud) | |
validatePayload = jwt.ValidatePayload(&pl.Payload, iatValidator, audValidator) | |
) | |
// Header validation not ok | |
hd, err := jwt.Verify(token, k, &pl, validatePayload, jwt.ValidateHeader) | |
if err != nil { | |
fmt.Println(err) | |
} | |
// Payload validation ok | |
hd, err = jwt.Verify(token, k, &pl, validatePayload) | |
fmt.Printf("%+v\n", hd) | |
fmt.Printf("Payload %+v\n", pl) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment