I'm trying to generate a pem string using this library :
"github.com/lestrrat-go/jwx/jwk"
But so far I coudn't, I'm guessing it's because I'm new to go and there's something I'm not understanding. I have read the doc found here : doc
My code fetchs a JWK from a server, once I have it, I try to generate a pem string here :
// validationCrt contains my []byte from the server
set, err := jwk.Parse(validationCrt)
if err != nil {
println(err)
}
// I suspect this piece of code is actually a mess
// but I just can't understand what I'm doing wrong
key, err := jwk.GetPublicKey(set)
if err != nil {
log.Printf("failed to create public key: %s", err)
}
I am really lost I tried multiple ways (with and without this lib) and it seems I can't find any example out there (they usually explain how to generate a key, or go from pem to jwt, but my go app it's a client.)
Any help would be appreciated
Authorization server usually provides an endpoint to obtain JSON Web Keyset (JWKS). So I think validationCrt
would indeed be a JSON Web Keyset. Nevertheless, you can json.Unmarshal()
to JWK/JWKS (square/go-jose) and obtain the reference of the public key via .Key
.
Getting to PEM format is as simple as
...
pubData, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return err
}
if err := pem.Encode(os.Stdout, &pem.Block{
Type: "PUBLIC KEY",
Bytes: pubData,
}); err != nil {
return err
}
...