Debian Codegraph @

pkg/client/rs/resource_server.go

raw
1 package rs
2
3 import (
4 "context"
5 "errors"
6 "net/http"
7 "time"
8
9 "github.com/zitadel/oidc/v3/pkg/client"
10 httphelper "github.com/zitadel/oidc/v3/pkg/http"
11 "github.com/zitadel/oidc/v3/pkg/oidc"
12 )
13
14 type ResourceServer interface {
15 IntrospectionURL() string
16 TokenEndpoint() string
17 HttpClient() *http.Client
18 AuthFn() (any, error)
19 }
20
21 type resourceServer struct {
22 issuer string
23 tokenURL string
24 introspectURL string
25 httpClient *http.Client
26 authFn func() (any, error)
27 }
28
29 func (r *resourceServer) IntrospectionURL() string {
30 return r.introspectURL
31 }
32
33 func (r *resourceServer) TokenEndpoint() string {
34 return r.tokenURL
35 }
36
37 func (r *resourceServer) HttpClient() *http.Client {
38 return r.httpClient
39 }
40
41 func (r *resourceServer) AuthFn() (any, error) {
42 return r.authFn()
43 }
44
45 func NewResourceServerClientCredentials(ctx context.Context, issuer, clientID, clientSecret string, option ...Option) (ResourceServer, error) {
46 authorizer := func() (any, error) {
47 return httphelper.AuthorizeBasic(clientID, clientSecret), nil
48 }
49 return newResourceServer(ctx, issuer, authorizer, option...)
50 }
51
52 func NewResourceServerJWTProfile(ctx context.Context, issuer, clientID, keyID string, key []byte, options ...Option) (ResourceServer, error) {
53 signer, err := client.NewSignerFromPrivateKeyByte(key, keyID)
54 if err != nil {
55 return nil, err
56 }
57 authorizer := func() (any, error) {
58 assertion, err := client.SignedJWTProfileAssertion(clientID, []string{issuer}, time.Hour, signer)
59 if err != nil {
60 return nil, err
61 }
62 return client.ClientAssertionFormAuthorization(assertion), nil
63 }
64 return newResourceServer(ctx, issuer, authorizer, options...)
65 }
66
67 func newResourceServer(ctx context.Context, issuer string, authorizer func() (any, error), options ...Option) (*resourceServer, error) {
68 rs := &resourceServer{
69 issuer: issuer,
70 httpClient: httphelper.DefaultHTTPClient,
71 }
72 for _, optFunc := range options {
73 optFunc(rs)
74 }
75 if rs.introspectURL == "" || rs.tokenURL == "" {
76 config, err := client.Discover(ctx, rs.issuer, rs.httpClient)
77 if err != nil {
78 return nil, err
79 }
80 if rs.tokenURL == "" {
81 rs.tokenURL = config.TokenEndpoint
82 }
83 if rs.introspectURL == "" {
84 rs.introspectURL = config.IntrospectionEndpoint
85 }
86 }
87 if rs.tokenURL == "" {
88 return nil, errors.New("tokenURL is empty: please provide with either `WithStaticEndpoints` or a discovery url")
89 }
90 rs.authFn = authorizer
91 return rs, nil
92 }
93
94 func NewResourceServerFromKeyFile(ctx context.Context, issuer, path string, options ...Option) (ResourceServer, error) {
95 c, err := client.ConfigFromKeyFile(path)
96 if err != nil {
97 return nil, err
98 }
99 return NewResourceServerJWTProfile(ctx, issuer, c.ClientID, c.KeyID, []byte(c.Key), options...)
100 }
101
102 type Option func(*resourceServer)
103
104 // WithClient provides the ability to set an http client to be used for the resource server
105 func WithClient(client *http.Client) Option {
106 return func(server *resourceServer) {
107 server.httpClient = client
108 }
109 }
110
111 // WithStaticEndpoints provides the ability to set static token and introspect URL
112 func WithStaticEndpoints(tokenURL, introspectURL string) Option {
113 return func(server *resourceServer) {
114 server.tokenURL = tokenURL
115 server.introspectURL = introspectURL
116 }
117 }
118
119 // Introspect calls the [RFC7662] Token Introspection
120 // endpoint and returns the response in an instance of type R.
121 // [*oidc.IntrospectionResponse] can be used as a good example, or use a custom type if type-safe
122 // access to custom claims is needed.
123 //
124 // [RFC7662]: https://www.rfc-editor.org/rfc/rfc7662
125 func Introspect[R any](ctx context.Context, rp ResourceServer, token string) (resp R, err error) {
126 ctx, span := client.Tracer.Start(ctx, "Introspect")
127 defer span.End()
128
129 if rp.IntrospectionURL() == "" {
130 return resp, errors.New("resource server: introspection URL is empty")
131 }
132 authFn, err := rp.AuthFn()
133 if err != nil {
134 return resp, err
135 }
136 req, err := httphelper.FormRequest(ctx, rp.IntrospectionURL(), &oidc.IntrospectionRequest{Token: token}, client.Encoder, authFn)
137 if err != nil {
138 return resp, err
139 }
140
141 if err := httphelper.HttpRequest(rp.HttpClient(), req, &resp); err != nil {
142 return resp, err
143 }
144 return resp, nil
145 }