auth

command
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 9 Imported by: 0

README

Auth

JWT-like token authentication using HMAC-SHA256 with no external dependencies. Includes login, auth middleware, and protected route groups.

Run

go run -tags kruda_stdjson ./examples/auth/

Test

# Public endpoint
curl http://localhost:3000/

# Login (get token)
curl -X POST http://localhost:3000/login \
  -d '{"username":"admin","password":"secret"}'

# Access protected route with token
curl http://localhost:3000/protected \
  -H "Authorization: Bearer <token>"

# Get profile
curl http://localhost:3000/protected/me \
  -H "Authorization: Bearer <token>"

Documentation

Overview

Example: Auth — JWT-like Authentication with HMAC-SHA256

Demonstrates a token-based authentication pattern using ONLY stdlib crypto packages (no external JWT library):

  • HMAC-SHA256 token generation and validation
  • Login endpoint that issues tokens
  • Auth middleware that validates tokens on protected routes
  • Token payload with expiration

This is a simplified JWT-like pattern. For production, consider a full JWT library or Kruda's future contrib/jwt package.

Run: go run -tags kruda_stdjson ./examples/auth/ Test:

curl http://localhost:3000/                                          → public
curl -X POST http://localhost:3000/login -d '{"username":"admin","password":"secret"}'
curl http://localhost:3000/protected -H "Authorization: Bearer <token>"

Jump to

Keyboard shortcuts

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