|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// OAuthConfig holds OAuth configuration |
| 14 | +type OAuthConfig struct { |
| 15 | + TokenURL string |
| 16 | + ClientID string |
| 17 | + ClientPassword string |
| 18 | + Scopes string |
| 19 | +} |
| 20 | + |
| 21 | +// TokenResponse represents the OAuth token response |
| 22 | +type TokenResponse struct { |
| 23 | + AccessToken string `json:"access_token"` |
| 24 | + TokenType string `json:"token_type"` |
| 25 | + ExpiresIn int `json:"expires_in"` |
| 26 | + Scope string `json:"scope,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +// OAuthClient handles OAuth authentication |
| 30 | +type OAuthClient struct { |
| 31 | + config *OAuthConfig |
| 32 | + token *TokenResponse |
| 33 | + tokenExpiry time.Time |
| 34 | +} |
| 35 | + |
| 36 | +// NewOAuthClient creates a new OAuth client |
| 37 | +// Returns nil if OAuth is not configured (no AUTH_URL set) |
| 38 | +// Returns an error if OAuth is partially configured (missing required parameters) |
| 39 | +func NewOAuthClient() (*OAuthClient, error) { |
| 40 | + config := &OAuthConfig{ |
| 41 | + TokenURL: os.Getenv("AUTH_URL"), |
| 42 | + ClientID: os.Getenv("FERN_AUTH_CLIENT_ID"), |
| 43 | + ClientPassword: os.Getenv("FERN_AUTH_CLIENT_SECRET"), |
| 44 | + Scopes: os.Getenv("FERN_CLIENT_SCOPE"), |
| 45 | + } |
| 46 | + |
| 47 | + // If token URL is not set, OAuth is disabled - this is OK |
| 48 | + if config.TokenURL == "" { |
| 49 | + return nil, nil |
| 50 | + } |
| 51 | + |
| 52 | + // If AUTH_URL is set, validate that we have all required OAuth parameters |
| 53 | + var missingParams []string |
| 54 | + if config.ClientID == "" { |
| 55 | + missingParams = append(missingParams, "FERN_AUTH_CLIENT_ID") |
| 56 | + } |
| 57 | + if config.ClientPassword == "" { |
| 58 | + missingParams = append(missingParams, "FERN_AUTH_CLIENT_SECRET") |
| 59 | + } |
| 60 | + |
| 61 | + if len(missingParams) > 0 { |
| 62 | + return nil, fmt.Errorf("OAuth configuration error: AUTH_URL is set but missing required parameters: %s", strings.Join(missingParams, ", ")) |
| 63 | + } |
| 64 | + |
| 65 | + return &OAuthClient{ |
| 66 | + config: config, |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +// GetToken fetches a new OAuth token or returns the cached one if still valid |
| 71 | +func (c *OAuthClient) GetToken() (string, error) { |
| 72 | + if c == nil { |
| 73 | + return "", nil |
| 74 | + } |
| 75 | + |
| 76 | + // Check if we have a valid cached token |
| 77 | + if c.token != nil && time.Now().Before(c.tokenExpiry) { |
| 78 | + return c.token.AccessToken, nil |
| 79 | + } |
| 80 | + |
| 81 | + // Fetch new token |
| 82 | + if err := c.fetchToken(); err != nil { |
| 83 | + return "", fmt.Errorf("failed to fetch OAuth token: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + return c.token.AccessToken, nil |
| 87 | +} |
| 88 | + |
| 89 | +// fetchToken fetches a new OAuth token from the authorization server |
| 90 | +func (c *OAuthClient) fetchToken() error { |
| 91 | + // Prepare the token request |
| 92 | + data := url.Values{} |
| 93 | + data.Set("grant_type", "client_credentials") |
| 94 | + data.Set("client_id", c.config.ClientID) |
| 95 | + data.Set("client_secret", c.config.ClientPassword) |
| 96 | + |
| 97 | + // Add scopes if provided |
| 98 | + if c.config.Scopes != "" { |
| 99 | + data.Set("scope", c.config.Scopes) |
| 100 | + } |
| 101 | + |
| 102 | + req, err := http.NewRequest("POST", c.config.TokenURL, strings.NewReader(data.Encode())) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("failed to create token request: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 108 | + |
| 109 | + // Send the request |
| 110 | + client := &http.Client{ |
| 111 | + Timeout: 30 * time.Second, |
| 112 | + } |
| 113 | + resp, err := client.Do(req) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("failed to send token request: %w", err) |
| 116 | + } |
| 117 | + defer resp.Body.Close() |
| 118 | + |
| 119 | + if resp.StatusCode != http.StatusOK { |
| 120 | + return fmt.Errorf("token request failed with status: %d", resp.StatusCode) |
| 121 | + } |
| 122 | + |
| 123 | + // Parse the response |
| 124 | + var tokenResp TokenResponse |
| 125 | + if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { |
| 126 | + return fmt.Errorf("failed to decode token response: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + // Store the token and calculate expiry |
| 130 | + c.token = &tokenResp |
| 131 | + // Subtract 30 seconds from expiry to ensure we refresh before it actually expires |
| 132 | + expiryDuration := time.Duration(tokenResp.ExpiresIn-30) * time.Second |
| 133 | + c.tokenExpiry = time.Now().Add(expiryDuration) |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// AddAuthHeader adds the OAuth bearer token to the request header if OAuth is enabled |
| 139 | +func (c *OAuthClient) AddAuthHeader(req *http.Request) error { |
| 140 | + if c == nil { |
| 141 | + // OAuth is not enabled |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + token, err := c.GetToken() |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + if token != "" { |
| 151 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +// IsEnabled returns whether OAuth is enabled |
| 158 | +func (c *OAuthClient) IsEnabled() bool { |
| 159 | + return c != nil |
| 160 | +} |
| 161 | + |
| 162 | +// HTTPClient returns an http.Client with OAuth authentication if enabled |
| 163 | +func (c *OAuthClient) HTTPClient() *http.Client { |
| 164 | + return &http.Client{ |
| 165 | + Timeout: 30 * time.Second, |
| 166 | + Transport: &OAuthTransport{ |
| 167 | + Base: http.DefaultTransport, |
| 168 | + OAuthClient: c, |
| 169 | + }, |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// OAuthTransport is an http.RoundTripper that adds OAuth authentication |
| 174 | +type OAuthTransport struct { |
| 175 | + Base http.RoundTripper |
| 176 | + OAuthClient *OAuthClient |
| 177 | +} |
| 178 | + |
| 179 | +// RoundTrip implements the http.RoundTripper interface |
| 180 | +func (t *OAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 181 | + // Clone the request to avoid modifying the original |
| 182 | + reqCopy := req.Clone(req.Context()) |
| 183 | + |
| 184 | + // Add OAuth header |
| 185 | + if err := t.OAuthClient.AddAuthHeader(reqCopy); err != nil { |
| 186 | + return nil, err |
| 187 | + } |
| 188 | + |
| 189 | + // Use the base transport to send the request |
| 190 | + return t.Base.RoundTrip(reqCopy) |
| 191 | +} |
0 commit comments