Skip to content

Commit

Permalink
fix(@tsed/passport): Use originalUrl when authorize and authenticate …
Browse files Browse the repository at this point in the history
…are used on method.
  • Loading branch information
Romain Lenzotti committed May 27, 2020
1 parent 3ba44be commit c84a1ce
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
21 changes: 20 additions & 1 deletion packages/passport/src/decorators/authenticate.spec.ts
Expand Up @@ -16,7 +16,26 @@ describe("@Authenticate", () => {
options: {
security: "security"
},
protocol: "local"
protocol: "local",
originalUrl: true
});
});
it("should store data (without originalUrl)", () => {
class Test {
@Authenticate("local", {security: "security", originalUrl: false})
test() {}
}

const store = Store.fromMethod(Test, "test");

expect(store.get(PassportMiddleware)).to.deep.equal({
method: "authenticate",
options: {
security: "security",
originalUrl: false
},
protocol: "local",
originalUrl: false
});
});
});
10 changes: 8 additions & 2 deletions packages/passport/src/decorators/authenticate.ts
@@ -1,12 +1,18 @@
import {UseAuth} from "@tsed/common";
import {AuthenticateOptions} from "passport";
import {AuthenticateOptions as PassportAuthenticateOptions} from "passport";
import {PassportMiddleware} from "../middlewares/PassportMiddleware";

export function Authenticate(protocol: string | string[] = "*", options: AuthenticateOptions & {security?: any} = {}): Function {
export interface AuthenticateOptions extends PassportAuthenticateOptions {
security?: any;
originalUrl?: boolean;
}

export function Authenticate(protocol: string | string[] = "*", options: AuthenticateOptions = {}): Function {
return UseAuth(PassportMiddleware, {
protocol,
method: "authenticate",
security: options.security,
originalUrl: options.originalUrl === undefined ? true : options.originalUrl,
options
});
}
19 changes: 19 additions & 0 deletions packages/passport/src/decorators/authorize.spec.ts
Expand Up @@ -16,6 +16,25 @@ describe("@Authorize", () => {
options: {
security: "security"
},
originalUrl: true,
protocol: "local"
});
});
it("should store data (without originalUrl)", () => {
class Test {
@Authorize("local", {security: "security", originalUrl: false})
test() {}
}

const store = Store.fromMethod(Test, "test");

expect(store.get(PassportMiddleware)).to.deep.equal({
method: "authorize",
options: {
security: "security",
originalUrl: false
},
originalUrl: false,
protocol: "local"
});
});
Expand Down
8 changes: 7 additions & 1 deletion packages/passport/src/decorators/authorize.ts
Expand Up @@ -2,11 +2,17 @@ import {UseAuth} from "@tsed/common";
import {AuthenticateOptions} from "passport";
import {PassportMiddleware} from "../middlewares/PassportMiddleware";

export function Authorize(protocol: string | string[] = "*", options: AuthenticateOptions & {security?: any} = {}): Function {
export interface AuthorizeOptions extends AuthenticateOptions {
security?: any;
originalUrl?: boolean;
}

export function Authorize(protocol: string | string[] = "*", options: AuthorizeOptions = {}): Function {
return UseAuth(PassportMiddleware, {
protocol,
method: "authorize",
security: options.security,
originalUrl: options.originalUrl === undefined ? true : options.originalUrl,
options
});
}
5 changes: 4 additions & 1 deletion packages/passport/src/middlewares/PassportMiddleware.spec.ts
@@ -1,7 +1,7 @@
import {Unauthorized} from "@tsed/exceptions";
import {expect} from "chai";
import * as Passport from "passport";
import * as Sinon from "sinon";
import {Unauthorized} from "@tsed/exceptions";
import {PassportMiddleware} from "./PassportMiddleware";

const sandbox = Sinon.createSandbox();
Expand Down Expand Up @@ -99,6 +99,8 @@ describe("PassportMiddleware", () => {
} as any;

const request: any = {
url: "/",
originalUrl: "/rest",
query: {
protocol: "basic"
}
Expand All @@ -117,6 +119,7 @@ describe("PassportMiddleware", () => {
middleware.use(request, endpoint);

// THEN
expect(request.url).to.eq(request.originalUrl);
Passport.authenticate.should.have.been.calledWithExactly("basic", {});
});
it("should throw errors", () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/passport/src/middlewares/PassportMiddleware.ts
@@ -1,6 +1,6 @@
import {EndpointInfo, Inject, Middleware, Req} from "@tsed/common";
import * as Passport from "passport";
import {Unauthorized} from "@tsed/exceptions";
import * as Passport from "passport";
import {ProtocolsService} from "../services/ProtocolsService";
import {getProtocolsFromRequest} from "../utils/getProtocolsFromRequest";

Expand All @@ -14,13 +14,17 @@ export class PassportMiddleware {
return;
}

const {options, protocol, method} = endpoint.store.get(PassportMiddleware);
const {options, protocol, method, originalUrl = true} = endpoint.store.get(PassportMiddleware);
const protocols = getProtocolsFromRequest(request, protocol, this.protocolsService.getProtocolsNames());

if (protocols.length === 0) {
throw new Unauthorized("Not authorized");
}

if (originalUrl) {
request.url = request.originalUrl;
}

// @ts-ignore
return Passport[method](protocols.length === 1 ? protocols[0] : protocols, options);
}
Expand Down

0 comments on commit c84a1ce

Please sign in to comment.