httphandler

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 30 Imported by: 1 module, 1 package

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.

README

Sign In

Signs people in with providers, such as Google, GitHub and LDAP, which say who they are: an identity, which is exchanged once they have. Its paths are under auth/, and need no authentication.

  • Redirect providers: such as Google and GitHub, are signed in with at their own pages. login redirects to the provider, which redirects back to callback, with a code, which is exchanged, with PKCE.
  • Password providers: such as LDAP, are signed in with a username and password, posted to login.
  • State: the state and PKCE verifier of a sign-in are in a cookie, which is encrypted, for the provider's paths, for ten minutes, and cleared by the callback.
  • Sessions: a session's token is returned once, and set as an HttpOnly cookie, for the API's paths, so a browser sends it. A request has it as a bearer token, or the cookie.
  • Returning: with return, a path on the server, such as /, signing in redirects there (303), rather than returning the session, as a browser does.
  • Exchange: an identity is exchanged for what the server makes of it, such as a session, with its token, which is returned once, or else the identity itself.

Sign In

GET /auth/provider

Lists the providers: their names, which are in their paths, labels, to show, and kinds, redirect or password.

GET /auth/{provider}/login

Redirects to a redirect provider's sign-in page, with the state of the sign-in in a cookie. With return, the callback redirects to the path, once signed in.

POST /auth/{provider}/login

Signs in with a password provider, with a username and password, as JSON or a form, and redirects to return, when it's set, or else returns what the identity is exchanged for, such as a session. A username which isn't found, and a password which isn't right, are the same 401.

GET /auth/{provider}/callback

Where a redirect provider returns, with a code and the state, or an error. The state must be the sign-in's, from its cookie, which is cleared. Returns what the identity is exchanged for.

GET /auth/session

Returns the session of the request's token: who signed in, with which provider, their scopes, and when it expires.

POST /auth/logout

Signs out: ends the session of the request's token, and clears its cookie.

OAuth

With OAuth, the server is an OAuth authorization server for its API, as MCP clients expect: they discover it, register, and are authorized by whoever's signed in, for a session of their own.

  • Discovery: the API's protected resource metadata (RFC 9728) names this server, whose metadata (RFC 8414) has its endpoints. A 401 says where the metadata is.
  • Clients: are public, with PKCE (S256), and aren't kept: a client's ID is its registration, encrypted. They redirect to loopback URIs, on any port, or to ones the server allows.
  • Authorizing: whoever's signed in authorizes a client, without being asked, or else signs in first, and returns. The redirect must still be allowed. The code lasts a minute, and is exchanged once: the session store remembers it, when it can (the file store does), so servers which share it, and restarts, don't exchange it again.
  • Tokens: a code is exchanged for a session of its own, with the scopes asked for which the person has, which ends with theirs, and can be revoked. There are no refresh tokens.
GET /.well-known/oauth-protected-resource

Returns the API's protected resource metadata: the resource, which is the API's URL, and its authorization server, which is this one. It's also at the path with the API's prefix after it, such as /.well-known/oauth-protected-resource/api.

GET /.well-known/oauth-authorization-server

Returns the authorization server's metadata: its issuer, which is the server's URL, its endpoints, and what they support.

POST /auth/register

Registers a client (RFC 7591), with its redirect_uris, and returns its client_id, which is at most 4 KiB. A redirect URI is a loopback one, such as http://127.0.0.1:8080/callback, or one the server allows.

GET /auth/authorize

Authorizes a client, with PKCE: redirects to its redirect URI, with a code, the state and the issuer, as iss, or an error. When no one's signed in, it redirects to sign in, which returns here. Without a valid client, or redirect URI, it's 400.

POST /auth/token

Exchanges a code, once, with its client_id, redirect_uri and code_verifier, as a form, for a session's token, as access_token, which is a bearer token, and its scope, which is always there, even when it's empty.

Documentation

Overview

Package httphandler registers the HTTP handlers which sign people in with providers, such as Google, GitHub and LDAP, and exchange who they are for what the server makes of it

Index

Constants

This section is empty.

Variables

View Source
var HandlerDoc []byte

Functions

func Providers added in v0.0.15

func Providers(providers []provider.Provider) []schema.ProviderMeta

Providers returns what the providers are, for anyone to see, such as on a sign-in page, or in the server's configuration

func RegisterHandlers added in v0.0.15

func RegisterHandlers(providers []provider.Provider, opt ...Opt) func(*httprouter.Router) error

RegisterHandlers registers signing in with the providers, which need unique names: listing them, a redirect provider's sign-in and callback, and a password provider's sign-in

func ResourceMetadataPath added in v0.0.15

func ResourceMetadataPath(prefix string) string

ResourceMetadataPath is the path of the protected resource metadata of the API, at prefix, such as /api, which a 401's challenge refers to

Types

type ExchangeFunc added in v0.0.15

type ExchangeFunc func(ctx context.Context, identity *schema.Identity) (any, error)

ExchangeFunc is what's done with an identity, once someone has signed in, whose result is the response, or whose error refuses it

type Opt added in v0.0.15

type Opt func(*options) error

Opt is an option of the handlers

func WithBaseURL added in v0.0.15

func WithBaseURL(base string) Opt

WithBaseURL is the URL the server is reached at, such as https://auth.example.com, which providers redirect back to. Without it, it's the request's scheme and host.

func WithExchange added in v0.0.15

func WithExchange(fn ExchangeFunc, response *jsonschema.Schema) Opt

WithExchange does fn with an identity, once someone has signed in, and returns its result, whose schema is the response's, in the OpenAPI specification, such as a session's. Without it, the identity is returned.

func WithKey added in v0.0.15

func WithKey(key []byte) Opt

WithKey seals the state of sign-ins with the key, of 32 bytes, so they can be completed by another server with the key, or after a restart. Without it, the key is random.

func WithOAuth added in v0.0.15

func WithOAuth(redirects ...string) Opt

WithOAuth makes the server an OAuth authorization server, as MCP clients expect, which issues its sessions to clients: it publishes its metadata, registers clients, and authorizes them, for someone who's signed in, with a code, which they exchange, with PKCE, for a session's token. It needs WithSessions. Clients are redirected to loopback URIs, such as http://127.0.0.1:8080/callback, on any port, or to one of the redirects, such as https://app.example.com/callback, exactly, as people aren't asked to approve them.

func WithSessions added in v0.0.15

func WithSessions(sessions store.SessionStore) Opt

WithSessions sets a session's token as a cookie, when signing in is exchanged for a session, for a browser, and registers getting the session and signing out, which need the security scheme, which is registered first

func WithSignIn added in v0.0.15

func WithSignIn(path string) Opt

WithSignIn is the path of the sign-in page, such as /login, which authorizing a client redirects to, with the path to return to, as return, when no one's signed in. Without it, it's the redirect provider's sign-in, when there's only one.

Jump to

Keyboard shortcuts

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