Skip to content

Commit

Permalink
Improve the typing of the Strategy class hierarchy
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
forty committed Mar 22, 2021
1 parent 3a486db commit 9835da1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/passport-saml/index.ts
@@ -1,6 +1,6 @@
import type { CacheItem, CacheProvider } from "./inmemory-cache-provider";
import { SAML } from "./saml";
import Strategy = require("./strategy");
import { Strategy } from "./strategy";
import MultiSamlStrategy = require("./multiSamlStrategy");
import type {
Profile,
Expand Down
7 changes: 3 additions & 4 deletions src/passport-saml/multiSamlStrategy.ts
@@ -1,7 +1,7 @@
import * as util from "util";
import * as saml from "./saml";
import { CacheProvider as InMemoryCacheProvider } from "./inmemory-cache-provider";
import SamlStrategy = require("./strategy");
import { AbstractStrategy } from "./strategy";
import type { Request } from "express";
import {
AuthenticateOptions,
Expand All @@ -13,7 +13,7 @@ import {
VerifyWithRequest,
} from "./types";

class MultiSamlStrategy extends SamlStrategy {
class MultiSamlStrategy extends AbstractStrategy {
static readonly newSamlProviderOnConstruct = false;

_options: SamlConfig & MultiSamlConfig;
Expand Down Expand Up @@ -65,7 +65,6 @@ class MultiSamlStrategy extends SamlStrategy {
});
}

/** @ts-expect-error typescript disallows changing method signature in a subclass */
generateServiceProviderMetadata(
req: Request,
decryptionCert: string | null,
Expand All @@ -86,7 +85,7 @@ class MultiSamlStrategy extends SamlStrategy {
Object.setPrototypeOf(strategy, this);
return callback(
null,
super.generateServiceProviderMetadata.call(strategy, decryptionCert, signingCert)
this._generateServiceProviderMetadata.call(strategy, decryptionCert, signingCert)
);
});
}
Expand Down
17 changes: 12 additions & 5 deletions src/passport-saml/strategy.ts
Expand Up @@ -12,9 +12,7 @@ import {
} from "./types";
import { Profile } from "./types";

class Strategy extends PassportStrategy {
static readonly newSamlProviderOnConstruct = true;

export abstract class AbstractStrategy extends PassportStrategy {
name: string;
_verify: VerifyWithRequest | VerifyWithoutRequest;
_saml: saml.SAML | undefined;
Expand Down Expand Up @@ -172,7 +170,7 @@ class Strategy extends PassportStrategy {
.catch((err) => callback(err));
}

generateServiceProviderMetadata(
protected _generateServiceProviderMetadata(
decryptionCert: string | null,
signingCert?: string | null
): string {
Expand All @@ -189,4 +187,13 @@ class Strategy extends PassportStrategy {
}
}

export = Strategy;
export class Strategy extends AbstractStrategy {
static readonly newSamlProviderOnConstruct = true;

generateServiceProviderMetadata(
decryptionCert: string | null,
signingCert?: string | null
): string {
return this._generateServiceProviderMetadata(decryptionCert, signingCert);
}
}
11 changes: 6 additions & 5 deletions test/multiSamlStrategy.spec.ts
Expand Up @@ -2,7 +2,8 @@
import * as express from "express";
import * as sinon from "sinon";
import * as should from "should";
import { Strategy as SamlStrategy, MultiSamlStrategy, SAML } from "../src/passport-saml";
import { MultiSamlStrategy, SAML } from "../src/passport-saml";
import { AbstractStrategy } from "../src/passport-saml/strategy";
import {
MultiSamlConfig,
SamlOptionsCallback,
Expand All @@ -20,7 +21,7 @@ describe("MultiSamlStrategy()", function () {
return { cert: FAKE_CERT };
}
const strategy = new MultiSamlStrategy({ getSamlOptions }, noop);
strategy.should.be.an.instanceOf(SamlStrategy);
strategy.should.be.an.instanceOf(AbstractStrategy);
});

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

describe("MultiSamlStrategy#authenticate", function () {
beforeEach(function () {
this.superAuthenticateStub = sinon.stub(SamlStrategy.prototype, "authenticate");
this.superAuthenticateStub = sinon.stub(AbstractStrategy.prototype, "authenticate");
});

afterEach(function () {
Expand Down Expand Up @@ -154,7 +155,7 @@ describe("MultiSamlStrategy#authorize", function () {

describe("MultiSamlStrategy#logout", function () {
beforeEach(function () {
this.superLogoutMock = sinon.stub(SamlStrategy.prototype, "logout");
this.superLogoutMock = sinon.stub(AbstractStrategy.prototype, "logout");
});

afterEach(function () {
Expand Down Expand Up @@ -229,7 +230,7 @@ describe("MultiSamlStrategy#logout", function () {
describe("MultiSamlStrategy#generateServiceProviderMetadata", function () {
beforeEach(function () {
this.superGenerateServiceProviderMetadata = sinon
.stub(SamlStrategy.prototype, "generateServiceProviderMetadata")
.stub(AbstractStrategy.prototype as any, "_generateServiceProviderMetadata") // force type to any to ignore TS protected property
.returns("My Metadata Result");
});

Expand Down

0 comments on commit 9835da1

Please sign in to comment.