Documentation
¶
Overview ¶
Package oauth is the gatekeeper's OAuth 2.1 authorization server for MCP connectors (docs/security/G4.md): the metadata documents, dynamic client registration and client ID metadata documents, authorize with its in-memory transactions, the consent API the admin SPA calls, token, revoke, and core.TokenValidator for the MCP access check.
One issuer at the application's root serves every resource, and a token is bound to one resource. The authorize request names the resource with the RFC 8707 resource <public URL><Paths.Path(id)> (Family Hub, with core.PerResource: <public URL>/mcp/<id>; with core.SingleResource: <public URL>/mcp), or, without one, with the scope mcp:<id>; the consent makes a grant of (person, client, resource); the validator refuses the token for any other resource.
The rules: only ChatGPT's and Claude's callbacks are accepted and they tell the assistant, PKCE S256 is required, every secret, code and token is kept only as its sha256, and a refresh token is spent on use, so one that comes back revokes its whole family. An application may also declare native apps of its own (Options.NativeApps), which register by DCR with a loopback or a private-use URI scheme callback (RFC 8252) and are public clients under the same rules.
A person acts only while core.Person.Active: the consent API, the code exchange and the refresh refuse anyone disabled or past the end of their term (Person.AccessUntil), and an access token never outlives that end. The grant itself stays, so a term the owner extends gives the person their connections back.
The package came from Family Hub's internal/oauth. What it sends to the assistants and the SPA is byte-for-byte what Family Hub sent, its wording included, except the one rename of the consent view (docs/security/G4.md, "Compatibility"). The texts name a resource and the application by Brand.ResourceWord and Brand.AppWord, whose defaults are Family Hub's "module" and "hub".
Index ¶
Constants ¶
const AppAssistantPrefix = "app:"
AppAssistantPrefix starts the core.Assistant of a native app's client: "app:<ID>".
Variables ¶
This section is empty.
Functions ¶
func NativeAppID ¶ added in v0.3.0
NativeAppID returns the NativeApp.ID of a native app's client from its assistant, and false for any other client.
func ResourceMetadataURL ¶
func ResourceMetadataURL(publicURL string, paths core.ResourcePaths, id core.ResourceID) string
ResourceMetadataURL is where the protected-resource metadata of id is served: the well-known between the public URL and the resource's MCP path (RFC 9728 §3.1; Family Hub: <public URL>/.well-known/oauth-protected-resource/mcp/<id>). The MCP access check names it in the WWW-Authenticate of a 401.
func ResourceURL ¶
func ResourceURL(publicURL string, paths core.ResourcePaths, id core.ResourceID) string
ResourceURL is the RFC 8707 resource of id: its MCP URL, the public URL followed by paths.Path(id) (Family Hub: <public URL>/mcp/<id>).
Types ¶
type NativeApp ¶ added in v0.3.0
type NativeApp struct {
ID string // ^[a-z][a-z0-9-]{1,30}$, e.g. "desk"; the software_id of its registrations
Name string // what the consent screen calls it, e.g. "Desk"; at most 64 characters, none invisible
// Ports, when set, are the only loopback ports the app's redirect may use (1 to 65535, no repeats): another
// program on the computer that listens on a port of its own cannot receive the app's code then. None (the
// default) allows any port, as RFC 8252 §7.3 describes.
Ports []int
// Schemes, when set, are private-use URI schemes of the app (RFC 8252 §7.1), e.g. "com.example.desk": a client of
// the app may register <scheme>:/oauth/callback for each, next to the loopback callbacks, and authorize matches it
// exactly. A scheme is a lowercase reverse-DNS name with at least one dot (RFC 8252 §7.1 asks for a domain the
// app's maker controls), at most 128 characters, and no scheme comes twice, in one app or across apps. None (the
// default) allows the loopback callbacks only.
Schemes []string
}
NativeApp is a program of the application's own that runs on the person's computer or phone and connects the way an assistant does, but with a loopback or a private-use URI scheme redirect (RFC 8252 §7.3, §7.1): it registers by DCR with software_id ID, and its client's core.Assistant is AppAssistantPrefix + ID. The application maps that to its own client label.
type Options ¶
type Options struct {
// Site.PublicURL is the issuer: https://<host> without a path (http only on a loopback host). Site.ClientIP says
// where the client address of the rate limits and the audit comes from. New refuses a Site that fails
// Site.Validate, an empty or unknown ClientIP mode included.
Site core.Site
// Brand.TxnCookie names the cookie that binds a consent to the browser that started it; Brand.Product titles the
// error pages and gives the realm of a Basic challenge ("Family Hub": realm="family-hub"); Brand.ResourceWord and
// Brand.AppWord are the words of the error texts ("module" and "hub" by default).
Brand core.Brand
Store store.Store
// Sessions resolves the signed-in person of the consent API (package auth).
Sessions core.Sessions
// Catalog says which resources can be connected and by which accounts. A resource id must match
// ^[a-z][a-z0-9-]{1,30}$ (Family Hub's module ids; Homie's "home").
Catalog core.ResourceCatalog
// Paths maps resources to their MCP paths (Family Hub: core.PerResource{Prefix: "/mcp/"}); a resource's URL is
// Site.PublicURL + Paths.Path(id).
Paths core.ResourcePaths
Logger *slog.Logger // nil: discard
Clock core.Clock // nil: the system clock in UTC
// ConsentStepUpOff makes approving a new grant one click. By default it needs a fresh step-up (a sign-in counts
// for 5 minutes); reconnecting an existing grant is one click either way. Family Hub: FH_CONSENT_STEPUP=off.
ConsentStepUpOff bool
// TxnBindingOff stops binding an authorize transaction to the browser that started it. Only for an assistant
// window that loses the cookie. Family Hub: FH_OAUTH_TXN_BINDING=off.
TxnBindingOff bool
// RefuseConnectorCallback refuses ChatGPT's per-connector callback /connector/oauth/<id>, which closes the
// mix-up of T2.R H1. Family Hub: FH_OAUTH_CONNECTOR_CALLBACK=refuse.
RefuseConnectorCallback bool
// NativeApps are the application's own programs that may connect from the person's computer or phone with a
// loopback or, with NativeApp.Schemes, a private-use URI scheme redirect (RFC 8252), each by DCR with its ID as
// software_id. None (the default) changes nothing: only ChatGPT's and Claude's callbacks are accepted, with the
// same bytes. New refuses an ID that does not match ^[a-z][a-z0-9-]{1,30}$ or comes twice, an empty or invisible
// Name, a bad or repeated port, and a scheme that is not a lowercase reverse-DNS name or comes twice.
NativeApps []NativeApp
}
Options are the dependencies and switches of New.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the authorization server. It serves the OAuth routes and the consent API (core.RouteRegistrar) and implements core.TokenValidator for the MCP access check.
func New ¶
New returns the authorization server of o.Site; its issuer is the public URL. It fetches client ID metadata documents over HTTPS from the assistants' hosts only.
func (*Server) RegisterRoutes ¶
RegisterRoutes adds the OAuth routes to mux: the metadata, register, authorize, token and revoke for the assistants, and the consent API for the SPA.
func (*Server) RevokeGrant ¶
func (s *Server) RevokeGrant(ctx context.Context, id core.GrantID, actor core.PersonID, ip string) error
RevokeGrant revokes grant id with every token of it and audits oauth.grant.revoke, by actor from ip. The caller has checked that actor may (the connections API of package people). A revoked grant is left as it is; an unknown one is core.ErrNotFound.
func (*Server) ValidateAccess ¶
func (s *Server) ValidateAccess(ctx context.Context, token string, r core.ResourceID) (core.AccessInfo, error)
ValidateAccess implements core.TokenValidator. token must be a live access token, found by its sha256, whose grant is active, younger than grantTTL, and for resource r. A known access token that has ended is errEnded; any other token is a bare core.ErrInvalidToken. A store failure is returned as itself, so the MCP access check can answer 5xx rather than send the assistant to sign in again.
It reads no person: a term reaches it as the token's expiry, and the MCP access check refuses a person who is not active, as it refuses a disabled one.
It also records the grant's last use, at most once a minute, for the connections list.