-
Notifications
You must be signed in to change notification settings - Fork 335
/
Copy pathBase.test.ts
243 lines (216 loc) · 10.6 KB
/
Base.test.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { Base } from '../Base';
import { AuthErrorReason, AuthStateParams, AuthStatus } from '../types';
import { TokenVerificationError } from '../util/errors';
const mockCrossOrigin = jest.fn();
const mockFetchInterstitial = jest.fn();
const mockIsDevelopmentOrStaging = jest.fn();
const mockIsProduction = jest.fn();
jest.mock('../util/jwt', () => ({
checkClaims: jest.fn(),
}));
jest.mock('../util/request', () => ({
checkCrossOrigin: () => mockCrossOrigin(),
}));
jest.mock('../util/clerkApiKey', () => ({
isDevelopmentOrStaging: () => mockIsDevelopmentOrStaging(),
isProduction: () => mockIsProduction(),
}));
const environmentJWTKey = 'CLERK_JWT_KEY';
const mockInterstitialValue = '';
/* An otherwise bare state on a request. */
const defaultAuthState: AuthStateParams = {
host: 'clerk.dev',
fetchInterstitial: () => mockFetchInterstitial(),
userAgent: 'Mozilla/',
};
const mockImportKey = jest.fn();
const mockVerifySignature = jest.fn();
const mockBase64Decode = jest.fn();
const mockLoadCryptoKeyFunction = jest.fn();
describe('Base verifySessionToken', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('uses supplied crypto key function by default when present', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
testBase.verifyJwt = jest.fn();
testBase.loadCryptoKey = jest.fn();
await testBase.verifySessionToken('1');
expect(testBase.verifyJwt).toHaveBeenCalledTimes(1);
expect(testBase.loadCryptoKey).not.toHaveBeenCalled();
expect(mockLoadCryptoKeyFunction).toHaveBeenCalledTimes(1);
});
it('throws on failed public key fetching', () => {
mockLoadCryptoKeyFunction.mockImplementationOnce(() => {
throw new Error();
});
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
testBase.verifyJwt = jest.fn();
testBase.loadCryptoKey = jest.fn();
expect(async () => await testBase.verifySessionToken('1')).rejects.toThrow(TokenVerificationError);
expect(testBase.verifyJwt).not.toHaveBeenCalled();
expect(testBase.loadCryptoKey).not.toHaveBeenCalled();
expect(mockLoadCryptoKeyFunction).toHaveBeenCalledTimes(1);
});
it('uses supplied jwtKey versus loadCryptoKey or CLERK_JWT_KEY', async () => {
jest.resetModules();
process.env.CLERK_JWT_KEY = environmentJWTKey;
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
testBase.verifyJwt = jest.fn();
testBase.loadCryptoKey = jest.fn();
await testBase.verifySessionToken('1', { jwtKey: 'test_jwt_key' });
expect(testBase.verifyJwt).toHaveBeenCalledTimes(1);
expect(testBase.loadCryptoKey).toHaveBeenCalledTimes(1);
expect(mockLoadCryptoKeyFunction).not.toHaveBeenCalled();
expect(testBase.loadCryptoKey).toHaveBeenCalledWith('test_jwt_key');
});
it('uses environment variable when loadCryptoKey function is not present', async () => {
const mockImportKey = jest.fn();
const mockVerifySignature = jest.fn();
const mockBase64Decode = jest.fn();
jest.resetModules();
process.env.CLERK_JWT_KEY = environmentJWTKey;
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode);
testBase.verifyJwt = jest.fn();
testBase.loadCryptoKey = jest.fn();
await testBase.verifySessionToken('1');
expect(testBase.verifyJwt).toHaveBeenCalledTimes(1);
expect(testBase.loadCryptoKey).toHaveBeenCalledTimes(1);
expect(testBase.loadCryptoKey).toHaveBeenCalledWith(environmentJWTKey);
});
});
describe('Base getAuthState', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('uses header token if present to build the state', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
testBase.buildAuthenticatedState = jest.fn();
await testBase.getAuthState({ ...defaultAuthState, headerToken: 'test_header_token' });
expect(testBase.buildAuthenticatedState).toHaveBeenCalledTimes(1);
});
it('returns correct errorReason on failed pk fetching', async () => {
mockLoadCryptoKeyFunction.mockImplementationOnce(() => {
throw new Error();
});
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
const authStateResult = await testBase.getAuthState({ ...defaultAuthState, headerToken: 'test_header_token' });
expect(authStateResult).toEqual({
status: AuthStatus.SignedOut,
errorReason: AuthErrorReason.PublicKeyFetchError,
});
});
it('returns signed out on development non browser requests', async () => {
const nonBrowserUserAgent = 'curl';
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
testBase.buildAuthenticatedState = jest.fn();
mockIsDevelopmentOrStaging.mockImplementationOnce(() => true);
const authStateResult = await testBase.getAuthState({
...defaultAuthState,
userAgent: nonBrowserUserAgent,
clientUat: '12345',
});
expect(testBase.buildAuthenticatedState).toHaveBeenCalledTimes(0);
expect(authStateResult).toEqual({
status: AuthStatus.SignedOut,
errorReason: AuthErrorReason.HeaderMissingNonBrowser,
});
});
it('returns signed out when no header token is present in cross-origin request', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
mockCrossOrigin.mockImplementationOnce(() => true);
const authStateResult = await testBase.getAuthState({ ...defaultAuthState, origin: 'https://clerk.dev' });
expect(mockCrossOrigin).toHaveBeenCalledTimes(1);
expect(authStateResult).toEqual({ status: AuthStatus.SignedOut, errorReason: AuthErrorReason.HeaderMissingCORS });
});
it('returns interstitial when in development we find no clientUat', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
mockFetchInterstitial.mockImplementationOnce(() => Promise.resolve(mockInterstitialValue));
mockIsDevelopmentOrStaging.mockImplementationOnce(() => true);
const authStateResult = await testBase.getAuthState({
...defaultAuthState,
fetchInterstitial: mockFetchInterstitial,
});
expect(mockFetchInterstitial).toHaveBeenCalledTimes(1);
expect(authStateResult).toEqual({
status: AuthStatus.Interstitial,
interstitial: mockInterstitialValue,
errorReason: AuthErrorReason.UATMissing,
});
});
it('returns signed out on first production load without cookieToken and clientUat', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
mockIsProduction.mockImplementationOnce(() => true);
const authStateResult = await testBase.getAuthState({ ...defaultAuthState });
expect(authStateResult).toEqual({ status: AuthStatus.SignedOut, errorReason: AuthErrorReason.CookieAndUATMissing });
});
it('returns interstitial on development after auth action on Clerk-hosted UIs', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
mockFetchInterstitial.mockImplementationOnce(() => Promise.resolve(mockInterstitialValue));
mockIsDevelopmentOrStaging.mockImplementationOnce(() => true);
mockCrossOrigin.mockImplementationOnce(() => true);
const authStateResult = await testBase.getAuthState({
...defaultAuthState,
fetchInterstitial: mockFetchInterstitial,
referrer: 'https://random.clerk.hosted.ui',
clientUat: '12345',
});
expect(mockFetchInterstitial).toHaveBeenCalledTimes(1);
expect(authStateResult).toEqual({
status: AuthStatus.Interstitial,
interstitial: mockInterstitialValue,
errorReason: AuthErrorReason.CrossOriginReferrer,
});
});
it('returns signed out on clientUat signaled as 0', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
mockIsProduction.mockImplementationOnce(() => true);
const authStateResult = await testBase.getAuthState({ ...defaultAuthState, clientUat: '0' });
expect(authStateResult).toEqual({ status: AuthStatus.SignedOut, errorReason: AuthErrorReason.StandardSignedOut });
});
it('returns interstitial when on production and have a valid clientUat value without cookieToken', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
mockIsProduction.mockImplementationOnce(() => true);
mockFetchInterstitial.mockImplementationOnce(() => Promise.resolve(mockInterstitialValue));
const authStateResult = await testBase.getAuthState({
...defaultAuthState,
clientUat: '12345',
fetchInterstitial: mockFetchInterstitial,
});
expect(mockFetchInterstitial).toHaveBeenCalledTimes(1);
expect(authStateResult).toEqual({
status: AuthStatus.Interstitial,
interstitial: mockInterstitialValue,
errorReason: AuthErrorReason.CookieMissing,
});
});
it('returns sessionClaims cookieToken is available and clientUat is valid', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
const validJwtToken = { sessionClaims: { iat: Number(new Date()) + 50 } };
testBase.buildAuthenticatedState = jest.fn().mockImplementationOnce(() => validJwtToken);
const authStateResult = await testBase.getAuthState({
...defaultAuthState,
clientUat: String(new Date().getTime()),
cookieToken: 'valid_token',
});
expect(authStateResult).toEqual(validJwtToken);
});
it('returns interstitial cookieToken is valid but token iat is in past date', async () => {
const testBase = new Base(mockImportKey, mockVerifySignature, mockBase64Decode, mockLoadCryptoKeyFunction);
const validJwtToken = { sessionClaims: { iat: Number(new Date()) - 50 } };
testBase.buildAuthenticatedState = jest.fn().mockImplementationOnce(() => validJwtToken);
mockFetchInterstitial.mockImplementationOnce(() => Promise.resolve(mockInterstitialValue));
const authStateResult = await testBase.getAuthState({
...defaultAuthState,
clientUat: String(new Date().getTime()),
cookieToken: 'valid_token',
fetchInterstitial: mockFetchInterstitial,
});
expect(mockFetchInterstitial).toHaveBeenCalledTimes(1);
expect(authStateResult).toEqual({
status: AuthStatus.Interstitial,
interstitial: mockInterstitialValue,
errorReason: AuthErrorReason.CookieOutDated,
});
});
});