diff options
| author | haturatu <taro@eyes4you.org> | 2026-04-22 04:46:24 +0900 |
|---|---|---|
| committer | haturatu <taro@eyes4you.org> | 2026-04-22 04:46:24 +0900 |
| commit | 4b0271272e983fdb85d4b816cbd91f34d91a7884 (patch) | |
| tree | ad92ba661f0f9e8b16d93f333ebf43ffd331a892 /middleware/auth.go | |
| parent | d18b4f9d615d8942ff604a53be92da2c8e853635 (diff) | |
Diffstat (limited to 'middleware/auth.go')
| -rw-r--r-- | middleware/auth.go | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/middleware/auth.go b/middleware/auth.go index 3d6484f..45236aa 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -7,7 +7,6 @@ import ( "auth-proxy/types" "auth-proxy/utils" "context" - "database/sql" "net/http" "strings" ) @@ -32,8 +31,8 @@ func SessionAuth(next http.Handler) http.Handler { } tokenStr := cookie.Value - user, err := models.GetUserByToken(tokenStr) // This still relies on the session token in the DB - if err != nil && err != sql.ErrNoRows { + user, err := models.GetUserByToken(tokenStr) + if err != nil { logging.AppLog.Error("Error validating session token", "error", err, "path", r.URL.Path, "ip", ip) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return @@ -85,9 +84,15 @@ func BearerAuth(next http.Handler) http.Handler { return } + if payload.TokenType != "access" { + logging.SecurityLog.Warn("API ACCESS DENIED", "reason", "unexpected token type", "token_type", payload.TokenType, "path", r.URL.Path, "ip", ip) + http.Error(w, "Unauthorized: Invalid token type", http.StatusUnauthorized) + return + } + user, err := models.GetUserByID(payload.UserID) if err != nil || user == nil { - if err == sql.ErrNoRows || user == nil { + if user == nil { logging.SecurityLog.Error("API ACCESS DENIED", "reason", "user from token not found", "user_id", payload.UserID, "path", r.URL.Path, "ip", ip) http.Error(w, "Unauthorized: User not found", http.StatusUnauthorized) } else { @@ -166,4 +171,4 @@ func HandleOptions(next http.Handler) http.Handler { } next.ServeHTTP(w, r) }) -}
\ No newline at end of file +} |
