forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoidc_auth_test.ts
270 lines (239 loc) · 9.36 KB
/
oidc_auth_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { notStrictEqual, strictEqual } from 'node:assert';
import { OutgoingHttpHeaders } from 'node:http';
import https from 'node:https';
import { base64url } from 'rfc4648';
import { User } from './config_types.js';
import { OpenIDConnectAuth } from './oidc_auth.js';
function encode(value: string): string {
return base64url.stringify(new TextEncoder().encode(value));
}
function makeJWT(header: string, payload: object, signature: string): string {
return encode(header) + '.' + encode(JSON.stringify(payload)) + '.' + encode(signature);
}
describe('OIDCAuth', () => {
let auth: OpenIDConnectAuth;
beforeEach(() => {
auth = new OpenIDConnectAuth();
});
it('should correctly parse a JWT', () => {
const jwt = OpenIDConnectAuth.decodeJWT(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.5mhBHqs5_DTLdINd9p5m7ZJ6XD0Xc55kIaCRY5r6HRA',
);
notStrictEqual(jwt, null);
});
it('should correctly parse time from token', () => {
const time = Math.floor(Date.now() / 1000);
const token = makeJWT('{}', { exp: time }, 'fake');
const timeOut = OpenIDConnectAuth.expirationFromToken(token);
strictEqual(timeOut, time);
});
it('should be true for oidc user', () => {
const user = {
authProvider: {
name: 'oidc',
},
} as User;
strictEqual(auth.isAuthProvider(user), true);
});
it('should be false for other user', () => {
const user = {
authProvider: {
name: 'azure',
},
} as User;
strictEqual(auth.isAuthProvider(user), false);
});
it('should be false for null user.authProvider', () => {
const user = {} as User;
strictEqual(auth.isAuthProvider(user), false);
});
it('authorization should be undefined if token missing', async () => {
const user = {
authProvider: {
name: 'oidc',
config: {
'client-id': 'id',
'client-secret': 'clientsecret',
'refresh-token': 'refreshtoken',
'idp-issuer-url': 'https://www.google.com/',
},
},
} as User;
const opts = {} as https.RequestOptions;
opts.headers = {} as OutgoingHttpHeaders;
await auth.applyAuthentication(user, opts);
strictEqual(opts.headers.Authorization, undefined);
});
it('authorization should be undefined if client-id missing', async () => {
const past = 100;
const token = makeJWT('{}', { exp: past }, 'fake');
const user = {
authProvider: {
name: 'oidc',
config: {
'id-token': token,
'client-secret': 'clientsecret',
'refresh-token': 'refreshtoken',
'idp-issuer-url': 'https://www.google.com/',
},
},
} as User;
const opts = {} as https.RequestOptions;
opts.headers = {} as OutgoingHttpHeaders;
await auth.applyAuthentication(user, opts);
strictEqual(opts.headers.Authorization, undefined);
});
it('authorization should be work if client-secret missing', async () => {
const user = {
authProvider: {
name: 'oidc',
config: {
'id-token': 'fakeToken',
'client-id': 'id',
'refresh-token': 'refreshtoken',
'idp-issuer-url': 'https://www.google.com/',
},
},
} as User;
const opts = {} as https.RequestOptions;
opts.headers = {} as OutgoingHttpHeaders;
(auth as any).currentTokenExpiration = Date.now() / 1000 + 1000;
await auth.applyAuthentication(user, opts, {});
strictEqual(opts.headers.Authorization, 'Bearer fakeToken');
});
it('authorization should be undefined if refresh-token missing', async () => {
const past = 100;
const token = makeJWT('{}', { exp: past }, 'fake');
const user = {
authProvider: {
name: 'oidc',
config: {
'id-token': token,
'client-id': 'id',
'client-secret': 'clientsecret',
'idp-issuer-url': 'https://www.google.com/',
},
},
} as User;
const opts = {} as https.RequestOptions;
opts.headers = {} as OutgoingHttpHeaders;
await auth.applyAuthentication(user, opts);
strictEqual(opts.headers.Authorization, undefined);
});
it('authorization should work if refresh-token missing but token is unexpired', async () => {
const future = Date.now() / 1000 + 1000000;
const token = makeJWT('{}', { exp: future }, 'fake');
const user = {
authProvider: {
name: 'oidc',
config: {
'id-token': token,
'client-id': 'id',
'client-secret': 'clientsecret',
'idp-issuer-url': 'https://www.google.com/',
},
},
} as User;
const opts = {} as https.RequestOptions;
opts.headers = {} as OutgoingHttpHeaders;
await auth.applyAuthentication(user, opts);
strictEqual(opts.headers.Authorization, `Bearer ${token}`);
});
it('authorization should be undefined if idp-issuer-url missing', async () => {
const past = 100;
const token = makeJWT('{}', { exp: past }, 'fake');
const user = {
authProvider: {
name: 'oidc',
config: {
'id-token': token,
'client-id': 'id',
'client-secret': 'clientsecret',
'refresh-token': 'refreshtoken',
},
},
} as User;
const opts = {} as https.RequestOptions;
opts.headers = {} as OutgoingHttpHeaders;
await auth.applyAuthentication(user, opts, {});
strictEqual(opts.headers.Authorization, undefined);
});
it('return token when it is still active', async () => {
const user = {
authProvider: {
name: 'oidc',
config: {
'id-token': 'fakeToken',
'client-id': 'id',
'client-secret': 'clientsecret',
'refresh-token': 'refreshtoken',
'idp-issuer-url': 'https://www.google.com/',
},
},
} as User;
const opts = {} as https.RequestOptions;
opts.headers = {} as OutgoingHttpHeaders;
(auth as any).currentTokenExpiration = Date.now() / 1000 + 1000;
await auth.applyAuthentication(user, opts, {});
strictEqual(opts.headers.Authorization, 'Bearer fakeToken');
});
it('return new token when the current expired', async () => {
const user = {
authProvider: {
name: 'oidc',
config: {
'id-token': 'fakeToken',
'client-id': 'id',
'client-secret': 'clientsecret',
'refresh-token': 'refreshtoken',
'idp-issuer-url': 'https://www.google.com/',
},
},
} as User;
const opts = {} as https.RequestOptions;
opts.headers = {} as OutgoingHttpHeaders;
(auth as any).currentTokenExpiration = Date.now() / 1000 - 5000;
const newExpiration = Date.now() / 1000 + 120;
await auth.applyAuthentication(user, opts, {
refresh: async (token) => {
return {
expires_at: newExpiration,
id_token: 'newToken',
refresh_token: 'newRefreshToken',
};
},
});
strictEqual(opts.headers.Authorization, 'Bearer newToken');
strictEqual((auth as any).currentTokenExpiration, newExpiration);
// Check also the new refresh token sticks in the user config
strictEqual(user.authProvider.config['refresh-token'], 'newRefreshToken');
});
it('return a new token when the its the first time we see this user', async () => {
const user = {
authProvider: {
name: 'oidc',
config: {
'id-token': 'fakeToken',
'client-id': 'id',
'client-secret': 'clientsecret',
'refresh-token': 'refreshtoken',
'idp-issuer-url': 'https://www.google.com/',
},
},
} as User;
const opts = {} as https.RequestOptions;
opts.headers = {} as OutgoingHttpHeaders;
const newExpiration = Date.now() / 1000 + 120;
(auth as any).currentTokenExpiration = 0;
await auth.applyAuthentication(user, opts, {
refresh: async (token) => {
return {
expires_at: newExpiration,
id_token: 'newToken',
};
},
});
strictEqual(opts.headers.Authorization, 'Bearer newToken');
strictEqual((auth as any).currentTokenExpiration, newExpiration);
});
});