|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package jwtbearer |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "golang.org/x/oauth2" |
| 15 | + |
| 16 | + "github.com/stacklok/toolhive/pkg/networking" |
| 17 | + "github.com/stacklok/toolhive/pkg/oauthproto" |
| 18 | +) |
| 19 | + |
| 20 | +// Config holds configuration for an OAuth 2.0 JWT Bearer Grant (RFC 7523). |
| 21 | +type Config struct { |
| 22 | + // TokenURL is the target authorization server's token endpoint (required). |
| 23 | + TokenURL string |
| 24 | + |
| 25 | + // ClientID is the OAuth client identifier at the target AS. When both ClientID |
| 26 | + // and ClientSecret are set, the request is authenticated with HTTP Basic per |
| 27 | + // RFC 6749 Section 2.3.1. Public-client identification via a body client_id |
| 28 | + // parameter (RFC 6749 Section 3.2.1) is not supported — XAA / ID-JAG §8.1 |
| 29 | + // requires confidential clients, and that is the only intended consumer. |
| 30 | + ClientID string |
| 31 | + |
| 32 | + // ClientSecret is the OAuth client secret at the target AS. |
| 33 | + ClientSecret string //nolint:gosec // G101: field name, not a credential |
| 34 | + |
| 35 | + // Scopes are the requested scopes for the access token. |
| 36 | + Scopes []string |
| 37 | + |
| 38 | + // AssertionProvider returns the JWT assertion (e.g., the ID-JAG from Step A). |
| 39 | + // Called on each Token() invocation; must not be nil. The returned JWT must |
| 40 | + // satisfy RFC 7523 Section 3 (iss/sub/aud/exp); aud should typically be the |
| 41 | + // target AS's token endpoint (TokenURL). The provider must be safe for |
| 42 | + // concurrent use — Token() may be called from multiple goroutines (e.g., |
| 43 | + // when wrapped in oauth2.ReuseTokenSource). |
| 44 | + AssertionProvider func() (string, error) |
| 45 | + |
| 46 | + // HTTPClient is the HTTP client to use. If nil, oauthproto.DefaultHTTPClient() |
| 47 | + // is used. |
| 48 | + HTTPClient *http.Client |
| 49 | +} |
| 50 | + |
| 51 | +// Validate checks that the Config contains all required fields. |
| 52 | +func (c *Config) Validate() error { |
| 53 | + if c.TokenURL == "" { |
| 54 | + return fmt.Errorf("TokenURL is required") |
| 55 | + } |
| 56 | + |
| 57 | + if c.AssertionProvider == nil { |
| 58 | + return fmt.Errorf("AssertionProvider is required") |
| 59 | + } |
| 60 | + |
| 61 | + // Validate TokenURL: must be https (or http on localhost) per RFC 6749 Section 3.2 |
| 62 | + // and RFC 7523 Section 7. Reuses the repo-wide endpoint validator for scheme, |
| 63 | + // and adds host and fragment checks that it does not perform. |
| 64 | + if err := networking.ValidateEndpointURL(c.TokenURL); err != nil { |
| 65 | + return fmt.Errorf("TokenURL: %w", err) |
| 66 | + } |
| 67 | + u, err := url.Parse(c.TokenURL) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("TokenURL is not a valid URL: %w", err) |
| 70 | + } |
| 71 | + if u.Host == "" { |
| 72 | + return fmt.Errorf("TokenURL must include a host") |
| 73 | + } |
| 74 | + if u.Fragment != "" { |
| 75 | + return fmt.Errorf("TokenURL must not contain a fragment") |
| 76 | + } |
| 77 | + if u.User != nil { |
| 78 | + return fmt.Errorf("TokenURL must not contain embedded credentials") |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// String implements fmt.Stringer for Config, redacting sensitive fields. |
| 85 | +func (c *Config) String() string { |
| 86 | + assertion := oauthproto.Redact("") |
| 87 | + if c.AssertionProvider != nil { |
| 88 | + assertion = oauthproto.Redact("set") |
| 89 | + } |
| 90 | + |
| 91 | + return fmt.Sprintf("Config{TokenURL: %s, ClientID: %s, ClientSecret: %s, Scopes: %v, Assertion: %s}", |
| 92 | + c.TokenURL, c.ClientID, oauthproto.Redact(c.ClientSecret), c.Scopes, assertion) |
| 93 | +} |
| 94 | + |
| 95 | +// Compile-time assertion that *tokenSource implements oauth2.TokenSource. |
| 96 | +var _ oauth2.TokenSource = (*tokenSource)(nil) |
| 97 | + |
| 98 | +// TokenSource returns an oauth2.TokenSource that performs the JWT Bearer grant. |
| 99 | +func (c *Config) TokenSource(ctx context.Context) oauth2.TokenSource { |
| 100 | + return &tokenSource{ |
| 101 | + ctx: ctx, |
| 102 | + conf: c, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// tokenSource implements oauth2.TokenSource for the JWT Bearer grant. |
| 107 | +type tokenSource struct { |
| 108 | + ctx context.Context |
| 109 | + conf *Config |
| 110 | +} |
| 111 | + |
| 112 | +// Token implements the oauth2.TokenSource interface. |
| 113 | +// It performs the JWT Bearer grant and returns an oauth2.Token. |
| 114 | +func (ts *tokenSource) Token() (*oauth2.Token, error) { |
| 115 | + conf := ts.conf |
| 116 | + |
| 117 | + if err := conf.Validate(); err != nil { |
| 118 | + return nil, fmt.Errorf("invalid config: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + assertion, err := conf.AssertionProvider() |
| 122 | + if err != nil { |
| 123 | + return nil, fmt.Errorf("failed to get assertion: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + data := buildFormData(assertion, conf.Scopes) |
| 127 | + |
| 128 | + req, err := oauthproto.NewFormRequest(ts.ctx, conf.TokenURL, data, conf.ClientID, conf.ClientSecret) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("jwtbearer: build request: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + resp, err := oauthproto.DoTokenRequest(conf.HTTPClient, req) |
| 134 | + if err != nil { |
| 135 | + // Scrub RetrieveError.Body so raw upstream content cannot leak into |
| 136 | + // error strings via err.Error(). pkg/oauthproto deliberately preserves |
| 137 | + // Body for general-purpose callers; jwtbearer opts back into the |
| 138 | + // stricter behavior because its errors propagate through vmcp / runner |
| 139 | + // paths that may log them. Matches pkg/oauthproto/tokenexchange. |
| 140 | + var retrieveErr *oauth2.RetrieveError |
| 141 | + if errors.As(err, &retrieveErr) { |
| 142 | + retrieveErr.Body = nil |
| 143 | + } |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + // RFC 6749 Section 5.1 requires token_type in the response. The shared |
| 148 | + // oauthproto.ParseTokenResponse is intentionally permissive on this field |
| 149 | + // (matching x/oauth2); the JWT Bearer grant tightens it back. |
| 150 | + if resp.Token.TokenType == "" { |
| 151 | + return nil, fmt.Errorf("jwtbearer: server returned empty token_type (required by RFC 6749 Section 5.1)") |
| 152 | + } |
| 153 | + |
| 154 | + return resp.Token, nil |
| 155 | +} |
| 156 | + |
| 157 | +// buildFormData constructs the form data for a JWT Bearer grant request. |
| 158 | +func buildFormData(assertion string, scopes []string) url.Values { |
| 159 | + data := url.Values{} |
| 160 | + data.Set("grant_type", oauthproto.GrantTypeJWTBearer) |
| 161 | + data.Set("assertion", assertion) |
| 162 | + |
| 163 | + if len(scopes) > 0 { |
| 164 | + data.Set("scope", strings.Join(scopes, " ")) |
| 165 | + } |
| 166 | + |
| 167 | + return data |
| 168 | +} |
0 commit comments