lfxv2

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 39 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package lfxv2 provides client utilities for interacting with LFX v2 APIs, including OAuth2 token exchange.

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

A single *Clients instance should be created once at startup (via NewClients) and shared across all tool invocations. Per-request, call TokenFromRequest to resolve authentication (MCP token exchange in HTTP mode, or a static LFX token in stdio mode) 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) {
    var tokenInfo *auth.TokenInfo
    if req.Extra != nil {
        tokenInfo = req.Extra.TokenInfo
    }

    ctx, err := sharedClients.TokenFromRequest(ctx, tokenInfo)
    if err != nil {
        return nil, nil, err
    }

    // Make API calls - token exchange and caching happen automatically.
    result, err := sharedClients.Project.GetOneProjectBase(ctx, &projectservice.GetOneProjectBasePayload{})
    // ...
}

Token Caching

Exchanged tokens are cached per MCP token inside the long-lived *Clients instance to avoid redundant token-exchange round-trips on every request. The cache is goroutine-safe and automatically expires tokens with a fixed buffer of 5 minutes before their exp claim.

Static LFX Token (stdio mode)

When ClientConfig.StaticLFXToken is set, it is used directly as the bearer token for every LFX API call, bypassing token exchange, M2M, and the token cache entirely. This is intended for stdio mode, where an operator supplies an already-valid LFX access token obtained out of band (e.g. via `lfx auth token`) instead of going through SSO, CTE, or M2M flows.

Package lfxv2 provides client utilities for interacting with LFX v2 APIs, including OAuth2 token exchange.

Package lfxv2 provides client configuration for LFX v2 API services; this file holds the committee client's request encoder.

Package lfxv2 provides client utilities for interacting with LFX v2 APIs, including OAuth2 token exchange.

Package lfxv2 provides client utilities for interacting with LFX v2 APIs, including OAuth2 token exchange.

Package lfxv2 provides client utilities for interacting with LFX v2 APIs, including OAuth2 token exchange.

Package lfxv2 provides client utilities for LFX v2 APIs, including strict response decoding for fetch-merge-put writes.

Package lfxv2 provides client utilities for interacting with LFX v2 APIs, including OAuth2 token exchange.

Index

Constants

This section is empty.

Variables

View Source
var ErrAccessRefused = errors.New("request refused without a service message")

ErrAccessRefused is returned (wrapped) when an upstream LFX v2 service call is answered with HTTP 401 or 403 on an endpoint that declares that status, and the response carries no service-authored message. A status the endpoint does not declare surfaces as Goa's invalid_response error instead; see IsRefusalWithoutServiceMessage. Match it with errors.Is; UpstreamStatus tells a 401 from a 403.

View Source
var ErrProjectNotFound = errors.New("project not found")

ErrProjectNotFound is returned by Resolve when the query service returns no results for the given slug. Callers can use errors.Is to distinguish this "not found" case from transport or other operational errors.

Functions

func ExtractMCPToken

func ExtractMCPToken(tokenInfo *auth.TokenInfo) (string, error)

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.

For requests authenticated via a static API key (Extra["api_key_auth"]==true), the apiKeyMCPToken sentinel is returned so that the token-exchange layer uses the client_credentials grant rather than attempting to exchange a bearer token.

func IsRefusalWithoutServiceMessage added in v0.19.0

func IsRefusalWithoutServiceMessage(err error) bool

IsRefusalWithoutServiceMessage reports whether err is an upstream 401 or 403 that carries no service-authored message. That is either ErrAccessRefused (a status the endpoint declares) or Goa's invalid_response error for a 401 or 403 the endpoint does not declare, whose body, if any, is not a JSON object with a non-empty "message".

func UpstreamStatus added in v0.19.0

func UpstreamStatus(err error) int

UpstreamStatus returns the HTTP status an LFX v2 service answered with when err shows that status was 401, 403 or 404, and 0 otherwise. It reads the three forms such an answer takes once a Goa-generated client has handled it:

  • a 401 or 403 without a service-authored message (ErrAccessRefused), which keeps its status;
  • Goa's invalid_response error, for a status the endpoint does not declare;
  • the service's typed error, for a status the endpoint declares, by its Goa error name: "Unauthorized", "Forbidden", or "NotFound". Every client this module uses names its typed 404 "NotFound"; only the meeting service declares "Unauthorized".

A declared status whose body the client cannot decode or validate surfaces as Goa's decoding or validation error, which does not carry the status.

func WithMCPToken

func WithMCPToken(ctx context.Context, token string) context.Context

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 WithStrictDecoding added in v0.20.0

func WithStrictDecoding(ctx context.Context) context.Context

WithStrictDecoding returns a context that, when used for an LFX v2 API call whose client was built with strictAwareDecoder, causes a successful (2xx) JSON response to be decoded with json.Decoder.DisallowUnknownFields.

This exists for fetch-merge-put write handlers: they GET a resource, merge the caller's fields over it, and PUT the whole thing back. If the service has added a field the pinned client module predates, the default decoder silently drops it, the merge never carries it forward, and the PUT erases it — with a valid ETag and no error. Calling this before such a GET turns that silent data loss into a refused write instead.

Plain read tools must not set this: a newer, read-only field added upstream would otherwise block them too. Set it only immediately before the GET that precedes a client-side merge-and-PUT.

Types

type AccessCheckClient added in v0.6.0

type AccessCheckClient struct {
	// contains filtered or unexported fields
}

AccessCheckClient calls the V2 access-check endpoint to verify that the authenticated user has a particular relationship to a resource.

The access-check endpoint is backed by OpenFGA and evaluates relationships transitively. For example, an owner of a parent project is implicitly a writer on child projects.

func NewAccessCheckClient added in v0.6.0

func NewAccessCheckClient(apiURL string, httpClient *http.Client) *AccessCheckClient

NewAccessCheckClient creates a client for V2 access-check calls.

apiURL is the base URL of the V2 API (e.g., "https://lfx-api.v2.cluster.lfx.dev"). httpClient should be a plain HTTP client — the user's exchanged V2 token is passed explicitly per-request, not via an auth interceptor.

func (*AccessCheckClient) CheckAccess added in v0.6.0

func (c *AccessCheckClient) CheckAccess(ctx context.Context, token string, requests []string) (map[string]bool, error)

CheckAccess sends a batch of access-check requests and returns the results as a map from request string to granted status.

token is the user's exchanged V2 bearer token (not the MCP token or API key). requests are formatted as "object:id#relation" (e.g., "project:uuid#writer").

The V2 access-check endpoint returns results in an unordered list. Each result is a tab-delimited string in the format:

<request>@<user>\ttrue|false

This method parses those results and matches them back to the original requests. The returned map is keyed by the original request string.

func (*AccessCheckClient) CheckProjectAccess added in v0.6.0

func (c *AccessCheckClient) CheckProjectAccess(ctx context.Context, token string, projectUUID, relation string) error

CheckProjectAccess verifies the user has the specified relation to a project.

Returns nil if access is allowed. Returns an error describing the denial if access is denied or if the check fails.

type ClientConfig

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

	// StaticLFXToken, when set, is used directly as the bearer token for every
	// LFX API call, bypassing token exchange and M2M client-credentials
	// entirely. It is intended only for stdio mode, where an operator supplies
	// an already-valid LFX access token obtained out of band (e.g. via
	// `lfx auth token`) instead of going through SSO, CTE, or M2M flows.
	StaticLFXToken string

	// DebugLogger is used for debug-level HTTP request/response logging.
	// If nil, debug logging is disabled.
	DebugLogger *slog.Logger
}

ClientConfig holds configuration for LFX v2 API clients.

type ClientCredentialsClient added in v0.6.0

type ClientCredentialsClient struct {
	// contains filtered or unexported fields
}

ClientCredentialsClient handles OAuth2 client credentials grants for M2M authentication. It caches tokens and refreshes them automatically when they expire.

func NewClientCredentialsClient added in v0.6.0

func NewClientCredentialsClient(cfg ClientCredentialsConfig) (*ClientCredentialsClient, error)

NewClientCredentialsClient creates a new OAuth2 client credentials client.

func (*ClientCredentialsClient) GetToken added in v0.6.0

func (c *ClientCredentialsClient) GetToken(ctx context.Context) (string, error)

GetToken returns a valid bearer token, fetching a new one if the cached token has expired.

type ClientCredentialsConfig added in v0.6.0

type ClientCredentialsConfig struct {
	// TokenEndpoint is the OAuth2 token endpoint URL (e.g., "https://example.auth0.com/oauth/token").
	TokenEndpoint string

	// ClientID is the M2M client ID.
	ClientID string

	// ClientSecret is the M2M client secret.
	// Ignored if ClientAssertionSigningKey is provided.
	ClientSecret string

	// ClientAssertionSigningKey is the PEM-encoded RSA private key for generating client assertions.
	// If provided, this takes precedence over ClientSecret for client authentication.
	ClientAssertionSigningKey string

	// Audience is the resource server identifier (the "aud" claim in the issued token).
	Audience string

	// HTTPClient is the HTTP client to use. If nil, a default client with 30s timeout is created.
	HTTPClient *http.Client
}

ClientCredentialsConfig holds configuration for OAuth2 client credentials grants.

type Clients

type Clients struct {
	Committee   *committeeservice.Client
	MailingList *mailinglist.Client
	Meeting     *meetingservice.Client
	Member      *memberservice.Client
	Project     *projectservice.Client
	QuerySvc    *querysvc.Client
	AccessCheck *AccessCheckClient
	// contains filtered or unexported fields
}

Clients holds initialized LFX v2 API service clients.

func NewClients

func NewClients(_ context.Context, cfg ClientConfig) (*Clients, error)

NewClients initializes and returns LFX v2 API service clients.

func (*Clients) CheckRelations added in v0.19.2

func (c *Clients) CheckRelations(ctx context.Context, requests []string) (map[string]bool, error)

CheckRelations evaluates relation requests ("object:id#relation") as the caller whose MCP token is on ctx, with that caller's exchanged LFX token. Requests are de-duplicated and sent in batches of accessCheckBatchSize. The result holds one answer per distinct request; it is strict: a request the service did not answer is an error, never a silent false, so callers that narrow what they return can fail closed on a transport or contract fault rather than treat a missing answer as a denial that reads as absence.

func (*Clients) GetExchangedToken added in v0.6.0

func (c *Clients) GetExchangedToken(ctx context.Context) (string, error)

GetExchangedToken returns the exchanged V2 API token for the MCP token stored in the context. It reuses the token cache, so repeated calls within the same request are cheap. This is used by the access-check client which needs the V2 token as an explicit string rather than via the auth interceptor.

func (*Clients) TokenFromRequest added in v0.19.0

func (c *Clients) TokenFromRequest(ctx context.Context, tokenInfo *auth.TokenInfo) (context.Context, error)

TokenFromRequest resolves how the current tool invocation should authenticate to LFX APIs and returns the context to use for the rest of the call.

In HTTP mode, tokenInfo is the MCP OAuth bearer token verified by the server's auth middleware (req.Extra.TokenInfo); this extracts the raw MCP token from it and attaches it to ctx via WithMCPToken so the auth interceptor can exchange it.

In stdio mode there is no MCP-level OAuth, so tokenInfo is always nil (the stdio transport never populates req.Extra). If a static LFX token was configured at startup (see ClientConfig.StaticLFXToken), ctx is returned unchanged: the auth interceptor already has the static token and uses it directly. If no static token is configured either, authentication is not possible and an error is returned.

func (*Clients) WithMCPToken added in v0.4.6

func (c *Clients) WithMCPToken(ctx context.Context, mcpToken string) context.Context

WithMCPToken attaches mcpToken to ctx so the auth interceptor can retrieve it when forwarding requests to LFX APIs. Call this once per inbound request before invoking any LFX API method on the shared *Clients instance.

type SlugResolver added in v0.6.0

type SlugResolver struct {
	// contains filtered or unexported fields
}

SlugResolver resolves project slugs to V2 UUIDs with in-memory caching.

Slug-to-UUID mappings are stable, so the cache has no TTL. The resolver accepts a Clients instance per call because each call may use a different user's auth context for the V2 query service, but the cached mappings are user-independent.

func NewSlugResolver added in v0.6.0

func NewSlugResolver() *SlugResolver

NewSlugResolver creates a new resolver with an empty cache.

func (*SlugResolver) Resolve added in v0.6.0

func (r *SlugResolver) Resolve(ctx context.Context, clients *Clients, slug string) (string, error)

Resolve looks up the V2 UUID for a project slug. It checks the in-memory cache first and falls back to the V2 query service on a cache miss.

type TokenExchangeClient

type TokenExchangeClient struct {
	// contains filtered or unexported fields
}

TokenExchangeClient handles OAuth2 token exchange per RFC 8693.

func NewTokenExchangeClient

func NewTokenExchangeClient(cfg TokenExchangeConfig) (*TokenExchangeClient, error)

NewTokenExchangeClient creates a new RFC 8693 token exchange client.

func (*TokenExchangeClient) ClientCredentials added in v0.4.1

func (c *TokenExchangeClient) ClientCredentials(ctx context.Context) (*TokenExchangeResponse, error)

ClientCredentials obtains an LFX API token using the client_credentials grant. This is used when the caller presents an M2M token, which Auth0 cannot exchange via RFC 8693 token exchange (it requires a user subject). Instead, the MCP server mints a fresh LFX token using its own client identity.

func (*TokenExchangeClient) ExchangeToken

func (c *TokenExchangeClient) ExchangeToken(ctx context.Context, subjectToken string) (*TokenExchangeResponse, error)

ExchangeToken exchanges a subject token for a new access token per RFC 8693.

type TokenExchangeConfig

type TokenExchangeConfig struct {
	// TokenEndpoint is the OAuth2 token endpoint URL (e.g., "https://example.auth0.com/oauth/token").
	TokenEndpoint string

	// ClientID is the M2M client ID for token exchange.
	ClientID string

	// ClientSecret is the M2M client secret for token exchange.
	// Ignored if ClientAssertionSigningKey is provided.
	ClientSecret string

	// ClientAssertionSigningKey is the PEM-encoded RSA private key for generating client assertions.
	// If provided, this takes precedence over ClientSecret for client authentication.
	// The key is used to sign a JWT assertion per RFC 7523.
	ClientAssertionSigningKey string

	// SubjectTokenType is the token type of the incoming subject token (e.g., LFX MCP API identifier).
	SubjectTokenType string

	// Audience is the target audience for the exchanged token (e.g., LFX V2 API identifier).
	Audience string

	// HTTPClient is the HTTP client to use for token exchange.
	// If nil, a default client with 30s timeout will be created.
	HTTPClient *http.Client
}

TokenExchangeConfig holds configuration for OAuth2 token exchange (RFC 8693).

type TokenExchangeResponse

type TokenExchangeResponse struct {
	AccessToken     string `json:"access_token"`
	IssuedTokenType string `json:"issued_token_type,omitempty"`
	TokenType       string `json:"token_type"`
	ExpiresIn       int    `json:"expires_in"`
	Scope           string `json:"scope,omitempty"`
	RefreshToken    string `json:"refresh_token,omitempty"`
}

TokenExchangeResponse represents the response from OAuth2 token exchange per RFC 8693.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL