Skip to content

Commit

Permalink
Merge fix-passport-package
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jan 18, 2020
2 parents 1bc8897 + c0fd24b commit 32f20cd
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 30 deletions.
2 changes: 0 additions & 2 deletions examples/passportjs/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {GlobalAcceptMimesMiddleware, ServerLoader, ServerSettings} from "@tsed/c
import "@tsed/swagger";
import {CalendarCtrl} from "./controllers/calendars/CalendarCtrl";
import {PassportCtrl} from "./controllers/passport/PassportCtrl";
import {RestCtrl} from "./controllers/RestCtrl";

const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
Expand All @@ -21,7 +20,6 @@ const rootDir = __dirname;
},
mount: {
"/rest": [
RestCtrl,
CalendarCtrl,
PassportCtrl
]
Expand Down
13 changes: 0 additions & 13 deletions examples/passportjs/src/controllers/RestCtrl.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/common/src/mvc/builders/HandlerBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class HandlerBuilder {
*
* @returns {any}
*/
public build(injector: InjectorService): any {
public build(injector: InjectorService): (...args: any[]) => void {
const {hasErrorParam} = this.handlerMetadata;

this.debug = injector.settings.debug;
Expand Down
3 changes: 1 addition & 2 deletions packages/passport/src/PassportModule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {BeforeRoutesInit, Constant, ExpressApplication, Module, OnInit, Provider, ProviderScope} from "@tsed/common";

import * as Passport from "passport";
import {ProtocolRegistry} from "./registries/ProtocolRegistries";
import {PassportSerializerService} from "./services/PassportSerializerService";
import {ProtocolsService} from "./services/ProtocolsService";

Expand All @@ -25,7 +24,7 @@ export class PassportModule implements OnInit, BeforeRoutesInit {
Passport.serializeUser(this.passportSerializer.serialize.bind(this.passportSerializer));
Passport.deserializeUser(this.passportSerializer.deserialize.bind(this.passportSerializer));

ProtocolRegistry.forEach((provider: Provider<any>) => this.protocolsService.invoke(provider));
this.protocolsService.getProtocols().forEach((provider: Provider<any>) => this.protocolsService.invoke(provider));

return undefined;
}
Expand Down
5 changes: 1 addition & 4 deletions packages/passport/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ export * from "./decorators/authenticate";
export * from "./decorators/overrideProtocol";
export * from "./decorators/protocol";

export * from "./interfaces/IProtocolOptions";
export * from "./interfaces/IProtocol";
export * from "./interfaces/OnInstall";
export * from "./interfaces/OnVerify";
export * from "./interfaces";

export * from "./controllers/PassportCtrl";
export * from "./protocols/LocalProtocol";
Expand Down
22 changes: 22 additions & 0 deletions packages/passport/src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Type} from "@tsed/core";
import {IProtocolOptions} from "./IProtocolOptions";

declare global {
namespace TsED {
interface Configuration {
passport: {
userProperty?: string;
pauseStream?: string;
userInfoModel?: Type<any>;
protocols: {
[protocolName: string]: IProtocolOptions;
};
};
}
}
}

export * from "./IProtocolOptions";
export * from "./IProtocol";
export * from "./OnInstall";
export * from "./OnVerify";
5 changes: 3 additions & 2 deletions packages/passport/src/protocols/LocalProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {OnVerify} from "../interfaces/OnVerify";
}
})
export class LocalProtocol implements OnVerify, OnInstall {
static REG_MAIL = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

$onVerify(@Req() request: Req, @BodyParams() credentials: any): Promise<any> | any {
const {email} = credentials;

Expand All @@ -27,8 +29,7 @@ export class LocalProtocol implements OnVerify, OnInstall {
$onInstall(strategy: Strategy): void {}

protected checkEmail(email: string) {
const regEmail = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!(email && regEmail.test(email))) {
if (!(email && LocalProtocol.REG_MAIL.test(email))) {
throw new BadRequest("Email is invalid");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class PassportSerializerService {
try {
const obj = this.converterService.serialize(user);

// remove password from serialized object
delete obj.password;

done(null, JSON.stringify(obj));
Expand Down
15 changes: 9 additions & 6 deletions packages/passport/src/services/ProtocolsService.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import {HandlerBuilder, HandlerMetadata, HandlerType, InjectorService, Service} from "@tsed/common";
import {Provider} from "@tsed/di";
import * as Express from "express";
import * as Passport from "passport";
import {Strategy} from "passport-strategy";
import {IProtocol} from "../interfaces/IProtocol";
import {IProtocolOptions} from "../interfaces/IProtocolOptions";
import {IProtocol, IProtocolOptions} from "../interfaces";
import {PROVIDER_TYPE_PROTOCOL} from "../registries/ProtocolRegistries";

@Service()
export class ProtocolsService {
readonly strategies: Map<string, Strategy> = new Map();

constructor(private injector: InjectorService) {}

invoke(provider: Provider<any>): any {
public getProtocols(): Provider<any>[] {
return Array.from(this.injector.getProviders(PROVIDER_TYPE_PROTOCOL));
}

public invoke(provider: Provider<any>): any {
const {name, useStrategy: strategy, settings} = this.getOptions(provider);
const handler = this.createHandler(provider);
const protocol = this.injector.get<IProtocol>(provider.provide)!;
Expand Down Expand Up @@ -56,9 +59,9 @@ export class ProtocolsService {
});

const builder = new HandlerBuilder(handlerMetadata);
const middleware: any = builder.build(this.injector);
const middleware = builder.build(this.injector);

return (req: Express.Request, ...args: any[]) => {
return (req: any, ...args: any[]) => {
const done = args[args.length - 1];

return middleware(req, req.res, (err: any) => {
Expand Down

0 comments on commit 32f20cd

Please sign in to comment.