forked from mtusi2026/mtusi2026lab3
118 lines
3.4 KiB
Go
118 lines
3.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"regexp"
|
|
"unicode/utf8"
|
|
|
|
"golang.org/x/sync/singleflight"
|
|
|
|
"mtusi2026/internal/auth"
|
|
"mtusi2026/internal/cache"
|
|
"mtusi2026/internal/model"
|
|
"mtusi2026/internal/sessionstore"
|
|
"mtusi2026/internal/store"
|
|
)
|
|
|
|
var usernameRe = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
|
|
|
type api struct {
|
|
store *store.Store
|
|
sessionStore sessionstore.Store
|
|
chatCache *cache.Cache
|
|
chatFlight singleflight.Group
|
|
}
|
|
|
|
func (a *api) authMiddleware(next func(w http.ResponseWriter, r *http.Request, u model.User, requestID string)) http.HandlerFunc {
|
|
return func(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
|
|
}
|
|
|
|
userID, err := a.sessionStore.GetUserID(r.Context(), token)
|
|
if err != nil {
|
|
if errors.Is(err, sessionstore.ErrNotFound) {
|
|
w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`)
|
|
writeError(w, http.StatusUnauthorized, requestID, codeUnauthorized, "invalid, expired or revoked token")
|
|
return
|
|
}
|
|
|
|
writeError(w, http.StatusServiceUnavailable, requestID, codeInternal, "session service unavailable")
|
|
return
|
|
}
|
|
|
|
u, ok, err := a.store.GetUserByID(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusServiceUnavailable, requestID, codeInternal, "database unavailable")
|
|
return
|
|
}
|
|
if !ok {
|
|
w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`)
|
|
writeError(w, http.StatusUnauthorized, requestID, codeUnauthorized, "invalid session user")
|
|
return
|
|
}
|
|
|
|
next(w, r, u, requestID)
|
|
}
|
|
}
|
|
|
|
func validDisplayName(s string) bool {
|
|
return validText(s, 100)
|
|
}
|
|
|
|
func validPassword(s string) bool {
|
|
n := utf8.RuneCountInString(s)
|
|
return n >= 12 && n <= 128
|
|
}
|
|
|
|
func (a *api) handleRegisterUser(w http.ResponseWriter, r *http.Request) {
|
|
requestID := newRequestID()
|
|
|
|
var req model.CreateUserRequest
|
|
if err := decodeJSONStrict(r, &req); err != nil {
|
|
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "malformed request body")
|
|
return
|
|
}
|
|
|
|
if !usernameRe.MatchString(req.Username) {
|
|
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "username must match ^[a-z0-9_]{3,32}$")
|
|
return
|
|
}
|
|
if !validDisplayName(req.DisplayName) {
|
|
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "display_name must be 1-100 non-blank code points")
|
|
return
|
|
}
|
|
if !validPassword(req.Password) {
|
|
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "password must be 12-128 code points")
|
|
return
|
|
}
|
|
|
|
passwordHash, err := auth.HashPassword(req.Password)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, requestID, codeInternal, "internal error")
|
|
return
|
|
}
|
|
|
|
u, err := a.store.CreateUser(r.Context(), req.Username, req.DisplayName, passwordHash)
|
|
if err != nil {
|
|
if err == store.ErrUsernameTaken {
|
|
writeError(w, http.StatusConflict, requestID, codeConflict, "username already taken")
|
|
return
|
|
}
|
|
writeError(w, http.StatusInternalServerError, requestID, codeInternal, "internal error")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, requestID, u)
|
|
}
|
|
|
|
func (a *api) handleCurrentUser(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
|
|
writeJSON(w, http.StatusOK, requestID, u)
|
|
}
|