httphandler

package
v0.0.22 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 26 Imported by: 1 module, 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.

README

Credential Handlers

Credentials

Credentials store secret material, such as API keys or tokens, under a key, for other parts of the system to authenticate with third-party services. A credential's value is only returned with the passphrase it was encrypted with, so only for backends which encrypt with their own passphrases (file and pg). Other backends, such as AWS and Bitwarden Secrets Manager, never return values over HTTP.

GET /credential

List the stored credentials in key order, without their values. Credentials from the environment aren't listed. Returns count, and each credential's key and description, with the time it was last changed, when the backend records it. Needs the credential:read scope.

PUT /credential

Store a credential, replacing any credential with the same key. The request body is a JSON object with the key, an identifier of up to 64 letters, digits, _ and -, starting with a letter, and the credentials, which can be any JSON value, and, optionally, its description, such as what it's for, which is kept when it isn't set, and removed when it's empty. Returns the key, and the description, when it's set. Returns 400 if the key or credentials are invalid, 403 if the backend refuses the write, or 503 if the backend has no passphrase. Needs the credential:write scope.

GET /credential/{key}

Get the key of a stored credential, its description, and the time it was last changed, without its value. Use it to check that a credential exists. Returns 400 if the key is invalid, or 404 if there's no credential for the key. Needs the credential:read scope.

POST /credential/{key}

Get a credential's value, with the passphrase it was encrypted with as the request body, as text/plain, which must match exactly, including any spaces or line endings. This is a POST rather than a GET, as the request has a body, and the response is sent with Cache-Control: no-store, so the value isn't cached. Returns the credential as JSON. Returns 400 if the key or request body is invalid, 403 if the passphrase isn't the credential's passphrase, or 404 if there's no credential for the key. Only for backends which encrypt with their own passphrases. Needs the credential:decrypt scope.

PATCH /credential/{key}

Change a stored credential's description, without its value. The request body is a JSON object with its description, which is removed when it's empty. Returns the key, and the description. Returns 400 if the key or request body is invalid, 403 if the backend refuses the write, or 404 if there's no credential for the key. Needs the credential:write scope.

DELETE /credential/{key}

Delete a stored credential. A credential from the environment can't be deleted. Returns 400 if the key is invalid, or 404 if there's no credential for the key. Needs the credential:write scope.

POST /credential/{key}/rotate

Re-encrypt a credential with the latest passphrase version, without changing its value, after a new passphrase version is added. Returns 304 if the credential already uses the latest passphrase version, 400 if the key is invalid, 404 if there's no credential for the key, or 503 if no passphrase is configured. Only for backends which encrypt with their own passphrases. Needs the credential:write scope.

POST /credential/{key}/signin

Store a sign-in, as the credential, when the server registers an authorization server for the key's prefix, such as GOOGLE_ for Google. A client, such as a command line tool, gets how to sign in from a server, signs in at the authorization server with PKCE, which redirects to a loopback address with a code, and sends the code here. The server exchanges it with the OAuth client which is the value of the client credential, such as GOOGLE_CLIENT, as its client_id and client_secret, or the file Google's console downloads, so the client's secret stays on the server. The grant, with its tokens, is stored without the secret. The request body is a JSON object with the code, its code_verifier, and the redirect_uri, which must be a loopback address. Returns the key, the server's issuer, and the scopes it granted, without the tokens. Returns 400 if the key, request body or redirect is invalid, the key isn't a sign-in's, or the server refuses the code, 403 if the backend refuses the write, or 503 if there's no client, the server refuses the client, or the server fails. Needs the credential:write scope.

POST /credential/{key}/authorize

Sign in, in a browser, when the server signs in in a browser with the key's prefix's authorization server, whose client, such as GOOGLE_CLIENT, is a web application's OAuth client, whose redirect URIs are the server's callback, and, optionally, a fixed loopback address of a command line tool. A desktop app's client can't redirect to the server, so it's refused. The request body is a JSON object with the redirect_uri, the server's credential-signin/callback, as the browser reaches it, which must be registered with the client, and be https, or http of a loopback host, and the scopes to ask for, such as those of a sign-in error. Returns the url the person signs in at, with a state, and a PKCE challenge, whose verifier the server keeps, for ten minutes, and sets an HttpOnly, SameSite=Lax cookie of the callback's path, Secure when it's https, so only this browser completes the sign-in. Returns 400 if the key, request body or redirect is invalid, the key isn't a sign-in's, its server isn't signed in with in a browser, or its client is a desktop app's, or 503 if there's no client's credential, or it isn't a client. Needs the credential:write scope.

GET /credential-signin/callback

The redirect of a sign-in which was asked for with POST /credential/{key}/authorize, of the browser which asked for it, of its cookie: the server exchanges the code, of the state, which is done once, with the client's secret, and the kept verifier, and stores the grant, without the secret, as the key's credential, with the scopes it asked for, when the server doesn't say which it granted. Returns a page which says the person signed in, or why they aren't, which posts a credential-signin message, of the key, whether it's ok, and the error, to the window which opened it, of the same origin, and closes, once they are. Needs no scope, as its state, which is random, is the sign-in's.

Documentation

Index

Constants

View Source
const (

	// SignInMessage is the type of the message the callback's page posts to
	// the window which opened it, of the key, and whether it's signed in,
	// or why it isn't
	SignInMessage = "credential-signin"
)
View Source
const (

	// Table is the table of the changes which are published, which a web UI
	// follows
	Table = "credential"
)

Variables

View Source
var HandlerDoc []byte

Functions

func RegisterManagerHandlers

func RegisterManagerHandlers(manager credential.CredentialManager, auth bool, with ...Opt) func(*httprouter.Router) error

RegisterManagerHandlers registers the handlers for a credential manager: the provider's handlers, and listing, getting the metadata of, deleting and, when the manager encrypts with its own passphrases, rotating credentials. Register the provider's or the manager's handlers, not both. When auth is true, the handlers need the credential scopes.

func RegisterProviderHandlers

func RegisterProviderHandlers(provider credential.CredentialProvider, auth bool, with ...Opt) func(*httprouter.Router) error

RegisterProviderHandlers registers the handlers for a credential provider: storing a credential, and, when the provider encrypts with its own passphrases, getting a value with its passphrase. Values are never returned without the passphrase. When auth is true, the handlers need the credential scopes.

Types

type Opt

type Opt func(*options)

Opt is an option of the handlers

func WithPublisher

func WithPublisher(publisher Publisher) Opt

WithPublisher publishes a change of a credential, by its key, without its value, after it's stored, described, rotated or deleted, so a web UI which follows the changes loads the credentials again

func WithSignIn added in v0.0.20

func WithSignIn(prefix string, server authclient.ServerMetadata, client string) Opt

WithSignIn stores the sign-ins with the server, such as Google, as the credentials whose keys have the prefix, such as GOOGLE_, other than the client's: a code, which a client authorized at a loopback address, is exchanged with the client, which is the value of the credential with the client key, as its client_id and client_secret, and the grant is stored, without the secret. It can be given more than once: a key is the sign-in of the server with the longest of its prefixes, and no key of a client is a sign-in.

func WithWebSignIn added in v0.0.22

func WithWebSignIn(prefix string, opts ...oauth2.AuthCodeOption) Opt

WithWebSignIn signs in, in a browser, with the client of the sign-ins of the server of the prefix (see WithSignIn), which must be a web application's, such as the file Google's console downloads of one, whose redirect URIs are the server's callback, credential-signin/callback, and, optionally, a fixed loopback address, of a command line tool, as a web application's client redirects to the URIs it registers, rather than any loopback port. POST credential/{key}/authorize returns the URL a person signs in at, which redirects to the callback, which exchanges the code, and stores the grant. The options are of the URL, such as Google's access_type=offline, so the grant can be refreshed, and prompt=consent, as Google only issues a refresh token when the person consents, so a sign-in again, without one, isn't stored without its refresh token. A sign-in is completed only by the browser which asked for it, of a cookie, which authorize sets.

type Publisher

type Publisher interface {
	Publish(schema.Change) int
}

Publisher publishes changes, such as go-server's broadcaster, which a web UI follows

Jump to

Keyboard shortcuts

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