Documentation
¶
Overview ¶
Package core holds the identifiers, domain types, errors and cross-package interfaces of MCP Gatekeeper.
It is frozen (docs/PLAN.md §4.1). Agents never edit it: a change request goes in the agent's report, and the orchestrator merges it between waves.
The types came from Family Hub's internal/core. The one rename: a module became a resource, because an application decides what an MCP endpoint stands for (Family Hub: one module; Homie: the whole home).
Index ¶
- Constants
- Variables
- func ClientIP(r *http.Request, mode ClientIPMode) string
- func NewID(prefix string) string
- func WithViewer(ctx context.Context, v Viewer) context.Context
- type AccessInfo
- type Account
- type AccountID
- type Assistant
- type AuditEntry
- type AuthCode
- type Brand
- type ClientID
- type ClientIPMode
- type ClientKind
- type Clock
- type Grant
- type GrantID
- type Invite
- type InviteKind
- type Middleware
- type OAuthClient
- type Passkey
- type PerResource
- type Person
- type PersonID
- type ResourceCatalog
- type ResourceID
- type ResourceInfo
- type ResourcePaths
- type Role
- type RouteRegistrar
- type Session
- type Sessions
- type SingleResource
- type Site
- type Token
- type TokenKind
- type TokenValidator
- type ToolInfo
- type Viewer
Constants ¶
const ( PrefixAccount = "acc" PrefixPerson = "per" PrefixPasskey = "pk" PrefixInvite = "inv" PrefixGrant = "grt" PrefixClient = "cli" PrefixTxn = "txn" )
Prefixes for NewID.
Variables ¶
var ( ErrNotFound = errors.New("not found") ErrConflict = errors.New("conflict") ErrExpired = errors.New("expired") ErrUsed = errors.New("already used") ErrNoSession = errors.New("no session") ErrStepUpRequired = errors.New("step-up required") ErrForbidden = errors.New("forbidden") ErrInvalidToken = errors.New("invalid token") )
Errors shared by the packages; wrap them with %w.
Functions ¶
func ClientIP ¶
func ClientIP(r *http.Request, mode ClientIPMode) string
ClientIP returns the client address of r under mode, falling back to the TCP peer.
The application must make sure only a trusted peer can set the header that mode reads (Family Hub deletes CF-Connecting-IP and X-Forwarded-For from any peer outside its edge network before this runs).
Types ¶
type AccessInfo ¶
type AccessInfo struct {
GrantID GrantID
PersonID PersonID
AccountID AccountID
ResourceID ResourceID
ClientID ClientID
Assistant Assistant
Scope string
ExpiresAt time.Time
}
AccessInfo is what a valid access token stands for.
type AuditEntry ¶
type AuditEntry struct {
ID string // assigned by the store; newer entries sort after older ones
At time.Time
AccountID AccountID // empty: a system event
Actor PersonID // empty: the CLI or the system
Action string // dotted, e.g. "auth.login", "module.deploy"
Target string
Detail map[string]string
IP string
}
AuditEntry is one line of the audit log. Detail never holds secrets or application data.
type AuthCode ¶
type AuthCode struct {
CodeHash []byte
GrantID GrantID
ClientID ClientID
RedirectURI string
Resource string
Scope string
CodeChallenge string
CreatedAt time.Time
ExpiresAt time.Time
}
AuthCode is a one-time authorization code. Only its sha256 is stored.
type Brand ¶
type Brand struct {
Product string // shown in passkey prompts and pages, e.g. "Family Hub"
SessionCookie string // e.g. "__Host-fh_session"; must start with "__Host-"
TxnCookie string // the consent-transaction binding cookie, e.g. "__Host-fh_txn"; must start with "__Host-"
CSRFHeader string // e.g. "X-Family-Hub"; every mutating /api call must send it with the value "1"
}
Brand holds the application-specific names the gatekeeper puts on the wire. Changing one of them for a running application signs everybody out (cookies) or breaks old browser tabs (header), so an application keeps its values.
type ClientID ¶
type ClientID string
ClientID is an OAuth client_id: an id issued by DCR or, for CIMD, the URL of the metadata document.
type ClientIPMode ¶
type ClientIPMode string
ClientIPMode says where the client address of a request comes from.
const ( ClientIPCloudflare ClientIPMode = "cf-connecting-ip" // behind cloudflared: the CF-Connecting-IP header ClientIPForwarded ClientIPMode = "x-forwarded-for" // behind one reverse proxy: the last X-Forwarded-For hop ClientIPRemote ClientIPMode = "remote" // no proxy: the TCP peer )
Client address sources.
type ClientKind ¶
type ClientKind string
ClientKind says how an OAuth client registered.
const ( ClientDCR ClientKind = "dcr" ClientCIMD ClientKind = "cimd" )
Client kinds.
type Grant ¶
type Grant struct {
ID GrantID
PersonID PersonID
AccountID AccountID
ClientID ClientID
ResourceID ResourceID
Scope string
CreatedAt time.Time
LastUsedAt *time.Time
RevokedAt *time.Time
}
Grant is a consent: one person let one assistant client use one resource. Revoking it revokes its tokens.
type GrantID ¶
type GrantID string
GrantID names one consent: a person, an assistant's client and a resource.
type Invite ¶
type Invite struct {
ID string
TokenHash []byte
Kind InviteKind
AccountID AccountID // empty for InviteSetup
Name string // the person's name as the inviter typed it
CreatedBy PersonID // empty for InviteSetup
CreatedAt time.Time
ExpiresAt time.Time
UsedAt *time.Time
RevokedAt *time.Time
}
Invite is a one-time link. Only the sha256 of the token is stored.
type InviteKind ¶
type InviteKind string
InviteKind says what redeeming an invite creates.
const ( InviteSetup InviteKind = "setup" // the first superadmin and their account (CLI only) InviteOwner InviteKind = "owner" // the owner of a new account, made by the superadmin InviteMember InviteKind = "member" // a member of an existing account, made by its owner )
Invite kinds.
type OAuthClient ¶
type OAuthClient struct {
ID ClientID
Kind ClientKind
Assistant Assistant
Name string
RedirectURIs []string
AuthMethod string // "none", "client_secret_post" or "client_secret_basic"
SecretHash []byte // sha256 of the client secret; nil for "none"
FetchedAt time.Time // CIMD: when the metadata document was last fetched
CreatedAt time.Time
LastUsedAt *time.Time
}
OAuthClient is a registered assistant client.
type Passkey ¶
type Passkey struct {
ID string
PersonID PersonID
CredentialID []byte
Data []byte
Name string
CreatedAt time.Time
LastUsedAt *time.Time
}
Passkey is a stored WebAuthn credential. Data belongs to package auth (the credential as JSON).
type PerResource ¶
type PerResource struct {
Prefix string // ends with "/"
}
PerResource serves every resource under its own path: Prefix + id (Family Hub: Prefix "/mcp/").
func (PerResource) FromPath ¶
func (p PerResource) FromPath(path string) (ResourceID, bool)
FromPath implements ResourcePaths.
func (PerResource) Path ¶
func (p PerResource) Path(id ResourceID) string
Path implements ResourcePaths.
type Person ¶
type Person struct {
ID PersonID
AccountID AccountID
Name string
Role Role
Superadmin bool // manages accounts and the application; never sees other accounts' data
CreatedAt time.Time
DisabledAt *time.Time
}
Person is someone who signs in with a passkey. Every person belongs to exactly one account.
type ResourceCatalog ¶
type ResourceCatalog interface {
// Resource returns a resource that can be connected right now, or ErrNotFound.
Resource(ctx context.Context, id ResourceID) (ResourceInfo, error)
// Available reports whether account a may use resource id now (Family Hub: the module is switched on for it).
Available(ctx context.Context, a AccountID, id ResourceID) (bool, error)
}
ResourceCatalog tells the gatekeeper which MCP resources exist; the application implements it (Family Hub: its running modules; Homie: one resource, the home).
type ResourceID ¶
type ResourceID string
ResourceID names what one MCP endpoint serves, chosen by the application (e.g. "notes", "home").
type ResourceInfo ¶
type ResourceInfo struct {
ID ResourceID
Name string
Description string
Tools []ToolInfo
}
ResourceInfo describes a resource for the OAuth metadata and the consent screen.
type ResourcePaths ¶
type ResourcePaths interface {
// Path returns the MCP path of a resource, e.g. "/mcp/notes" or "/mcp".
Path(id ResourceID) string
// FromPath returns the resource an MCP path belongs to: the path itself or anything below it.
FromPath(path string) (ResourceID, bool)
}
ResourcePaths maps resources to their MCP paths on the Site and back.
type RouteRegistrar ¶
RouteRegistrar adds a package's routes to the application's mux (Go 1.22 patterns, e.g. "POST /oauth/token").
type Session ¶
type Session struct {
IDHash []byte
PersonID PersonID
CreatedAt time.Time
LastSeenAt time.Time
ExpiresAt time.Time // the absolute end
StepUpUntil time.Time // zero when there is no fresh step-up
UserAgent string
}
Session is a browser session. Only the sha256 of the cookie value is stored.
type Sessions ¶
type Sessions interface {
// Viewer returns the signed-in person of r, or ErrNoSession.
Viewer(r *http.Request) (Viewer, error)
// RequireViewer runs, for mutating methods, the CSRF checks of docs/PLAN.md §4.4 first (403
// {"error":"forbidden"}). Then: 401 {"error":"no_session"} without a session; otherwise it stores the Viewer in
// the request context (read it with ViewerFrom) and calls next.
RequireViewer(next http.Handler) http.Handler
// RequireStepUp is placed after RequireViewer: 403 {"error":"step_up_required"} unless the session has a fresh
// step-up.
RequireStepUp(next http.Handler) http.Handler
// SameOrigin runs only the CSRF checks of a mutating request (403 {"error":"forbidden"}).
SameOrigin(next http.Handler) http.Handler
}
Sessions resolves browser sessions; package auth implements it.
type SingleResource ¶
type SingleResource struct {
At string // the path, e.g. "/mcp"
ID ResourceID
}
SingleResource serves one resource at one path (Homie: At "/mcp", ID "home").
func (SingleResource) FromPath ¶
func (s SingleResource) FromPath(path string) (ResourceID, bool)
FromPath implements ResourcePaths.
func (SingleResource) Path ¶
func (s SingleResource) Path(ResourceID) string
Path implements ResourcePaths.
type Site ¶
type Site struct {
PublicURL string // "https://hub.example.com": no path, no trailing slash; also the OAuth issuer and the WebAuthn origin
RPID string // the WebAuthn relying party ID: the host of PublicURL
ClientIP ClientIPMode
}
Site says where the application is served. Every package that builds URLs or checks origins takes one.
type Token ¶
type Token struct {
Hash []byte
GrantID GrantID
Kind TokenKind
Family string // the refresh rotation family; reusing a rotated refresh token revokes the family
CreatedAt time.Time
ExpiresAt time.Time
UsedAt *time.Time // refresh tokens: when it was rotated
RevokedAt *time.Time
}
Token is an opaque OAuth token. Only its sha256 is stored.
type TokenValidator ¶
type TokenValidator interface {
// ValidateAccess returns the access behind token if it is live and was issued for resource r; otherwise
// ErrInvalidToken.
ValidateAccess(ctx context.Context, token string, r ResourceID) (AccessInfo, error)
}
TokenValidator checks access tokens; package oauth implements it.