Skip to content

Commit 2e10f6d

Browse files
committed
Improve the typing of the Strategy class hierarchy.
The current expected ts error causes problems with some earlier version of TS. Also, it does not make any sense to have a method overide a method with a different signature, just because they have the same name.
1 parent 0798e4d commit 2e10f6d

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

src/passport-saml/index.ts

+4-2
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

+3-8
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

+13-4
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

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as express from "express";
33
import * as sinon from "sinon";
44
import * as should from "should";
5-
import { Strategy as SamlStrategy, MultiSamlStrategy, SAML } from "../../src/passport-saml";
5+
import { MultiSamlStrategy, SAML, AbstractStrategy } from "../../src/passport-saml";
66
import {
77
MultiSamlConfig,
88
SamlOptionsCallback,
@@ -20,7 +20,7 @@ describe("MultiSamlStrategy()", function () {
2020
return { cert: FAKE_CERT };
2121
}
2222
const strategy = new MultiSamlStrategy({ getSamlOptions }, noop);
23-
strategy.should.be.an.instanceOf(SamlStrategy);
23+
strategy.should.be.an.instanceOf(AbstractStrategy);
2424
});
2525

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

3434
describe("MultiSamlStrategy#authenticate", function () {
3535
beforeEach(function () {
36-
this.superAuthenticateStub = sinon.stub(SamlStrategy.prototype, "authenticate");
36+
this.superAuthenticateStub = sinon.stub(AbstractStrategy.prototype, "authenticate");
3737
});
3838

3939
afterEach(function () {
@@ -154,7 +154,7 @@ describe("MultiSamlStrategy#authorize", function () {
154154

155155
describe("MultiSamlStrategy#logout", function () {
156156
beforeEach(function () {
157-
this.superLogoutMock = sinon.stub(SamlStrategy.prototype, "logout");
157+
this.superLogoutMock = sinon.stub(AbstractStrategy.prototype, "logout");
158158
});
159159

160160
afterEach(function () {
@@ -229,7 +229,7 @@ describe("MultiSamlStrategy#logout", function () {
229229
describe("MultiSamlStrategy#generateServiceProviderMetadata", function () {
230230
beforeEach(function () {
231231
this.superGenerateServiceProviderMetadata = sinon
232-
.stub(SamlStrategy.prototype, "generateServiceProviderMetadata")
232+
.stub(SAML.prototype, "generateServiceProviderMetadata")
233233
.returns("My Metadata Result");
234234
});
235235

0 commit comments

Comments
 (0)