forked from mtusi2026/mtusi2026lab3
96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"mtusi2026/internal/auth"
|
|
"mtusi2026/internal/config"
|
|
"mtusi2026/internal/model"
|
|
)
|
|
|
|
const sessionTTLEnvKey = "SESSION_TTL_SECONDS"
|
|
|
|
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 {
|
|
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
|
|
}
|