auth

package
v1.9.3 Latest Latest
Warning

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

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

Documentation

Overview

Package auth implements JWT authentication and API key management for Engram Cloud.

It handles user registration, login, token generation/validation, and API key creation with bcrypt password hashing and HMAC-SHA256 JWTs.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidCredentials is returned for failed login attempts. The message
	// is deliberately generic to avoid leaking whether the username exists.
	ErrInvalidCredentials = errors.New("invalid credentials")

	// ErrWeakPassword is returned when a password is shorter than 8 characters.
	ErrWeakPassword = errors.New("password must be at least 8 characters")

	// ErrSecretTooShort is returned when the JWT secret is shorter than 32 bytes.
	ErrSecretTooShort = errors.New("jwt secret must be at least 32 bytes")

	// ErrInvalidToken is returned for any token validation failure.
	ErrInvalidToken = errors.New("invalid token")

	// ErrTokenExpired is returned when a token has expired.
	ErrTokenExpired = errors.New("token has expired")

	// ErrWrongTokenType is returned when a token's type claim doesn't match expectations.
	ErrWrongTokenType = errors.New("wrong token type")
)

Functions

func GenerateAPIKey

func GenerateAPIKey() (plainKey, hash string, err error)

GenerateAPIKey creates a new API key with the eng_ prefix and returns both the plain key (for display to the user) and its SHA-256 hash (for storage). Uses crypto/rand for cryptographically secure random bytes.

func ValidateAPIKey

func ValidateAPIKey(store *cloudstore.CloudStore, key string) (*cloudstore.CloudUser, error)

ValidateAPIKey validates an API key by hashing it with SHA-256 and looking up the user by api_key_hash in the store.

Types

type AuthResult

type AuthResult struct {
	UserID       string `json:"user_id"`
	Username     string `json:"username"`
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	ExpiresIn    int    `json:"expires_in"` // seconds until access token expires
}

AuthResult is returned by Register and Login on success.

type Claims

type Claims struct {
	UserID   string `json:"user_id"`
	Username string `json:"username"`
	Type     string `json:"type"` // "access" or "refresh"
	jwt.RegisteredClaims
}

Claims represents the custom JWT claims for Engram Cloud tokens.

type Service

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

Service handles JWT authentication and user registration/login for Engram Cloud.

func NewService

func NewService(store *cloudstore.CloudStore, jwtSecret string) (*Service, error)

NewService creates a new auth Service. The jwtSecret MUST be at least 32 bytes.

func (*Service) GenerateTokenPair

func (s *Service) GenerateTokenPair(userID, username string) (accessToken, refreshToken string, err error)

GenerateTokenPair creates an access token (1h) and refresh token (7d) for the given user. Both are signed with HMAC-SHA256.

func (*Service) Login

func (s *Service) Login(identifier, password string) (*AuthResult, error)

Login authenticates a user by username or email and password, returning JWT tokens. On any failure (wrong password, nonexistent user), it returns ErrInvalidCredentials with no information about which part failed. Uses bcrypt.CompareHashAndPassword which is inherently constant-time.

func (*Service) RefreshAccessToken

func (s *Service) RefreshAccessToken(refreshTokenStr string) (newAccessToken string, err error)

RefreshAccessToken validates a refresh token and issues a new access token. It verifies that the token type is "refresh", then generates a fresh access token for the same user.

func (*Service) Register

func (s *Service) Register(username, email, password string) (*AuthResult, error)

Register creates a new user and returns an AuthResult with JWT tokens. Password must be at least 8 characters.

func (*Service) ValidateAccessToken

func (s *Service) ValidateAccessToken(tokenStr string) (*Claims, error)

ValidateAccessToken parses and validates an access token string. It checks the HMAC-SHA256 signature, expiry, and verifies that the token type is "access".

Jump to

Keyboard shortcuts

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