-
-
Notifications
You must be signed in to change notification settings - Fork 387
/
discover_issuer.test.js
234 lines (200 loc) · 10 KB
/
discover_issuer.test.js
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
const { expect } = require('chai');
const nock = require('nock');
const sinon = require('sinon');
const { Issuer } = require('../../lib');
const fail = () => { throw new Error('expected promise to be rejected'); };
['useGot', 'useRequest'].forEach((httpProvider) => {
describe(`Issuer#discover() - using ${httpProvider.substring(3).toLowerCase()}`, function () {
before(function () {
Issuer[httpProvider]();
});
afterEach(nock.cleanAll);
describe('custom /.well-known', function () {
it('accepts and assigns the discovered metadata', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/example-configuration')
.reply(200, {
authorization_endpoint: 'https://op.example.com/o/oauth2/v2/auth',
issuer: 'https://op.example.com',
jwks_uri: 'https://op.example.com/oauth2/v3/certs',
token_endpoint: 'https://op.example.com/oauth2/v4/token',
userinfo_endpoint: 'https://op.example.com/oauth2/v3/userinfo',
});
return Issuer.discover('https://op.example.com/.well-known/example-configuration').then(function (issuer) {
expect(issuer).to.have.property('authorization_endpoint', 'https://op.example.com/o/oauth2/v2/auth');
expect(issuer).to.have.property('issuer', 'https://op.example.com');
expect(issuer).to.have.property('jwks_uri', 'https://op.example.com/oauth2/v3/certs');
expect(issuer).to.have.property('token_endpoint', 'https://op.example.com/oauth2/v4/token');
expect(issuer).to.have.property('userinfo_endpoint', 'https://op.example.com/oauth2/v3/userinfo');
});
});
});
describe('/.well-known/openid-configuration', function () {
it('accepts and assigns the discovered metadata', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/openid-configuration')
.reply(200, {
authorization_endpoint: 'https://op.example.com/o/oauth2/v2/auth',
issuer: 'https://op.example.com',
jwks_uri: 'https://op.example.com/oauth2/v3/certs',
token_endpoint: 'https://op.example.com/oauth2/v4/token',
userinfo_endpoint: 'https://op.example.com/oauth2/v3/userinfo',
});
return Issuer.discover('https://op.example.com/.well-known/openid-configuration').then(function (issuer) {
expect(issuer).to.have.property('authorization_endpoint', 'https://op.example.com/o/oauth2/v2/auth');
expect(issuer).to.have.property('issuer', 'https://op.example.com');
expect(issuer).to.have.property('jwks_uri', 'https://op.example.com/oauth2/v3/certs');
expect(issuer).to.have.property('token_endpoint', 'https://op.example.com/oauth2/v4/token');
expect(issuer).to.have.property('userinfo_endpoint', 'https://op.example.com/oauth2/v3/userinfo');
});
});
it('can be discovered by ommiting the well-known part', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/openid-configuration')
.reply(200, {
issuer: 'https://op.example.com',
});
return Issuer.discover('https://op.example.com').then(function (issuer) {
expect(issuer).to.have.property('issuer', 'https://op.example.com');
});
});
it('discovering issuers with path components (with trailing slash)', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/oidc/.well-known/openid-configuration')
.reply(200, {
issuer: 'https://op.example.com/oidc',
});
return Issuer.discover('https://op.example.com/oidc/').then(function (issuer) {
expect(issuer).to.have.property('issuer', 'https://op.example.com/oidc');
});
});
it('discovering issuers with path components (without trailing slash)', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/oidc/.well-known/openid-configuration')
.reply(200, {
issuer: 'https://op.example.com/oidc',
});
return Issuer.discover('https://op.example.com/oidc').then(function (issuer) {
expect(issuer).to.have.property('issuer', 'https://op.example.com/oidc');
});
});
it('discovering issuers with well known uri including path and query', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/oidc/.well-known/openid-configuration')
.query({ foo: 'bar' })
.reply(200, {
issuer: 'https://op.example.com/oidc',
});
return Issuer.discover('https://op.example.com/oidc/.well-known/openid-configuration?foo=bar').then(function (issuer) {
expect(issuer).to.have.property('issuer', 'https://op.example.com/oidc');
});
});
});
describe('/.well-known/oauth-authorization-server', function () {
it('accepts and assigns the discovered metadata', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/oauth-authorization-server')
.reply(200, {
authorization_endpoint: 'https://op.example.com/o/oauth2/v2/auth',
issuer: 'https://op.example.com',
jwks_uri: 'https://op.example.com/oauth2/v3/certs',
token_endpoint: 'https://op.example.com/oauth2/v4/token',
userinfo_endpoint: 'https://op.example.com/oauth2/v3/userinfo',
});
return Issuer.discover('https://op.example.com/.well-known/oauth-authorization-server').then(function (issuer) {
expect(issuer).to.have.property('authorization_endpoint', 'https://op.example.com/o/oauth2/v2/auth');
expect(issuer).to.have.property('issuer', 'https://op.example.com');
expect(issuer).to.have.property('jwks_uri', 'https://op.example.com/oauth2/v3/certs');
expect(issuer).to.have.property('token_endpoint', 'https://op.example.com/oauth2/v4/token');
expect(issuer).to.have.property('userinfo_endpoint', 'https://op.example.com/oauth2/v3/userinfo');
});
});
it('discovering issuers with well known uri including path and query', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/oauth-authorization-server/oauth2')
.query({ foo: 'bar' })
.reply(200, {
issuer: 'https://op.example.com/oauth2',
});
return Issuer.discover('https://op.example.com/.well-known/oauth-authorization-server/oauth2?foo=bar').then(function (issuer) {
expect(issuer).to.have.property('issuer', 'https://op.example.com/oauth2');
});
});
it('can be discovered by ommiting the well-known part', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/oauth-authorization-server')
.reply(200, {
issuer: 'https://op.example.com',
});
return Issuer.discover('https://op.example.com').then(function (issuer) {
expect(issuer).to.have.property('issuer', 'https://op.example.com');
});
});
it('discovering issuers with path components (with trailing slash)', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/oauth-authorization-server/oauth2/')
.reply(200, {
issuer: 'https://op.example.com/oauth2',
});
return Issuer.discover('https://op.example.com/oauth2/').then(function (issuer) {
expect(issuer).to.have.property('issuer', 'https://op.example.com/oauth2');
});
});
});
it('is rejected with OpenIdConnectError upon oidc error', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/openid-configuration')
.reply(500, {
error: 'server_error',
error_description: 'bad things are happening',
});
return Issuer.discover('https://op.example.com')
.then(fail, function (error) {
expect(error).to.have.property('error', 'server_error');
});
});
it('is rejected with when non 200 is returned', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/openid-configuration')
.reply(500, 'Internal Server Error');
return Issuer.discover('https://op.example.com')
.then(fail, function (error) {
expect(error).to.be.an.instanceof(Issuer.httpClient.HTTPError);
});
});
it('is rejected with JSON.parse error upon invalid response', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/openid-configuration')
.reply(200, '{"notavalid"}');
return Issuer.discover('https://op.example.com')
.then(fail, function (error) {
expect(error).to.be.an.instanceof(SyntaxError);
expect(error).to.have.property('message').matches(/Unexpected token/);
});
});
it('is rejected when no body is returned', function () {
nock('https://op.example.com', { allowUnmocked: true })
.get('/.well-known/openid-configuration')
.reply(301);
return Issuer.discover('https://op.example.com')
.then(fail, function (error) {
expect(error).to.have.property('message', 'expected 200 OK with body, got 301 Moved Permanently without one');
});
});
it('is rejected with the AggregateError if not handled', function () {
const err = new Error('foo');
sinon.stub(Issuer.httpClient, 'get').rejects(err);
return Issuer.discover('https://op.example.com')
.then(() => {
Issuer.httpClient.get.restore();
fail();
}, function (error) {
Issuer.httpClient.get.restore();
expect(error.name).to.equal('AggregateError');
for (const e of error) { // eslint-disable-line no-restricted-syntax
expect(e).to.equal(err);
}
});
});
});
});