forked from mtusi2026/mtusi2026lab5
lab5 na4
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/sync/singleflight"
|
||||
|
||||
"mtusi2026/internal/auth"
|
||||
"mtusi2026/internal/blobstore"
|
||||
"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
|
||||
blobs blobstore.Store
|
||||
uploadSem chan struct{}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
dbCtx, dbCancel := context.WithTimeout(r.Context(), 3*time.Second)
|
||||
defer dbCancel()
|
||||
|
||||
u, ok, err := a.store.GetUserByID(dbCtx, userID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusServiceUnavailable, requestID, codeUnavailable, "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)
|
||||
}
|
||||
Reference in New Issue
Block a user