auth

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package auth provides session-based and JWT authentication for OniWorks. Session auth is the default. JWT is opt-in for API-only applications.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCredentials = errors.New("auth: invalid email or password")
	ErrInvalidToken       = errors.New("auth: invalid or expired token")
	ErrUnauthenticated    = errors.New("auth: unauthenticated")
	// ErrJWTNotConfigured is returned when a JWT operation is attempted but no
	// (or too short a) signing secret was provided to NewGuard. An empty secret
	// would let anyone forge tokens, so JWT operations fail closed.
	ErrJWTNotConfigured = errors.New("auth: JWT secret not configured (must be at least 32 bytes)")
)

Functions

func CheckPassword

func CheckPassword(hash, password string) bool

CheckPassword verifies a plaintext password against a bcrypt hash.

func HashPassword

func HashPassword(password string, cost ...int) (string, error)

HashPassword bcrypt-hashes a plaintext password. An optional cost may be supplied (bcrypt.MinCost..bcrypt.MaxCost); it defaults to bcrypt.DefaultCost.

auth.HashPassword("s3cret")      // default cost
auth.HashPassword("s3cret", 12)  // explicit cost

Types

type Claims

type Claims struct {
	UserID int64  `json:"uid"`
	Email  string `json:"email"`
	jwt.RegisteredClaims
}

Claims is the JWT payload.

type Guard

type Guard struct {
	// contains filtered or unexported fields
}

Guard manages authentication state for the current request.

func NewGuard

func NewGuard(provider UserProvider, sessions *session.Manager, jwtSecret string) *Guard

NewGuard creates a Guard.

func (*Guard) Attempt

func (g *Guard) Attempt(ctx context.Context, email, password string, sess *session.Session) (User, error)

Attempt verifies credentials and creates an authenticated session on success.

On success the session ID is rotated (Regenerate) to defeat session fixation. When the email is unknown a dummy bcrypt comparison is still performed so the not-found path takes the same time as a wrong-password path, preventing account enumeration via response timing.

func (*Guard) Check

func (g *Guard) Check(ctx context.Context, sess *session.Session) bool

Check reports whether the session has an authenticated user.

func (*Guard) IssueToken

func (g *Guard) IssueToken(user User, ttl time.Duration) (string, error)

IssueToken creates a signed JWT for the given user.

func (*Guard) Logout

func (g *Guard) Logout(ctx context.Context, sess *session.Session) error

Logout removes the user from the session and rotates the session ID, so the pre-logout session ID (which may have leaked, e.g. in logs or a stolen cookie) can no longer be replayed. This mirrors what Attempt does on login.

func (*Guard) ParseToken

func (g *Guard) ParseToken(tokenStr string) (*Claims, error)

ParseToken validates a JWT and returns the embedded Claims.

It fails closed if the signing secret is unconfigured/too short, restricts the accepted algorithm to HS256 (blocking algorithm-confusion and "alg:none" attacks), and requires the token to carry an expiry so non-expiring tokens are never accepted.

func (*Guard) UserFromSession

func (g *Guard) UserFromSession(ctx context.Context, sess *session.Session) (User, error)

UserFromSession retrieves the authenticated user from the session. Returns nil (and no error) if the session has no authenticated user.

func (*Guard) UserFromToken

func (g *Guard) UserFromToken(ctx context.Context, tokenStr string) (User, error)

UserFromToken resolves the user from a parsed JWT token string.

type User

type User interface {
	GetID() int64
	GetEmail() string
	GetPassword() string // bcrypt hash
}

User is the interface your application's User model must implement for auth to work.

type UserProvider

type UserProvider interface {
	FindByID(ctx context.Context, id int64) (User, error)
	FindByEmail(ctx context.Context, email string) (User, error)
}

UserProvider retrieves users from a data source.

Jump to

Keyboard shortcuts

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