Skip to content

Commit ec5577d

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 3a486db commit ec5577d

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

src/passport-saml/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CacheItem, CacheProvider } from "./inmemory-cache-provider";
22
import { SAML } from "./saml";
3-
import Strategy = require("./strategy");
3+
import { Strategy } from "./strategy";
44
import MultiSamlStrategy = require("./multiSamlStrategy");
55
import type {
66
Profile,

src/passport-saml/multiSamlStrategy.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as util from "util";
22
import * as saml from "./saml";
33
import { CacheProvider as InMemoryCacheProvider } from "./inmemory-cache-provider";
4-
import SamlStrategy = require("./strategy");
4+
import { AbstractStrategy } from "./strategy";
55
import type { Request } from "express";
66
import {
77
AuthenticateOptions,
@@ -13,7 +13,7 @@ import {
1313
VerifyWithRequest,
1414
} from "./types";
1515

16-
class MultiSamlStrategy extends SamlStrategy {
16+
class MultiSamlStrategy extends AbstractStrategy {
1717
static readonly newSamlProviderOnConstruct = false;
1818

1919
_options: SamlConfig & MultiSamlConfig;
@@ -65,7 +65,6 @@ class MultiSamlStrategy extends SamlStrategy {
6565
});
6666
}
6767

68-
/** @ts-expect-error typescript disallows changing method signature in a subclass */
6968
generateServiceProviderMetadata(
7069
req: Request,
7170
decryptionCert: string | null,
@@ -86,7 +85,7 @@ class MultiSamlStrategy extends SamlStrategy {
8685
Object.setPrototypeOf(strategy, this);
8786
return callback(
8887
null,
89-
super.generateServiceProviderMetadata.call(strategy, decryptionCert, signingCert)
88+
this._generateServiceProviderMetadata.call(strategy, decryptionCert, signingCert)
9089
);
9190
});
9291
}

src/passport-saml/strategy.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import {
1212
} from "./types";
1313
import { Profile } from "./types";
1414

15-
class Strategy extends PassportStrategy {
16-
static readonly newSamlProviderOnConstruct = true;
15+
export abstract class AbstractStrategy extends PassportStrategy {
1716

1817
name: string;
1918
_verify: VerifyWithRequest | VerifyWithoutRequest;
@@ -172,7 +171,7 @@ class Strategy extends PassportStrategy {
172171
.catch((err) => callback(err));
173172
}
174173

175-
generateServiceProviderMetadata(
174+
protected _generateServiceProviderMetadata(
176175
decryptionCert: string | null,
177176
signingCert?: string | null
178177
): string {
@@ -189,4 +188,13 @@ class Strategy extends PassportStrategy {
189188
}
190189
}
191190

192-
export = Strategy;
191+
export class Strategy extends AbstractStrategy {
192+
static readonly newSamlProviderOnConstruct = true;
193+
194+
generateServiceProviderMetadata(
195+
decryptionCert: string | null,
196+
signingCert?: string | null
197+
): string {
198+
return this._generateServiceProviderMetadata(decryptionCert, signingCert);
199+
}
200+
}

test/multiSamlStrategy.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
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 } from "../src/passport-saml";
6+
import { AbstractStrategy } from "../src/passport-saml/strategy";
67
import {
78
MultiSamlConfig,
89
SamlOptionsCallback,
@@ -20,7 +21,7 @@ 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);
2425
});
2526

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

3435
describe("MultiSamlStrategy#authenticate", function () {
3536
beforeEach(function () {
36-
this.superAuthenticateStub = sinon.stub(SamlStrategy.prototype, "authenticate");
37+
this.superAuthenticateStub = sinon.stub(AbstractStrategy.prototype, "authenticate");
3738
});
3839

3940
afterEach(function () {
@@ -154,7 +155,7 @@ describe("MultiSamlStrategy#authorize", function () {
154155

155156
describe("MultiSamlStrategy#logout", function () {
156157
beforeEach(function () {
157-
this.superLogoutMock = sinon.stub(SamlStrategy.prototype, "logout");
158+
this.superLogoutMock = sinon.stub(AbstractStrategy.prototype, "logout");
158159
});
159160

160161
afterEach(function () {
@@ -229,7 +230,7 @@ describe("MultiSamlStrategy#logout", function () {
229230
describe("MultiSamlStrategy#generateServiceProviderMetadata", function () {
230231
beforeEach(function () {
231232
this.superGenerateServiceProviderMetadata = sinon
232-
.stub(SamlStrategy.prototype, "generateServiceProviderMetadata")
233+
.stub(AbstractStrategy.prototype as any, "_generateServiceProviderMetadata") // force type to any to ignore TS protected property
233234
.returns("My Metadata Result");
234235
});
235236

0 commit comments

Comments
 (0)