local

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package local provides local authentication for offline operation.

This package implements a fallback authentication system that works without network connectivity. It uses:

  • SQLite for local credential storage
  • Bcrypt for secure password hashing (12 rounds)
  • Session management for local tokens
  • JWT-compatible token generation for offline use

The local auth system is Tier 3 in the authentication hierarchy:

  • Tier 1: Local JWT validation (fast)
  • Tier 2: API token validation (network-dependent)
  • Tier 3: Local authentication fallback (offline)

Features:

  • Register new local users
  • Authenticate with username/password
  • Generate local access/refresh tokens
  • Session management
  • Password validation with bcrypt

Example usage:

import "github.com/AINative-studio/ainative-code/internal/auth/local"

// Create local auth store
store, err := local.NewStore("path/to/db.sqlite")
if err != nil {
    log.Fatal(err)
}

// Register user
err = store.Register("user@example.com", "secure-password")
if err != nil {
    log.Fatal(err)
}

// Authenticate
session, err := store.Authenticate("user@example.com", "secure-password")
if err != nil {
    log.Fatal(err)
}

// Use session tokens
fmt.Printf("Access Token: %s\n", session.AccessToken)

Index

Constants

View Source
const (
	// BcryptCost is the cost factor for bcrypt hashing (12 rounds)
	BcryptCost = 12

	// LocalTokenDuration is the lifetime of local access tokens
	LocalTokenDuration = 24 * time.Hour

	// LocalRefreshDuration is the lifetime of local refresh tokens
	LocalRefreshDuration = 7 * 24 * time.Hour
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Session

type Session struct {
	ID           int64
	UserID       int64
	AccessToken  string
	RefreshToken string
	ExpiresAt    time.Time
	CreatedAt    time.Time
}

Session represents an authentication session.

func (*Session) ToTokenPair

func (s *Session) ToTokenPair() *jwt.TokenPair

ToTokenPair converts a session to a JWT token pair format.

type Store

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

Store manages local authentication and credentials.

func NewStore

func NewStore(dbPath string) (*Store, error)

NewStore creates a new local auth store with SQLite backend.

func (*Store) Authenticate

func (s *Store) Authenticate(email, password string) (*Session, error)

Authenticate validates credentials and creates a session.

func (*Store) Close

func (s *Store) Close() error

Close closes the database connection.

func (*Store) DeleteAllSessions

func (s *Store) DeleteAllSessions(userID int64) error

DeleteAllSessions deletes all sessions for a user.

func (*Store) DeleteSession

func (s *Store) DeleteSession(accessToken string) error

DeleteSession deletes a session by access token.

func (*Store) GetUser

func (s *Store) GetUser(userID int64) (*User, error)

GetUser returns a user by ID.

func (*Store) RefreshSession

func (s *Store) RefreshSession(refreshToken string) (*Session, error)

RefreshSession creates a new session using a refresh token.

func (*Store) Register

func (s *Store) Register(email, password string) error

Register creates a new local user with hashed password.

func (*Store) ValidateToken

func (s *Store) ValidateToken(accessToken string) (int64, error)

ValidateToken validates an access token and returns the user ID.

type User

type User struct {
	ID           int64
	Email        string
	PasswordHash string
	CreatedAt    time.Time
	UpdatedAt    time.Time
}

User represents a local user account.

Jump to

Keyboard shortcuts

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