fixed cache Refactoring from app router to page router Refactoring from app router to page router Add authentication with JWT base ui done protect the dashboard added transitions styling type trick added cleanup Add enable-disable logic add transactional emails Improvements on delay and frontend added description on select implement auth with magic link migrate to mariadb webhook signature working Adding async processing with queues Implemented webhook reply functionality Add son execution logs update status logic Add recent activity monitoring to sons show son performance in dashboard add readme implement listmonk connector listmonk-connector-v1 Create CODE_OF_CONDUCT.md Create LICENSE
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/troneras/ghost-listmonk-connector/services"
|
|
"github.com/troneras/ghost-listmonk-connector/utils"
|
|
)
|
|
|
|
func AuthRequired(userService *services.UserService) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
authHeader := c.GetHeader("Authorization")
|
|
if authHeader == "" {
|
|
utils.ErrorLogger.Println("Authorization header is required")
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
bearerToken := strings.Split(authHeader, " ")
|
|
if len(bearerToken) != 2 {
|
|
utils.ErrorLogger.Println("Invalid token format")
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token format"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
tokenString := bearerToken[1]
|
|
|
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
return nil, jwt.ErrSignatureInvalid
|
|
}
|
|
return []byte(utils.GetConfig().JWT_SECRET), nil
|
|
})
|
|
|
|
if err != nil {
|
|
utils.ErrorLogger.Println("Invalid token")
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
|
userID, ok := claims["user_id"].(string)
|
|
if !ok {
|
|
utils.ErrorLogger.Println("Invalid user_id in token claims")
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token claims"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
user, err := userService.GetUserByID(userID)
|
|
if err != nil {
|
|
utils.ErrorLogger.Printf("Failed to get user: %v", err)
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid user"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
c.Set("user", user)
|
|
c.Next()
|
|
} else {
|
|
utils.ErrorLogger.Println("Invalid token claims")
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token claims"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
}
|
|
}
|