- Updated response methods to standardize Content-Type header to "application/json" across OAuth endpoints, enhancing compliance with JSON standards. - Refactored error and success response methods to streamline response generation without unnecessary wrappers, improving clarity and maintainability. - Enhanced security by ensuring all responses include appropriate OAuth security headers, aligning with best practices for sensitive endpoints. - Simplified test assertions for Content-Type in OAuth tests, ensuring consistency in response validation.
30 lines
564 B
Go
30 lines
564 B
Go
package oauth
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Guard is the OAuth guard middleware
|
|
func (s *Service) Guard(c *gin.Context) {
|
|
// Get the token from the request
|
|
token := c.GetHeader("Authorization")
|
|
|
|
// Validate the token
|
|
if token == "" {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
// Validate the token
|
|
_, err := s.VerifyToken(strings.TrimPrefix(token, "Bearer "))
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
}
|