summaryrefslogtreecommitdiff
path: root/types/types.go
blob: 79059d43fe254c29fff4130439e20ed808389f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package types

import (
	"database/sql"
	"sync"
	"time"
)

// PathConfig holds the configurable paths for the application routes and assets.
type PathConfig struct {
	// Paths
	Login           string
	Register        string
	Logout          string
	Account         string
	AccountPassword string
	Admin           string
	AdminUsersAPI   string
	Assets          string
	FrontPath       string
	APIPath         string

	// Booleans
	ProtectFrontend bool
	ProtectAPI      bool
	RegisterEnabled bool

	// Security Policies
	MaxLoginAttempts       int
	LockoutDurationMinutes int
	RateLimitMaxRequests   int
	RateLimitWindowSeconds int
	PasswordPolicy         string

	// Token Durations
	TokenDurationHours        int
	AccessTokenDurationMinutes int
	RefreshTokenDurationDays  int
}

// ContextKey is a custom type for context keys to avoid collisions.
type ContextKey string

// SessionDataContextKey is the key for storing session data in the request context.
const SessionDataContextKey = ContextKey("sessionData")

// User represents a user in the database
type User struct {
	ID           int64        `json:"id"`
	Username     string       `json:"username"`
	PasswordHash string       `json:"-"`
	Role         string       `json:"role"`
	IsActive     bool         `json:"is_active"`
	FailedLogins int          `json:"failed_logins"`
	LastLoginAt  sql.NullTime `json:"last_login_at"`
	CreatedAt    time.Time    `json:"created_at"`
	UpdatedAt    time.Time    `json:"updated_at"`
}

// AuthToken represents a token in the database
type AuthToken struct {
	ID        int64
	UserID    int64
	Token     string
	ExpiresAt time.Time
}

// RefreshToken represents a refresh token in the database
type RefreshToken struct {
	ID        int64
	UserID    int64
	Token     string
	ExpiresAt time.Time
	IsRevoked bool
}

// JWTPayload defines the claims for the access token.
type JWTPayload struct {
	UserID int64  `json:"user_id"`
	Role   string `json:"role"`
	Exp    int64  `json:"exp"`
}

// RateLimiter represents a user-specific rate limiter.
type RateLimiter struct {
	Requests map[string][]int64
	Mutex    sync.Mutex
	Max      int
	Window   time.Duration
}

// WorkerPool manages a pool of goroutines to execute tasks concurrently.
type WorkerPool struct {
	Workers    int
	TaskQueue  chan func()
	ResultChan chan interface{}
	Wg         sync.WaitGroup
	Active     int64
}