-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtenant.ts
69 lines (64 loc) · 1.74 KB
/
tenant.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
checkMandatoryValue,
DecodedJWT,
JwtKeyMapping,
readPropertyWithWarn
} from './jwt';
/**
* Mapping between key name in the Tenant and key name in decoded JWT and the
*/
export const mappingTenantFields: JwtKeyMapping<
Tenant,
RegisteredJWTClaimsTenant
> = {
id: { keyInJwt: 'zid', extractorFunction: tenantId },
name: { keyInJwt: 'zdn', extractorFunction: tenantName }
};
/**
* Get the tenant id of a decoded JWT.
* @param decodedToken - Token to read the tenant id from.
* @returns The tenant id if available.
*/
export function tenantId(decodedToken: DecodedJWT): string | undefined {
return readPropertyWithWarn(decodedToken, mappingTenantFields.id.keyInJwt);
}
/**
* Get the tenant name of a decoded JWT.
* @param decodedToken - Token to read the tenant id from.
* @returns The tenant id if available.
*/
export function tenantName(decodedToken: DecodedJWT): string | undefined {
const extAttr = readPropertyWithWarn(decodedToken, 'ext_attr');
if (extAttr) {
return readPropertyWithWarn(extAttr, 'zdn');
}
return undefined;
}
/**
* Keys in the JWT related to the user
*/
export interface RegisteredJWTClaimsTenant {
zid?: string;
zdn?: string;
}
/**
* Representation of the tenant. A tenant represents the customer account on cloud foundry.
*/
export interface Tenant {
id: string;
name?: string;
}
/**
* Creates a tenant object from the decoded JWT.
*
* @param decodedJWT - Decoded JWT token
* @returns Representation of the tenant.
* @exception Error Raised if no id is found in the decoded JWT.
*/
export function tenantFromJwt(decodedJWT: DecodedJWT): Tenant {
checkMandatoryValue('id', mappingTenantFields, decodedJWT);
return {
id: tenantId(decodedJWT)!,
name: tenantName(decodedJWT)
};
}