-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
589 lines (496 loc) · 16 KB
/
auth.go
File metadata and controls
589 lines (496 loc) · 16 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package server
import (
"bytes"
"context"
"crypto/rand"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/golang-jwt/jwt/v5"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/bcrypt"
)
type User struct {
ID int `json:"id"`
Email string `json:"email"`
Password string `json:"-"` // Never include in JSON
IsAdmin bool `json:"is_admin"`
SetupToken string `json:"-"` // Never include in JSON
SetupTokenExpiry time.Time `json:"-"` // Never include in JSON
IsSetup bool `json:"is_setup"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type AuthClaims struct {
UserID int `json:"user_id"`
Email string `json:"email"`
IsAdmin bool `json:"is_admin"`
jwt.RegisteredClaims
}
type AuthManager struct {
db *sql.DB
jwtSecret []byte
}
func NewAuthManager(dbPath string) (*AuthManager, error) {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}
am := &AuthManager{
db: db,
jwtSecret: []byte("your-secret-key-change-this-in-production"), // TODO: Use env var
}
if err := am.createTables(); err != nil {
return nil, fmt.Errorf("failed to create tables: %w", err)
}
// Create default admin user if no users exist
if err := am.createDefaultAdmin(); err != nil {
log.Printf("Warning: Failed to create default admin: %v", err)
}
return am, nil
}
func (am *AuthManager) createTables() error {
query := `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
password TEXT,
is_admin BOOLEAN DEFAULT FALSE,
setup_token TEXT,
setup_token_expiry DATETIME,
is_setup BOOLEAN DEFAULT FALSE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TRIGGER IF NOT EXISTS update_users_updated_at
AFTER UPDATE ON users
FOR EACH ROW
BEGIN
UPDATE users SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;
`
_, err := am.db.Exec(query)
return err
}
func (am *AuthManager) createDefaultAdmin() error {
var count int
err := am.db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count)
if err != nil {
return err
}
if count == 0 {
// Create default admin with the old password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte("ahsahbeequen"), bcrypt.DefaultCost)
if err != nil {
return err
}
_, err = am.db.Exec(`
INSERT INTO users (email, password, is_admin, is_setup)
VALUES (?, ?, ?, ?)
`, "admin@comp3007.local", string(hashedPassword), true, true)
return err
}
return nil
}
func (am *AuthManager) CreateUser(email string, isAdmin bool) (*User, error) {
// Generate setup token
token, err := generateSecureToken()
if err != nil {
return nil, fmt.Errorf("failed to generate setup token: %w", err)
}
expiry := time.Now().Add(7 * 24 * time.Hour) // 7 days
result, err := am.db.Exec(`
INSERT INTO users (email, is_admin, setup_token, setup_token_expiry)
VALUES (?, ?, ?, ?)
`, email, isAdmin, token, expiry)
if err != nil {
return nil, fmt.Errorf("failed to create user: %w", err)
}
id, err := result.LastInsertId()
if err != nil {
return nil, fmt.Errorf("failed to get user ID: %w", err)
}
user := &User{
ID: int(id),
Email: email,
IsAdmin: isAdmin,
SetupToken: token,
SetupTokenExpiry: expiry,
IsSetup: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
return user, nil
}
func (am *AuthManager) GetUserByEmail(email string) (*User, error) {
user := &User{}
var password sql.NullString
var setupToken sql.NullString
var setupTokenExpiry sql.NullTime
err := am.db.QueryRow(`
SELECT id, email, password, is_admin, setup_token, setup_token_expiry, is_setup, created_at, updated_at
FROM users WHERE email = ?
`, email).Scan(&user.ID, &user.Email, &password, &user.IsAdmin, &setupToken, &setupTokenExpiry, &user.IsSetup, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
// Handle nullable fields
if password.Valid {
user.Password = password.String
}
if setupToken.Valid {
user.SetupToken = setupToken.String
}
if setupTokenExpiry.Valid {
user.SetupTokenExpiry = setupTokenExpiry.Time
}
return user, nil
}
func (am *AuthManager) GetUserByID(id int) (*User, error) {
user := &User{}
var password sql.NullString
var setupToken sql.NullString
var setupTokenExpiry sql.NullTime
err := am.db.QueryRow(`
SELECT id, email, password, is_admin, setup_token, setup_token_expiry, is_setup, created_at, updated_at
FROM users WHERE id = ?
`, id).Scan(&user.ID, &user.Email, &password, &user.IsAdmin, &setupToken, &setupTokenExpiry, &user.IsSetup, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
// Handle nullable fields
if password.Valid {
user.Password = password.String
}
if setupToken.Valid {
user.SetupToken = setupToken.String
}
if setupTokenExpiry.Valid {
user.SetupTokenExpiry = setupTokenExpiry.Time
}
return user, nil
}
func (am *AuthManager) GetUserBySetupToken(token string) (*User, error) {
user := &User{}
var password sql.NullString
var setupToken sql.NullString
var setupTokenExpiry sql.NullTime
err := am.db.QueryRow(`
SELECT id, email, password, is_admin, setup_token, setup_token_expiry, is_setup, created_at, updated_at
FROM users WHERE setup_token = ? AND setup_token_expiry > CURRENT_TIMESTAMP
`, token).Scan(&user.ID, &user.Email, &password, &user.IsAdmin, &setupToken, &setupTokenExpiry, &user.IsSetup, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
// Handle nullable fields
if password.Valid {
user.Password = password.String
}
if setupToken.Valid {
user.SetupToken = setupToken.String
}
if setupTokenExpiry.Valid {
user.SetupTokenExpiry = setupTokenExpiry.Time
}
return user, nil
}
func (am *AuthManager) GetAllUsers() ([]*User, error) {
rows, err := am.db.Query(`
SELECT id, email, is_admin, is_setup, created_at, updated_at
FROM users ORDER BY created_at DESC
`)
if err != nil {
return nil, err
}
defer rows.Close()
var users []*User
for rows.Next() {
user := &User{}
err := rows.Scan(&user.ID, &user.Email, &user.IsAdmin, &user.IsSetup, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
return nil, err
}
users = append(users, user)
}
return users, nil
}
func (am *AuthManager) SetupUserPassword(token, password string) error {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("failed to hash password: %w", err)
}
result, err := am.db.Exec(`
UPDATE users
SET password = ?, is_setup = TRUE, setup_token = NULL, setup_token_expiry = NULL, updated_at = CURRENT_TIMESTAMP
WHERE setup_token = ? AND setup_token_expiry > CURRENT_TIMESTAMP
`, string(hashedPassword), token)
if err != nil {
return fmt.Errorf("failed to update user password: %w", err)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("failed to check affected rows: %w", err)
}
if rowsAffected == 0 {
return fmt.Errorf("invalid or expired setup token")
}
return nil
}
func (am *AuthManager) UpdateUserPassword(userID int, password string) error {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("failed to hash password: %w", err)
}
_, err = am.db.Exec(`
UPDATE users
SET password = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
`, string(hashedPassword), userID)
if err != nil {
return fmt.Errorf("failed to update user password: %w", err)
}
return nil
}
func (am *AuthManager) RegenerateSetupToken(userID int) (string, error) {
token, err := generateSecureToken()
if err != nil {
return "", fmt.Errorf("failed to generate setup token: %w", err)
}
expiry := time.Now().Add(7 * 24 * time.Hour) // 7 days
_, err = am.db.Exec(`
UPDATE users
SET setup_token = ?, setup_token_expiry = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
`, token, expiry, userID)
if err != nil {
return "", fmt.Errorf("failed to update setup token: %w", err)
}
return token, nil
}
func (am *AuthManager) ValidateCredentials(email, password string) (*User, error) {
user, err := am.GetUserByEmail(email)
if err != nil {
return nil, err
}
if user == nil {
return nil, fmt.Errorf("user not found")
}
if !user.IsSetup {
return nil, fmt.Errorf("user account not set up")
}
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
if err != nil {
return nil, fmt.Errorf("invalid credentials")
}
return user, nil
}
func (am *AuthManager) GenerateJWT(user *User) (string, error) {
claims := AuthClaims{
UserID: user.ID,
Email: user.Email,
IsAdmin: user.IsAdmin,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
IssuedAt: jwt.NewNumericDate(time.Now()),
NotBefore: jwt.NewNumericDate(time.Now()),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(am.jwtSecret)
}
func (am *AuthManager) ValidateJWT(tokenString string) (*AuthClaims, error) {
token, err := jwt.ParseWithClaims(tokenString, &AuthClaims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return am.jwtSecret, nil
})
if err != nil {
return nil, err
}
if claims, ok := token.Claims.(*AuthClaims); ok && token.Valid {
return claims, nil
}
return nil, fmt.Errorf("invalid token")
}
func (am *AuthManager) RequireAuth(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if config.AuthDisabled {
next(w, r)
return
}
cookie, err := r.Cookie("auth_token")
if err != nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
claims, err := am.ValidateJWT(cookie.Value)
if err != nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
// Add user info to request context
r = r.WithContext(WithUserContext(r.Context(), claims))
next(w, r)
}
}
func (am *AuthManager) RequireAdmin(next http.HandlerFunc) http.HandlerFunc {
return am.RequireAuth(func(w http.ResponseWriter, r *http.Request) {
if config.AuthDisabled {
next(w, r)
return
}
claims := GetUserFromContext(r.Context())
if claims == nil || !claims.IsAdmin {
http.Error(w, "Access denied: admin required", http.StatusForbidden)
return
}
next(w, r)
})
}
func (am *AuthManager) SendSetupEmail(user *User, baseURL string) error {
setupURL := fmt.Sprintf("%s/setup?token=%s", baseURL, user.SetupToken)
// For development, log the setup URL
log.Printf("Setup email for %s: %s", user.Email, setupURL)
htmlBody := fmt.Sprintf(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>COMP 3007 Account Setup</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; margin: 0; padding: 0; background-color: #f4f4f4; }
.container { max-width: 600px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; margin-top: 20px; }
.header { background: #2563eb; color: white; padding: 20px; border-radius: 8px 8px 0 0; text-align: center; margin: -20px -20px 20px -20px; }
.button { display: inline-block; background: #2563eb; color: white; padding: 12px 24px; text-decoration: none; border-radius: 5px; margin: 10px 0; }
.footer { margin-top: 20px; padding-top: 20px; border-top: 1px solid #eee; font-size: 12px; color: #666; }
.url-box { background: #f8f9fa; padding: 10px; border-radius: 4px; font-family: monospace; font-size: 12px; word-break: break-all; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h2 style="margin: 0;">Welcome to COMP 3007</h2>
<p style="margin: 5px 0 0 0;">Programming Paradigms</p>
</div>
<p>Hello!</p>
<p>Your account has been created for the COMP 3007 course website. To get started, you need to set up your password.</p>
<p style="text-align: center;">
<a href="%s" class="button">Set Up Your Account</a>
</p>
<p><strong>Important:</strong> This setup link will expire in 7 days. If you don't set up your account within this time, please contact your instructor.</p>
<p>If the button above doesn't work, you can copy and paste this URL into your browser:</p>
<div class="url-box">%s</div>
<div class="footer">
<p>This email was sent automatically by the COMP 3007 course management system. If you believe you received this email in error, please contact your instructor.</p>
<p>COMP 3007 - Programming Paradigms</p>
</div>
</div>
</body>
</html>`, setupURL, setupURL)
return am.sendEmailWithResend(user.Email, "COMP 3007 Account Setup", htmlBody)
}
// ResendEmailRequest represents the JSON payload for Resend API
type ResendEmailRequest struct {
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Html string `json:"html"`
}
// ResendEmailResponse represents the response from Resend API
type ResendEmailResponse struct {
Id string `json:"id"`
}
func (am *AuthManager) sendEmailWithResend(to, subject, htmlBody string) error {
apiKey := os.Getenv("RESEND_API_KEY")
fromEmail := os.Getenv("FROM_EMAIL")
// For development/testing, just log the email if API key is not configured
if apiKey == "" {
log.Printf("Resend API key not configured, logging email instead:")
log.Printf("TO: %s", to)
log.Printf("SUBJECT: %s", subject)
log.Printf("BODY: %s", htmlBody)
return nil
}
if fromEmail == "" {
fromEmail = "onboarding@resend.dev" // Resend's test domain
}
// Prepare the email payload
emailRequest := ResendEmailRequest{
From: fromEmail,
To: []string{to},
Subject: subject,
Html: htmlBody,
}
// Convert to JSON
jsonData, err := json.Marshal(emailRequest)
if err != nil {
return fmt.Errorf("failed to marshal email request: %w", err)
}
// Create HTTP request
req, err := http.NewRequest("POST", "https://api.resend.com/emails", bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create HTTP request: %w", err)
}
// Set headers
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// Send request
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to send HTTP request: %w", err)
}
defer resp.Body.Close()
// Check response status
if resp.StatusCode != http.StatusOK {
// Read the response body for more detailed error information
bodyBytes, _ := io.ReadAll(resp.Body)
bodyString := string(bodyBytes)
log.Printf("Resend API error response: %s", bodyString)
return fmt.Errorf("Resend API returned status %d: %s - %s", resp.StatusCode, resp.Status, bodyString)
}
// Parse response
var emailResponse ResendEmailResponse
if err := json.NewDecoder(resp.Body).Decode(&emailResponse); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
}
log.Printf("Email sent successfully to %s via Resend API (ID: %s)", to, emailResponse.Id)
return nil
}
func generateSecureToken() (string, error) {
bytes := make([]byte, 32)
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
// Context helpers for user information
type contextKey string
const userContextKey contextKey = "user"
func WithUserContext(ctx context.Context, claims *AuthClaims) context.Context {
return context.WithValue(ctx, userContextKey, claims)
}
func GetUserFromContext(ctx context.Context) *AuthClaims {
if claims, ok := ctx.Value(userContextKey).(*AuthClaims); ok {
return claims
}
return nil
}