forked from mtusi2026/mtusi2026lab7
114 lines
2.7 KiB
Go
114 lines
2.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"errors"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"mtusi2026lab7/internal/application"
|
|
"mtusi2026lab7/internal/transport/http/dto"
|
|
"mtusi2026lab7/internal/transport/http/middleware"
|
|
"mtusi2026lab7/internal/transport/http/response"
|
|
)
|
|
|
|
const basicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
|
|
|
func (h *Handler) CreateSession(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
requestID := middleware.GetRequestID(r.Context())
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
if !ok || username == "" ||
|
|
!utf8.ValidString(username) || !utf8.ValidString(password) {
|
|
writeBasicUnauthorized(w, requestID)
|
|
return
|
|
}
|
|
identifierHash := sha256.Sum256([]byte(strings.ToLower(username)))
|
|
if h.loginRateLimiter != nil {
|
|
allowed, retryAfter, err := h.loginRateLimiter.AllowLoginAttempt(
|
|
r.Context(), identifierHash, time.Now().UTC(), h.loginRateWindow, h.loginRateLimit,
|
|
)
|
|
if err != nil {
|
|
response.WriteError(w, http.StatusServiceUnavailable, response.CodeUnavailable,
|
|
"service is temporarily unavailable", requestID)
|
|
return
|
|
}
|
|
if !allowed {
|
|
seconds := int(math.Ceil(retryAfter.Seconds()))
|
|
if seconds < 1 {
|
|
seconds = 1
|
|
}
|
|
w.Header().Set("Retry-After", strconv.Itoa(seconds))
|
|
response.WriteError(w, http.StatusTooManyRequests, response.CodeRateLimited,
|
|
"too many login attempts", requestID)
|
|
return
|
|
}
|
|
}
|
|
|
|
session, err := h.service.CreateSession(r.Context(), application.CreateSessionCommand{
|
|
Username: username,
|
|
Password: password,
|
|
})
|
|
if errors.Is(err, application.ErrCredentialState) {
|
|
response.WriteError(
|
|
w,
|
|
http.StatusInternalServerError,
|
|
response.CodeInternalError,
|
|
"service is temporarily unavailable",
|
|
requestID,
|
|
)
|
|
return
|
|
}
|
|
if errors.Is(err, application.ErrInvalidCredentials) {
|
|
writeBasicUnauthorized(w, requestID)
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.WriteError(
|
|
w,
|
|
http.StatusServiceUnavailable,
|
|
response.CodeUnavailable,
|
|
"service is temporarily unavailable",
|
|
requestID,
|
|
)
|
|
return
|
|
}
|
|
if h.loginRateLimiter != nil {
|
|
if err = h.loginRateLimiter.ResetLoginAttempts(r.Context(), identifierHash); err != nil {
|
|
response.WriteError(w, http.StatusServiceUnavailable, response.CodeUnavailable,
|
|
"service is temporarily unavailable", requestID)
|
|
return
|
|
}
|
|
}
|
|
|
|
h.setSessionCookie(w, session.Token, session.ExpiresAt)
|
|
|
|
response.WriteJSON(
|
|
w,
|
|
http.StatusCreated,
|
|
dto.SessionFromDomain(session),
|
|
requestID,
|
|
)
|
|
}
|
|
func writeBasicUnauthorized(
|
|
w http.ResponseWriter,
|
|
requestID string,
|
|
) {
|
|
w.Header().Set("WWW-Authenticate", basicChallenge)
|
|
|
|
response.WriteError(
|
|
w,
|
|
http.StatusUnauthorized,
|
|
response.CodeUnauthorized,
|
|
"invalid username or password",
|
|
requestID,
|
|
)
|
|
}
|