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
- type Session
- type Store
- func (s *Store) Authenticate(email, password string) (*Session, error)
- func (s *Store) Close() error
- func (s *Store) DeleteAllSessions(userID int64) error
- func (s *Store) DeleteSession(accessToken string) error
- func (s *Store) GetUser(userID int64) (*User, error)
- func (s *Store) RefreshSession(refreshToken string) (*Session, error)
- func (s *Store) Register(email, password string) error
- func (s *Store) ValidateToken(accessToken string) (int64, error)
- type User
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 ¶
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 (*Store) Authenticate ¶
Authenticate validates credentials and creates a session.
func (*Store) DeleteAllSessions ¶
DeleteAllSessions deletes all sessions for a user.
func (*Store) DeleteSession ¶
DeleteSession deletes a session by access token.
func (*Store) RefreshSession ¶
RefreshSession creates a new session using a refresh token.
Click to show internal directories.
Click to hide internal directories.