auth

package
v0.1.2 Latest Latest
Warning

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

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

Documentation

Overview

Package auth provides JWT generation/validation and role definitions for okpos.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIncorrectCredentials = errors.New("auth: incorrect credentials")
	ErrTokenExpired         = errors.New("auth: token expired")
	ErrInvalidToken         = errors.New("auth: invalid token")
	ErrWrongTokenType       = errors.New("auth: wrong token type")
)

Functions

func ComparePasswords

func ComparePasswords(password, hashed string) error

ComparePasswords returns nil if the plaintext password matches the hash.

func HashPassword

func HashPassword(password string, options ...hashOption) (string, error)

HashPassword hashes a plaintext password using bcrypt.

func WithMinCost

func WithMinCost() hashOption

WithMinCost sets bcrypt cost to the minimum (4). Use in tests and seed scripts to avoid slow hashing at high iteration counts.

Types

type Auth

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

Auth holds the signing secret and token expiry used for all JWT operations.

func New

func New(secret string, expiryHours int) *Auth

New creates an Auth instance. expiryHours controls access token lifetime.

func (*Auth) GenerateFileAccessJWT

func (a *Auth) GenerateFileAccessJWT(
	fileID, companyID uuid.UUID,
	ttl time.Duration,
) (string, error)

GenerateFileAccessJWT issues a short-lived capability token scoped to a single file, used to build presigned download URLs for the local filesystem storage driver. It carries no user identity — CompanyID is included only so the download handler can reuse the same company-scoped repo lookup every other authenticated path uses.

func (*Auth) GenerateJWTPair

func (a *Auth) GenerateJWTPair(payload *JWTPayload) (*JWTTokens, error)

GenerateJWTPair issues a new access + refresh token pair for the given payload.

func (*Auth) ValidateJWT

func (a *Auth) ValidateJWT(signedToken string, purposes ...string) (*JWTClaims, error)

ValidateJWT parses and validates a signed token. Optionally checks its subject.

type JWTClaims

type JWTClaims struct {
	UserID    uuid.UUID  `json:"user_id"`
	Name      string     `json:"name"`
	Email     string     `json:"email"`
	Role      UserRole   `json:"role,omitempty"`
	CompanyID *uuid.UUID `json:"company_id,omitempty"` // nil for owners
	// FileID is set only on tokens with Subject == FileAccessTokenSubject —
	// the single file this capability token grants download access to.
	FileID *uuid.UUID `json:"file_id,omitempty"`
	jwt.RegisteredClaims
}

JWTClaims are the claims embedded in every okpos JWT.

type JWTPayload

type JWTPayload struct {
	ID        uuid.UUID
	Name      string
	Email     string
	AuthID    uuid.UUID
	Role      UserRole
	CompanyID *uuid.UUID // nil for owners
}

JWTPayload is the input for generating JWT tokens.

type JWTTokens

type JWTTokens struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
}

JWTTokens holds an access/refresh token pair returned after login or refresh.

type TokenSubject

type TokenSubject string

TokenSubject identifies the purpose of a JWT.

const (
	AccessTokenSubject  TokenSubject = "okpos-access"
	RefreshTokenSubject TokenSubject = "okpos-refresh"
	// FileAccessTokenSubject identifies a short-lived, single-file capability
	// token used by the local filesystem storage driver's presigned download
	// URLs. Unlike access/refresh tokens it carries no user identity — anyone
	// holding the token can download the referenced file until it expires.
	FileAccessTokenSubject TokenSubject = "okpos-file"
)

type UserRole

type UserRole string

UserRole is the role assigned to a system user.

const (
	CashierRole UserRole = "cashier" // int16: 0
	ManagerRole UserRole = "manager" // int16: 1
	OwnerRole   UserRole = "owner"   // int16: 2
)

func IntToUserRole

func IntToUserRole(r int16) UserRole

IntToUserRole converts the stored int16 to a UserRole.

func (UserRole) AsInt16

func (r UserRole) AsInt16() int16

AsInt16 converts a UserRole to its database representation.

func (UserRole) AtLeast

func (r UserRole) AtLeast(minRole UserRole) bool

AtLeast returns true if this role is at or above the given minimum role.

Jump to

Keyboard shortcuts

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