Add JSONWebKeySet generator

This commit is contained in:
Melon 2024-07-27 19:27:13 +01:00
parent 1fc34736a2
commit 4e2c18918f
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 31 additions and 0 deletions

1
go.mod
View File

@ -7,6 +7,7 @@ toolchain go1.22.3
require (
github.com/1f349/rsa-helper v0.0.2
github.com/becheran/wildmatch-go v1.0.0
github.com/go-jose/go-jose/v4 v4.0.4
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/subcommands v1.2.0
github.com/pkg/errors v0.9.1

30
jwks.go Normal file
View File

@ -0,0 +1,30 @@
package mjwt
import (
"encoding/json"
"github.com/go-jose/go-jose/v4"
"io"
)
func WriteJwkSetJson(w io.Writer, issuers []*Issuer) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
var j jose.JSONWebKeySet
for _, issuer := range issuers {
// get public key from private key
key, err := issuer.PrivateKey()
if err != nil {
return err
}
pubKey := &key.PublicKey
// format as JWK
j.Keys = append(j.Keys, jose.JSONWebKey{
Algorithm: issuer.signing.Alg(),
Use: "sig",
KeyID: issuer.kid,
Key: pubKey,
})
}
return enc.Encode(j)
}