Help us shape the future of Go! Take the 2026 Go Developer Survey

auth

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: AGPL-3.0 Imports: 30 Imported by: 0

Documentation

Overview

Package auth is sign-in (ADR-0024, docs/architecture/auth.md): accounts, sign-in by email and one-time code, the browser (OAuth 2.0 + PKCE) and device (RFC 8628) flows, access and refresh tokens, sessions and JWKS. It knows no members, roles or workspaces: a workspace platform runs it for its own accounts and says who may sign in (Config.Allow); the site will run it for its buyers. There is no registration: the first sign-in of an email makes its account.

Index

Constants

View Source
const (
	AccessTTL      = 15 * time.Minute
	SessionIdle    = 30 * 24 * time.Hour // a session not refreshed for this long ends
	SessionMax     = 90 * 24 * time.Hour // and any session ends after this
	CodeTTL        = 10 * time.Minute    // one-time codes in emails
	CodeAttempts   = 5
	AuthCodeTTL    = time.Minute // authorization codes of the browser sign-in
	DeviceCodeTTL  = 10 * time.Minute
	DeviceInterval = 5 * time.Second
	KeyRotation    = 30 * 24 * time.Hour // a new signing key after this

	// Codes sent per hour.
	LimitPerEmail  = 5
	LimitPerIP     = 30
	LimitPerDevice = 10
)

Lifetimes and limits of docs/architecture/auth.md.

View Source
const ClientID = "devopus-cli"

ClientID is the only OAuth client: the devopus CLI.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Issuer is the public URL of the service: the iss of tokens and the base of the
	// sign-in pages.
	Issuer string
	Mailer mail.Mailer
	Log    *slog.Logger
	// Allow says whether an email may sign in: on a platform, its owner, its members
	// and the invited. Others get the same answer and no email, and their code never
	// works. nil lets everyone in.
	Allow func(ctx context.Context, email string) (bool, error)
	// External is a sign-in elsewhere the browser sign-in of the CLI offers; nil
	// offers none.
	External *External
	// TrustProxy takes the client address from X-Forwarded-For: only behind a proxy
	// that sets it.
	TrustProxy bool
	// Now is the clock; time.Now when nil.
	Now func() time.Time
}

Config configures the service.

type External added in v0.4.0

type External struct {
	// Label is the button's text: "Continue with your Devopus account".
	Label string
	// URL is where the button leads; authorize is the CLI's browser sign-in request,
	// empty for the web.
	URL func(authorize url.Values) string
}

External is a sign-in elsewhere the sign-in pages offer next to the email.

type Service

type Service struct {
	authv1connect.UnimplementedAuthServiceHandler
	// contains filtered or unexported fields
}

Service is the Auth service.

func New

func New(ctx context.Context, pool *pgxpool.Pool, cfg Config) (*Service, error)

New opens the service on a migrated database and makes sure it has a signing key.

func (*Service) AuthorizeVerified added in v0.4.0

func (s *Service) AuthorizeVerified(ctx context.Context, email, ip string, authorize url.Values) (string, error)

AuthorizeVerified ends the CLI's browser sign-in (the request in authorize) for a checked email: the URL of the CLI's callback with the authorization code.

func (*Service) ClientIP added in v0.4.0

func (s *Service) ClientIP(peer string, h http.Header) string

clientIP is the caller's address: the peer, or the first X-Forwarded-For entry behind a trusted proxy. ClientIP is the address of a request's client, from X-Forwarded-For when the service trusts a proxy.

func (*Service) Handler

func (s *Service) Handler() http.Handler

Handler serves the RPCs, the sign-in pages and JWKS.

func (*Service) JWKS

func (s *Service) JWKS() token.JWKS

JWKS is the published key set, for tests and servers in the same process.

func (*Service) OpenVerified added in v0.4.0

func (s *Service) OpenVerified(ctx context.Context, email, device, ip, audience string) (*authv1.Tokens, error)

OpenVerified opens a session for a checked email: the tokens of a browser that came from the site.

func (*Service) RotateKeys

func (s *Service) RotateKeys(ctx context.Context, force bool) error

RotateKeys loads the signing keys and makes a new one when there is none, when the newest is older than KeyRotation, or when force is set.

func (*Service) Run

func (s *Service) Run(ctx context.Context)

Run does the background work until ctx ends: picks up keys other instances made, rotates the signing key, deletes what expired.

func (*Service) Sign

func (s *Service) Sign(c token.Claims) (string, error)

Sign signs claims with the current key: for tests of the process sign-in runs in (a token about to expire, one for another address).

func (*Service) SignAccess

func (s *Service) SignAccess(userID, email, audience string) (string, error)

SignAccess signs an access token for a user, with no session behind it: for the tests and tools of the process sign-in runs in. It cannot be refreshed.

func (*Service) Verify

func (s *Service) Verify(access string) (token.Claims, error)

Verify checks an access token Auth issued for Devopus itself.

Directories

Path Synopsis
Package mail sends Auth's emails: the sign-in codes.
Package mail sends Auth's emails: the sign-in codes.
Package store is the database of accounts and sign-in (schema auth): the schema (migrations), the queries (sqlc) and the migration runner.
Package store is the database of accounts and sign-in (schema auth): the schema (migrations), the queries (sqlc) and the migration runner.
Package token signs and checks Devopus access tokens: JWTs signed with Ed25519 (alg EdDSA) whose public keys Auth publishes as JWKS.
Package token signs and checks Devopus access tokens: JWTs signed with Ed25519 (alg EdDSA) whose public keys Auth publishes as JWKS.

Jump to

Keyboard shortcuts

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