forked from mtusi2026/mtusi2026lab3
refactor: разделил приложение на domain application adapters и HTTP DTO
This commit is contained in:
@@ -3,25 +3,16 @@ package handlers
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"mtusi2026lab3/internal/domain"
|
||||
"mtusi2026lab3/internal/security"
|
||||
"mtusi2026lab3/internal/store"
|
||||
"mtusi2026lab3/internal/application"
|
||||
"mtusi2026lab3/internal/transport/http/dto"
|
||||
"mtusi2026lab3/internal/transport/http/middleware"
|
||||
"mtusi2026lab3/internal/transport/http/response"
|
||||
)
|
||||
|
||||
const basicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
|
||||
// dummyPasswordHash keeps the expensive Argon2id path for an unknown username.
|
||||
// The all-zero digest is intentionally not a real user's credential; only the
|
||||
// encoded parameters and lengths need to be valid for constant-cost checking.
|
||||
const dummyPasswordHash = "$argon2id$v=19$m=19456,t=2,p=1$" +
|
||||
"AAAAAAAAAAAAAAAAAAAAAA$" +
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
|
||||
func (h *Handler) CreateSession(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
@@ -35,32 +26,11 @@ func (h *Handler) CreateSession(
|
||||
return
|
||||
}
|
||||
|
||||
user, passwordHash, err := h.store.GetUserByUsername(
|
||||
r.Context(),
|
||||
username,
|
||||
)
|
||||
userNotFound := errors.Is(err, store.ErrNotFound)
|
||||
if userNotFound {
|
||||
passwordHash = dummyPasswordHash
|
||||
}
|
||||
if err != nil {
|
||||
if !userNotFound {
|
||||
response.WriteError(
|
||||
w,
|
||||
http.StatusServiceUnavailable,
|
||||
response.CodeUnavailable,
|
||||
"service is temporarily unavailable",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
passwordMatches, err := security.VerifyPassword(
|
||||
password,
|
||||
passwordHash,
|
||||
)
|
||||
if err != nil {
|
||||
session, err := h.service.CreateSession(r.Context(), application.CreateSessionCommand{
|
||||
Username: username,
|
||||
Password: password,
|
||||
})
|
||||
if errors.Is(err, application.ErrCredentialState) {
|
||||
response.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
@@ -70,31 +40,11 @@ func (h *Handler) CreateSession(
|
||||
)
|
||||
return
|
||||
}
|
||||
if userNotFound || !passwordMatches {
|
||||
if errors.Is(err, application.ErrInvalidCredentials) {
|
||||
writeBasicUnauthorized(w, requestID)
|
||||
return
|
||||
}
|
||||
|
||||
token, tokenHash, err := security.GenerateSessionToken()
|
||||
if err != nil {
|
||||
response.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
response.CodeInternalError,
|
||||
"failed to create session",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
expiresAt := time.Now().UTC().Add(h.sessionTTL)
|
||||
|
||||
if err := h.store.CreateSession(
|
||||
r.Context(),
|
||||
tokenHash,
|
||||
user.ID,
|
||||
expiresAt,
|
||||
); err != nil {
|
||||
response.WriteError(
|
||||
w,
|
||||
http.StatusServiceUnavailable,
|
||||
@@ -105,50 +55,13 @@ func (h *Handler) CreateSession(
|
||||
return
|
||||
}
|
||||
|
||||
session := domain.Session{
|
||||
Token: token,
|
||||
TokenType: "Bearer",
|
||||
ExpiresAt: expiresAt,
|
||||
User: user,
|
||||
}
|
||||
|
||||
response.WriteJSON(
|
||||
w,
|
||||
http.StatusCreated,
|
||||
session,
|
||||
dto.SessionFromDomain(session),
|
||||
requestID,
|
||||
)
|
||||
}
|
||||
|
||||
func (h *Handler) DeleteCurrentSession(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
tokenHash := middleware.GetCurrentSessionTokenHash(r.Context())
|
||||
|
||||
if err := h.store.DeleteSession(
|
||||
r.Context(),
|
||||
tokenHash,
|
||||
); err != nil {
|
||||
response.WriteError(
|
||||
w,
|
||||
http.StatusServiceUnavailable,
|
||||
response.CodeUnavailable,
|
||||
"service is temporarily unavailable",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteJSON(
|
||||
w,
|
||||
http.StatusNoContent,
|
||||
nil,
|
||||
requestID,
|
||||
)
|
||||
}
|
||||
|
||||
func writeBasicUnauthorized(
|
||||
w http.ResponseWriter,
|
||||
requestID string,
|
||||
|
||||
Reference in New Issue
Block a user