http-minimal

command
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

README

examples/http-minimal

Minimal HTTP integration that demonstrates the goAuth "golden path":

POST /login     →  obtain access + refresh tokens
POST /refresh   →  rotate tokens (cookie-based)
POST /logout    →  destroy session
GET  /protected →  middleware-guarded route

Run

go run ./examples/http-minimal

No external Redis required — uses miniredis in-process.

Try it

# Login (add "remember":true for a durable session capped by
# Config.Session.MaxSessionDuration)
curl -s -X POST http://localhost:8080/login \
  -d '{"username":"alice@example.com","password":"correct-horse","remember":true}' \
  -c cookies.txt

# Access a protected route
TOKEN=$(curl -s -X POST http://localhost:8080/login \
  -d '{"username":"alice@example.com","password":"correct-horse"}' | jq -r .access_token)

curl -s http://localhost:8080/protected \
  -H "Authorization: Bearer $TOKEN"

# Refresh
curl -s -X POST http://localhost:8080/refresh -b cookies.txt -c cookies.txt

# Logout
curl -s -X POST http://localhost:8080/logout \
  -H "Authorization: Bearer $TOKEN"

Integration in your project

  1. Replace stubProvider with your real database-backed UserProvider.
  2. Generate Ed25519 keys with go run ./cmd/goauth-keygen (or use goAuth.DefaultConfig(), which generates ephemeral keys). Set JWT.KeyID and JWT.VerifyKeys from day one so the key-rotation ceremony in docs/ops.md works without a flag day.
  3. Point redis.NewClient at your real Redis instance.
  4. Copy the handler patterns and middleware wiring into your router (middleware.RequireJWTOnly / RequireHybrid / RequireStrict for per-route validation modes).
  5. For WebAuthn/FIDO2 second factor, implement WebAuthnCredentialProvider on your provider and enable Config.WebAuthn — see docs/webauthn.md.

See docs/api-reference.md for the full API surface.

Documentation

Overview

Package main demonstrates a minimal HTTP integration with goAuth.

It starts a local HTTP server on :8080 backed by miniredis (no external Redis required) and an in-memory user provider stub.

Endpoints:

POST /login     — JSON {"username":"...", "password":"...", "remember":true|false}
POST /refresh   — rotates tokens via the refresh-token cookie
POST /logout    — destroys the current session (by access token)
GET  /protected — middleware-guarded route (requires valid access token)

Run:

go run ./examples/http-minimal

Then:

# login (stores refresh cookie in cookie jar)
curl -i -c jar.txt -X POST localhost:8080/login \
  -H 'Content-Type: application/json' \
  -d '{"username":"alice@example.com","password":"correct-horse"}'

# call protected (uses access token from login response)
curl -i localhost:8080/protected -H "Authorization: Bearer <ACCESS_TOKEN>"

# refresh (uses cookie jar)
curl -i -b jar.txt -c jar.txt -X POST localhost:8080/refresh

# logout (invalidates session by access token, clears refresh cookie)
curl -i -b jar.txt -c jar.txt -X POST localhost:8080/logout \
  -H "Authorization: Bearer <ACCESS_TOKEN>"

Jump to

Keyboard shortcuts

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