-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
219 lines (197 loc) · 7.8 KB
/
Copy pathauth.go
File metadata and controls
219 lines (197 loc) · 7.8 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
package main
import (
"crypto/subtle"
"encoding/json"
"net/http"
"strings"
)
// POST /auth/register — single-user registration (only one account allowed).
func handleRegister(w http.ResponseWriter, r *http.Request) {
email, password, token := parseRegistration(r)
if cfg.RegistrationToken != "" {
if subtle.ConstantTimeCompare([]byte(cfg.RegistrationToken), []byte(token)) != 1 {
respondError(w, r, http.StatusForbidden, "INVALID_TOKEN", "Invalid registration token")
return
}
}
if !validEmail(email) || !validPassword(password) {
respondError(w, r, http.StatusBadRequest, "VALIDATION_ERROR", "Invalid email or password")
return
}
hash, err := hashPassword(password)
if err != nil {
respondError(w, r, http.StatusInternalServerError, "INTERNAL_ERROR", "Registration failed")
return
}
domain := maskEmail(email)
// Atomic single-user enforcement: only insert if no account exists
result, err := store.Exec("INSERT INTO account (email, password_data) SELECT ?,? WHERE (SELECT COUNT(*) FROM account) = 0", email, hash)
if err != nil {
if isUniqueViolation(err) {
emitEvent("registration.failure", clientIP(r), 0, r.UserAgent(), 409, map[string]any{"email": domain})
} else {
logError("registration.insert", err)
}
// Identical response to prevent enumeration
respondSuccess(w, r, http.StatusCreated, "Registration successful", "/#registered")
return
}
rows, _ := result.RowsAffected()
if rows == 0 {
// Account already exists — return identical response to prevent enumeration
respondSuccess(w, r, http.StatusCreated, "Registration successful", "/#registered")
return
}
emitEvent("registration.success", clientIP(r), 0, r.UserAgent(), 201, map[string]any{"email": domain})
respondSuccess(w, r, http.StatusCreated, "Registration successful", "/#registered")
}
// POST /auth/login
func handleLogin(w http.ResponseWriter, r *http.Request) {
ip := clientIP(r)
// Parse all login fields from the body once
email, password, challengeNonce, challengeSolution := parseLoginRequest(r)
// Adaptive PoW challenge
ch := computeChallenge(ip, cfg.AccessSecret)
if ch != nil {
if challengeNonce == "" || challengeSolution == "" {
emitEvent("challenge.issued", ip, 0, r.UserAgent(), 403, map[string]any{"difficulty": ch.Difficulty})
jsonChallenge(w, "CHALLENGE_REQUIRED", "Proof of work required", ch)
return
}
if !verifySignedNonce(challengeNonce, cfg.AccessSecret, ip) || !verifySolution(challengeNonce, challengeSolution, ch.Difficulty) {
emitEvent("challenge.failed", ip, 0, r.UserAgent(), 403, nil)
jsonChallenge(w, "CHALLENGE_FAILED", "Invalid proof of work", ch)
return
}
}
domain := maskEmail(email)
var userID int
var storedHash string
err := store.QueryRow("SELECT id, password_data FROM account WHERE email = ?", email).Scan(&userID, &storedHash)
if err != nil {
rejectConstantTime(password)
emitEvent("login.failure", ip, 0, r.UserAgent(), 401, map[string]any{"email": domain})
respondError(w, r, http.StatusUnauthorized, "INVALID_CREDENTIALS", "Invalid email or password")
return
}
if !verifyPassword(password, storedHash) {
emitEvent("login.failure", ip, userID, r.UserAgent(), 401, map[string]any{"email": domain})
respondError(w, r, http.StatusUnauthorized, "INVALID_CREDENTIALS", "Invalid email or password")
return
}
sid, err := createSession(userID, r.UserAgent(), ip)
if err != nil {
respondError(w, r, http.StatusInternalServerError, "INTERNAL_ERROR", "Login failed")
return
}
if err := setTokenCookies(w, userID, sid); err != nil {
respondError(w, r, http.StatusInternalServerError, "INTERNAL_ERROR", "Login failed")
return
}
emitEvent("login.success", ip, userID, r.UserAgent(), 200, map[string]any{"sessionId": sid})
respondSuccess(w, r, http.StatusOK, "Login successful", "/#logged-in")
}
// POST /auth/logout
func handleLogout(w http.ResponseWriter, r *http.Request) {
claims := getClaims(r)
endSession(claims.SID)
clearTokenCookies(w)
emitEvent("session.revoke", clientIP(r), claims.UID, r.UserAgent(), 200, map[string]any{"sessionId": claims.SID})
respondSuccess(w, r, http.StatusOK, "Logged out", "/#logged-out")
}
// POST /account/password
func handlePasswordChange(w http.ResponseWriter, r *http.Request) {
claims := getClaims(r)
current, newPw := parsePasswordChange(r)
if !validPassword(current) || !validPassword(newPw) {
respondError(w, r, http.StatusBadRequest, "VALIDATION_ERROR", "Invalid password")
return
}
if normalizePassword(current) == normalizePassword(newPw) {
respondError(w, r, http.StatusBadRequest, "VALIDATION_ERROR", "New password must differ from current")
return
}
var storedHash string
err := store.QueryRow("SELECT password_data FROM account WHERE id = ?", claims.UID).Scan(&storedHash)
if err != nil {
respondError(w, r, http.StatusInternalServerError, "INTERNAL_ERROR", "Password change failed")
return
}
if !verifyPassword(current, storedHash) {
respondError(w, r, http.StatusUnauthorized, "INVALID_CREDENTIALS", "Current password is incorrect")
return
}
hash, err := hashPassword(newPw)
if err != nil {
respondError(w, r, http.StatusInternalServerError, "INTERNAL_ERROR", "Password change failed")
return
}
if _, err = store.Exec("UPDATE account SET password_data = ? WHERE id = ?", hash, claims.UID); err != nil {
logError("account.password_update", err)
respondError(w, r, http.StatusInternalServerError, "INTERNAL_ERROR", "Password change failed")
return
}
endAllSessions(claims.UID)
clearTokenCookies(w)
emitEvent("password.change", clientIP(r), claims.UID, r.UserAgent(), 200, map[string]any{"sessionId": claims.SID})
emitEvent("session.revoke_all", clientIP(r), claims.UID, r.UserAgent(), 200, nil)
respondSuccess(w, r, http.StatusOK, "Password changed", "/#password-changed")
}
// GET /account/me
func handleMe(w http.ResponseWriter, r *http.Request) {
claims := getClaims(r)
var email string
if err := store.QueryRow("SELECT email FROM account WHERE id = ?", claims.UID).Scan(&email); err != nil {
respondError(w, r, http.StatusInternalServerError, "INTERNAL_ERROR", "Account lookup failed")
return
}
// Check if gateway (claw) is reachable
gatewayStatus := "unconfigured"
if cfg.GatewayURL != "" {
gatewayStatus = "configured"
}
jsonOK(w, map[string]any{
"userId": claims.UID,
"email": email,
"gateway": gatewayStatus,
})
}
// GET /auth/status — public endpoint to check if registration is needed.
func handleAuthStatus(w http.ResponseWriter, r *http.Request) {
var count int
if err := store.QueryRow("SELECT COUNT(*) FROM account").Scan(&count); err != nil {
logError("auth.status", err)
count = 1 // fail closed: assume registered
}
resp := map[string]any{"registered": count > 0}
if count == 0 && cfg.RegistrationToken != "" {
resp["requiresToken"] = true
}
jsonOK(w, resp)
}
func jsonChallenge(w http.ResponseWriter, code, msg string, ch *Challenge) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(map[string]any{
"error": msg, "code": code, "challenge": ch,
})
}
func parseLoginRequest(r *http.Request) (email, password, challengeNonce, challengeSolution string) {
r.Body = http.MaxBytesReader(nil, r.Body, maxBodySize)
ct := r.Header.Get("Content-Type")
if strings.Contains(ct, "application/json") {
var body struct {
Email string `json:"email"`
Password string `json:"password"`
ChallengeNonce string `json:"challengeNonce"`
ChallengeSolution string `json:"challengeSolution"`
}
json.NewDecoder(r.Body).Decode(&body)
return strings.ToLower(strings.TrimSpace(body.Email)), body.Password, body.ChallengeNonce, body.ChallengeSolution
}
r.ParseForm()
return strings.ToLower(strings.TrimSpace(r.FormValue("email"))), r.FormValue("password"), r.FormValue("challengeNonce"), r.FormValue("challengeSolution")
}
func nowUnix() int64 {
return timeNow().Unix()
}