-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathstrategy.spec.ts
370 lines (328 loc) · 12.2 KB
/
strategy.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
"use strict";
import type * as express from "express";
import { expect } from "chai";
import * as sinon from "sinon";
import { Strategy as SamlStrategy } from "../src";
import { Profile, SAML } from "@node-saml/node-saml";
import {
RequestWithUser,
VerifiedCallback,
VerifyWithoutRequest,
PassportSamlConfig,
} from "../src/types";
import { FAKE_CERT } from "./types";
const noop = () => undefined;
describe("Strategy()", function () {
it("should require ctor `options` argument", function () {
// @ts-ignore
expect(() => new SamlStrategy(noop)).to.throw("Mandatory SAML options missing");
});
it("should require that `signonVerify` be a function", function () {
// @ts-ignore
expect(() => new SamlStrategy({}, {})).to.throw(
"SAML authentication strategy requires a verify function",
);
});
describe("authenticate", function () {
let getAuthorizeFormStub: sinon.SinonStub;
let getAuthorizeUrlStub: sinon.SinonStub;
let getLogoutResponseUrlStub: sinon.SinonStub;
let getLogoutUrlAsyncStub: sinon.SinonStub;
let validateRedirectAsyncStub: sinon.SinonStub;
let validatePostResponseAsyncStub: sinon.SinonStub;
let validatePostRequestAsyncStub: sinon.SinonStub;
let errorStub: sinon.SinonStub;
let redirectStub: sinon.SinonStub;
let successStub: sinon.SinonStub;
let requestWithUser = {} as unknown as RequestWithUser;
let requestWithUserGetResponse = {} as unknown as RequestWithUser;
let requestWithUserPostResponse = {} as unknown as RequestWithUser;
let requestWithUserPostRequest = {} as unknown as RequestWithUser;
let logoutSpy: sinon.SinonSpy;
beforeEach(function () {
getAuthorizeFormStub = sinon.stub(SAML.prototype, "getAuthorizeFormAsync").resolves();
getAuthorizeUrlStub = sinon.stub(SAML.prototype, "getAuthorizeUrlAsync").resolves();
getLogoutResponseUrlStub = sinon.stub(SAML.prototype, "getLogoutResponseUrl");
getLogoutUrlAsyncStub = sinon.stub(SAML.prototype, "getLogoutUrlAsync").resolves();
validateRedirectAsyncStub = sinon.stub(SAML.prototype, "validateRedirectAsync").resolves();
validatePostResponseAsyncStub = sinon
.stub(SAML.prototype, "validatePostResponseAsync")
.resolves();
validatePostRequestAsyncStub = sinon
.stub(SAML.prototype, "validatePostRequestAsync")
.resolves();
errorStub = sinon.stub(SamlStrategy.prototype, "error");
redirectStub = sinon.stub(SamlStrategy.prototype, "redirect");
successStub = sinon.stub(SamlStrategy.prototype, "success");
logoutSpy = sinon.spy();
requestWithUser = {
logout: logoutSpy,
res: { send: noop },
} as unknown as RequestWithUser;
requestWithUserGetResponse = {
query: { SAMLResponse: {} },
url: "https://www.example.com/?key=value",
logout: logoutSpy,
res: { send: noop },
} as unknown as RequestWithUser;
requestWithUserPostResponse = {
body: { SAMLResponse: {} },
logout: logoutSpy,
res: { send: noop },
} as unknown as RequestWithUser;
requestWithUserPostRequest = {
body: { SAMLRequest: {} },
url: "https://www.example.com/?key=value",
logout: logoutSpy,
res: { send: noop },
} as unknown as RequestWithUser;
});
afterEach(function () {
getAuthorizeFormStub.restore();
getAuthorizeUrlStub.restore();
getLogoutResponseUrlStub.restore();
getLogoutUrlAsyncStub.restore();
validateRedirectAsyncStub.restore();
validatePostResponseAsyncStub.restore();
validatePostRequestAsyncStub.restore();
errorStub.restore();
redirectStub.restore();
successStub.restore();
logoutSpy.resetHistory();
});
it("calls getAuthorizeForm when authnRequestBinding is HTTP-POST for login-request", function (done) {
const strategy = new SamlStrategy(
{
authnRequestBinding: "HTTP-POST",
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
},
noop,
noop,
);
// This returns immediately, but calls async functions; need to turn event loop
strategy.authenticate(requestWithUser, {});
setImmediate(() => {
sinon.assert.notCalled(errorStub);
sinon.assert.calledOnce(getAuthorizeFormStub);
done();
});
});
it("calls getAuthorizeForm when authnRequestBinding is not HTTP-POST for logout-request", function (done) {
const strategy = new SamlStrategy(
{
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
},
noop,
noop,
);
// This returns immediately, but calls async functions; need to turn event loop
strategy.authenticate(requestWithUser, { samlFallback: "logout-request" });
setImmediate(() => {
sinon.assert.notCalled(errorStub);
sinon.assert.calledOnce(getLogoutUrlAsyncStub);
done();
});
});
it("calls getAuthorizeUrl when authnRequestBinding is not HTTP-POST for login-request", function (done) {
const strategy = new SamlStrategy(
{ idpCert: FAKE_CERT, issuer: "onesaml_login", callbackUrl: "https://www.example.com" },
noop,
noop,
);
// This returns immediately, but calls async functions; need to turn event loop
strategy.authenticate(requestWithUser, {});
setImmediate(() => {
sinon.assert.notCalled(errorStub);
sinon.assert.calledOnce(getAuthorizeUrlStub);
sinon.assert.calledOnce(redirectStub);
done();
});
});
it("determines that logout was unsuccessful where user doesn't match, POST", function (done) {
const strategy = new SamlStrategy(
{
idpCert: FAKE_CERT,
passReqToCallback: true,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
},
function (req: express.Request, _profile: Profile | null, cb: VerifiedCallback) {
// for signon
cb(new Error("Logout shouldn't call signon."));
},
function (req: express.Request, _profile: Profile | null, cb: VerifiedCallback) {
// for logout
if (_profile) {
cb(null, { name: _profile.nameID });
}
},
);
validatePostResponseAsyncStub.resolves({
profile: {
ID: "ID",
issuer: "issuer",
nameID: "some other user",
nameIDFormat: "nameIDFormat",
},
loggedOut: true,
});
// Pretend we already loaded a users session from a cookie or something
// by calling `strategy.authenticate` when the request comes in
requestWithUserPostResponse.user = {
name: "some user",
};
// This returns immediately, but calls async functions; need to turn event loop
strategy.authenticate(requestWithUserPostResponse, {});
setImmediate(() => {
sinon.assert.notCalled(errorStub);
sinon.assert.calledOnceWithMatch(
getLogoutResponseUrlStub,
sinon.match.any,
sinon.match.any,
sinon.match.any,
false,
sinon.match.func,
);
sinon.assert.calledOnce(getLogoutResponseUrlStub);
sinon.assert.calledOnce(logoutSpy);
done();
});
});
it("determines that logout was successful where user matches, GET", function (done) {
const strategy = new SamlStrategy(
{ idpCert: FAKE_CERT, issuer: "onesaml_login", callbackUrl: "https://www.example.com" },
function (_profile: Profile | null, cb: VerifiedCallback) {
// for signon
cb(new Error("Logout shouldn't call signon."));
},
function (_profile: Profile | null, cb: VerifiedCallback) {
// for logout
if (_profile) {
cb(null, { name: _profile.nameID });
}
},
);
validateRedirectAsyncStub.resolves({
profile: {
ID: "ID",
issuer: "issuer",
nameID: "some user",
nameIDFormat: "nameIDFormat",
},
loggedOut: true,
});
// Pretend we already loaded a users session from a cookie or something
// by calling `strategy.authenticate` when the request comes in
requestWithUserGetResponse.user = {
name: "some user",
};
// This returns immediately, but calls async functions; need to turn event loop
strategy.authenticate(requestWithUserGetResponse, {});
getLogoutResponseUrlStub.yields(null, requestWithUserGetResponse.url);
setImmediate(() => {
sinon.assert.notCalled(errorStub);
sinon.assert.calledOnceWithMatch(
getLogoutResponseUrlStub,
sinon.match.any,
sinon.match.any,
sinon.match.any,
true,
sinon.match.func,
);
sinon.assert.calledOnce(getLogoutResponseUrlStub);
sinon.assert.calledOnceWithMatch(redirectStub, requestWithUserGetResponse.url);
sinon.assert.calledOnce(logoutSpy);
done();
});
});
it("determines that signon was successful where user matches, POST", function (done) {
const strategy = new SamlStrategy(
{ idpCert: FAKE_CERT, issuer: "onesaml_login", callbackUrl: "https://www.example.com" },
function (_profile: Profile | null, cb: VerifiedCallback) {
// for signon
if (_profile) {
cb(null, { name: _profile.nameID });
}
},
function (_profile: Profile | null, cb: VerifiedCallback) {
// for logout
cb(new Error("Signon shouldn't call logout."));
},
);
validatePostRequestAsyncStub.resolves({
profile: {
ID: "ID",
issuer: "issuer",
nameID: "some user",
nameIDFormat: "nameIDFormat",
},
});
// Pretend we already loaded a users session from a cookie or something
// by calling `strategy.authenticate` when the request comes in
requestWithUserPostRequest.user = {
name: "some user",
};
// This returns immediately, but calls async functions; need to turn event loop
strategy.authenticate(requestWithUserPostRequest, {});
setImmediate(() => {
sinon.assert.notCalled(errorStub);
sinon.assert.calledOnceWithMatch(successStub, requestWithUserPostRequest.user, undefined);
done();
});
});
});
describe("logout", function () {
let getLogoutUrlAsyncStub: sinon.SinonStub;
beforeEach(function () {
getLogoutUrlAsyncStub = sinon.stub(SAML.prototype, "getLogoutUrlAsync").resolves();
});
afterEach(function () {
getLogoutUrlAsyncStub.restore();
});
it("should call through to get logout URL", function () {
new SamlStrategy(
{ idpCert: FAKE_CERT, issuer: "onesaml_login", callbackUrl: "https://www.example.com" },
noop,
noop,
).logout(
{
// @ts-expect-error
query: "",
},
noop,
);
sinon.assert.calledOnce(getLogoutUrlAsyncStub);
});
});
describe("generateServiceProviderMetadata", function () {
let generateServiceProviderMetadataStub: sinon.SinonStub;
beforeEach(function () {
generateServiceProviderMetadataStub = sinon.stub(
SAML.prototype,
"generateServiceProviderMetadata",
);
});
afterEach(function () {
generateServiceProviderMetadataStub.restore();
});
it("should call through to generate metadata", function () {
const samlConfig: PassportSamlConfig = {
idpCert: FAKE_CERT,
issuer: "onesaml_login",
callbackUrl: "https://www.example.com",
};
const signonVerify: VerifyWithoutRequest = function (): void {
throw Error("This shouldn't be called to generate metadata");
};
const logoutVerify: VerifyWithoutRequest = function (): void {
throw Error("This shouldn't be called to generate metadata");
};
new SamlStrategy(samlConfig, signonVerify, logoutVerify).generateServiceProviderMetadata("");
sinon.assert.calledOnce(generateServiceProviderMetadataStub);
});
});
});