Private
Public Access
actualizado tema tokens
This commit is contained in:
1 parent
38afdcc742
commit
441e2f5833
28 files changed
+506
-194
No files matched your search
@@ -7,11 +7,10 @@ import (
|
||||
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
@@ -37,13 +36,20 @@ func Login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
accessToken, err := generarToken(*user, 15*time.Minute)
|
||||
usuarioConPermisos, err := r.ObtenerRepositorios().Usuario.PrecargarGruposPermisosPorID(user.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "No se pudieron cargar los permisos del usuario"})
|
||||
return
|
||||
}
|
||||
permisos := utils.PermisosDeUsuario(usuarioConPermisos)
|
||||
|
||||
accessToken, err := jwtutils.GenerarToken(user.ID, user.Email, user.Name, jwtutils.TokenTypeAccess, permisos, 15*time.Minute)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate token"})
|
||||
return
|
||||
}
|
||||
|
||||
refreshToken, err := generarToken(*user, 24*time.Hour)
|
||||
refreshToken, err := jwtutils.GenerarToken(user.ID, user.Email, user.Name, jwtutils.TokenTypeRefresh, permisos, 24*time.Hour)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate refresh token"})
|
||||
return
|
||||
@@ -54,18 +60,6 @@ func Login(c *gin.Context) {
|
||||
RefreshToken: refreshToken,
|
||||
TokenType: "Bearer",
|
||||
ExpiresIn: 900,
|
||||
Permissions: permisos,
|
||||
})
|
||||
}
|
||||
|
||||
func generarToken(user models_seguridad.Usuario, duration time.Duration) (string, error) {
|
||||
claims := jwt.MapClaims{
|
||||
"sub": user.ID,
|
||||
"email": user.Email,
|
||||
"name": user.Name,
|
||||
"exp": time.Now().Add(duration).Unix(),
|
||||
"iat": time.Now().Unix(),
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString(jwtutils.JWTSecret)
|
||||
}
|
||||
+10
-42
@@ -2,64 +2,32 @@ package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
func Me(c *gin.Context) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "authorization header required"})
|
||||
userID, ok := c.Get("userID")
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "usuario no autenticado"})
|
||||
return
|
||||
}
|
||||
|
||||
parts := strings.Split(authHeader, " ")
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid authorization header format"})
|
||||
return
|
||||
}
|
||||
|
||||
tokenString := parts[1]
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.ErrSignatureInvalid
|
||||
}
|
||||
return jwtutils.JWTSecret, nil
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
|
||||
return
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
id, ok := userID.(uint)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token claims"})
|
||||
return
|
||||
}
|
||||
|
||||
sub, ok := claims["sub"].(string)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token claims"})
|
||||
return
|
||||
}
|
||||
userID, _ := strconv.ParseUint(sub, 10, 32)
|
||||
user, err := r.ObtenerRepositorios().Usuario.ObtenerPorID(uint(userID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
|
||||
return
|
||||
}
|
||||
permisos, _ := utils.PermisosDelContexto(c)
|
||||
|
||||
c.JSON(http.StatusOK, dto.MeResponse{
|
||||
ID: user.ID,
|
||||
Email: user.Email,
|
||||
Name: user.Name,
|
||||
ID: id,
|
||||
Email: c.GetString("email"),
|
||||
Name: c.GetString("name"),
|
||||
Permissions: permisos,
|
||||
})
|
||||
}
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
func Refresh(c *gin.Context) {
|
||||
@@ -21,43 +21,32 @@ func Refresh(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
tokenString := req.RefreshToken
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.ErrSignatureInvalid
|
||||
}
|
||||
return jwtutils.JWTSecret, nil
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
claims, err := jwtutils.ValidarTokenDeTipo(req.RefreshToken, jwtutils.TokenTypeRefresh)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid refresh token"})
|
||||
return
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
sub, ok := jwtutils.ClaimUint(claims, jwtutils.ClaimSubject)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token claims"})
|
||||
return
|
||||
}
|
||||
|
||||
sub, ok := claims["sub"].(float64)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token claims"})
|
||||
return
|
||||
}
|
||||
user, err := r.ObtenerRepositorios().Usuario.ObtenerPorID(uint(sub))
|
||||
user, err := r.ObtenerRepositorios().Usuario.PrecargarGruposPermisosPorID(sub)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
|
||||
return
|
||||
}
|
||||
permisos := utils.PermisosDeUsuario(user)
|
||||
|
||||
accessToken, err := generarToken(*user, 15*time.Minute)
|
||||
accessToken, err := jwtutils.GenerarToken(user.ID, user.Email, user.Name, jwtutils.TokenTypeAccess, permisos, 15*time.Minute)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate token"})
|
||||
return
|
||||
}
|
||||
|
||||
newRefreshToken, err := generarToken(*user, 24*time.Hour)
|
||||
newRefreshToken, err := jwtutils.GenerarToken(user.ID, user.Email, user.Name, jwtutils.TokenTypeRefresh, permisos, 24*time.Hour)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate refresh token"})
|
||||
return
|
||||
@@ -68,5 +57,6 @@ func Refresh(c *gin.Context) {
|
||||
RefreshToken: newRefreshToken,
|
||||
TokenType: "Bearer",
|
||||
ExpiresIn: 900,
|
||||
Permissions: permisos,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user