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 Apr 5, 2021
1 parent 0798e4d commit e37ee06
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
6 changes: 4 additions & 2 deletions src/passport-saml/index.ts
@@ -1,7 +1,8 @@
import type { CacheItem, CacheProvider } from "../node-saml/inmemory-cache-provider";
import { SAML } from "../node-saml";
import Strategy = require("./strategy");
import MultiSamlStrategy = require("./multiSamlStrategy");
import { Strategy, AbstractStrategy } from "./strategy";
import { MultiSamlStrategy } from "./multiSamlStrategy";

import type {
Profile,
SamlConfig,
Expand All @@ -12,6 +13,7 @@ import type {

export {
SAML,
AbstractStrategy,
Strategy,
MultiSamlStrategy,
CacheItem,
Expand Down
11 changes: 3 additions & 8 deletions src/passport-saml/multiSamlStrategy.ts
@@ -1,6 +1,5 @@
import * as util from "util";
import { SAML } from "../node-saml";
import SamlStrategy = require("./strategy");
import { AbstractStrategy } from "./strategy";
import type { Request } from "express";
import {
AuthenticateOptions,
Expand All @@ -11,9 +10,8 @@ import {
VerifyWithRequest,
} from "./types";

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

_options: SamlConfig & MultiSamlConfig;

constructor(options: MultiSamlConfig, verify: VerifyWithRequest);
Expand Down Expand Up @@ -63,7 +61,6 @@ class MultiSamlStrategy extends SamlStrategy {
});
}

/** @ts-expect-error typescript disallows changing method signature in a subclass */
generateServiceProviderMetadata(
req: Request,
decryptionCert: string | null,
Expand All @@ -84,7 +81,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 All @@ -94,5 +91,3 @@ class MultiSamlStrategy extends SamlStrategy {
super.error(err);
}
}

export = MultiSamlStrategy;
17 changes: 13 additions & 4 deletions src/passport-saml/strategy.ts
Expand Up @@ -10,8 +10,8 @@ import {
} from "./types";
import { Profile } from "./types";

class Strategy extends PassportStrategy {
static readonly newSamlProviderOnConstruct = true;
export abstract class AbstractStrategy extends PassportStrategy {
static readonly newSamlProviderOnConstruct: boolean;

name: string;
_verify: VerifyWithRequest | VerifyWithoutRequest;
Expand Down Expand Up @@ -178,7 +178,7 @@ class Strategy extends PassportStrategy {
.catch((err) => callback(err));
}

generateServiceProviderMetadata(
protected _generateServiceProviderMetadata(
decryptionCert: string | null,
signingCert?: string | null
): string {
Expand All @@ -195,4 +195,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);
}
}
10 changes: 5 additions & 5 deletions test/passport-saml/multiSamlStrategy.spec.ts
Expand Up @@ -2,7 +2,7 @@
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, AbstractStrategy } from "../../src/passport-saml";
import {
MultiSamlConfig,
SamlOptionsCallback,
Expand All @@ -20,7 +20,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 +33,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 +154,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 +229,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 e37ee06

Please sign in to comment.