forked from mtusi2026/mtusi2026lab3
116 lines
4.3 KiB
Diff
116 lines
4.3 KiB
Diff
diff --git a/service/internal/pkg/encrypting/encryptor.go b/service/internal/pkg/encrypting/encryptor.go
|
|
index a526e8c..1495a5b 100644
|
|
--- a/service/internal/pkg/encrypting/encryptor.go
|
|
+++ b/service/internal/pkg/encrypting/encryptor.go
|
|
@@ -3,7 +3,9 @@ package encrypting
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
+ "crypto/subtle"
|
|
"encoding/hex"
|
|
+ "strings"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
@@ -30,13 +32,13 @@ func (e *encryptor) EncryptSecret(_ context.Context, secret string) (string, err
|
|
return string(res), nil
|
|
}
|
|
|
|
-func (e *encryptor) EncryptSecretUsingSum256(_ context.Context, secret string) (string, error) {
|
|
- sum := sha256.Sum256([]byte(secret))
|
|
- res, err := e.generateFromPasswordFunc([]byte(hex.EncodeToString(sum[:])), e.cost)
|
|
- if err != nil {
|
|
- return "", ErrFailedToEncryptPassword
|
|
+// EncryptSecretUsingSum256 hashes a high-entropy session token, not a password.
|
|
+func (e *encryptor) EncryptSecretUsingSum256(ctx context.Context, secret string) (string, error) {
|
|
+ if err := ctx.Err(); err != nil {
|
|
+ return "", err
|
|
}
|
|
- return string(res), nil
|
|
+ sum := sha256.Sum256([]byte(secret))
|
|
+ return "sha256:" + hex.EncodeToString(sum[:]), nil
|
|
}
|
|
|
|
func (e *encryptor) CompareHashAndSecret(_ context.Context, hash, password string) error {
|
|
@@ -47,11 +49,24 @@ func (e *encryptor) CompareHashAndSecret(_ context.Context, hash, password strin
|
|
return nil
|
|
}
|
|
|
|
-func (e *encryptor) CompareHashAndSecretUsingSum256(_ context.Context, hash, password string) error {
|
|
- sum := sha256.Sum256([]byte(password))
|
|
- err := e.compareHashAndPasswordFunc([]byte(hash), []byte(hex.EncodeToString(sum[:])))
|
|
- if err != nil {
|
|
- return ErrFailedToComparePasswords
|
|
+func (e *encryptor) CompareHashAndSecretUsingSum256(ctx context.Context, hash, secret string) error {
|
|
+ if err := ctx.Err(); err != nil {
|
|
+ return err
|
|
}
|
|
- return nil
|
|
+ sum := sha256.Sum256([]byte(secret))
|
|
+ if payload, ok := strings.CutPrefix(hash, "sha256:"); ok {
|
|
+ stored, err := hex.DecodeString(payload)
|
|
+ if err != nil || len(stored) != sha256.Size || subtle.ConstantTimeCompare(stored, sum[:]) != 1 {
|
|
+ return ErrFailedToComparePasswords
|
|
+ }
|
|
+ return nil
|
|
+ }
|
|
+ // Preserve existing lab2/lab3 sessions across a rolling deployment.
|
|
+ // Passwords continue to use Argon2id; only old random-token fingerprints use this fallback.
|
|
+ if strings.HasPrefix(hash, "$2") {
|
|
+ if err := e.compareHashAndPasswordFunc([]byte(hash), []byte(hex.EncodeToString(sum[:]))); err == nil {
|
|
+ return nil
|
|
+ }
|
|
+ }
|
|
+ return ErrFailedToComparePasswords
|
|
}
|
|
diff --git a/service/internal/pkg/rest/framework/framework.go b/service/internal/pkg/rest/framework/framework.go
|
|
index 273dd79..0b976c8 100644
|
|
--- a/service/internal/pkg/rest/framework/framework.go
|
|
+++ b/service/internal/pkg/rest/framework/framework.go
|
|
@@ -197,7 +197,16 @@ func Token(r *http.Request) (string, bool) {
|
|
return token, true
|
|
}
|
|
|
|
+type sessionContextKey struct{}
|
|
+
|
|
+func WithVerifiedSession(r *http.Request, data *dto.UserDataInToken) *http.Request {
|
|
+ return r.WithContext(context.WithValue(r.Context(), sessionContextKey{}, *data))
|
|
+}
|
|
+
|
|
func ParseToken(r *http.Request, provider jwtprovider.Provider) (*dto.UserDataInToken, error) {
|
|
+ if data, ok := r.Context().Value(sessionContextKey{}).(dto.UserDataInToken); ok {
|
|
+ return &data, nil
|
|
+ }
|
|
foundHeader := r.Header.Get(headers.HeaderAuthorization)
|
|
if foundHeader == "" {
|
|
return nil, apperr.New(apperr.CodeUnauthorized, "missing authorization header")
|
|
diff --git a/service/internal/pkg/rest/middleware/auth.go b/service/internal/pkg/rest/middleware/auth.go
|
|
index 0a5ef18..a39c053 100644
|
|
--- a/service/internal/pkg/rest/middleware/auth.go
|
|
+++ b/service/internal/pkg/rest/middleware/auth.go
|
|
@@ -39,12 +39,13 @@ func (a *authMiddleware) WithAdminAccess(next http.Handler) http.Handler {
|
|
|
|
func (a *authMiddleware) privateAccessCheckCallback(next http.Handler) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
- if _, err := a.verifySession(r); err != nil {
|
|
+ data, err := a.verifySession(r)
|
|
+ if err != nil {
|
|
framework.JSONError(r.Context(), w, err)
|
|
return
|
|
}
|
|
|
|
- next.ServeHTTP(w, r)
|
|
+ next.ServeHTTP(w, framework.WithVerifiedSession(r, data))
|
|
}
|
|
}
|
|
|
|
@@ -61,7 +62,7 @@ func (a *authMiddleware) operatorAccessCheckCallback(expectedRole role.Role, nex
|
|
return
|
|
}
|
|
|
|
- next.ServeHTTP(w, r)
|
|
+ next.ServeHTTP(w, framework.WithVerifiedSession(r, u))
|
|
}
|
|
}
|
|
|