Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Commit c6ec4b1

Browse files
authored
Add session and refresh token expiration utilities (#71)
1 parent e3b2979 commit c6ec4b1

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ You can also use the following functions to assist with various actions managing
212212
`getSessionToken()` - Get current session token.
213213
`getRefreshToken()` - Get current refresh token.
214214
`refresh(token = getRefreshToken())` - Force a refresh on current session token using an existing valid refresh token.
215+
`isSessionTokenExpired(token = getSessionToken())` - Check whether the current session token is expired. Provide a session token if is not persisted.
216+
`isRefreshTokenExpired(token = getRefreshToken())` - Check whether the current refresh token is expired. Provide a refresh token if is not persisted.
215217
`getJwtRoles(token = getSessionToken(), tenant = '')` - Get current roles from an existing session token. Provide tenant id for specific tenant roles.
216218
`getJwtPermissions(token = getSessionToken(), tenant = '')` - Fet current permissions from an existing session token. Provide tenant id for specific tenant permissions.
217219

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ export {
88
getJwtPermissions,
99
getJwtRoles,
1010
getRefreshToken,
11-
getSessionToken
11+
getSessionToken,
12+
isSessionTokenExpired,
13+
isRefreshTokenExpired
1214
} from './sdk';

src/sdk.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ export const getRefreshToken = () => {
4646
return '';
4747
};
4848

49+
export const isSessionTokenExpired = (token = getSessionToken()) =>
50+
globalSdk?.isJwtExpired(token);
51+
52+
export const isRefreshTokenExpired = (token = getRefreshToken()) =>
53+
globalSdk?.isJwtExpired(token);
54+
4955
export const getJwtPermissions = wrapInTry(
5056
(token = getSessionToken(), tenant?: string) =>
5157
globalSdk?.getJwtPermissions(token, tenant)

tests/utilityFunctions.test.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import createSdk, {
2+
refresh,
3+
getJwtPermissions,
4+
getJwtRoles,
5+
getRefreshToken,
6+
getSessionToken,
7+
isSessionTokenExpired,
8+
isRefreshTokenExpired
9+
} from '../src/sdk';
10+
11+
jest.mock('@descope/web-js-sdk', () => () => ({
12+
getSessionToken: jest.fn(),
13+
getRefreshToken: jest.fn(),
14+
isJwtExpired: jest.fn(),
15+
getJwtPermissions: jest.fn(),
16+
getJwtRoles: jest.fn(),
17+
refresh: jest.fn()
18+
}));
19+
20+
const sdk = createSdk({ projectId: 'test' });
21+
22+
describe('utility functions', () => {
23+
it('should call getSessionToken from sdk', () => {
24+
getSessionToken();
25+
expect(sdk.getSessionToken).toHaveBeenCalled();
26+
});
27+
28+
it('should warn when using getSessionToken in non browser environment', () => {
29+
const warnSpy = jest.spyOn(console, 'warn');
30+
31+
const origWindow = window;
32+
Object.defineProperty(global, 'window', {
33+
value: undefined,
34+
writable: true,
35+
configurable: true
36+
});
37+
38+
jest.resetModules();
39+
40+
// eslint-disable-next-line @typescript-eslint/no-var-requires
41+
const { getSessionToken: getSessionTokenLocal } = require('../src/sdk');
42+
43+
getSessionTokenLocal();
44+
45+
global.window = origWindow;
46+
jest.resetModules();
47+
48+
expect(warnSpy).toHaveBeenCalledWith(
49+
'Get session token is not supported in SSR'
50+
);
51+
expect(sdk.getSessionToken).not.toHaveBeenCalled();
52+
});
53+
54+
it('should call getRefreshToken from sdk', () => {
55+
getRefreshToken();
56+
expect(sdk.getRefreshToken).toHaveBeenCalled();
57+
});
58+
59+
it('should warn when using getRefreshToken in non browser environment', () => {
60+
const warnSpy = jest.spyOn(console, 'warn');
61+
62+
const origWindow = window;
63+
Object.defineProperty(global, 'window', {
64+
value: undefined,
65+
writable: true,
66+
configurable: true
67+
});
68+
69+
jest.resetModules();
70+
71+
// eslint-disable-next-line @typescript-eslint/no-var-requires
72+
const { getRefreshToken: getRefreshTokenLocal } = require('../src/sdk');
73+
74+
getRefreshTokenLocal();
75+
76+
global.window = origWindow;
77+
jest.resetModules();
78+
79+
expect(warnSpy).toHaveBeenCalledWith(
80+
'Get refresh token is not supported in SSR'
81+
);
82+
expect(sdk.getRefreshToken).not.toHaveBeenCalled();
83+
});
84+
85+
it('should call refresh token with the session token', async () => {
86+
(sdk.refresh as jest.Mock).getMockImplementation();
87+
await refresh('test');
88+
expect(sdk.refresh).toHaveBeenCalledWith('test');
89+
});
90+
91+
it('should call getJwtPermissions with the session token when not provided', () => {
92+
(sdk.getSessionToken as jest.Mock).mockReturnValueOnce('session');
93+
getJwtPermissions();
94+
expect(sdk.getJwtPermissions).toHaveBeenCalledWith('session', undefined);
95+
});
96+
97+
it('should call isSessionJwtExpired with the session token when not provided', () => {
98+
(sdk.getSessionToken as jest.Mock).mockReturnValueOnce('session');
99+
jest.spyOn(sdk, 'isJwtExpired').mockReturnValueOnce(false);
100+
isSessionTokenExpired();
101+
expect(sdk.isJwtExpired).toHaveBeenCalledWith('session');
102+
});
103+
104+
it('should call isRefreshJwtExpired with the refresh token when not provided', () => {
105+
(sdk.getRefreshToken as jest.Mock).mockReturnValueOnce('refresh');
106+
jest.spyOn(sdk, 'isJwtExpired').mockReturnValueOnce(false);
107+
isRefreshTokenExpired();
108+
expect(sdk.isJwtExpired).toHaveBeenCalledWith('refresh');
109+
});
110+
111+
it('should call getJwtRoles with the session token when not provided', () => {
112+
(sdk.getSessionToken as jest.Mock).mockReturnValueOnce('session');
113+
jest.spyOn(sdk, 'getJwtRoles').mockReturnValueOnce([]);
114+
getJwtRoles();
115+
expect(sdk.getJwtRoles).toHaveBeenCalledWith('session', undefined);
116+
});
117+
118+
it('should call getJwtRoles with the session token when not provided', () => {
119+
jest.spyOn(console, 'error').mockImplementation(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function
120+
jest.spyOn(sdk, 'getJwtRoles').mockImplementation(() => {
121+
throw new Error('session token');
122+
});
123+
getJwtRoles();
124+
expect(console.error).toHaveBeenCalled(); // eslint-disable-line no-console
125+
});
126+
});

0 commit comments

Comments
 (0)