Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
305 lines (248 loc) · 8.62 KB
/
Copy pathmain.go
File metadata and controls
305 lines (248 loc) · 8.62 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"fmt"
"log"
"strings"
"sync"
"github.com/sixcolors/argon2id"
"golang.org/x/crypto/bcrypt"
)
// User represents a user in our system
type User struct {
Email string
Password string // This would be the hashed password in a real system
ID int
}
// MigrationUserStore demonstrates automatic migration from bcrypt to argon2id
type MigrationUserStore struct {
users map[string]*User
nextID int
mu sync.RWMutex
}
func NewMigrationUserStore() *MigrationUserStore {
return &MigrationUserStore{
users: make(map[string]*User),
nextID: 1,
}
}
// isBcryptHash checks if a hash is in bcrypt format
func isBcryptHash(hash string) bool {
return strings.HasPrefix(hash, "$2a$") || strings.HasPrefix(hash, "$2b$") || strings.HasPrefix(hash, "$2y$")
}
// CreateUserWithBcrypt creates a user with a bcrypt hash (simulating legacy users)
func (s *MigrationUserStore) CreateUserWithBcrypt(email, password string) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, exists := s.users[email]; exists {
return fmt.Errorf("user already exists")
}
// Hash password with bcrypt (simulating legacy system)
bcryptHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("failed to hash password: %w", err)
}
s.users[email] = &User{
Email: email,
Password: string(bcryptHash),
ID: s.nextID,
}
s.nextID++
log.Printf("✅ Created user with bcrypt: %s", email)
return nil
}
// CreateUserWithArgon2id creates a user with argon2id hash
func (s *MigrationUserStore) CreateUserWithArgon2id(email, password string) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, exists := s.users[email]; exists {
return fmt.Errorf("user already exists")
}
// Hash password with argon2id
argon2idHash, err := argon2id.GenerateFromPassword([]byte(password), nil)
if err != nil {
return fmt.Errorf("failed to hash password: %w", err)
}
s.users[email] = &User{
Email: email,
Password: string(argon2idHash),
ID: s.nextID,
}
s.nextID++
log.Printf("✅ Created user with argon2id: %s", email)
return nil
}
// Login authenticates a user and automatically migrates bcrypt hashes to argon2id
func (s *MigrationUserStore) Login(email, password string) (*User, error) {
s.mu.RLock()
user, exists := s.users[email]
s.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("user not found")
}
hash := user.Password
// Check hash type and verify password
if isBcryptHash(hash) {
// Legacy bcrypt hash - verify with bcrypt
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
if err != nil {
log.Printf("❌ Bcrypt login failed for %s", email)
return nil, fmt.Errorf("invalid credentials")
}
log.Printf("✅ Bcrypt login successful for %s - migrating to argon2id", email)
// Password is correct, migrate to argon2id
return s.migrateUserToArgon2id(user, password)
}
// Modern argon2id hash - verify with argon2id
err := argon2id.CompareHashAndPassword([]byte(hash), []byte(password))
if err != nil {
log.Printf("❌ Argon2id login failed for %s", email)
return nil, fmt.Errorf("invalid credentials")
}
log.Printf("✅ Argon2id login successful for %s", email)
// Check if we should upgrade parameters
return s.checkAndUpgradeHash(user, password)
}
// migrateUserToArgon2id migrates a user from bcrypt to argon2id
func (s *MigrationUserStore) migrateUserToArgon2id(user *User, password string) (*User, error) {
// Generate new argon2id hash
newHash, err := argon2id.GenerateFromPassword([]byte(password), nil)
if err != nil {
return nil, fmt.Errorf("failed to migrate hash: %w", err)
}
// Update user in store with proper synchronization
s.mu.Lock()
defer s.mu.Unlock()
// Double-check the hash hasn't changed (prevent double migration)
if !isBcryptHash(user.Password) {
log.Printf("⚠️ User %s already migrated, skipping", user.Email)
return user, nil
}
user.Password = string(newHash)
log.Printf("🔄 Migrated %s from bcrypt to argon2id", user.Email)
return user, nil
}
// checkAndUpgradeHash checks if the current argon2id hash needs parameter upgrade
func (s *MigrationUserStore) checkAndUpgradeHash(user *User, password string) (*User, error) {
// Define stronger parameters for future upgrades
strongerParams := &argon2id.Params{
Time: 6, // More iterations than default (3)
Memory: 128 * 1024, // More memory than default (64*1024)
Threads: 4, // More threads than default (2)
KeyLen: 32,
}
// Check if rehash is needed
needsRehash, err := argon2id.NeedsRehash([]byte(user.Password), strongerParams)
if err != nil {
// If we can't check, just return the user (don't fail login)
log.Printf("⚠️ Could not check rehash for %s: %v", user.Email, err)
return user, nil
}
if needsRehash {
log.Printf("🔄 Upgrading hash parameters for %s", user.Email)
// Generate new hash with stronger parameters
newHash, err := argon2id.GenerateFromPassword([]byte(password), strongerParams)
if err != nil {
// If upgrade fails, just return the user (don't fail login)
log.Printf("⚠️ Could not upgrade hash for %s: %v", user.Email, err)
return user, nil
}
// Update user in store with proper synchronization
s.mu.Lock()
defer s.mu.Unlock()
// Double-check the hash still needs upgrading (prevent race conditions)
currentNeedsRehash, err := argon2id.NeedsRehash([]byte(user.Password), strongerParams)
if err != nil || !currentNeedsRehash {
log.Printf("⚠️ Hash for %s already upgraded or changed, skipping", user.Email)
return user, nil
}
user.Password = string(newHash)
log.Printf("✅ Upgraded hash parameters for %s", user.Email)
}
return user, nil
}
// GetUser returns a user (for inspection)
func (s *MigrationUserStore) GetUser(email string) (*User, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
user, exists := s.users[email]
return user, exists
}
// createUsers creates test users with different hash types
func createUsers(store *MigrationUserStore) {
fmt.Println("1. Creating users with different hash types:")
// Legacy user with bcrypt
err := store.CreateUserWithBcrypt("legacy@example.com", "password123")
if err != nil {
log.Fatal("Failed to create legacy user:", err)
}
// Modern user with argon2id
err = store.CreateUserWithArgon2id("modern@example.com", "password123")
if err != nil {
log.Fatal("Failed to create modern user:", err)
}
fmt.Println()
}
// demonstrateLogin demonstrates login and automatic migration
func demonstrateLogin(store *MigrationUserStore) {
fmt.Println("2. Logging in users (triggers automatic migration):")
// Login legacy user (should migrate from bcrypt to argon2id)
fmt.Println(" Logging in legacy user...")
user1, err := store.Login("legacy@example.com", "password123")
if err != nil {
log.Fatal("Login failed:", err)
}
fmt.Printf(" ✅ Legacy user logged in: %s (ID: %d)\n", user1.Email, user1.ID)
// Check if hash was migrated
if user, exists := store.GetUser("legacy@example.com"); exists {
if !isBcryptHash(user.Password) {
fmt.Println(" 🔄 Hash successfully migrated to argon2id!")
}
}
// Login modern user (should check for parameter upgrades)
fmt.Println(" Logging in modern user...")
user2, err := store.Login("modern@example.com", "password123")
if err != nil {
log.Fatal("Login failed:", err)
}
fmt.Printf(" ✅ Modern user logged in: %s (ID: %d)\n", user2.Email, user2.ID)
fmt.Println()
}
// testFailedLogin demonstrates failed login handling
func testFailedLogin(store *MigrationUserStore) {
fmt.Println("3. Testing failed login:")
_, err := store.Login("legacy@example.com", "wrongpassword")
if err != nil {
fmt.Println(" ✅ Correctly rejected wrong password")
}
fmt.Println()
}
// showHashInfo displays information about stored hashes
func showHashInfo(store *MigrationUserStore) {
fmt.Println("4. Hash information:")
store.mu.RLock()
for email, user := range store.users {
hashType := "argon2id"
if isBcryptHash(user.Password) {
hashType = "bcrypt"
}
fmt.Printf(" %s: %s hash\n", email, hashType)
}
store.mu.RUnlock()
fmt.Println()
fmt.Println("=== Migration Example Complete ===")
fmt.Println("This example shows how to:")
fmt.Println("• Detect bcrypt vs argon2id hashes")
fmt.Println("• Verify passwords with the appropriate algorithm")
fmt.Println("• Automatically migrate users during login")
fmt.Println("• Upgrade hash parameters for existing argon2id users")
}
func main() {
fmt.Println("=== Argon2ID Migration Example ===")
fmt.Println("Demonstrating automatic migration from bcrypt to argon2id")
fmt.Println()
store := NewMigrationUserStore()
createUsers(store)
demonstrateLogin(store)
testFailedLogin(store)
showHashInfo(store)
}