forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoidc_auth_delayed_test.ts
178 lines (156 loc) · 5.65 KB
/
oidc_auth_delayed_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
import { expect } from 'chai';
import * as request from 'request';
import { base64url } from 'rfc4648';
import { TextEncoder } from 'util';
import { User } from './config_types';
import { DelayedOpenIDConnectAuth } from './oidc_auth_delayed';
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', () => {
var auth: DelayedOpenIDConnectAuth;
beforeEach(() => {
auth = new DelayedOpenIDConnectAuth();
});
it('should be true for oidc user', () => {
const user = {
authProvider: {
name: 'oidc',
},
} as User;
expect(auth.isAuthProvider(user)).to.equal(true);
});
it('should be false for other user', () => {
const user = {
authProvider: {
name: 'azure',
},
} as User;
expect(auth.isAuthProvider(user)).to.equal(false);
});
it('should be false for null user.authProvider', () => {
const user = {} as User;
expect(auth.isAuthProvider(user)).to.equal(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 request.Options;
opts.headers = [];
await auth.applyAuthentication(user, opts);
expect(opts.headers.Authorization).to.be.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 request.Options;
opts.headers = [];
await auth.applyAuthentication(user, opts);
expect(opts.headers.Authorization).to.be.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 request.Options;
opts.headers = [];
(auth as any).currentTokenExpiration = Date.now() / 1000 + 1000;
await auth.applyAuthentication(user, opts, {
refresh: () => {
return {
id_token: 'fakeToken',
refresh_token: 'fakerToken',
};
},
});
expect(opts.headers.Authorization).to.equal('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 request.Options;
opts.headers = [];
await auth.applyAuthentication(user, opts);
expect(opts.headers.Authorization).to.be.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 request.Options;
opts.headers = [];
await auth.applyAuthentication(user, opts);
expect(opts.headers.Authorization).to.equal(`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 request.Options;
opts.headers = [];
await auth.applyAuthentication(user, opts, {});
expect(opts.headers.Authorization).to.be.undefined;
});
});