|
| 1 | +// Copyright (c) David Bond, Tailscale Inc, & Contributors |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package tailscale |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/base64" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "net/http" |
| 13 | + "net/url" |
| 14 | + "strings" |
| 15 | + "sync" |
| 16 | + "time" |
| 17 | + |
| 18 | + "golang.org/x/oauth2" |
| 19 | +) |
| 20 | + |
| 21 | +var _ Auth = &IdentityFederation{} |
| 22 | + |
| 23 | +// tokenExchangeResponse represents the response from the Tailscale token exchange endpoint. |
| 24 | +type tokenExchangeResponse struct { |
| 25 | + AccessToken string `json:"access_token"` |
| 26 | + TokenType string `json:"token_type"` |
| 27 | + ExpiresIn int `json:"expires_in"` // in seconds |
| 28 | + Scope string `json:"scope"` |
| 29 | +} |
| 30 | + |
| 31 | +// jwtClaims represents the claims in a JWT token (minimal set for validation). |
| 32 | +type jwtClaims struct { |
| 33 | + Exp int64 `json:"exp"` |
| 34 | +} |
| 35 | + |
| 36 | +// IdentityFederation configures identity federation authentication. |
| 37 | +type IdentityFederation struct { |
| 38 | + // ClientID is the ID of the Tailscale OAuth client. |
| 39 | + ClientID string |
| 40 | + // IDTokenFunc returns an identity token from the IdP to exchange for a Tailscale API token. |
| 41 | + // The client calls this function to obtain a fresh ID token and reauthenticate when the API token |
| 42 | + // and cached ID token have expired. For static tokens, return the token directly. If a static token |
| 43 | + // expires, the client cannot automatically refresh the API token; the consumer is responsible to create a new client |
| 44 | + // with a fresh ID token. |
| 45 | + IDTokenFunc func() (string, error) |
| 46 | +} |
| 47 | + |
| 48 | +// identityFederationTokenSource implements oauth2.TokenSource using identity federation. |
| 49 | +type identityFederationTokenSource struct { |
| 50 | + http *http.Client |
| 51 | + baseURL string |
| 52 | + clientID string |
| 53 | + idTokenFunc func() (string, error) |
| 54 | + |
| 55 | + mu sync.Mutex // protects the below fields |
| 56 | + idToken string |
| 57 | +} |
| 58 | + |
| 59 | +// HTTPClient implements the [Auth] interface. |
| 60 | +func (i *IdentityFederation) HTTPClient(orig *http.Client, baseURL string) *http.Client { |
| 61 | + s := &identityFederationTokenSource{ |
| 62 | + http: orig, |
| 63 | + baseURL: baseURL, |
| 64 | + clientID: i.ClientID, |
| 65 | + idTokenFunc: i.IDTokenFunc, |
| 66 | + } |
| 67 | + |
| 68 | + return &http.Client{ |
| 69 | + Transport: &oauth2.Transport{ |
| 70 | + Base: orig.Transport, |
| 71 | + Source: oauth2.ReuseTokenSource(nil, s), |
| 72 | + }, |
| 73 | + CheckRedirect: orig.CheckRedirect, |
| 74 | + Jar: orig.Jar, |
| 75 | + Timeout: orig.Timeout, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Token implements oauth2.TokenSource by exchanging an ID token for an API access token. |
| 80 | +func (i *identityFederationTokenSource) Token() (*oauth2.Token, error) { |
| 81 | + i.mu.Lock() |
| 82 | + defer i.mu.Unlock() |
| 83 | + |
| 84 | + if i.idToken == "" || validateIDToken(i.idToken) != nil { |
| 85 | + idToken, err := i.idTokenFunc() |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("failed to fetch ID token: %w", err) |
| 88 | + } |
| 89 | + if err := validateIDToken(idToken); err != nil { |
| 90 | + return nil, fmt.Errorf("fetched ID token is invalid: %w", err) |
| 91 | + } |
| 92 | + i.idToken = idToken |
| 93 | + } |
| 94 | + |
| 95 | + exchangeURL := fmt.Sprintf("%s/api/v2/oauth/token-exchange", i.baseURL) |
| 96 | + values := url.Values{ |
| 97 | + "client_id": {i.clientID}, |
| 98 | + "jwt": {i.idToken}, |
| 99 | + }.Encode() |
| 100 | + |
| 101 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, exchangeURL, strings.NewReader(values)) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("failed to create token exchange request: %w", err) |
| 104 | + } |
| 105 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 106 | + |
| 107 | + resp, err := i.http.Do(req) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("unexpected token exchange request error: %w", err) |
| 110 | + } |
| 111 | + defer resp.Body.Close() |
| 112 | + |
| 113 | + if resp.StatusCode >= http.StatusBadRequest { |
| 114 | + b, _ := io.ReadAll(resp.Body) |
| 115 | + return nil, fmt.Errorf("token exchange failed with status %d: %s", resp.StatusCode, string(b)) |
| 116 | + } |
| 117 | + |
| 118 | + var tokenResp tokenExchangeResponse |
| 119 | + if err = json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { |
| 120 | + return nil, fmt.Errorf("failed to decode token exchange response: %w", err) |
| 121 | + } |
| 122 | + |
| 123 | + return &oauth2.Token{ |
| 124 | + AccessToken: tokenResp.AccessToken, |
| 125 | + TokenType: tokenResp.TokenType, |
| 126 | + Expiry: time.Now().Add(time.Duration(tokenResp.ExpiresIn) * time.Second), |
| 127 | + }, nil |
| 128 | +} |
| 129 | + |
| 130 | +// validateIDToken decodes and validates the ID token's expiration claim |
| 131 | +// to give a more helpful error if the token is expired or malformed. |
| 132 | +func validateIDToken(idToken string) error { |
| 133 | + parts := strings.Split(idToken, ".") |
| 134 | + if len(parts) != 3 { |
| 135 | + return fmt.Errorf("invalid JWT format: expected 3 parts separated by '.', got %d", len(parts)) |
| 136 | + } |
| 137 | + |
| 138 | + payload, err := base64.RawURLEncoding.DecodeString(parts[1]) |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("failed to decode JWT payload: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + var claims jwtClaims |
| 144 | + if err := json.Unmarshal(payload, &claims); err != nil { |
| 145 | + return fmt.Errorf("failed to parse JWT claims: %w", err) |
| 146 | + } |
| 147 | + |
| 148 | + if claims.Exp == 0 { |
| 149 | + return fmt.Errorf("JWT is missing 'exp' (expiration) claim") |
| 150 | + } |
| 151 | + |
| 152 | + expirationTime := time.Unix(claims.Exp, 0) |
| 153 | + if time.Now().After(expirationTime) { |
| 154 | + return fmt.Errorf("ID token has expired (expired at %s)", expirationTime.Format(time.RFC3339)) |
| 155 | + } |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
0 commit comments