Private
Public Access
primeros cambios hechos para tener el multitenant
This commit is contained in:
1 parent
34ad4424f9
commit
4e082e8767
132 files changed
+6202
-762
No files matched your search
+42
-12
@@ -11,13 +11,19 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// MapClaims es un alias para no filtrar el paquete jwt a los consumidores.
|
||||
type MapClaims = jwt.MapClaims
|
||||
|
||||
const (
|
||||
TokenTypeAccess = "access"
|
||||
TokenTypeRefresh = "refresh"
|
||||
|
||||
ClaimPermissions = "permissions"
|
||||
ClaimType = "typ"
|
||||
ClaimSubject = "sub"
|
||||
ClaimPermissions = "permissions"
|
||||
ClaimType = "typ"
|
||||
ClaimSubject = "sub"
|
||||
ClaimEmpresaID = "empresa_id"
|
||||
ClaimEmpresaRol = "empresa_rol"
|
||||
ClaimPlataformaAdmin = "es_plataforma_admin"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -50,21 +56,25 @@ func GetSecret() ([]byte, error) {
|
||||
return jwtSecret, nil
|
||||
}
|
||||
|
||||
// GenerarToken firma un token HS256 con los datos del usuario y sus permisos planos.
|
||||
func GenerarToken(userID uint, email, name, tokenType string, permissions []uint, duration time.Duration) (string, error) {
|
||||
// GenerarToken firma un token HS256 con los datos del usuario, su empresa
|
||||
// activa y sus permisos planos.
|
||||
func GenerarToken(userID uint, empresaID uint, empresaRol string, esPlataformaAdmin bool, email, name, tokenType string, permissions []uint, duration time.Duration) (string, error) {
|
||||
secret, err := GetSecret()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
claims := jwt.MapClaims{
|
||||
ClaimSubject: userID,
|
||||
"email": email,
|
||||
"name": name,
|
||||
ClaimPermissions: permissions,
|
||||
ClaimType: tokenType,
|
||||
"exp": time.Now().Add(duration).Unix(),
|
||||
"iat": time.Now().Unix(),
|
||||
ClaimSubject: userID,
|
||||
"email": email,
|
||||
"name": name,
|
||||
ClaimEmpresaID: empresaID,
|
||||
ClaimEmpresaRol: empresaRol,
|
||||
ClaimPlataformaAdmin: esPlataformaAdmin,
|
||||
ClaimPermissions: permissions,
|
||||
ClaimType: tokenType,
|
||||
"exp": time.Now().Add(duration).Unix(),
|
||||
"iat": time.Now().Unix(),
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
@@ -147,6 +157,26 @@ func ClaimUint(claims jwt.MapClaims, key string) (uint, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// ClaimBool interpreta un claim booleano.
|
||||
func ClaimBool(claims jwt.MapClaims, key string) bool {
|
||||
value, ok := claims[key]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
v, _ := value.(bool)
|
||||
return v
|
||||
}
|
||||
|
||||
// ClaimString interpreta un claim string.
|
||||
func ClaimString(claims jwt.MapClaims, key string) string {
|
||||
value, ok := claims[key]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
v, _ := value.(string)
|
||||
return v
|
||||
}
|
||||
|
||||
// PermisosDesdeClaims extrae la lista plana de IDs de permisos del token.
|
||||
func PermisosDesdeClaims(claims jwt.MapClaims) []uint {
|
||||
raw, ok := claims[ClaimPermissions]
|
||||
|
||||
@@ -11,7 +11,7 @@ func TestTokenRoundTrip(t *testing.T) {
|
||||
os.Setenv("JWTString", "test-secret")
|
||||
t.Cleanup(func() { os.Unsetenv("JWTString") })
|
||||
|
||||
token, err := GenerarToken(7, "a@b.c", "Nombre", TokenTypeAccess, []uint{1, 2, 4}, time.Minute)
|
||||
token, err := GenerarToken(7, 3, "OWNER", false, "a@b.c", "Nombre", TokenTypeAccess, []uint{1, 2, 4}, time.Minute)
|
||||
if err != nil {
|
||||
t.Fatalf("generar token: %v", err)
|
||||
}
|
||||
@@ -25,6 +25,14 @@ func TestTokenRoundTrip(t *testing.T) {
|
||||
t.Fatalf("sub esperado 7, obtenido %v (%v)", id, ok)
|
||||
}
|
||||
|
||||
if empresa, ok := ClaimUint(claims, ClaimEmpresaID); !ok || empresa != 3 {
|
||||
t.Fatalf("empresa esperada 3, obtenida %v (%v)", empresa, ok)
|
||||
}
|
||||
|
||||
if rol := ClaimString(claims, ClaimEmpresaRol); rol != "OWNER" {
|
||||
t.Fatalf("rol esperado OWNER, obtenido %q", rol)
|
||||
}
|
||||
|
||||
if got := PermisosDesdeClaims(claims); !reflect.DeepEqual(got, []uint{1, 2, 4}) {
|
||||
t.Fatalf("permisos esperados [1 2 4], obtenidos %v", got)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user