modified: serv_nginx/api_bb/pkg/middleware/auth.go
add some logs on debug level
This commit is contained in:
@@ -26,14 +26,14 @@ func AuthMiddleware(jwtService service.JWTService, userRepo repository.UserRepos
|
|||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
var tokenString string
|
var tokenString string
|
||||||
logger := logger.Get()
|
logger := logger.Get()
|
||||||
|
|
||||||
// Пробуем получить токен из заголовка Authorization
|
// Пробуем получить токен из заголовка Authorization
|
||||||
authHeader := r.Header.Get("Authorization")
|
authHeader := r.Header.Get("Authorization")
|
||||||
if strings.HasPrefix(authHeader, "Bearer ") {
|
if strings.HasPrefix(authHeader, "Bearer ") {
|
||||||
tokenString = strings.TrimPrefix(authHeader, "Bearer ")
|
tokenString = strings.TrimPrefix(authHeader, "Bearer ")
|
||||||
logger.Debug("Token found in Authorization header")
|
logger.Debug("Token found in Authorization header")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Если нет в заголовке, пробуем из куки
|
// Если нет в заголовке, пробуем из куки
|
||||||
if tokenString == "" {
|
if tokenString == "" {
|
||||||
cookie, err := r.Cookie("auth_token")
|
cookie, err := r.Cookie("auth_token")
|
||||||
@@ -44,50 +44,50 @@ func AuthMiddleware(jwtService service.JWTService, userRepo repository.UserRepos
|
|||||||
logger.Debug("No auth_token cookie found", zap.Error(err))
|
logger.Debug("No auth_token cookie found", zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if tokenString == "" {
|
if tokenString == "" {
|
||||||
logger.Debug("No token found in request")
|
logger.Debug("No token found in request")
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := jwtService.ValidateToken(tokenString)
|
token, err := jwtService.ValidateToken(tokenString)
|
||||||
if err != nil || !token.Valid {
|
if err != nil || !token.Valid {
|
||||||
logger.Warn("Invalid token",
|
logger.Warn("Invalid token",
|
||||||
zap.Error(err),
|
zap.Error(err),
|
||||||
zap.Bool("token_valid", token != nil && token.Valid))
|
zap.Bool("token_valid", token != nil && token.Valid))
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userID, err := jwtService.ExtractUserID(token)
|
userID, err := jwtService.ExtractUserID(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to extract user ID from token",
|
logger.Error("Failed to extract user ID from token",
|
||||||
zap.Error(err))
|
zap.Error(err))
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("Extracted user ID from token",
|
logger.Debug("Extracted user ID from token",
|
||||||
zap.Any("user_id", userID))
|
zap.Any("user_id", userID))
|
||||||
|
|
||||||
user, err := userRepo.FindByID(userID)
|
user, err := userRepo.FindByID(userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to find user by ID",
|
logger.Error("Failed to find user by ID",
|
||||||
zap.Any("user_id", userID),
|
zap.Any("user_id", userID),
|
||||||
zap.Error(err))
|
zap.Error(err))
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Добавляем пользователя в контекст
|
// Добавляем пользователя в контекст
|
||||||
ctx := context.WithValue(r.Context(), UserIDKey, userID)
|
ctx := context.WithValue(r.Context(), UserIDKey, userID)
|
||||||
ctx = context.WithValue(ctx, UserKey, user)
|
ctx = context.WithValue(ctx, UserKey, user)
|
||||||
|
|
||||||
logger.Debug("User authenticated successfully",
|
logger.Debug("User authenticated successfully",
|
||||||
zap.Any("user_id", userID),
|
zap.Any("user_id", userID),
|
||||||
zap.String("username", user.FirstName))
|
zap.String("username", user.FirstName))
|
||||||
|
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -98,13 +98,16 @@ func RequireAuth(next http.Handler) http.Handler {
|
|||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
logger := logger.Get()
|
logger := logger.Get()
|
||||||
userID := r.Context().Value(UserIDKey)
|
userID := r.Context().Value(UserIDKey)
|
||||||
|
logger.Debug("RequireAuth method")
|
||||||
|
logger.Debug("Extracted user ID from token",
|
||||||
|
zap.Any("user_id", userID))
|
||||||
|
|
||||||
if userID == nil {
|
if userID == nil {
|
||||||
logger.Warn("Authentication required but no user ID in context")
|
logger.Warn("Authentication required but no user ID in context")
|
||||||
http.Error(w, `{"error": "Authentication required"}`, http.StatusUnauthorized)
|
http.Error(w, `{"error": "Authentication required"}`, http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("User authenticated", zap.Any("user_id", userID))
|
logger.Debug("User authenticated", zap.Any("user_id", userID))
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
@@ -114,14 +117,17 @@ func RequireAuth(next http.Handler) http.Handler {
|
|||||||
func GetUserFromContext(ctx context.Context) (*models.User, bool) {
|
func GetUserFromContext(ctx context.Context) (*models.User, bool) {
|
||||||
logger := logger.Get()
|
logger := logger.Get()
|
||||||
user, ok := ctx.Value(UserKey).(*models.User)
|
user, ok := ctx.Value(UserKey).(*models.User)
|
||||||
|
logger.Debug("GetUserFromContext method")
|
||||||
|
logger.Debug("Extracted user ID from token",
|
||||||
|
zap.Any("user_id", user.ID))
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
logger.Debug("No user found in context")
|
logger.Debug("No user found in context")
|
||||||
} else {
|
} else {
|
||||||
logger.Debug("User retrieved from context",
|
logger.Debug("User retrieved from context",
|
||||||
zap.Any("user_id", user.ID),
|
zap.Any("user_id", user.ID),
|
||||||
zap.String("username", user.FirstName))
|
zap.String("username", user.FirstName))
|
||||||
}
|
}
|
||||||
|
|
||||||
return user, ok
|
return user, ok
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user