auth

package
v1.25.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package auth provides JWT-based authentication and authorization middleware for vatel HTTP endpoints.

It is the recommended starting point for green-field projects. Wire it once globally (token decode) and per-endpoint (permission check):

cfg := &auth.Config{
    Decoder:    myJWTDecoder,
    Authorizer: myAuthorizer,
}

v := vatel.NewVatel()
v.AddMiddleware(vatel.OnAuthentication, cfg.Middleware())

v.Add(
    vatel.GET("/orders",      ordersCtrl, cfg.RequirePerms(map[string]uint{"orders:read": 0})),
    vatel.POST("/orders",     createCtrl, cfg.RequirePerms(map[string]uint{"orders:write": 1})),
    vatel.GET("/healthcheck", healthCtrl),  // public — no RequirePerms
)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authorizer

type Authorizer interface {
	IsAllowed(requestPerms []byte, endpointPerms ...uint) (bool, error)
}

Authorizer checks whether a token's permission bytes satisfy the required bit positions. This is the bitset-based permission model used by this package. Implement it via github.com/axkit/bitset or any compatible library.

type Config

type Config struct {
	// Decoder decodes the Authorization header value into a Tokener. It is
	// required when Middleware is used. It receives the raw header value:
	// nothing strips a "Bearer " prefix, so strip it in the decoder if your
	// clients send one. If Decoder is nil, a request carrying an
	// Authorization header is treated like one with a bad token: public
	// endpoints serve it as unauthenticated, RequireAuth and RequirePerms
	// reject it with vatel.ErrInternalError (500).
	Decoder vatel.TokenDecoder

	// Authorizer checks whether the token's permission bytes satisfy the
	// endpoint's required permissions. It is required by RequirePerms; a nil
	// Authorizer fails RequirePerms endpoints with vatel.ErrInternalError (500).
	Authorizer Authorizer

	// RevokeCheck optionally verifies that the token has not been revoked
	// (omit it for stateless JWTs). Like Decoder, it receives the raw
	// Authorization header value. If it returns an error, RequireAuth and
	// RequirePerms endpoints fail with vatel.ErrInternalError (500); public
	// endpoints serve the request as unauthenticated.
	RevokeCheck vatel.RevokeTokenChecker

	// Disabled skips all auth checks. Useful for tests and local development.
	// Set it before the server starts serving; it is read on every request
	// without synchronisation, so changing it while requests are served is a
	// data race.
	Disabled bool
}

Config holds the auth dependencies for HTTP endpoints.

func (*Config) Middleware

func (c *Config) Middleware() func(vatel.Context, vatel.EndpointMeta) error

Middleware returns a OnAuthentication middleware that:

  1. Reads the Authorization header.
  2. Optionally checks token revocation (if RevokeCheck is set).
  3. Decodes the token and stores the payload on ctx via SetTokenPayload.
  4. If the payload's Debug() is true, calls ctx.SetVerboseError(true) to enable detailed error responses for this request.

A bad token (invalid, expired, revoked) does not fail the request here: public endpoints ignore it and serve the request as unauthenticated. RequireAuth and RequirePerms reject the request with the token error (401). A failed revocation check or an unset Decoder is handled the same way, except that RequireAuth and RequirePerms reject with 500.

Endpoints that require authentication or specific permissions should add RequirePerms (or RequireAuth) as an additional EndpointOption.

func (*Config) RequireAuth

func (c *Config) RequireAuth() vatel.EndpointOption

RequireAuth returns an EndpointOption that adds an OnAuthorization middleware rejecting requests that carry no token payload. Use this for endpoints that require any valid authenticated user but no specific permission.

func (*Config) RequirePerms

func (c *Config) RequirePerms(perms map[string]uint) vatel.EndpointOption

RequirePerms returns an EndpointOption that adds an OnAuthorization middleware verifying the token holds all of the named permissions.

perms maps each permission name to its bit position in the token's permission bytes. Bit positions are passed directly to Authorizer.IsAllowed; names are used for error reporting and are appended, sorted, to the endpoint's Perms, so middleware sees them in EndpointMeta.Perms.

Config.Disabled is checked on every request, so it may be set after the option is created, but before the server starts serving (see Config.Disabled). A nil Config.Authorizer fails the request with vatel.ErrInternalError (500).

Jump to

Keyboard shortcuts

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