auth

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package auth provides signed access and rotating refresh-token primitives.

Index

Constants

View Source
const APITokenPrefix = "gk_"

APITokenPrefix define package-level implementation state.

View Source
const MinimumSecretLength = 32

MinimumSecretLength is the smallest accepted signing secret in bytes.

Variables

This section is empty.

Functions

func Can

func Can(c *gin.Context, a string) bool

Can performs this package operation.

func HashAPIToken

func HashAPIToken(token string) string

HashAPIToken performs this package operation.

func HashRefreshToken

func HashRefreshToken(token string) string

HashRefreshToken performs this package operation.

func Issue

func Issue(userID string, secret []byte, ttl time.Duration) (string, error)

Issue signs a standalone token without constructing a Manager.

func LoginSession

func LoginSession(c *gin.Context, userID string) error

LoginSession records the authenticated user in the encrypted session.

func LogoutSession

func LogoutSession(c *gin.Context) error

LogoutSession removes the authenticated user from the encrypted session.

func NewRefreshToken

func NewRefreshToken() (plain, hash string, err error)

NewRefreshToken returns a random plaintext refresh token and its SHA-256 hash.

func RequireAuth

func RequireAuth(manager *Manager) gin.HandlerFunc

RequireAuth authenticates requests with a Bearer token, stores the verified claims in the request context, and rejects unauthenticated requests with a canonical 401 envelope. It panics when manager is nil so a wiring mistake fails at route registration, not per request.

func RequireLogin

func RequireLogin(manager *Manager) gin.HandlerFunc

RequireLogin authenticates requests with an OAuth browser session when one is present, otherwise it requires the same Bearer token accepted by RequireAuth. Install SessionMiddleware after the session middleware.

func RequireToken

func RequireToken(store TokenStore, abilities ...string) gin.HandlerFunc

RequireToken performs this package operation.

func SessionMiddleware

func SessionMiddleware() gin.HandlerFunc

SessionMiddleware restores an OAuth-authenticated user ID from the encrypted session cookie. It must run after session.Middleware and before RequireLogin.

func UserID

func UserID(c *gin.Context) string

UserID returns the authenticated user ID, or "" when unauthenticated.

func ValidateSecret

func ValidateSecret(secret []byte) error

ValidateSecret rejects secrets shorter than MinimumSecretLength.

Types

type APIToken

type APIToken struct {
	// ID store data used by this type.
	ID string
	// UserID store data used by this type.
	UserID string
	// Name store data used by this type.
	Name string
	// TokenHash store data used by this type.
	TokenHash string
	// Abilities store data used by this type.
	Abilities []string
	// ExpiresAt store data used by this type.
	ExpiresAt *time.Time
	// LastUsedAt store data used by this type.
	LastUsedAt *time.Time
	// RevokedAt store data used by this type.
	RevokedAt *time.Time
}

APIToken defines an implementation type used by this package.

func NewAPIToken

func NewAPIToken(userID, name string, abilities []string, expiresIn *int64) (APIToken, string, error)

NewAPIToken performs this package operation.

func TokenFromContext

func TokenFromContext(c *gin.Context) (*APIToken, bool)

TokenFromContext performs this package operation.

type Claims

type Claims struct {
	// UserID is the authenticated account identifier.
	UserID string `json:"sub"`
	jwt.RegisteredClaims
}

Claims is the verified subject carried by a signed access token.

func ClaimsFromContext

func ClaimsFromContext(c *gin.Context) (*Claims, bool)

ClaimsFromContext returns the claims stored by RequireAuth.

func Parse

func Parse(token string, secret []byte) (*Claims, error)

Parse verifies a standalone token without constructing a Manager.

type Manager

type Manager struct {

	// Issuer is embedded in every token when non-empty.
	Issuer string
	// AccessTTL is the default lifetime used by Issue.
	AccessTTL time.Duration
	// contains filtered or unexported fields
}

Manager signs and verifies access tokens with one immutable secret.

func New

func New(secret []byte, issuer string, accessTTL time.Duration) (*Manager, error)

New validates secret and returns a manager for access-token operations.

func (*Manager) Issue

func (m *Manager) Issue(userID string, ttl ...time.Duration) (string, error)

Issue signs an access token for userID using the supplied or default TTL.

func (*Manager) Parse

func (m *Manager) Parse(tokenString string) (*Claims, error)

Parse verifies a signed access token and returns its subject claims.

type TokenStore

type TokenStore interface {
	// FindByTokenHash define an operation required by this interface.
	FindByTokenHash(context.Context, string) (*APIToken, error)
	// TouchLastUsed define an operation required by this interface.
	TouchLastUsed(context.Context, string, time.Time) error
}

TokenStore defines an implementation type used by this package.

Jump to

Keyboard shortcuts

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