How do you get tenant name in JWT on server side?
# ask-a-descoper
s
How do you get tenant name in JWT on server side?
āœ… 1
d
Hi, I'm Descope's AI assistant. I've put together an initial response below based on your question. A member of the support team will take over and follow up shortly. Getting the Tenant Name from a JWT To get the tenant name from a JWT on the server side, you need to validate the session and extract tenant information from the JWT response. The JWT contains tenant information in the
tenants
claim, which includes tenant IDs and their associated roles/permissions. However, to get the actual tenant name (not just ID), you'll need to use the management API to load tenant details. Node.js Example
Copy code
// First validate the session to get JWT claims
const authInfo = await descopeClient.validateSession(sessionToken);
// Get tenant ID from the dct (Descope Current Tenant) claim
const currentTenantId = authInfo.token.dct;
// Load tenant details to get the name
const tenant = await descopeClient.management.tenant.load(currentTenantId);
const tenantName = tenant.name;
Python Example
Copy code
# Validate session
jwt_response = descope_client.validate_session(session_token)
# Get current tenant ID
current_tenant_id = jwt_response.get('dct')
# Load tenant to get name
tenant = descope_client.mgmt.tenant.load(current_tenant_id)
tenant_name = tenant['name']
Go Example
Copy code
// Validate session
authInfo, err := descopeClient.Auth.ValidateSessionWithToken(context.Background(), sessionToken)
// Get current tenant ID from dct claim
currentTenantID := authInfo.Token["dct"].(string)
// Load tenant details
tenant, err := descopeClient.Management.Tenant().Load(context.Background(), currentTenantID)
tenantName := tenant.Name
JWT Structure The JWT structure includes: • dct (Descope Current Tenant): The active tenant ID • tenants: Object containing all tenant IDs with their roles and permissions To get the tenant name, you must use the management API since the JWT only contains tenant IDs, not names.
šŸ‘Ž 1
s
I thought it was discouraged to use the management API frequently?
I don't get why the session in client side has the tenant name but not in the jwt..
c
You should use a jwt template with something like tenant.name as the dynamic value https://docs.descope.com/management/jwt-templates @Atharva Joshi can you share an example?
šŸ‘ 1
s
I tried doing that but the issue was it will only set the current tenant the user is set to. But not all the tenant's name the user is part of
a
Hey @Shaumik Mondal, To acheive this you can add a scriptlet and a custom claims action to add the tenant Names as shown in the images.
šŸ™ 1
āž• 1
s
Thank you! I will try this
šŸ™Œ 1