This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
metadata.go
64 lines (52 loc) · 1.76 KB
/
metadata.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package authzserver
import (
"context"
"crypto/rsa"
"encoding/json"
"fmt"
"net/http"
"github.com/flyteorg/flyteadmin/auth"
"github.com/flyteorg/flyteadmin/auth/config"
"github.com/lestrrat-go/jwx/jwk"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flyteadmin/auth/interfaces"
)
var (
tokenRelativeURL = config.MustParseURL("/oauth2/token")
authorizeRelativeURL = config.MustParseURL("/oauth2/authorize")
authorizeCallbackRelativeURL = config.MustParseURL("/oauth2/authorize_callback")
jsonWebKeysURL = config.MustParseURL("/oauth2/jwks")
oauth2MetadataEndpoint = config.MustParseURL(auth.OAuth2MetadataEndpoint)
)
// GetJSONWebKeysEndpoint serves requests to the jwks endpoint.
// ref: https://tools.ietf.org/html/rfc7517
func GetJSONWebKeysEndpoint(authCtx interfaces.AuthenticationContext) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
s := authCtx.OAuth2Provider().KeySet()
raw, err := json.Marshal(s)
if err != nil {
http.Error(writer, fmt.Errorf("failed to write public key. Error: %w", err).Error(), http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
size, err := writer.Write(raw)
if err != nil {
logger.Errorf(context.Background(), "Wrote JSONWebKeys response size %d, err %s", size, err)
}
}
}
func newJSONWebKeySet(publicKeys []rsa.PublicKey) (jwk.Set, error) {
s := jwk.NewSet()
for _, publicKey := range publicKeys {
key, err := jwk.New(publicKey)
if err != nil {
return nil, fmt.Errorf("failed to write public key. Error: %w", err)
}
err = jwk.AssignKeyID(key)
if err != nil {
return nil, fmt.Errorf("failed to write public key. Error: %w", err)
}
s.Add(key)
}
return s, nil
}