Skip to main content

JWT Claims & Profile Types

Data: 2026-07-07
Fonte: Análise de código coezzion-service-auth


1. Estrutura do JWT

Arquivo: src/Auth.API/Application/Services/Implementations/TokenService.cs:53-58

var claims = new List<Claim>
{
new("schema", authData.Schema),
new("profile", profileId.ToString()),
new(JwtRegisteredClaimNames.UniqueName, userName), // "unique_name"
new(JwtRegisteredClaimNames.Sub, userId.ToString()) // "sub"
};
ClaimValorExemplo
schemaSchema da organização"arezzo"
profileProfileId (int as string)"23"
unique_nameNome de usuário"joao.silva"
subUserId (int as string)"1542"

NÃO existe claim cpf. O CPF é retornado no response body (UserToken.Document), não no JWT.

2. TokenValidationParameters

Arquivo: coezzion-nuget-authentication/src/Coezzion.Authentication/Configuration/AuthenticationConfig.cs:20-30

options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
// ...
};
  • ValidateIssuer = false — NÃO valida issuer
  • ValidateAudience = false — NÃO valida audience
  • ValidateIssuerSigningKey = true — assinatura HMAC é validada (protege contra tampering)
  • Sem NameClaimType customizado → usa defaults do ASP.NET Core

3. Como obter CPF do usuário autenticado

Usa-se IUserProvider do nuget Coezzion.Common.Providers (registrado via DI):

Interface: coezzion-nuget-common/src/Coezzion.Common/Providers/IUserProvider.cs

public interface IUserProvider
{
int GetUserId(); // ClaimTypes.NameIdentifier → sub (parse automático)
string GetSchemaName();
string GetProfile(); // claim "profile" → ProfileId
}

Uso no controller:

var userId = _userProvider.GetUserId(); // int, sem parse manual
var profileStr = _userProvider.GetProfile(); // string, ex: "23"

var user = await _userRepository.GetByIdAsync(userId); // UserModel + Profile
var cpf = user.Cpf;
var profileType = (ProfileType)int.Parse(profileStr);

4. Profile Types (Enum)

Arquivo: coezzion-nuget-common/src/Coezzion.Common/Enums/ProfileType.cs

IdEnumNome
1Coezzion_AdministradorAdministrador Portal
2Coezzion_UsuarioCoezzion Usuário
3EasyEasy
4ViewersVisualizadores
7PickUp_And_DeliveryRetire e Entrega
8CrmCRM
9MerchanMerchan
10FranchiseeFranqueado
11Business_ConsultantConsultor Comercial
12DemonstrationDemonstração
13Commercial_ManagerGerente Comercial
20AdministradorAdministrador
21CoordenadorCoordenador
22GerenteGerente
23VendedorVendedor
24Gerente_TelevendasGerente Televendas
25Vendedor_TelevendasVendedor(a) Televendas

Perfis com bypass Admin: Coezzion_Administrador (1) e Administrador (20)

5. UserModel.Profile (EF Navigation)

Arquivo: coezzion-db-core/src/Core.DB/Entities/UserModel.cs:62-63

public int ProfileId { get; set; }
public ProfileModel Profile { get; set; }

GetByIdAsync já inclui .Include(x => x.Profile) + .Include(x => x.SystemPermissions) + .Include(x => x.Organizations).

6. Padrão existente de permissões por perfil

Arquivo: src/Auth.API/Application/Services/Implementations/AuthService.cs:175

ProfileType profileType = (ProfileType)user.ProfileId;
if (profileType != ProfileType.Coezzion_Administrador &&
profileType != ProfileType.Coezzion_Usuario &&
profileType != ProfileType.Easy &&
profileType != ProfileType.Viewers)
{
validate.AddError("O usuário informado não pode alterar a companhia");
}

Perfis 1-4 são "Coezzion support" — podem trocar de schema/organização. Para o bypass do fix #02, usamos apenas 1 e 20 (mais restritivo).