auth-sdk

command
v14.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

The auth SDK: auth.Auth[T] with a provider, ready-made token handlers and per-router verification.

You supply two things: a Configuration (the JWT keys) and a Provider[T] over your user store. In return the SDK gives you the whole token lifecycle (SigninHandler, RefreshHandler, SignoutHandler, SignoutAllHandler) plus the VerifyHandler middleware, EdDSA signatures and encrypted refresh tokens.

This example is self-contained: Configuration.BindRandom generates an ephemeral EdDSA key pair for access and refresh tokens, so every restart invalidates the tokens it issued. Production loads persistent keys from a file instead, with auth.MustLoad[Customer]("auth.yml"), and the loader prints a ready-to-edit example configuration when the file is missing.

SigninHandler and RefreshHandler switch on the request's media type, so a JSON body needs the Content-Type header; otherwise send a form instead. tom@example.com (password tom_password) signs in but is inactive, so the requireActive check below answers 401 "account disabled" on /me.

Learn: give a service sign-in, refresh, sign-out and JWKS endpoints by implementing one Provider[T] over your own user store. Before: ../sessions

Run:

go run main.go

Try:

curl -i -X POST -H "Content-Type: application/json" \
  http://localhost:8080/signin -d '{"username":"maria@example.com","password":"maria_password"}'   # 200, access_token and refresh_token
TOKEN=<access_token from the response above>
curl -i http://localhost:8080/me                                    # 401, no token
curl -i -H "Authorization: Bearer $TOKEN" http://localhost:8080/me  # 200, the Customer
curl -i -X POST -H "Content-Type: application/json" \
  http://localhost:8080/refresh -d '{"refresh_token":"<refresh_token>"}'   # 200, a fresh pair; both tokens rotate
curl -i -X POST -H "Authorization: Bearer $TOKEN" http://localhost:8080/me/signout   # 200, empty body
curl -i -H "Authorization: Bearer $TOKEN" http://localhost:8080/me  # 401, the token is revoked
curl http://localhost:8080/.well-known/jwks.json                    # 200, the public key, for other services

Next: ../../10-configuration/configuration

Jump to

Keyboard shortcuts

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