forked from mtusi2026/mtusi2026lab5
lab5 na4
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"mtusi2026/internal/auth"
|
||||
"mtusi2026/internal/config"
|
||||
"mtusi2026/internal/model"
|
||||
"mtusi2026/internal/store"
|
||||
)
|
||||
|
||||
const sessionTTLEnvKey = "SESSION_TTL_SECONDS"
|
||||
|
||||
var dummyPasswordHash = func() string {
|
||||
h, err := auth.HashPassword("timing-equalizer-password")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return h
|
||||
}()
|
||||
|
||||
func (a *api) handleCreateSession(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
writeError(w, http.StatusUnauthorized, requestID, codeUnauthorized, "missing or malformed Basic credentials")
|
||||
return
|
||||
}
|
||||
|
||||
u, passwordHash, err := a.store.GetUserByUsername(r.Context(), username)
|
||||
if err != nil {
|
||||
if !errors.Is(err, store.ErrUsernameNotFound) {
|
||||
writeError(w, http.StatusServiceUnavailable, requestID, codeUnavailable, "database unavailable")
|
||||
return
|
||||
}
|
||||
_, _ = auth.VerifyPassword(password, dummyPasswordHash)
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
writeError(w, http.StatusUnauthorized, requestID, codeUnauthorized, "invalid username or password")
|
||||
return
|
||||
}
|
||||
|
||||
valid, err := auth.VerifyPassword(password, passwordHash)
|
||||
if err != nil || !valid {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
writeError(w, http.StatusUnauthorized, requestID, codeUnauthorized, "invalid username or password")
|
||||
return
|
||||
}
|
||||
|
||||
token, err := auth.GenerateSessionToken()
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, requestID, codeInternal, "internal error")
|
||||
return
|
||||
}
|
||||
|
||||
ttl := config.ParseDurationSecondsEnv(sessionTTLEnvKey, 3600)
|
||||
expiresAt, err := a.sessionStore.Create(r.Context(), token, u.ID, ttl)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusServiceUnavailable, requestID, codeInternal, "session service unavailable")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, requestID, model.Session{
|
||||
Token: token,
|
||||
TokenType: "Bearer",
|
||||
ExpiresAt: expiresAt,
|
||||
User: u,
|
||||
})
|
||||
}
|
||||
|
||||
func (a *api) handleDeleteSession(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
|
||||
token, ok := bearerToken(r)
|
||||
if !ok {
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`)
|
||||
writeError(w, http.StatusUnauthorized, requestID, codeUnauthorized, "missing or malformed Bearer token")
|
||||
return
|
||||
}
|
||||
|
||||
deleted, err := a.sessionStore.Delete(r.Context(), token)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusServiceUnavailable, requestID, codeInternal, "session service unavailable")
|
||||
return
|
||||
}
|
||||
if !deleted {
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`)
|
||||
writeError(w, http.StatusUnauthorized, requestID, codeUnauthorized, "unknown or already revoked token")
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func bearerToken(r *http.Request) (string, bool) {
|
||||
h := r.Header.Get("Authorization")
|
||||
const prefix = "Bearer "
|
||||
if !strings.HasPrefix(h, prefix) {
|
||||
return "", false
|
||||
}
|
||||
token := strings.TrimPrefix(h, prefix)
|
||||
if token == "" {
|
||||
return "", false
|
||||
}
|
||||
return token, true
|
||||
}
|
||||
Reference in New Issue
Block a user