-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathmultiSamlStrategy.spec.ts
378 lines (338 loc) · 11.9 KB
/
multiSamlStrategy.spec.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"use strict";
import * as express from "express";
import { Strategy } from "passport-strategy";
import * as sinon from "sinon";
import { expect } from "chai";
import { MultiSamlStrategy, AbstractStrategy } from "../src";
import { SAML } from "@node-saml/node-saml";
import {
MultiStrategyConfig,
RequestWithUser,
StrategyOptionsCallback,
PassportSamlConfig,
} from "../src/types";
import * as assert from "assert";
import { FAKE_CERT } from "./types";
const noop = () => undefined;
describe("MultiSamlStrategy()", function () {
it("extends passport Strategy", function () {
function getSamlOptions() {
return {};
}
const strategy = new MultiSamlStrategy({ getSamlOptions }, noop, noop);
expect(strategy).to.be.an.instanceOf(AbstractStrategy);
expect(strategy).to.be.an.instanceOf(Strategy);
});
it("does not require any properties in the SamlOptionsCallback", function () {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const getSamlOptions: StrategyOptionsCallback = (err, samlOptions) => {
// do nothing; the return type is void
};
getSamlOptions(null, {});
});
it("throws if wrong finder is provided", function () {
function createStrategy() {
return new MultiSamlStrategy({} as MultiStrategyConfig, noop, noop);
}
assert.throws(createStrategy);
});
describe("authenticate", function () {
beforeEach(function () {
this.superAuthenticateStub = sinon.stub(AbstractStrategy.prototype, "authenticate");
});
afterEach(function () {
this.superAuthenticateStub.restore();
});
it("calls super with request and auth options", function (done) {
const superAuthenticateStub = this.superAuthenticateStub;
function getSamlOptions(req: express.Request, fn: StrategyOptionsCallback) {
try {
fn(null, {
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
});
sinon.assert.calledOnce(superAuthenticateStub);
done();
} catch (err2) {
done(err2);
}
}
const strategy = new MultiSamlStrategy(
{
getSamlOptions,
},
noop,
noop,
);
// @ts-expect-error
strategy.authenticate("random", "random");
});
it("passes options on to saml strategy", function (done) {
const passportOptions = {
passReqToCallback: true,
getSamlOptions: function (req: express.Request, fn: StrategyOptionsCallback) {
try {
fn(null, {
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
});
expect(strategy._passReqToCallback).to.equal(true);
done();
} catch (err2) {
done(err2);
}
},
};
const strategy = new MultiSamlStrategy(passportOptions, noop, noop);
// @ts-expect-error
strategy.authenticate("random", "random");
});
it("uses given options to setup internal saml provider", function (done) {
const superAuthenticateStub = this.superAuthenticateStub;
const samlOptions: PassportSamlConfig = {
issuer: "http://foo.issuer",
callbackUrl: "http://foo.callback",
idpCert: "deadbeef",
logoutCallbackUrl: "http://lvh/saml/callback",
acceptedClockSkewMs: -1,
identifierFormat: "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress",
logoutUrl: "http://foo.slo",
signatureAlgorithm: "sha256",
};
function getSamlOptions(req: express.Request, fn: StrategyOptionsCallback) {
try {
fn(null, samlOptions);
sinon.assert.calledOnce(superAuthenticateStub);
superAuthenticateStub.calledWith(
Object.assign({}, { cacheProvider: "mock cache provider" }, samlOptions),
);
done();
} catch (err2) {
done(err2);
}
}
const strategy = new MultiSamlStrategy(
// @ts-expect-error
{ getSamlOptions, cacheProvider: "mock cache provider" },
noop,
noop,
);
// @ts-expect-error
strategy.authenticate("random", "random");
});
});
describe("authorize", function () {
let getAuthorizeFormStub: sinon.SinonStub;
let getAuthorizeUrlStub: sinon.SinonStub;
let errorStub: sinon.SinonStub;
beforeEach(function () {
getAuthorizeFormStub = sinon.stub(SAML.prototype, "getAuthorizeFormAsync").resolves();
getAuthorizeUrlStub = sinon.stub(SAML.prototype, "getAuthorizeUrlAsync").resolves();
errorStub = sinon.stub(MultiSamlStrategy.prototype, "error");
});
afterEach(function () {
getAuthorizeFormStub.restore();
getAuthorizeUrlStub.restore();
errorStub.restore();
});
it("calls getAuthorizeForm when authnRequestBinding is HTTP-POST", function () {
function getSamlOptions(req: express.Request, fn: StrategyOptionsCallback) {
fn(null, {
authnRequestBinding: "HTTP-POST",
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
});
}
const strategy = new MultiSamlStrategy({ getSamlOptions }, noop, noop);
strategy.authenticate({} as RequestWithUser, {});
sinon.assert.notCalled(errorStub);
sinon.assert.calledOnce(getAuthorizeFormStub);
});
it("calls getAuthorizeUrl when authnRequestBinding is not HTTP-POST", function () {
function getSamlOptions(req: express.Request, fn: StrategyOptionsCallback) {
fn(null, {
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
});
}
const strategy = new MultiSamlStrategy({ getSamlOptions }, noop, noop);
strategy.authenticate({} as RequestWithUser, {});
sinon.assert.notCalled(errorStub);
sinon.assert.calledOnce(getAuthorizeUrlStub);
});
});
describe("logout", function () {
beforeEach(function () {
this.superLogoutMock = sinon.stub(AbstractStrategy.prototype, "logout");
});
afterEach(function () {
this.superLogoutMock.restore();
});
it("calls super with request and auth options", function (done) {
const superLogoutMock = this.superLogoutMock;
function getSamlOptions(req: express.Request, fn: StrategyOptionsCallback) {
try {
fn(null, {
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
});
sinon.assert.calledOnce(superLogoutMock);
done();
} catch (err2) {
done(err2);
}
}
const strategy = new MultiSamlStrategy({ getSamlOptions }, noop, noop);
// @ts-expect-error
strategy.logout("random", "random");
});
it("passes options on to saml strategy", function (done) {
const passportOptions = {
passReqToCallback: true,
getSamlOptions: function (req: express.Request, fn: StrategyOptionsCallback) {
try {
fn(null, {
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
});
expect(strategy._passReqToCallback).to.equal(true);
done();
} catch (err2) {
done(err2);
}
},
};
const strategy = new MultiSamlStrategy(passportOptions, noop, noop);
// @ts-expect-error
strategy.logout("random", "random");
});
it("uses given options to setup internal saml provider", function (done) {
const superLogoutMock = this.superLogoutMock;
const samlOptions: PassportSamlConfig = {
issuer: "http://foo.issuer",
callbackUrl: "http://foo.callback",
authnRequestBinding: "HTTP-POST",
idpCert: "deadbeef",
logoutCallbackUrl: "http://lvh/saml/callback",
acceptedClockSkewMs: -1,
identifierFormat: "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress",
logoutUrl: "http://foo.slo",
signatureAlgorithm: "sha256",
};
function getSamlOptions(req: express.Request, fn: StrategyOptionsCallback) {
try {
fn(null, samlOptions);
sinon.assert.calledOnce(superLogoutMock);
superLogoutMock.calledWith(Object.assign({}, samlOptions));
done();
} catch (err2) {
done(err2);
}
}
const strategy = new MultiSamlStrategy({ getSamlOptions }, noop, noop);
// @ts-expect-error
strategy.logout("random", sinon.spy());
});
});
describe("generateServiceProviderMetadata", function () {
beforeEach(function () {
this.superGenerateServiceProviderMetadata = sinon
.stub(SAML.prototype, "generateServiceProviderMetadata")
.returns("My Metadata Result");
});
afterEach(function () {
this.superGenerateServiceProviderMetadata.restore();
});
it("calls super with request and generateServiceProviderMetadata options", function (done) {
const superGenerateServiceProviderMetadata = this.superGenerateServiceProviderMetadata;
function getSamlOptions(req: express.Request, fn: StrategyOptionsCallback) {
try {
fn(null, {
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
});
sinon.assert.calledOnce(superGenerateServiceProviderMetadata);
superGenerateServiceProviderMetadata.calledWith("bar", "baz");
expect(req).to.equal("foo");
done();
} catch (err2) {
done(err2);
}
}
const strategy = new MultiSamlStrategy({ getSamlOptions }, noop, noop);
// @ts-expect-error
strategy.generateServiceProviderMetadata("foo", "bar", "baz", noop);
});
it("passes options on to saml strategy", function (done) {
const passportOptions: MultiStrategyConfig = {
passReqToCallback: true,
getSamlOptions: function (req: express.Request, fn: StrategyOptionsCallback) {
try {
fn(null, {
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
});
expect(strategy._passReqToCallback).to.equal(true);
done();
} catch (err2) {
done(err2);
}
},
};
const strategy = new MultiSamlStrategy(passportOptions, noop, noop);
// @ts-expect-error
strategy.generateServiceProviderMetadata("foo", "bar", "baz", noop);
});
it("should pass error to callback function", function (done) {
const passportOptions = {
getSamlOptions: function (req: express.Request, fn: StrategyOptionsCallback) {
fn(new Error("My error"));
},
};
const strategy = new MultiSamlStrategy(passportOptions, noop, noop);
// @ts-expect-error
strategy.generateServiceProviderMetadata("foo", "bar", "baz", function (error) {
try {
expect(error?.message).to.equal("My error");
done();
} catch (err2) {
done(err2);
}
});
});
it("should pass result to callback function", function (done) {
const passportOptions = {
getSamlOptions: function (req: express.Request, fn: StrategyOptionsCallback) {
fn(null, {
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
});
},
};
const strategy = new MultiSamlStrategy(passportOptions, noop, noop);
strategy.generateServiceProviderMetadata(
// @ts-expect-error
"foo",
"bar",
"baz",
function (error, result) {
try {
expect(result).to.equal("My Metadata Result");
done();
} catch (err2) {
done(err2);
}
},
);
});
});
});