aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjw2552018-03-04 16:27:28 +0000
committerjw2552018-03-04 16:27:28 +0000
commit68729be329d853abc121482bc0f19b9305d4a51f (patch)
treef1878bb8bf1b82c048fee5e3779c499475686a27 /src
parentstarted authenticating (diff)
parentImplement handling of unauthorised requests (diff)
downloadmedicine-68729be329d853abc121482bc0f19b9305d4a51f.tar.gz
medicine-68729be329d853abc121482bc0f19b9305d4a51f.zip

Merge branch ‘trh/payload-checker’ into testing-middleware

Diffstat (limited to 'src')
-rw-r--r--src/app.ts11
-rw-r--r--src/auth.ts29
-rw-r--r--src/errors/errorware.ts24
3 files changed, 37 insertions, 27 deletions
diff --git a/src/app.ts b/src/app.ts
index 8f86717..1ac0e5e 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -4,7 +4,7 @@ import * as expressJwt from 'express-jwt';
import * as logger from 'morgan';
import * as passport from 'passport';
-import { unauthorisedErr, default as authRouter } from './auth';
+import { default as authRouter, unauthorisedErr } from './auth';
import { RequestError, errorHandler } from './errors/errorware';
import FileRouter from './FileRouter';
@@ -15,7 +15,7 @@ import { default as User } from './db/model/User';
class App {
public express: ex.Express;
- private logEnabled: boolean = true;
+ private readonly logEnabled: boolean = true;
public constructor(enableLog: boolean) {
this.logEnabled = enableLog;
@@ -66,17 +66,16 @@ class App {
status: 'error',
error : 'invalid_route',
error_description: 'Endpoints start from /cs3099group-be-4/\n'
- +' Please refer to \'https://github.com/CS3099JH2017/cs3099jh/blob/master/protocols/BE01.md\''
+ + ' Please refer to \'https://github.com/CS3099JH2017/cs3099jh/blob/master/protocols/BE01.md\''
+ ' for further details.'
});
- })
+ });
this.express.use('/cs3099group-be-4', FileRouter);
this.express.use('/cs3099group-be-4', authRouter);
this.express.use('/', defRouter);
}
private errorware(): void {
- // TODO: this.express.use(authRouter.unauthorisedErr);
this.express.use(unauthorisedErr);
this.express.use(errorHandler);
}
@@ -198,6 +197,6 @@ class App {
}
export default new App(true).express;
-export function TestApp(){
+export function TestApp() {
return new App(false).express;
}
diff --git a/src/auth.ts b/src/auth.ts
index b40cddb..083357e 100644
--- a/src/auth.ts
+++ b/src/auth.ts
@@ -7,6 +7,8 @@ import * as passport from 'passport';
import { Strategy } from 'passport-local';
import { default as User } from './db/model/User';
+import { Errorware } from './errors/errorware';
+
export class AuthRouter {
public router: Router;
@@ -74,19 +76,6 @@ export class AuthRouter {
next();
}
- // Handle error if user is unauthorised
- public unauthorisedErr(
- err: Error, req: Request, res: Response, next: NextFunction
- ): void {
- if (err.name === 'UnauthorizedError') {
- next(new RequestError(
- 401, 'not_authorised',
- 'user does not have correct authorisation for task'
- ));
- }
- next();
- }
-
// Middleware to check privileges - admin
public isAdmin(req: Request, res: Response, next: NextFunction): void {
if (!req.user.admin) {
@@ -127,6 +116,20 @@ export class AuthRouter {
}
}
+// Handle error if user is unauthorised
+export const unauthorisedErr: Errorware =
+ (err: Error, req: Request, res: Response, next: NextFunction): void => {
+ console.log('fffff');
+ if (err.name === 'UnauthorizedError') {
+ console.log('efewfwf');
+ return next(new RequestError(
+ 401, 'not_authorised',
+ 'user does not have correct authorisation for task'
+ ));
+ }
+ next(err);
+};
+
const authRoutes: AuthRouter = new AuthRouter();
authRoutes.init();
diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts
index 45a76b3..e2355de 100644
--- a/src/errors/errorware.ts
+++ b/src/errors/errorware.ts
@@ -10,6 +10,13 @@ export interface ErrorResponseBlock {
}
export class RequestError extends Error {
+ public static is(test: Object): test is RequestError {
+ const err: RequestError = test as RequestError;
+ return err.code !== undefined
+ && err.name !== undefined
+ && err.responseBlock !== undefined;
+ }
+
public code: number;
public userMessage?: string;
// tslint:disable-next-line:no-any
@@ -41,16 +48,17 @@ export class RequestError extends Error {
}
}
-type Errorware =
+export type Errorware =
(err: Error, req: Request, res: Response, next: NextFunction) => void;
export const errorHandler: Errorware =
(err: Error, req: Request, res: Response, next: NextFunction): void => {
- if ((err as RequestError).responseBlock) {
- const rerr: RequestError = err as RequestError;
- res.status(rerr.code)
- .json(rerr.responseBlock());
- } else {
- res.send(err);
- }
+ console.log(err);
+ if (RequestError.is(err)) {
+ console.log('fewkuihfklsfv');
+ res.status(err.code)
+ .json(err.responseBlock());
+ } else {
+ res.send(err);
+ }
};