-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclient.go
More file actions
382 lines (328 loc) · 11.5 KB
/
Copy pathclient.go
File metadata and controls
382 lines (328 loc) · 11.5 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright The Linux Foundation and contributors.
// SPDX-License-Identifier: MIT
// Package lfxv2 provides client configuration for LFX v2 API services.
//
// # Token Exchange
//
// The package supports automatic OAuth2 token exchange (RFC 8693) for converting
// MCP access tokens into LFX API tokens. Token exchange is performed per-request
// using tokens stored in the request context.
//
// # Usage in MCP Tools
//
// Tools should extract the raw MCP token from the request and attach it to the
// context before making LFX API calls:
//
// func handleMyTool(ctx context.Context, req *mcp.CallToolRequest, args MyToolArgs) (*mcp.CallToolResult, any, error) {
// // Extract raw MCP token from request.
// mcpToken, err := lfxv2.ExtractMCPToken(req.Extra.TokenInfo)
// if err != nil {
// return nil, nil, err
// }
//
// // Attach token to context for LFX API calls.
// ctx = lfxv2.WithMCPToken(ctx, mcpToken)
//
// // Create clients with token exchange enabled.
// clients, err := lfxv2.NewClients(ctx, lfxv2.ClientConfig{
// APIDomain: "https://api.lfx.dev",
// TokenExchangeClient: tokenExchangeClient, // shared instance
// })
// if err != nil {
// return nil, nil, err
// }
//
// // Make API calls - token exchange happens automatically.
// result, err := clients.Project.GetOneProjectBase(ctx, &projectservice.GetOneProjectBasePayload{})
// // ...
// }
//
// # Token Caching
//
// Exchanged tokens are cached per MCP token to avoid redundant exchanges.
// The cache is thread-safe and automatically expires tokens with a 5-minute buffer.
package lfxv2
import (
"context"
"fmt"
"log/slog"
"net/http"
"net/http/httputil"
"net/url"
"sync"
"time"
goahttp "goa.design/goa/v3/http"
committeeservice "github.com/linuxfoundation/lfx-v2-committee-service/gen/committee_service"
committeehttpclient "github.com/linuxfoundation/lfx-v2-committee-service/gen/http/committee_service/client"
projecthttpclient "github.com/linuxfoundation/lfx-v2-project-service/api/project/v1/gen/http/project_service/client"
projectservice "github.com/linuxfoundation/lfx-v2-project-service/api/project/v1/gen/project_service"
queryhttpclient "github.com/linuxfoundation/lfx-v2-query-service/gen/http/query_svc/client"
querysvc "github.com/linuxfoundation/lfx-v2-query-service/gen/query_svc"
"github.com/modelcontextprotocol/go-sdk/auth"
)
// mcpTokenContextKey is used to store the MCP bearer token in request context.
type mcpTokenContextKey struct{}
// WithMCPToken returns a context with the MCP bearer token attached.
// This token will be used for OAuth2 token exchange when making LFX API calls.
func WithMCPToken(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, mcpTokenContextKey{}, token)
}
// mcpTokenFromContext extracts the MCP bearer token from the context.
func mcpTokenFromContext(ctx context.Context) string {
if token, ok := ctx.Value(mcpTokenContextKey{}).(string); ok {
return token
}
return ""
}
// ExtractMCPToken extracts the raw MCP token from auth.TokenInfo.Extra.
// This token can be passed to WithMCPToken for use in LFX API calls.
// Returns an error if the token cannot be extracted.
func ExtractMCPToken(tokenInfo *auth.TokenInfo) (string, error) {
if tokenInfo == nil || tokenInfo.Extra == nil {
return "", fmt.Errorf("tokenInfo or Extra map is nil")
}
rawToken, ok := tokenInfo.Extra["raw_token"].(string)
if !ok || rawToken == "" {
return "", fmt.Errorf("raw MCP token not found in TokenInfo.Extra")
}
return rawToken, nil
}
// ClientConfig holds configuration for LFX v2 API clients.
type ClientConfig struct {
// APIDomain is the LFX API base domain.
APIDomain string
// HTTPClient is the HTTP client to use for API calls.
// If nil, a default client with 30s timeout will be created.
HTTPClient *http.Client
// TokenExchangeClient is the RFC 8693 OAuth2 token exchange client.
// If provided, the client will automatically exchange MCP tokens (from request context)
// for target API tokens.
TokenExchangeClient *TokenExchangeClient
// DebugLogger is used for debug-level HTTP request/response logging.
// If nil, debug logging is disabled.
DebugLogger *slog.Logger
}
// Clients holds initialized LFX v2 API service clients.
type Clients struct {
Committee *committeeservice.Client
Project *projectservice.Client
QuerySvc *querysvc.Client
tokenExchangeClient *TokenExchangeClient
// Token cache: maps MCP token -> exchanged LFX token info.
mu sync.RWMutex
tokenCache map[string]*cachedToken
}
// cachedToken holds an exchanged LFX API token with its expiration.
type cachedToken struct {
accessToken string
expiry time.Time
}
// NewClients initializes and returns LFX v2 API service clients.
func NewClients(_ context.Context, cfg ClientConfig) (*Clients, error) {
if cfg.APIDomain == "" {
return nil, fmt.Errorf("APIDomain is required")
}
// Create HTTP client if not provided.
httpClient := cfg.HTTPClient
if httpClient == nil {
httpClient = &http.Client{
Timeout: 30 * time.Second,
}
}
// Wrap HTTP client with auth interceptor if token exchange is enabled.
clients := &Clients{
tokenExchangeClient: cfg.TokenExchangeClient,
tokenCache: make(map[string]*cachedToken),
}
if cfg.DebugLogger != nil {
httpClient = newDebugTransportClient(httpClient, cfg.DebugLogger)
}
if cfg.TokenExchangeClient != nil {
httpClient = clients.wrapWithAuthInterceptor(httpClient)
}
// Initialize committee service client.
committeeURL, err := url.Parse(cfg.APIDomain + "/committees")
if err != nil {
return nil, fmt.Errorf("failed to parse committee service URL: %w", err)
}
committeeHTTPClient := committeehttpclient.NewClient(
committeeURL.Scheme,
committeeURL.Host,
httpClient,
goahttp.RequestEncoder,
goahttp.ResponseDecoder,
false,
)
clients.Committee = committeeservice.NewClient(
committeeHTTPClient.CreateCommittee(),
committeeHTTPClient.GetCommitteeBase(),
committeeHTTPClient.UpdateCommitteeBase(),
committeeHTTPClient.DeleteCommittee(),
committeeHTTPClient.GetCommitteeSettings(),
committeeHTTPClient.UpdateCommitteeSettings(),
committeeHTTPClient.Readyz(),
committeeHTTPClient.Livez(),
committeeHTTPClient.CreateCommitteeMember(),
committeeHTTPClient.GetCommitteeMember(),
committeeHTTPClient.UpdateCommitteeMember(),
committeeHTTPClient.DeleteCommitteeMember(),
)
// Initialize project service client.
projectURL, err := url.Parse(cfg.APIDomain + "/projects")
if err != nil {
return nil, fmt.Errorf("failed to parse project service URL: %w", err)
}
projectHTTPClient := projecthttpclient.NewClient(
projectURL.Scheme,
projectURL.Host,
httpClient,
goahttp.RequestEncoder,
goahttp.ResponseDecoder,
false,
)
clients.Project = projectservice.NewClient(
projectHTTPClient.GetProjects(),
projectHTTPClient.CreateProject(),
projectHTTPClient.GetOneProjectBase(),
projectHTTPClient.GetOneProjectSettings(),
projectHTTPClient.UpdateProjectBase(),
projectHTTPClient.UpdateProjectSettings(),
projectHTTPClient.DeleteProject(),
projectHTTPClient.Readyz(),
projectHTTPClient.Livez(),
)
// Initialize query service client.
queryURL, err := url.Parse(cfg.APIDomain + "/query")
if err != nil {
return nil, fmt.Errorf("failed to parse query service URL: %w", err)
}
queryHTTPClient := queryhttpclient.NewClient(
queryURL.Scheme,
queryURL.Host,
httpClient,
goahttp.RequestEncoder,
goahttp.ResponseDecoder,
false,
)
clients.QuerySvc = querysvc.NewClient(
queryHTTPClient.QueryResources(),
queryHTTPClient.QueryResourcesCount(),
queryHTTPClient.QueryOrgs(),
queryHTTPClient.SuggestOrgs(),
queryHTTPClient.Readyz(),
queryHTTPClient.Livez(),
)
return clients, nil
}
// newDebugTransportClient wraps an HTTP client with a transport that logs the
// full HTTP wire dump of every outbound request and inbound response at DEBUG level.
func newDebugTransportClient(client *http.Client, logger *slog.Logger) *http.Client {
base := client.Transport
if base == nil {
base = http.DefaultTransport
}
return &http.Client{
Transport: &debugTransport{
transport: base,
logger: logger,
},
Timeout: client.Timeout,
}
}
// debugTransport is an http.RoundTripper that logs the full HTTP wire dump of
// every outbound request and inbound response at DEBUG level.
type debugTransport struct {
transport http.RoundTripper
logger *slog.Logger
}
// RoundTrip implements http.RoundTripper.
func (dt *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
reqDump, err := httputil.DumpRequestOut(req, true)
if err != nil {
dt.logger.Error("failed to dump outbound request", "error", err)
} else {
dt.logger.Debug("lfxv2 outbound request", "dump", string(reqDump))
}
resp, err := dt.transport.RoundTrip(req)
if err != nil {
dt.logger.Error("lfxv2 outbound request failed", "error", err, "url", req.URL.String())
return nil, err
}
respDump, err := httputil.DumpResponse(resp, true)
if err != nil {
dt.logger.Error("failed to dump inbound response", "error", err)
} else {
dt.logger.Debug("lfxv2 inbound response", "dump", string(respDump))
}
return resp, nil
}
// wrapWithAuthInterceptor wraps an HTTP client with automatic token exchange.
func (c *Clients) wrapWithAuthInterceptor(client *http.Client) *http.Client {
originalTransport := client.Transport
if originalTransport == nil {
originalTransport = http.DefaultTransport
}
return &http.Client{
Transport: &authInterceptor{
base: originalTransport,
clients: c,
},
Timeout: client.Timeout,
}
}
// authInterceptor intercepts HTTP requests to inject the exchanged LFX V2 API token.
type authInterceptor struct {
base http.RoundTripper
clients *Clients
}
// RoundTrip implements http.RoundTripper.
func (a *authInterceptor) RoundTrip(req *http.Request) (*http.Response, error) {
// Extract MCP token from request context.
mcpToken := mcpTokenFromContext(req.Context())
if mcpToken == "" {
return nil, fmt.Errorf("MCP token not found in request context")
}
// Get or exchange token for this MCP token.
lfxToken, err := a.clients.getOrExchangeToken(req.Context(), mcpToken)
if err != nil {
return nil, fmt.Errorf("failed to get LFX token: %w", err)
}
// Clone request to avoid modifying the original.
reqClone := req.Clone(req.Context())
reqClone.Header.Set("Authorization", "Bearer "+lfxToken)
return a.base.RoundTrip(reqClone)
}
// getOrExchangeToken gets a cached LFX token or performs token exchange.
func (c *Clients) getOrExchangeToken(ctx context.Context, mcpToken string) (string, error) {
if c.tokenExchangeClient == nil {
return "", fmt.Errorf("token exchange client not configured")
}
// Check cache first (read lock).
c.mu.RLock()
cached, exists := c.tokenCache[mcpToken]
c.mu.RUnlock()
// Return cached token if valid.
if exists && time.Now().Before(cached.expiry) {
return cached.accessToken, nil
}
// Need to exchange token (write lock).
c.mu.Lock()
defer c.mu.Unlock()
// Double-check cache in case another goroutine just updated it.
cached, exists = c.tokenCache[mcpToken]
if exists && time.Now().Before(cached.expiry) {
return cached.accessToken, nil
}
// Perform token exchange.
resp, err := c.tokenExchangeClient.ExchangeToken(ctx, mcpToken)
if err != nil {
return "", err
}
// Cache the exchanged token with 5-minute buffer.
expiryBuffer := 5 * time.Minute
c.tokenCache[mcpToken] = &cachedToken{
accessToken: resp.AccessToken,
expiry: time.Now().Add(time.Duration(resp.ExpiresIn)*time.Second - expiryBuffer),
}
return resp.AccessToken, nil
}