Skip to content

Commit 02022d7

Browse files
authored
Stop impersonation call (#513)
related to descope/etc#9294 + tests
1 parent 0a4e2d9 commit 02022d7

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,16 @@ const updatedJWTRes = await descopeClient.management.jwt.impersonate(
11351135
);
11361136
```
11371137

1138+
Once impersonation is done, you can call `stopImpersonation`, and get back a jwt of hte the actor
1139+
1140+
```typescript
1141+
const updatedJWTRes = await descopeClient.management.jwt.impersonate(
1142+
'<jwt string>',
1143+
{ k1: 'v1' },
1144+
't1',
1145+
);
1146+
```
1147+
11381148
Note 1: The generate code/link functions, work only for test users, will not work for regular users.
11391149
Note 2: In case of testing sign-in / sign-up operations with test users, need to make sure to generate the code prior calling the sign-in / sign-up operations.
11401150

lib/management/jwt.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,45 @@ describe('Management JWT', () => {
9292
});
9393
});
9494

95+
describe('stopImpersonation', () => {
96+
it('should send the correct request and receive correct response', async () => {
97+
const httpResponse = {
98+
ok: true,
99+
json: () => mockJWTResponse,
100+
clone: () => ({
101+
json: () => Promise.resolve(mockJWTResponse),
102+
}),
103+
status: 200,
104+
};
105+
mockHttpClient.post.mockResolvedValue(httpResponse);
106+
107+
const resp: SdkResponse<UpdateJWTResponse> = await management.jwt.stopImpersonation(
108+
'jwt',
109+
{ k1: 'v1' },
110+
't1',
111+
32,
112+
);
113+
114+
expect(mockHttpClient.post).toHaveBeenCalledWith(
115+
apiPaths.jwt.stopImpersonation,
116+
{
117+
jwt: 'jwt',
118+
customClaims: { k1: 'v1' },
119+
selectedTenant: 't1',
120+
refreshDuration: 32,
121+
},
122+
{ token: 'key' },
123+
);
124+
125+
expect(resp).toEqual({
126+
code: 200,
127+
data: mockJWTResponse,
128+
ok: true,
129+
response: httpResponse,
130+
});
131+
});
132+
});
133+
95134
describe('sign-in', () => {
96135
it('should send the correct request and receive correct response', async () => {
97136
const httpResponse = {

lib/management/jwt.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ const withJWT = (sdk: CoreSdk, managementKey?: string) => ({
3333
{ token: managementKey },
3434
),
3535
),
36+
stopImpersonation: (
37+
jwt: string,
38+
customClaims?: Record<string, any>,
39+
selectedTenant?: string,
40+
refreshDuration?: number,
41+
): Promise<SdkResponse<UpdateJWTResponse>> =>
42+
transformResponse(
43+
sdk.httpClient.post(
44+
apiPaths.jwt.stopImpersonation,
45+
{ jwt, customClaims, selectedTenant, refreshDuration },
46+
{ token: managementKey },
47+
),
48+
),
3649
signIn: (loginId: string, loginOptions?: MgmtLoginOptions): Promise<SdkResponse<JWTResponse>> =>
3750
transformResponse(
3851
sdk.httpClient.post(

lib/management/paths.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default {
9595
jwt: {
9696
update: '/v1/mgmt/jwt/update',
9797
impersonate: '/v1/mgmt/impersonate',
98+
stopImpersonation: '/v1/mgmt/stop/impersonation',
9899
signIn: '/v1/mgmt/auth/signin',
99100
signUp: '/v1/mgmt/auth/signup',
100101
signUpOrIn: '/v1/mgmt/auth/signup-in',

0 commit comments

Comments
 (0)