This repository was archived by the owner on May 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
187 lines (154 loc) · 4.59 KB
/
auth.go
File metadata and controls
187 lines (154 loc) · 4.59 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"errors"
"strings"
"sync"
"time"
"github.com/google/uuid"
)
// Challenge represents an authentication challenge
type Challenge struct {
Token uuid.UUID // Random challenge token
Address string // Address this challenge was created for
CreatedAt time.Time // When the challenge was created
ExpiresAt time.Time // When the challenge expires
Completed bool // Whether the challenge has been used
}
// AuthManager handles authentication challenges
type AuthManager struct {
challenges map[uuid.UUID]*Challenge // Challenge token -> Challenge
challengesMu sync.RWMutex
challengeTTL time.Duration
maxChallenges int
cleanupTicker *time.Ticker
authSessions map[string]time.Time // Address -> last active time
authSessionsMu sync.RWMutex
sessionTTL time.Duration
}
// NewAuthManager creates a new authentication manager
func NewAuthManager() *AuthManager {
am := &AuthManager{
challenges: make(map[uuid.UUID]*Challenge),
challengeTTL: 5 * time.Minute,
maxChallenges: 1000, // Prevent DoS
cleanupTicker: time.NewTicker(10 * time.Minute),
authSessions: make(map[string]time.Time),
sessionTTL: 24 * time.Hour,
}
// Start background cleanup
go am.cleanupExpiredChallenges()
return am
}
// GenerateChallenge creates a new challenge for a specific address
func (am *AuthManager) GenerateChallenge(address string) (uuid.UUID, error) {
// Normalize address
if !strings.HasPrefix(address, "0x") {
address = "0x" + address
}
// Create challenge with expiration
now := time.Now()
challenge := &Challenge{
Token: uuid.New(),
Address: address,
CreatedAt: now,
ExpiresAt: now.Add(am.challengeTTL),
Completed: false,
}
// Store challenge
am.challengesMu.Lock()
defer am.challengesMu.Unlock()
// Enforce max challenge limit (basic DoS protection)
if len(am.challenges) >= am.maxChallenges {
return uuid.UUID{}, errors.New("too many pending challenges")
}
am.challenges[challenge.Token] = challenge
return challenge.Token, nil
}
// ValidateChallenge validates a challenge response
func (am *AuthManager) ValidateChallenge(challengeToken uuid.UUID, address string) error {
// Normalize address
if !strings.HasPrefix(address, "0x") {
address = "0x" + address
}
// Get the challenge
am.challengesMu.Lock()
defer am.challengesMu.Unlock()
challenge, exists := am.challenges[challengeToken]
if !exists {
return errors.New("challenge not found")
}
// Verify the challenge was created for this address
if challenge.Address != address {
return errors.New("challenge was not created for this address")
}
// Check if challenge is expired
if time.Now().After(challenge.ExpiresAt) {
delete(am.challenges, challengeToken)
return errors.New("challenge expired")
}
// Check if challenge is already used
if challenge.Completed {
delete(am.challenges, challengeToken)
return errors.New("challenge already used")
}
// Mark challenge as completed
challenge.Completed = true
// Clean up
challenge.ExpiresAt = time.Now().Add(30 * time.Second) // Keep briefly for reference
// Register authenticated session
am.registerAuthSession(address)
return nil
}
// RegisterAuthSession registers an authenticated session
func (am *AuthManager) registerAuthSession(address string) {
am.authSessionsMu.Lock()
defer am.authSessionsMu.Unlock()
am.authSessions[address] = time.Now()
}
// ValidateSession checks if a session is valid
func (am *AuthManager) ValidateSession(address string) bool {
am.authSessionsMu.RLock()
defer am.authSessionsMu.RUnlock()
lastActive, exists := am.authSessions[address]
if !exists {
return false
}
// Check if session has expired
if time.Now().After(lastActive.Add(am.sessionTTL)) {
return false
}
return true
}
// UpdateSession updates the last active time for a session
func (am *AuthManager) UpdateSession(address string) bool {
am.authSessionsMu.Lock()
defer am.authSessionsMu.Unlock()
_, exists := am.authSessions[address]
if !exists {
return false
}
am.authSessions[address] = time.Now()
return true
}
// CleanupExpiredChallenges periodically removes expired challenges
func (am *AuthManager) cleanupExpiredChallenges() {
for range am.cleanupTicker.C {
now := time.Now()
// Cleanup challenges
am.challengesMu.Lock()
for token, challenge := range am.challenges {
if now.After(challenge.ExpiresAt) {
delete(am.challenges, token)
}
}
am.challengesMu.Unlock()
// Cleanup sessions
am.authSessionsMu.Lock()
for addr, lastActive := range am.authSessions {
if now.After(lastActive.Add(am.sessionTTL)) {
delete(am.authSessions, addr)
}
}
am.authSessionsMu.Unlock()
}
}