auth

command
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 8 Imported by: 0 modules, 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

Bearer auth example

Minimal framework.App with JWT secret + SQLite. framework.New mounts POST /api/v1/auth/{register,login,refresh,logout}, GET /api/v1/me, and the session-management routes under /api/v1/auth/sessions.

Run

go run ./examples/auth

Interactive docs: http://127.0.0.1:8080/docs.

E2E (D10 envelopes)

# register
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/register \
  -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct-horse"}'

# login
LOGIN=$(curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/login \
  -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct-horse"}')
echo "$LOGIN"

ACCESS=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["access_token"])' <<<"$LOGIN")
REFRESH=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["refresh_token"])' <<<"$LOGIN")

# protected route
curl -sS http://127.0.0.1:8080/api/v1/me -H "Authorization: Bearer $ACCESS"

# rotate refresh (old refresh is then invalid)
ROTATED=$(curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/refresh \
  -H 'Content-Type: application/json' \
  -d "{\"refresh_token\":\"$REFRESH\"}")
echo "$ROTATED"
ACCESS=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["access_token"])' <<<"$ROTATED")
REFRESH=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["refresh_token"])' <<<"$ROTATED")

# logout (revokes the current refresh token and bound access JWT)
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/logout \
  -H 'Content-Type: application/json' \
  -d "{\"refresh_token\":\"$REFRESH\"}"

Sessions

Each login is a session. Log in twice (for example, once per device) and manage the sessions from either one:

LOGIN=$(curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/login \
  -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct-horse"}')
ACCESS=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["access_token"])' <<<"$LOGIN")
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/login \
  -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct-horse"}' >/dev/null

# list: the session making the request has "current": true; each shows the
# device it signed in from (user_agent, client_address as a /24 or /48
# network by default, location if the app looks it up)
SESSIONS=$(curl -sS http://127.0.0.1:8080/api/v1/auth/sessions -H "Authorization: Bearer $ACCESS")
echo "$SESSIONS"
OTHER=$(python3 -c 'import json,sys; print(next(s["id"] for s in json.load(sys.stdin)["data"] if not s["current"]))' <<<"$SESSIONS")

# revoke one (a session keeps its id across refreshes)
curl -sS -X DELETE "http://127.0.0.1:8080/api/v1/auth/sessions/$OTHER" \
  -H "Authorization: Bearer $ACCESS"

# revoke every other session, or every session including this one
curl -sS -X DELETE "http://127.0.0.1:8080/api/v1/auth/sessions?scope=others" \
  -H "Authorization: Bearer $ACCESS"
curl -sS -X DELETE "http://127.0.0.1:8080/api/v1/auth/sessions?scope=all" \
  -H "Authorization: Bearer $ACCESS"

See docs/auth.md for the semantics.

Email verification

Registration mails a verification message. This example sets no mailer (framework.WithAuthMailer), so in development the server logs the message instead, token included (auth: email not sent (development LogMailer)):

# the sessions above were revoked: log in again
LOGIN=$(curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/login \
  -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct-horse"}')
ACCESS=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["access_token"])' <<<"$LOGIN")

# ask for a new link (once a minute; supersedes the earlier one)
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/verify-email/resend \
  -H "Authorization: Bearer $ACCESS"

# post the latest token in the log; GET /me says "email_verified": false
# until then
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/verify-email \
  -H 'Content-Type: application/json' -d '{"token":"<token from the log>"}'

See docs/auth.md for the token rules, the mailer, and auth.RequireVerifiedEmail.

The development JWT secret in main.go is not a production secret. Production config rejects secrets shorter than 32 characters.

Password reset

Ask for a reset link; the answer is the same whether or not the email is an account, and the development server logs the message (auth: email not sent (development LogMailer)):

curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/password/forgot \
  -H 'Content-Type: application/json' -d '{"email":"ada@example.com"}'
# 202 {"data":{"accepted":true}}

# set a new password with the token in the log: every session of ada ends
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/password/reset \
  -H 'Content-Type: application/json' \
  -d '{"token":"<token from the log>","password":"correct-horse-2"}'
# {"data":{"ok":true,"sessions_revoked":true}}

See docs/auth.md for the rules.

Password change

Signed in, change the password with the current one; every other session of the user is signed out, this one stays:

# the reset above signed every session out: log in with its password
LOGIN=$(curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/login \
  -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct-horse-2"}')
ACCESS=$(python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["access_token"])' <<<"$LOGIN")

curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/password/change \
  -H "Authorization: Bearer $ACCESS" -H 'Content-Type: application/json' \
  -d '{"current_password":"correct-horse-2","new_password":"correct-horse-3"}'
# {"data":{"ok":true,"other_sessions_revoked":true}}

See docs/auth.md.

Email change

Signed in, ask for a new address with the current password; it becomes the account's email once the link mailed to it (logged in development) is confirmed, and the old address is notified:

curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/email/change \
  -H "Authorization: Bearer $ACCESS" -H 'Content-Type: application/json' \
  -d '{"current_password":"correct-horse-3","new_email":"ada@new.example.com"}'
# 202 {"data":{"sent":true}}

curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/email/change/confirm \
  -H 'Content-Type: application/json' -d '{"token":"<token from the log>"}'
# {"data":{"id":1,"email":"ada@new.example.com","email_verified":true}}

See docs/auth.md.

Multi-factor authentication

Enroll an authenticator app (scan the otpauth_uri as a QR code), confirm it with a code, and keep the recovery codes; from then on the login asks for a code:

curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/mfa/totp -H "Authorization: Bearer $ACCESS" \
  -H 'Content-Type: application/json' -d '{"current_password":"correct-horse"}'
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/mfa/totp/confirm -H "Authorization: Bearer $ACCESS" \
  -H 'Content-Type: application/json' -d '{"code":"<code from the app>"}'

curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/login -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct-horse"}'
# 422: fields.mfa_code = ["a multi-factor code is required"]
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/login -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct-horse","mfa_code":"<code from the app>"}'

See docs/auth.md.

External identity providers

Set OIDC_ISSUER, OIDC_CLIENT_ID and OIDC_CLIENT_SECRET to sign in with an OpenID Connect provider (auth/oidc), registered there with the redirect URI http://127.0.0.1:8080/api/v1/auth/external/oidc/callback:

OIDC_ISSUER=https://accounts.google.com OIDC_CLIENT_ID=... OIDC_CLIENT_SECRET=... go run ./examples/auth

curl -sS -c jar -X POST http://127.0.0.1:8080/api/v1/auth/external/oidc/login \
  -H 'Content-Type: application/json' -d '{"return_to":"/"}'
# {"data":{"authorization_url":"https://accounts.google.com/o/oauth2/v2/auth?..."}}

Open the URL in the browser that holds the gombit_external cookie (a curl cookie jar can't follow the provider's sign-in). The callback redirects to /?gombit_code=.... Trade the code for tokens:

curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/external/exchange \
  -H 'Content-Type: application/json' -d '{"code":"<gombit_code>"}'

See docs/auth.md.

Account state

A superuser (or a user granted auth.users.manage) disables an account; its sessions end at once and it cannot sign in until it is enabled again:

curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/users/2/disable -H "Authorization: Bearer $ADMIN_ACCESS"
# {"data":{"id":2,"disabled":true}}
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/login \
  -H 'Content-Type: application/json' -d '{"email":"grace@example.com","password":"correct-horse"}'
# 403 {"error":{"code":"authorization","message":"account is disabled",...}}
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/users/2/enable -H "Authorization: Bearer $ADMIN_ACCESS"
# {"data":{"id":2,"disabled":false}}

See docs/auth.md.

Abuse protection

The credential endpoints are throttled. Six wrong passwords for one email from one address in a row: the sixth is refused before the password is checked, and says how long to wait:

for i in 1 2 3 4 5 6; do
  curl -sS -o /dev/null -w '%{http_code} ' -X POST http://127.0.0.1:8080/api/v1/auth/login \
    -H 'Content-Type: application/json' -d '{"email":"ada@example.com","password":"wrong"}'
done
# 401 401 401 401 401 429

curl -sS -i -X POST http://127.0.0.1:8080/api/v1/auth/login \
  -H 'Content-Type: application/json' -d '{"email":"ada@example.com","password":"wrong"}' | grep -i retry-after
# Retry-After: 1080

The address may still sign in to other accounts, and the account's owner from theirs. See docs/auth.md for every rule, and GOMBIT_HTTP_TRUSTED_PROXIES behind a proxy.

Audit trail

Every step above was recorded in auth_audit_events: the registration, the logins, the logout, the session revocations, the verification, the password reset and change, the email change, each with the user, the time, and the request (X-Request-Id, client IP, user agent). A failed login is recorded too:

curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/login \
  -H 'Content-Type: application/json' -H 'X-Request-Id: audit-demo-1' \
  -d '{"email":"ada@new.example.com","password":"wrong-password"}'
# auth.login.failed, request_id audit-demo-1, details {"reason":"wrong_password"}

Read them in Go with auth.ListAuditEvents(ctx, db.DB, auth.AuditQuery{Action: auth.AuditLoginFailed}). See docs/auth.md for what is recorded and how to send the events elsewhere.

Create an admin account (gombit createsuperuser)

The example uses an in-memory SQLite database, so point gombit createsuperuser at a file-backed database instead if you want the account to persist across runs:

GOMBIT_DATABASE_DRIVER=sqlite \
GOMBIT_DATABASE_DSN='file:auth-example.db?cache=shared&_fk=1' \
GOMBIT_JWT_SECRET='dev-only-example-jwt-secret-not-for-prod' \
go run ./cmd/gombit createsuperuser --no-input \
  --email admin@example.com --password correct-horse-battery-staple

See docs/cli.md and docs/auth.md.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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