Skip to content

Commit 2859c37

Browse files
committed
chore(types,shared): Added tests and did some cleanup
1 parent 14d61f8 commit 2859c37

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

packages/backend/src/tokens/authObjects.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ export function signedInAuthObject(
111111
sessionToken,
112112
fetcher: async (...args) => (await apiClient.sessions.getToken(...args)).jwt,
113113
});
114-
115114
return {
116115
actor,
117116
sessionClaims,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { __experimental_resolveSignedInAuthStateFromJWTClaims as resolveSignedInAuthStateFromJWTClaims } from '../authorization';
2+
3+
describe('resolveSignedInAuthStateFromJWTClaims', () => {
4+
const baseClaims = {
5+
exp: 1234567890,
6+
iat: 1234567890,
7+
iss: 'https://api.clerk.com',
8+
sub: 'sub',
9+
sid: 'sid',
10+
azp: 'azp',
11+
nbf: 1234567890,
12+
__raw: '',
13+
};
14+
15+
test('produced auth object with v2 matches v1', () => {
16+
const { sessionClaims: v2Claims, ...signedInAuthObjectV2 } = resolveSignedInAuthStateFromJWTClaims({
17+
...baseClaims,
18+
v: 2,
19+
org: {
20+
id: 'org_id',
21+
rol: 'admin',
22+
slg: 'org_slug',
23+
per: ['permission1', 'permission2'],
24+
},
25+
});
26+
27+
const { sessionClaims: v1Claims, ...signedInAuthObjectV1 } = resolveSignedInAuthStateFromJWTClaims({
28+
...baseClaims,
29+
org_id: 'org_id',
30+
org_role: 'admin',
31+
org_slug: 'org_slug',
32+
org_permissions: ['permission1', 'permission2'],
33+
v: undefined,
34+
});
35+
expect(signedInAuthObjectV1).toMatchObject(signedInAuthObjectV2);
36+
});
37+
38+
test('produced auth object with v2 matches v1 without having orgs', () => {
39+
const { sessionClaims: v2Claims, ...signedInAuthObjectV2 } = resolveSignedInAuthStateFromJWTClaims({
40+
...baseClaims,
41+
v: 2,
42+
});
43+
44+
const { sessionClaims: v1Claims, ...signedInAuthObjectV1 } = resolveSignedInAuthStateFromJWTClaims({
45+
...baseClaims,
46+
});
47+
expect(signedInAuthObjectV1).toMatchObject(signedInAuthObjectV2);
48+
});
49+
});

packages/shared/src/authorization.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ const __experimental_resolveSignedInAuthStateFromJWTClaims = (claims: JwtPayload
317317
orgRole = claims.org?.rol;
318318
orgSlug = claims.org?.slg;
319319

320-
// TODO(jwt-v2): when JWT version 2 is available, do proper handling for org permissions
321-
orgPermissions = (claims?.org_permissions as string[] | undefined) ?? undefined;
320+
orgPermissions = claims.org?.per;
322321
break;
323322
default:
324323
orgId = claims.org_id;

packages/types/src/jwtv2.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type JWTPayloadBase = {
4343
*
4444
* The version of the JWT payload.
4545
*/
46-
ver: number | undefined;
46+
v?: number | undefined;
4747

4848
/**
4949
* Encoded token supporting the `getRawString` method.
@@ -110,12 +110,7 @@ type JWTPayloadBase = {
110110

111111
export type VersionedJwtPayload =
112112
| {
113-
/**
114-
* @experimental
115-
*
116-
* The version of the JWT payload.
117-
*/
118-
v?: never;
113+
v?: undefined;
119114

120115
/**
121116
*
@@ -139,13 +134,7 @@ export type VersionedJwtPayload =
139134
org_role?: OrganizationCustomRoleKey;
140135
}
141136
| {
142-
/**
143-
* @experimental
144-
*
145-
* The version of the JWT payload.
146-
*/
147137
v: 2;
148-
149138
/**
150139
* @experimental - This structure is subject to change.
151140
*
@@ -166,6 +155,12 @@ export type VersionedJwtPayload =
166155
* Active organization role.
167156
*/
168157
rol?: OrganizationCustomRoleKey;
158+
159+
/**
160+
*
161+
* Active organization permissions.
162+
*/
163+
per?: OrganizationCustomPermissionKey[];
169164
};
170165
};
171166

0 commit comments

Comments
 (0)