piral-oidc 1.3.3-beta.6187 → 1.3.3-beta.6201
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/create.test.ts +7 -3
- package/src/setup.test.ts +258 -241
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-oidc",
|
|
3
|
-
"version": "1.3.3-beta.
|
|
3
|
+
"version": "1.3.3-beta.6201",
|
|
4
4
|
"description": "Plugin to integrate authentication using OpenID connect in Piral.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"piral",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"oidc-client": "^1.10.1"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
|
-
"piral-core": "1.3.3-beta.
|
|
69
|
+
"piral-core": "1.3.3-beta.6201"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "6e9f4e6f83514b5c38960ce015e073efc668f3d6"
|
|
72
72
|
}
|
package/src/create.test.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect, vitest, beforeEach } from 'vitest';
|
|
1
5
|
import { createOidcApi } from './create';
|
|
2
6
|
import { PiletOidcApi } from './types';
|
|
3
7
|
|
|
@@ -19,12 +23,12 @@ describe('Piral-Oidc create module', () => {
|
|
|
19
23
|
|
|
20
24
|
beforeEach(() => {
|
|
21
25
|
context = {
|
|
22
|
-
on:
|
|
26
|
+
on: vitest.fn(),
|
|
23
27
|
};
|
|
24
28
|
|
|
25
29
|
mock = {
|
|
26
|
-
token:
|
|
27
|
-
account:
|
|
30
|
+
token: vitest.fn(() => mockToken),
|
|
31
|
+
account: vitest.fn(() => mockProfile),
|
|
28
32
|
};
|
|
29
33
|
});
|
|
30
34
|
|
package/src/setup.test.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect, vitest, beforeAll, beforeEach, afterAll } from 'vitest';
|
|
1
5
|
import { UserManager } from 'oidc-client';
|
|
2
6
|
import { setupOidcClient } from './setup';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
jest.mock('oidc-client');
|
|
7
|
+
import { OidcConfig, OidcErrorType } from './types';
|
|
6
8
|
|
|
7
9
|
describe('Piral-Oidc setup module', () => {
|
|
8
10
|
const setWindowInIFrame = () =>
|
|
@@ -11,57 +13,45 @@ describe('Piral-Oidc setup module', () => {
|
|
|
11
13
|
Object.defineProperty(window, 'top', { value: window, configurable: true, writable: true });
|
|
12
14
|
const originalWindowLocation = window.location;
|
|
13
15
|
let oidcConfig: OidcConfig;
|
|
14
|
-
const MockUserManager = UserManager as jest.MockedClass<typeof UserManager>;
|
|
15
16
|
const user = {
|
|
16
17
|
access_token: '123',
|
|
17
18
|
expires_in: 100,
|
|
18
19
|
};
|
|
19
|
-
const mockGetUser =
|
|
20
|
-
const mockSigninCallback =
|
|
21
|
-
const mockSigninRedirectCallback =
|
|
22
|
-
const mockSigninRedirect =
|
|
23
|
-
const mockSigninSilent =
|
|
24
|
-
const mockSigninSilentCallback =
|
|
25
|
-
const mockSignoutRedirect =
|
|
26
|
-
const mockSignoutRedirectCallback =
|
|
27
|
-
const mockSignoutPopupCallback =
|
|
20
|
+
const mockGetUser = vitest.fn().mockResolvedValue(user).mockName('getUser');
|
|
21
|
+
const mockSigninCallback = vitest.fn().mockResolvedValue(undefined).mockName('signinCallback');
|
|
22
|
+
const mockSigninRedirectCallback = vitest.fn().mockResolvedValue(undefined).mockName('signinRedirectCallback');
|
|
23
|
+
const mockSigninRedirect = vitest.fn().mockResolvedValue(undefined).mockName('signinRedirect');
|
|
24
|
+
const mockSigninSilent = vitest.fn().mockResolvedValue(undefined).mockName('signinSilent');
|
|
25
|
+
const mockSigninSilentCallback = vitest.fn().mockResolvedValue(undefined).mockName('signinSilentCallback');
|
|
26
|
+
const mockSignoutRedirect = vitest.fn().mockResolvedValue(undefined).mockName('signoutRedirect');
|
|
27
|
+
const mockSignoutRedirectCallback = vitest.fn().mockResolvedValue(undefined).mockName('signoutRedirectCallback');
|
|
28
|
+
const mockSignoutPopupCallback = vitest.fn().mockResolvedValue(undefined).mockName('signoutPopupCallback');
|
|
28
29
|
|
|
29
30
|
const postLogoutRedirectUri = 'http://localhost:8000/post-logout';
|
|
30
31
|
const redirectUri = 'http://localhost:8000/callback';
|
|
31
32
|
const appUri = 'http://localhost:8000/app';
|
|
32
33
|
|
|
33
34
|
beforeAll(() => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
},
|
|
44
|
-
signinCallback: mockSigninCallback,
|
|
45
|
-
signinRedirectCallback: mockSigninRedirectCallback,
|
|
46
|
-
signinRedirect: mockSigninRedirect,
|
|
47
|
-
signinSilent: mockSigninSilent,
|
|
48
|
-
signinSilentCallback: mockSigninSilentCallback,
|
|
49
|
-
signoutRedirect: mockSignoutRedirect,
|
|
50
|
-
signoutRedirectCallback: mockSignoutRedirectCallback,
|
|
51
|
-
signoutPopupCallback: mockSignoutPopupCallback,
|
|
52
|
-
};
|
|
53
|
-
});
|
|
35
|
+
vitest.spyOn(UserManager.prototype, 'getUser').mockImplementation(mockGetUser);
|
|
36
|
+
vitest.spyOn(UserManager.prototype, 'signinCallback').mockImplementation(mockSigninCallback);
|
|
37
|
+
vitest.spyOn(UserManager.prototype, 'signinRedirectCallback').mockImplementation(mockSigninRedirectCallback);
|
|
38
|
+
vitest.spyOn(UserManager.prototype, 'signinRedirect').mockImplementation(mockSigninRedirect);
|
|
39
|
+
vitest.spyOn(UserManager.prototype, 'signinSilent').mockImplementation(mockSigninSilent);
|
|
40
|
+
vitest.spyOn(UserManager.prototype, 'signinSilentCallback').mockImplementation(mockSigninSilentCallback);
|
|
41
|
+
vitest.spyOn(UserManager.prototype, 'signoutRedirect').mockImplementation(mockSignoutRedirect);
|
|
42
|
+
vitest.spyOn(UserManager.prototype, 'signoutRedirectCallback').mockImplementation(mockSignoutRedirectCallback);
|
|
43
|
+
vitest.spyOn(UserManager.prototype, 'signoutPopupCallback').mockImplementation(mockSignoutPopupCallback);
|
|
54
44
|
});
|
|
55
45
|
|
|
56
46
|
afterAll(() => {
|
|
57
|
-
jest.unmock('oidc-client');
|
|
58
47
|
window.location = originalWindowLocation;
|
|
59
48
|
setWindowInTop();
|
|
60
49
|
});
|
|
61
50
|
|
|
62
51
|
beforeEach(() => {
|
|
63
|
-
|
|
52
|
+
vitest.clearAllMocks();
|
|
64
53
|
mockGetUser.mockResolvedValue(user);
|
|
54
|
+
//@ts-ignore
|
|
65
55
|
delete window.location;
|
|
66
56
|
//@ts-ignore
|
|
67
57
|
window.location = new URL('http://localhost:8000/');
|
|
@@ -77,240 +67,267 @@ describe('Piral-Oidc setup module', () => {
|
|
|
77
67
|
};
|
|
78
68
|
});
|
|
79
69
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
expect(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
handleAuthentication: expect.any(Function),
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('should call signoutRedirectCallback when on the post_logout_redirect_uri in the top frame', () => {
|
|
94
|
-
setWindowInTop();
|
|
95
|
-
expect(UserManager).not.toHaveBeenCalled();
|
|
96
|
-
expect(mockSignoutRedirectCallback).not.toHaveBeenCalled();
|
|
97
|
-
//@ts-ignore
|
|
98
|
-
window.location = new URL(postLogoutRedirectUri);
|
|
99
|
-
setupOidcClient(oidcConfig);
|
|
100
|
-
expect(UserManager).toHaveBeenCalledTimes(1);
|
|
101
|
-
expect(mockSignoutRedirectCallback).toHaveBeenCalledTimes(1);
|
|
102
|
-
expect(mockSignoutPopupCallback).not.toHaveBeenCalled();
|
|
70
|
+
it('setupOidcClient should return the following signature', () => {
|
|
71
|
+
const client = setupOidcClient(oidcConfig);
|
|
72
|
+
expect(client).toMatchObject({
|
|
73
|
+
login: expect.any(Function),
|
|
74
|
+
logout: expect.any(Function),
|
|
75
|
+
extendHeaders: expect.any(Function),
|
|
76
|
+
token: expect.any(Function),
|
|
77
|
+
account: expect.any(Function),
|
|
78
|
+
handleAuthentication: expect.any(Function),
|
|
103
79
|
});
|
|
80
|
+
});
|
|
104
81
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
expect(mockSignoutPopupCallback).toHaveBeenCalledTimes(1);
|
|
114
|
-
expect(mockSignoutRedirectCallback).not.toHaveBeenCalled();
|
|
115
|
-
});
|
|
82
|
+
it('should call signoutRedirectCallback when on the post_logout_redirect_uri in the top frame', () => {
|
|
83
|
+
setWindowInTop();
|
|
84
|
+
expect(mockSignoutRedirectCallback).not.toHaveBeenCalled();
|
|
85
|
+
//@ts-ignore
|
|
86
|
+
window.location = new URL(postLogoutRedirectUri);
|
|
87
|
+
setupOidcClient(oidcConfig);
|
|
88
|
+
expect(mockSignoutRedirectCallback).toHaveBeenCalledTimes(1);
|
|
89
|
+
expect(mockSignoutPopupCallback).not.toHaveBeenCalled();
|
|
116
90
|
});
|
|
117
91
|
|
|
118
|
-
|
|
119
|
-
|
|
92
|
+
it('should call signoutPopupCallback and signoutSilentCallback when on the post_logout_redirect_uri in an IFrame', () => {
|
|
93
|
+
setWindowInIFrame();
|
|
94
|
+
expect(mockSignoutPopupCallback).not.toHaveBeenCalled();
|
|
95
|
+
//@ts-ignore
|
|
96
|
+
window.location = new URL(postLogoutRedirectUri);
|
|
97
|
+
const client = setupOidcClient(oidcConfig);
|
|
98
|
+
expect(mockSignoutPopupCallback).toHaveBeenCalledTimes(1);
|
|
99
|
+
expect(mockSignoutRedirectCallback).not.toHaveBeenCalled();
|
|
100
|
+
});
|
|
120
101
|
|
|
121
|
-
|
|
122
|
-
|
|
102
|
+
it('login() should signInRedirect on the UserManager', async () => {
|
|
103
|
+
const client = setupOidcClient(oidcConfig);
|
|
104
|
+
const settings = client._.settings;
|
|
105
|
+
Object.defineProperty(settings, "popup_redirect_uri", {
|
|
106
|
+
get() {
|
|
107
|
+
return settings.popup_redirect_uri || settings.redirect_uri;
|
|
108
|
+
},
|
|
123
109
|
});
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
expect(mockSigninRedirect).toHaveBeenCalledTimes(1);
|
|
110
|
+
Object.defineProperty(settings, "post_logout_redirect_uri", {
|
|
111
|
+
get() {
|
|
112
|
+
return settings.post_logout_redirect_uri;
|
|
113
|
+
},
|
|
129
114
|
});
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
expect(mockSignoutRedirect).toHaveBeenCalledTimes(1);
|
|
115
|
+
Object.defineProperty(settings, "redirect_uri", {
|
|
116
|
+
get() {
|
|
117
|
+
return settings.redirect_uri;
|
|
118
|
+
},
|
|
135
119
|
});
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
120
|
+
Object.defineProperty(settings, "silent_redirect_uri", {
|
|
121
|
+
get() {
|
|
122
|
+
return settings.silent_redirect_uri || settings.redirect_uri;
|
|
123
|
+
},
|
|
140
124
|
});
|
|
125
|
+
expect(mockSigninRedirect).not.toHaveBeenCalled();
|
|
126
|
+
await client.login();
|
|
127
|
+
expect(mockSigninRedirect).toHaveBeenCalledTimes(1);
|
|
128
|
+
});
|
|
141
129
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
access_token: '456',
|
|
149
|
-
expires_in: 10000,
|
|
150
|
-
};
|
|
151
|
-
mockSigninSilent.mockResolvedValueOnce(userTwo);
|
|
152
|
-
mockGetUser.mockResolvedValueOnce(user);
|
|
153
|
-
const token = await client.token();
|
|
154
|
-
expect(token).toBe(userTwo.access_token);
|
|
155
|
-
});
|
|
130
|
+
it('logout() should call signoutRedirect on the UserManager', () => {
|
|
131
|
+
const client = setupOidcClient(oidcConfig);
|
|
132
|
+
expect(mockSignoutRedirect).not.toHaveBeenCalled();
|
|
133
|
+
client.logout();
|
|
134
|
+
expect(mockSignoutRedirect).toHaveBeenCalledTimes(1);
|
|
135
|
+
});
|
|
156
136
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
137
|
+
it('token() should get the token from the user manager', async () => {
|
|
138
|
+
const client = setupOidcClient(oidcConfig);
|
|
139
|
+
const token = await client.token();
|
|
140
|
+
expect(token).toBe(user.access_token);
|
|
141
|
+
});
|
|
161
142
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
143
|
+
it('token() should get a new token via signinSilent() with an expired user', async () => {
|
|
144
|
+
const client = setupOidcClient(oidcConfig);
|
|
145
|
+
const user: any = {
|
|
146
|
+
access_token: '123',
|
|
147
|
+
expires_in: 0,
|
|
148
|
+
};
|
|
149
|
+
const userTwo: any = {
|
|
150
|
+
access_token: '456',
|
|
151
|
+
expires_in: 10000,
|
|
152
|
+
};
|
|
153
|
+
mockSigninSilent.mockResolvedValueOnce(userTwo);
|
|
154
|
+
mockGetUser.mockResolvedValueOnce(user);
|
|
155
|
+
const token = await client.token();
|
|
156
|
+
expect(token).toBe(userTwo.access_token);
|
|
157
|
+
});
|
|
167
158
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
159
|
+
it('token() should reject without a user', () => {
|
|
160
|
+
const client = setupOidcClient(oidcConfig);
|
|
161
|
+
mockGetUser.mockResolvedValue(undefined);
|
|
162
|
+
return expect(client.token()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
|
|
163
|
+
});
|
|
173
164
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
},
|
|
181
|
-
};
|
|
182
|
-
mockGetUser.mockResolvedValue(user);
|
|
183
|
-
return expect(client.account()).resolves.toBe(user.profile);
|
|
184
|
-
});
|
|
165
|
+
it('token() rejects when UserManager rejects', async () => {
|
|
166
|
+
const client = setupOidcClient(oidcConfig);
|
|
167
|
+
const e = new Error('test error');
|
|
168
|
+
mockGetUser.mockRejectedValue(e);
|
|
169
|
+
await expect(client.token()).rejects.toHaveProperty('type', OidcErrorType.unknown);
|
|
170
|
+
});
|
|
185
171
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
},
|
|
193
|
-
};
|
|
194
|
-
mockGetUser.mockResolvedValue(user);
|
|
195
|
-
return expect(client.account()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
|
|
196
|
-
});
|
|
172
|
+
it('token() should reject when a user does not have an access_token', () => {
|
|
173
|
+
const client = setupOidcClient(oidcConfig);
|
|
174
|
+
mockGetUser.mockResolvedValue({});
|
|
175
|
+
mockSigninSilent.mockResolvedValue({});
|
|
176
|
+
return expect(client.token()).rejects.toHaveProperty('type', OidcErrorType.invalidToken);
|
|
177
|
+
});
|
|
197
178
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
179
|
+
it('account() should return the User profile', () => {
|
|
180
|
+
const client = setupOidcClient(oidcConfig);
|
|
181
|
+
const user: any = {
|
|
182
|
+
access_token: '123',
|
|
183
|
+
expires_in: 100,
|
|
184
|
+
profile: {
|
|
185
|
+
foo: 'bar',
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
mockGetUser.mockResolvedValue(user);
|
|
189
|
+
return expect(client.account()).resolves.toBe(user.profile);
|
|
190
|
+
});
|
|
202
191
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
192
|
+
it('account() should reject when user is expired', () => {
|
|
193
|
+
const client = setupOidcClient(oidcConfig);
|
|
194
|
+
const user: any = {
|
|
195
|
+
access_token: '123',
|
|
196
|
+
expires_in: 0,
|
|
197
|
+
profile: {
|
|
198
|
+
foo: 'bar',
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
mockGetUser.mockResolvedValue(user);
|
|
202
|
+
return expect(client.account()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
|
|
203
|
+
});
|
|
213
204
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const { shouldRender } = await client.handleAuthentication();
|
|
220
|
-
expect(mockSigninSilentCallback).toBeCalledTimes(0);
|
|
221
|
-
expect(mockSigninCallback).toBeCalledTimes(1);
|
|
222
|
-
expect(shouldRender).toBe(false);
|
|
223
|
-
});
|
|
205
|
+
it('account() should reject when user is not authenticated', () => {
|
|
206
|
+
const client = setupOidcClient(oidcConfig);
|
|
207
|
+
mockGetUser.mockResolvedValue(undefined);
|
|
208
|
+
return expect(client.account()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
|
|
209
|
+
});
|
|
224
210
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
211
|
+
it('handleAuthentication() calls signinSilentCallback when on the silent_redirect_uri path in an IFrame', async () => {
|
|
212
|
+
const client = setupOidcClient(oidcConfig);
|
|
213
|
+
setWindowInIFrame();
|
|
214
|
+
//@ts-ignore
|
|
215
|
+
window.location = new URL(redirectUri);
|
|
216
|
+
expect(mockSigninSilentCallback).toBeCalledTimes(0);
|
|
217
|
+
const { shouldRender } = await client.handleAuthentication();
|
|
218
|
+
expect(mockSigninSilentCallback).toBeCalledTimes(1);
|
|
219
|
+
expect(mockSigninCallback).toBeCalledTimes(0);
|
|
220
|
+
expect(shouldRender).toBe(false);
|
|
221
|
+
});
|
|
234
222
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
223
|
+
it('handleAuthentication() calls signinCallback when on the redirect_uri path in the main viewport', async () => {
|
|
224
|
+
const client = setupOidcClient(oidcConfig);
|
|
225
|
+
setWindowInTop();
|
|
226
|
+
//@ts-ignore
|
|
227
|
+
window.location = new URL(redirectUri);
|
|
228
|
+
expect(mockSigninCallback).toBeCalledTimes(0);
|
|
229
|
+
const { shouldRender } = await client.handleAuthentication();
|
|
230
|
+
expect(mockSigninSilentCallback).toBeCalledTimes(0);
|
|
231
|
+
expect(mockSigninCallback).toBeCalledTimes(1);
|
|
232
|
+
expect(shouldRender).toBe(false);
|
|
233
|
+
});
|
|
240
234
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
235
|
+
it('handleAuthentication() redirects to appUri when on the redirect_uri path in the main viewport and appUri is configured', async () => {
|
|
236
|
+
const client = setupOidcClient(oidcConfig);
|
|
237
|
+
setWindowInTop();
|
|
238
|
+
const url = new URL(redirectUri);
|
|
239
|
+
//@ts-ignore
|
|
240
|
+
window.location = url;
|
|
241
|
+
expect(window.location.href).not.toBe(appUri);
|
|
242
|
+
await client.handleAuthentication();
|
|
243
|
+
expect(window.location.href).toBe(appUri);
|
|
244
|
+
});
|
|
250
245
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
expect(shouldRender).toBe(true);
|
|
246
|
+
it('handleAuthentication() does not redirect to appUri when appUri is not configured', async () => {
|
|
247
|
+
const client = setupOidcClient(oidcConfig);
|
|
248
|
+
const secondClient = setupOidcClient({
|
|
249
|
+
...oidcConfig,
|
|
250
|
+
appUri: undefined,
|
|
257
251
|
});
|
|
258
252
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
});
|
|
253
|
+
setWindowInTop();
|
|
254
|
+
const url = new URL(redirectUri);
|
|
255
|
+
//@ts-ignore
|
|
256
|
+
window.location = url;
|
|
257
|
+
expect(window.location.href).not.toBe(appUri);
|
|
258
|
+
const { shouldRender } = await secondClient.handleAuthentication();
|
|
259
|
+
expect(window.location.href).not.toBe(appUri);
|
|
260
|
+
expect(shouldRender).toBe(true);
|
|
261
|
+
});
|
|
269
262
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
263
|
+
it('handleAuthentication() returns true when the user has an acess token on normal routes', async () => {
|
|
264
|
+
const client = setupOidcClient(oidcConfig);
|
|
265
|
+
const url = new URL(appUri);
|
|
266
|
+
//@ts-ignore
|
|
267
|
+
window.location = url;
|
|
268
|
+
const { shouldRender } = await client.handleAuthentication();
|
|
269
|
+
expect(shouldRender).toBe(true);
|
|
270
|
+
});
|
|
275
271
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
expect(await retrieveTokenPromise).toEqual(expected);
|
|
288
|
-
});
|
|
272
|
+
it('handleAuthentication() redirects to login when the user does not have an access token', async () => {
|
|
273
|
+
const client = setupOidcClient(oidcConfig);
|
|
274
|
+
const url = new URL(appUri);
|
|
275
|
+
mockGetUser.mockResolvedValue(undefined);
|
|
276
|
+
//@ts-ignore
|
|
277
|
+
window.location = url;
|
|
278
|
+
expect(mockSigninRedirect).toBeCalledTimes(0);
|
|
279
|
+
const { shouldRender } = await client.handleAuthentication();
|
|
280
|
+
expect(mockSigninRedirect).toBeCalledTimes(1);
|
|
281
|
+
expect(shouldRender).toBe(false);
|
|
282
|
+
});
|
|
289
283
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
...oidcConfig,
|
|
297
|
-
restrict: true,
|
|
298
|
-
});
|
|
299
|
-
await client2.extendHeaders(req);
|
|
300
|
-
expect(mockSetHeaders).not.toBeCalled();
|
|
301
|
-
});
|
|
284
|
+
it('handleAuthentication() rejects when token() rejects', async () => {
|
|
285
|
+
const client = setupOidcClient(oidcConfig);
|
|
286
|
+
const e = new Error('test error');
|
|
287
|
+
mockGetUser.mockRejectedValue(e);
|
|
288
|
+
await expect(client.handleAuthentication()).rejects.toHaveProperty('type', OidcErrorType.unknown);
|
|
289
|
+
});
|
|
302
290
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
291
|
+
it('extendHeaders() calls request.setHeaders with the authorization header when the user has a token', async () => {
|
|
292
|
+
const client = setupOidcClient(oidcConfig);
|
|
293
|
+
mockGetUser.mockResolvedValue(user);
|
|
294
|
+
const expected = { Authorization: `Bearer ${user.access_token}` };
|
|
295
|
+
const mockSetHeaders = vitest.fn().mockResolvedValue(undefined);
|
|
296
|
+
const req = {
|
|
297
|
+
setHeaders: mockSetHeaders,
|
|
298
|
+
};
|
|
299
|
+
expect(mockSetHeaders).not.toHaveBeenCalled();
|
|
300
|
+
await client.extendHeaders(req);
|
|
301
|
+
expect(mockSetHeaders).toBeCalledTimes(1);
|
|
302
|
+
const retrieveTokenPromise = mockSetHeaders.mock.calls[0][0];
|
|
303
|
+
expect(await retrieveTokenPromise).toEqual(expected);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('extendHeaders() is a noop when `restrict` is true in configuration', async () => {
|
|
307
|
+
const client = setupOidcClient(oidcConfig);
|
|
308
|
+
const mockSetHeaders = vitest.fn().mockResolvedValue(undefined);
|
|
309
|
+
const req = {
|
|
310
|
+
setHeaders: mockSetHeaders,
|
|
311
|
+
};
|
|
312
|
+
const client2 = setupOidcClient({
|
|
313
|
+
...oidcConfig,
|
|
314
|
+
restrict: true,
|
|
314
315
|
});
|
|
316
|
+
await client2.extendHeaders(req);
|
|
317
|
+
expect(mockSetHeaders).not.toBeCalled();
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it('extendHeaders() does nothing when the user has no token', async () => {
|
|
321
|
+
const client = setupOidcClient(oidcConfig);
|
|
322
|
+
mockGetUser.mockResolvedValue(undefined);
|
|
323
|
+
const mockSetHeaders = vitest.fn().mockResolvedValue(undefined);
|
|
324
|
+
const req = {
|
|
325
|
+
setHeaders: mockSetHeaders,
|
|
326
|
+
};
|
|
327
|
+
expect(mockSetHeaders).not.toHaveBeenCalled();
|
|
328
|
+
await client.extendHeaders(req);
|
|
329
|
+
expect(mockSetHeaders).toBeCalledTimes(1);
|
|
330
|
+
const retrieveTokenPromise = mockSetHeaders.mock.calls[0][0];
|
|
331
|
+
expect(await retrieveTokenPromise).toEqual(undefined);
|
|
315
332
|
});
|
|
316
333
|
});
|