Documentation
¶
Overview ¶
Package oauth provides GitLab-specific OAuth 2.0 support for HTTP mode.
It verifies bearer tokens against GitLab's user endpoint, caches verified identities without storing raw token material, normalizes legacy GitLab PRIVATE-TOKEN headers into Authorization headers, and serves the RFC 9728 Protected Resource Metadata endpoint used by MCP clients to discover the GitLab authorization server for a protected resource.
HTTP Mode Flow ¶
The package participates in the HTTP transport path as follows:
HTTP request
|
v
NormalizeAuthHeader
|
v
NewGitLabVerifier
|
v
GitLab /user endpoint and TokenCache
NormalizeAuthHeader preserves Bearer tokens while converting GitLab PRIVATE-TOKEN headers into Authorization headers for clients that still use legacy authentication. NewGitLabVerifier validates Bearer tokens with GitLab and stores verified identity metadata in TokenCache.
NewProtectedResourceHandler serves OAuth Protected Resource Metadata so MCP clients can discover the GitLab authorization server associated with the requested resource URL.
Index ¶
- Constants
- func MetadataURLFor(resourceID string) string
- func NewGitLabVerifier(gitlabURL string, skipTLS bool, cacheTTL time.Duration, cache *TokenCache) auth.TokenVerifier
- func NewProtectedResourceHandler(resourceURL, gitlabURL, requiredScope string) http.Handler
- func NormalizeAuthHeader(next http.Handler) http.Handler
- func RequiredScope(readOnly, safeMode bool) string
- type TokenCache
Constants ¶
const ( ScopeAPI = "api" ScopeReadAPI = "read_api" )
GitLab API scopes this server can operate under. api permits reads and writes; read_api is enough for a deployment that never mutates, and is what such a deployment asks for so users are not made to grant more.
Variables ¶
This section is empty.
Functions ¶
func MetadataURLFor ¶ added in v2.7.1
MetadataURLFor derives the RFC 9728 §3 protected-resource metadata URL from a resource identifier: the well-known path segment is inserted between the host component and the resource's path, so https://mcp.example.com/gitlab advertises its metadata at https://mcp.example.com/.well-known/oauth-protected-resource/gitlab and a path-less resource at the bare well-known URI. The identifier is validated at config load; a parse failure here would be a programming error, so the fallback simply appends the well-known path.
func NewGitLabVerifier ¶
func NewGitLabVerifier(gitlabURL string, skipTLS bool, cacheTTL time.Duration, cache *TokenCache) auth.TokenVerifier
NewGitLabVerifier returns an auth.TokenVerifier that validates Bearer tokens by calling the GitLab /api/v4/user endpoint. Verified identities are cached in cache (if non-nil) to avoid redundant API calls.
The returned verifier populates auth.TokenInfo with:
- UserID: the GitLab user's numeric ID (as string)
- Extra["username"]: the GitLab user's login name
- Extra["token"]: the raw token (for downstream GitLab client creation)
- Expiration: now + cacheTTL (so the SDK middleware honors TTL)
func NewProtectedResourceHandler ¶
NewProtectedResourceHandler returns an http.Handler that serves RFC 9728 Protected Resource Metadata. MCP clients use this endpoint to discover the GitLab authorization server associated with this resource.
requiredScope is the GitLab scope this deployment actually needs, which a client reads from scopes_supported to build its authorization request. It is the least privilege that works: a server that can only read asks for read_api rather than making every user grant full api.
The handler is registered at /.well-known/oauth-protected-resource.
func NormalizeAuthHeader ¶
NormalizeAuthHeader is HTTP middleware that converts GitLab's PRIVATE-TOKEN header into a standard Authorization: Bearer header. This allows the SDK's RequireBearerToken middleware to handle both OAuth tokens and legacy PRIVATE-TOKEN headers through a unified pipeline.
If the request already has an Authorization header, PRIVATE-TOKEN is ignored.
func RequiredScope ¶ added in v2.7.4
RequiredScope reports the least-privilege GitLab scope a deployment needs: read_api when no request can reach GitLab as a write, api otherwise. Safe mode counts as read-only here because it answers mutating calls with a preview instead of forwarding them.
Types ¶
type TokenCache ¶
type TokenCache struct {
// contains filtered or unexported fields
}
TokenCache is a thread-safe, TTL-based cache for verified token identities. Keys are SHA-256 hashes of raw tokens to avoid storing sensitive material.
func (*TokenCache) Cleanup ¶
func (c *TokenCache) Cleanup()
Cleanup removes all expired entries. Intended for periodic maintenance.
func (*TokenCache) Delete ¶
func (c *TokenCache) Delete(token string)
Delete is an alias for [Evict] for API ergonomics.
func (*TokenCache) Evict ¶
func (c *TokenCache) Evict(token string)
Evict removes the cache entry for the given raw token.
func (*TokenCache) Get ¶
func (c *TokenCache) Get(token string) (*auth.TokenInfo, bool)
Get returns the cached auth.TokenInfo for the given raw token if present and not expired. Expired entries are lazily evicted on read.
func (*TokenCache) Len ¶
func (c *TokenCache) Len() int
Len returns the total number of entries (including potentially expired ones).
func (*TokenCache) Put ¶
Put stores a auth.TokenInfo for the given raw token with the specified TTL.