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]
|
||||
|
||||
Reference in new issue
Block a user