Skip to content

Commit cbfdfcf

Browse files
Bind session hashes to configured secret
1 parent 4f14522 commit cbfdfcf

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

cmd/stackhost/main.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"crypto/hmac"
56
"crypto/rand"
67
"crypto/sha256"
78
"database/sql"
@@ -261,16 +262,14 @@ func (a *app) createSession(w http.ResponseWriter, r *http.Request, id int64) {
261262
return
262263
}
263264
token := hex.EncodeToString(seed)
264-
hash := sha256.Sum256([]byte(token))
265-
tokenHash := hex.EncodeToString(hash[:])
265+
tokenHash := a.sessionHash(token)
266266
now := time.Now().UTC()
267267
a.db.Exec("INSERT INTO sessions(id,user_id,token_hash,expires_at,created_at,last_seen_at,user_agent,ip_address) VALUES(?,?,?,?,?,?,?,?)", token, id, tokenHash, now.Add(24*time.Hour).Format(time.RFC3339), now.Format(time.RFC3339), now.Format(time.RFC3339), r.UserAgent(), r.RemoteAddr)
268268
http.SetCookie(w, &http.Cookie{Name: "stackhost_session", Value: token, Path: "/", HttpOnly: true, SameSite: http.SameSiteLaxMode, Secure: os.Getenv("STACKHOST_COOKIE_SECURE") == "true", MaxAge: 86400})
269269
}
270270
func (a *app) logout(w http.ResponseWriter, r *http.Request) {
271271
if c, err := r.Cookie("stackhost_session"); err == nil {
272-
hash := sha256.Sum256([]byte(c.Value))
273-
tokenHash := hex.EncodeToString(hash[:])
272+
tokenHash := a.sessionHash(c.Value)
274273
var userID int64
275274
_ = a.db.QueryRow("SELECT user_id FROM sessions WHERE token_hash=?", tokenHash).Scan(&userID)
276275
a.db.Exec("UPDATE sessions SET revoked_at=? WHERE token_hash=?", time.Now().UTC().Format(time.RFC3339), tokenHash)
@@ -288,10 +287,10 @@ func (a *app) auth(next http.HandlerFunc) http.HandlerFunc {
288287
jsonError(w, 401, "unauthorized", "Faça login para continuar.")
289288
return
290289
}
291-
hash := sha256.Sum256([]byte(c.Value))
290+
tokenHash := a.sessionHash(c.Value)
292291
var id int64
293292
var exp string
294-
err = a.db.QueryRow("SELECT user_id,expires_at FROM sessions WHERE token_hash=? AND revoked_at IS NULL", hex.EncodeToString(hash[:])).Scan(&id, &exp)
293+
err = a.db.QueryRow("SELECT user_id,expires_at FROM sessions WHERE token_hash=? AND revoked_at IS NULL", tokenHash).Scan(&id, &exp)
295294
if err != nil {
296295
jsonError(w, 401, "unauthorized", "Faça login para continuar.")
297296
return
@@ -301,12 +300,18 @@ func (a *app) auth(next http.HandlerFunc) http.HandlerFunc {
301300
jsonError(w, 401, "unauthorized", "Sessão expirada.")
302301
return
303302
}
304-
_, _ = a.db.Exec("UPDATE sessions SET last_seen_at=? WHERE token_hash=?", time.Now().UTC().Format(time.RFC3339), hex.EncodeToString(hash[:]))
303+
_, _ = a.db.Exec("UPDATE sessions SET last_seen_at=? WHERE token_hash=?", time.Now().UTC().Format(time.RFC3339), tokenHash)
305304
r = r.WithContext(context.WithValue(r.Context(), userKey{}, id))
306305
next(w, r)
307306
}
308307
}
309308

309+
func (a *app) sessionHash(token string) string {
310+
mac := hmac.New(sha256.New, []byte(a.sessionSecret))
311+
_, _ = mac.Write([]byte(token))
312+
return hex.EncodeToString(mac.Sum(nil))
313+
}
314+
310315
type userKey struct{}
311316

312317
func (a *app) me(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)