forked from gepaplexx/multena-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyfunc.go
More file actions
46 lines (40 loc) · 1.04 KB
/
keyfunc.go
File metadata and controls
46 lines (40 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/MicahParks/keyfunc/v3"
"github.com/MicahParks/jwkset"
)
var (
// ErrKeyfunc is returned when a keyfunc error occurs.
ErrKeyfunc = errors.New("failed keyfunc")
)
func NewCombinedJwks(ctx context.Context, urls []string, raw json.RawMessage) (keyfunc.Keyfunc, error) {
client, err := jwkset.NewDefaultHTTPClientCtx(ctx, urls)
if err != nil {
return nil, err
}
if raw != nil {
var jwks jwkset.JWKSMarshal
err := json.Unmarshal(raw, &jwks)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal raw JWK Set JSON", errors.Join(err, ErrKeyfunc))
}
jwkss, err := jwks.JWKSlice()
if err != nil {
return nil, fmt.Errorf("failed to create a slice of JWK from JWKSMarshal: %w", err)
}
for _, jwk := range jwkss {
err = client.KeyWrite(context.Background(), jwk)
if err != nil {
return nil, fmt.Errorf("failed to write JWK to storage: %w", err)
}
}
}
options := keyfunc.Options{
Storage: client,
}
return keyfunc.New(options)
}