feat: secure service connections and login

This commit is contained in:
2026-11-06 21:14:49 +03:00
parent cb51354889
commit 48918e8b19
59 changed files with 1980 additions and 316 deletions
+68 -1
View File
@@ -1,12 +1,16 @@
package httpapi
import (
"context"
"errors"
"io"
"math"
"net"
"net/http"
"strconv"
"strings"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
)
func (s *Server) createSession(w http.ResponseWriter, r *http.Request) *apiError {
@@ -21,6 +25,15 @@ func (s *Server) createSession(w http.ResponseWriter, r *http.Request) *apiError
if !ok {
return unauthorized()
}
allowed, retryAfter, err := s.loginLimiter.Allow(r.Context(), username, loginSource(r))
if err != nil {
return unavailable()
}
if !allowed {
retrySeconds := max(int64(1), int64(math.Ceil(retryAfter.Seconds())))
w.Header().Set("Retry-After", strconv.FormatInt(retrySeconds, 10))
return rateLimited()
}
credentials, err := s.userRepository.UserByUsername(r.Context(), username)
if errors.Is(err, model.ErrNotFound) {
if _, verifyErr := s.passwords.Verify(passwordValue, s.passwords.DummyHash()); verifyErr != nil {
@@ -38,6 +51,9 @@ func (s *Server) createSession(w http.ResponseWriter, r *http.Request) *apiError
if !verified {
return unauthorized()
}
if rehashErr := s.rehashPassword(r.Context(), credentials, passwordValue); rehashErr != nil {
return rehashErr
}
token, expiresAt, err := s.sessions.Create(r.Context(), credentials.ID, s.sessionTTL)
if err != nil {
return unavailable()
@@ -51,6 +67,57 @@ func (s *Server) createSession(w http.ResponseWriter, r *http.Request) *apiError
return nil
}
func (s *Server) rehashPassword(ctx context.Context, credentials model.UserCredentials, passwordValue string) *apiError {
needsRehash, err := s.passwords.NeedsRehash(credentials.PasswordHash)
if err != nil {
return internalError()
}
if !needsRehash {
return nil
}
passwordHash, err := s.passwords.Hash(passwordValue)
if err != nil {
return internalError()
}
updated, err := s.userRepository.UpdatePasswordHash(ctx, credentials.ID, credentials.PasswordHash, passwordHash)
if err != nil {
return unavailable()
}
if updated {
return nil
}
current, err := s.userRepository.UserByUsername(ctx, credentials.Username)
if errors.Is(err, model.ErrNotFound) {
return unauthorized()
}
if err != nil {
return unavailable()
}
verified, err := s.passwords.Verify(passwordValue, current.PasswordHash)
if err != nil {
return internalError()
}
if !verified {
return unauthorized()
}
return nil
}
func loginSource(r *http.Request) string {
forwarded := strings.Split(r.Header.Get("X-Forwarded-For"), ",")
// Nginx appends the address it observed last, so preceding client-supplied entries are not trusted.
if address := net.ParseIP(strings.TrimSpace(forwarded[len(forwarded)-1])); address != nil {
return address.String()
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err == nil {
if address := net.ParseIP(host); address != nil {
return address.String()
}
}
return "unknown"
}
func singleBasicAuth(r *http.Request) (string, string, bool) {
if len(r.Header.Values("Authorization")) != 1 {
return "", "", false