oauthas

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: Apache-2.0 Imports: 24 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 oauthas is GraphNest's OAuth 2.1 authorization server for MCP clients: dynamic public-client registration, PKCE authorization code flow gated by an explicit consent page, and rotating refresh tokens whose access tokens authenticate the /mcp endpoint as the granting user.

Index

Constants

View Source
const (
	// Lifetimes. Access tokens are short so revocation and GitHub-derived
	// access changes propagate within the hour; the grant itself is capped.
	AccessTokenTTL = time.Hour
	GrantTTL       = 30 * 24 * time.Hour

	RequestCookie                 = "__Host-graphnest_oauth_req"
	ResumePath                    = "/oauth/authorize/resume"
	MCPPath                       = "/mcp"
	ProtectedResourceMetadataPath = "/.well-known/oauth-protected-resource"
)
View Source
const (
	OperationClientRegistered = audit.OperationOAuthClientRegistered
	OperationConsentGranted   = audit.OperationOAuthConsentGranted
	OperationConsentDenied    = audit.OperationOAuthConsentDenied
	OperationGrantCreated     = audit.OperationOAuthGrantCreated
	OperationGrantRefreshed   = audit.OperationOAuthGrantRefreshed
	OperationGrantRevoked     = audit.OperationOAuthGrantRevoked
	OperationGrantReplay      = audit.OperationOAuthGrantReplay
)

Audit operations recorded by the authorization server.

View Source
const (
	AccessTokenPrefix  = "gno_"
	RefreshTokenPrefix = "gnr_"
	CodePrefix         = "gnac_"
	ClientIDPrefix     = "gnc_"
)

Token prefixes let the bearer authenticator route a token to the right table without a lookup and keep the kinds visually distinct in logs.

Variables

This section is empty.

Functions

func HasAccessTokenShape

func HasAccessTokenShape(token string) bool

HasAccessTokenShape reports whether a bearer credential is an OAuth access token, so the bearer path can pick an authenticator without a lookup.

Types

type GitHubAccess

type GitHubAccess interface {
	AccessibleRepositories(ctx context.Context, accessToken string) ([]int64, error)
}

GitHubAccess re-derives a user's repository access from GitHub with the user's own token; it is nil when GitHub-derived access is not configured.

type GitHubTokenSource

type GitHubTokenSource interface {
	Available(requestHash [32]byte, subject string) bool
	Transfer(requestHash [32]byte, subject string, codeHash [32]byte) bool
	TokenForCode(codeHash [32]byte) (string, bool)
	DeleteForCode(codeHash [32]byte)
}

GitHubTokenSource is the hand-off from the login that completed a pending authorization to the code exchange: consent moves the identity provider's token from the browser session to the authorization code, exchange removes it after the authorization code commits.

type ProviderTokens

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

ProviderTokens is the in-memory hand-off between a browser login that continues an MCP authorization and the code exchange that follows it. Login deposits the identity provider's user token under the pending request; consent moves it to the authorization code; exchange takes it and seals it into the grant. Entries expire with the authorization request, so nothing outlives the flow and nothing is ever written to disk in plaintext.

ponytail: a process-local map. A multi-replica deployment needs the login and the exchange to hit the same replica, or a shared sealed store.

func NewProviderTokens

func NewProviderTokens(now func() time.Time) *ProviderTokens

func (*ProviderTokens) Available

func (p *ProviderTokens) Available(requestHash [32]byte, subject string) bool

Available checks a pending handoff without consuming it.

func (*ProviderTokens) DeleteForCode

func (p *ProviderTokens) DeleteForCode(codeHash [32]byte)

DeleteForCode removes the token after its authorization code is consumed.

func (*ProviderTokens) Deposit

func (p *ProviderTokens) Deposit(_ context.Context, requestHash [32]byte, subject, providerToken string)

Deposit implements browserflow.ProviderTokenSink. The validated login binds its provider token and trusted GraphNest subject to one pending request.

func (*ProviderTokens) TokenForCode

func (p *ProviderTokens) TokenForCode(codeHash [32]byte) (string, bool)

TokenForCode returns the token attached to a live authorization code.

func (*ProviderTokens) Transfer

func (p *ProviderTokens) Transfer(requestHash [32]byte, subject string, codeHash [32]byte) bool

Transfer moves an owner-checked pending handoff to an authorization code.

type Sealer

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

Sealer encrypts GitHub user tokens at rest with AES-256-GCM. The grant ID is authenticated additional data so a ciphertext cannot be moved between rows.

func NewSealer

func NewSealer(key []byte) (*Sealer, error)

func (*Sealer) Open

func (s *Sealer) Open(grantID int64, ciphertext []byte) (string, error)

func (*Sealer) Seal

func (s *Sealer) Seal(reader io.Reader, grantID int64, plaintext string) ([]byte, error)

type Server

type Server struct {
	Origin          string
	LoginPath       string
	GitHubLoginPath string
	Store           authn.OAuthStore
	Sessions        SessionAuthenticator
	Sealer          *Sealer
	GitHub          GitHubAccess
	GitHubTokens    GitHubTokenSource
	Audit           audit.Recorder
	Now             func() time.Time
	Rand            io.Reader
	// UserName renders the consent page; nil falls back to the subject.
	UserName func(context.Context, authn.Principal) string
	// Limiter must enforce shared budgets before registration or token requests.
	Limiter authn.OAuthRequestLimiter
}

Server is the authorization server. All URLs derive from Origin.

func (*Server) Challenge

func (server *Server) Challenge(writer http.ResponseWriter, invalidToken bool)

Challenge writes the WWW-Authenticate header MCP clients use to discover this authorization server (RFC 9728 §5.1).

func (*Server) Register

func (server *Server) Register(mux *http.ServeMux)

Register mounts every endpoint. The /mcp handler itself stays where it is; use Challenge to decorate its 401 responses.

type SessionAuthenticator

type SessionAuthenticator interface {
	Authenticate(context.Context, string) (authn.Principal, error)
}

SessionAuthenticator resolves the browser session cookie to a principal.

Jump to

Keyboard shortcuts

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