Skip to content

Commit 4a83196

Browse files
authored
Improve the typing of the Strategy class hierarchy. (#554)
The current expected ts error causes problems with some earlier version of TS. Also, it does not make any sense to have a method override a method with a different signature, just because they have the same name.
1 parent 5618b65 commit 4a83196

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

src/passport-saml/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { CacheItem, CacheProvider } from "../node-saml/inmemory-cache-provider";
22
import { SAML } from "../node-saml";
3-
import Strategy = require("./strategy");
4-
import MultiSamlStrategy = require("./multiSamlStrategy");
3+
import { Strategy, AbstractStrategy } from "./strategy";
4+
import { MultiSamlStrategy } from "./multiSamlStrategy";
5+
56
import type {
67
Profile,
78
SamlConfig,
@@ -12,6 +13,7 @@ import type {
1213

1314
export {
1415
SAML,
16+
AbstractStrategy,
1517
Strategy,
1618
MultiSamlStrategy,
1719
CacheItem,

src/passport-saml/multiSamlStrategy.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import * as util from "util";
21
import { SAML } from "../node-saml";
3-
import SamlStrategy = require("./strategy");
2+
import { AbstractStrategy } from "./strategy";
43
import type { Request } from "express";
54
import {
65
AuthenticateOptions,
@@ -11,9 +10,8 @@ import {
1110
VerifyWithRequest,
1211
} from "./types";
1312

14-
class MultiSamlStrategy extends SamlStrategy {
13+
export class MultiSamlStrategy extends AbstractStrategy {
1514
static readonly newSamlProviderOnConstruct = false;
16-
1715
_options: SamlConfig & MultiSamlConfig;
1816

1917
constructor(options: MultiSamlConfig, verify: VerifyWithRequest);
@@ -63,7 +61,6 @@ class MultiSamlStrategy extends SamlStrategy {
6361
});
6462
}
6563

66-
/** @ts-expect-error typescript disallows changing method signature in a subclass */
6764
generateServiceProviderMetadata(
6865
req: Request,
6966
decryptionCert: string | null,
@@ -84,7 +81,7 @@ class MultiSamlStrategy extends SamlStrategy {
8481
Object.setPrototypeOf(strategy, this);
8582
return callback(
8683
null,
87-
super.generateServiceProviderMetadata.call(strategy, decryptionCert, signingCert)
84+
this._generateServiceProviderMetadata.call(strategy, decryptionCert, signingCert)
8885
);
8986
});
9087
}
@@ -94,5 +91,3 @@ class MultiSamlStrategy extends SamlStrategy {
9491
super.error(err);
9592
}
9693
}
97-
98-
export = MultiSamlStrategy;

src/passport-saml/strategy.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
} from "./types";
1111
import { Profile } from "./types";
1212

13-
class Strategy extends PassportStrategy {
14-
static readonly newSamlProviderOnConstruct = true;
13+
export abstract class AbstractStrategy extends PassportStrategy {
14+
static readonly newSamlProviderOnConstruct: boolean;
1515

1616
name: string;
1717
_verify: VerifyWithRequest | VerifyWithoutRequest;
@@ -178,7 +178,7 @@ class Strategy extends PassportStrategy {
178178
.catch((err) => callback(err));
179179
}
180180

181-
generateServiceProviderMetadata(
181+
protected _generateServiceProviderMetadata(
182182
decryptionCert: string | null,
183183
signingCert?: string | null
184184
): string {
@@ -195,4 +195,13 @@ class Strategy extends PassportStrategy {
195195
}
196196
}
197197

198-
export = Strategy;
198+
export class Strategy extends AbstractStrategy {
199+
static readonly newSamlProviderOnConstruct = true;
200+
201+
generateServiceProviderMetadata(
202+
decryptionCert: string | null,
203+
signingCert?: string | null
204+
): string {
205+
return this._generateServiceProviderMetadata(decryptionCert, signingCert);
206+
}
207+
}

test/passport-saml/multiSamlStrategy.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"use strict";
22
import * as express from "express";
3+
import { Strategy } from "passport-strategy";
34
import * as sinon from "sinon";
45
import * as should from "should";
5-
import { Strategy as SamlStrategy, MultiSamlStrategy, SAML } from "../../src/passport-saml";
6+
import { MultiSamlStrategy, SAML, AbstractStrategy } from "../../src/passport-saml";
67
import {
78
MultiSamlConfig,
89
SamlOptionsCallback,
@@ -20,7 +21,8 @@ describe("MultiSamlStrategy()", function () {
2021
return { cert: FAKE_CERT };
2122
}
2223
const strategy = new MultiSamlStrategy({ getSamlOptions }, noop);
23-
strategy.should.be.an.instanceOf(SamlStrategy);
24+
strategy.should.be.an.instanceOf(AbstractStrategy);
25+
strategy.should.be.an.instanceOf(Strategy);
2426
});
2527

2628
it("throws if wrong finder is provided", function () {
@@ -33,7 +35,7 @@ describe("MultiSamlStrategy()", function () {
3335

3436
describe("MultiSamlStrategy#authenticate", function () {
3537
beforeEach(function () {
36-
this.superAuthenticateStub = sinon.stub(SamlStrategy.prototype, "authenticate");
38+
this.superAuthenticateStub = sinon.stub(AbstractStrategy.prototype, "authenticate");
3739
});
3840

3941
afterEach(function () {
@@ -154,7 +156,7 @@ describe("MultiSamlStrategy#authorize", function () {
154156

155157
describe("MultiSamlStrategy#logout", function () {
156158
beforeEach(function () {
157-
this.superLogoutMock = sinon.stub(SamlStrategy.prototype, "logout");
159+
this.superLogoutMock = sinon.stub(AbstractStrategy.prototype, "logout");
158160
});
159161

160162
afterEach(function () {
@@ -229,7 +231,7 @@ describe("MultiSamlStrategy#logout", function () {
229231
describe("MultiSamlStrategy#generateServiceProviderMetadata", function () {
230232
beforeEach(function () {
231233
this.superGenerateServiceProviderMetadata = sinon
232-
.stub(SamlStrategy.prototype, "generateServiceProviderMetadata")
234+
.stub(SAML.prototype, "generateServiceProviderMetadata")
233235
.returns("My Metadata Result");
234236
});
235237

0 commit comments

Comments
 (0)