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

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

-rw-r--r--.gitignore1
-rw-r--r--.yarnclean45
-rw-r--r--package_old.json68
-rw-r--r--src/app.ts11
-rw-r--r--src/auth.ts29
-rw-r--r--src/errors/errorware.ts24
6 files changed, 83 insertions, 95 deletions
diff --git a/.gitignore b/.gitignore
index d3a47b9..fdd6ed3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,6 +11,7 @@ dist/
# PostreSQL log files
POSTGRES_logfile
+*logfile
# Vim swap files
*.sw*
diff --git a/.yarnclean b/.yarnclean
new file mode 100644
index 0000000..b591611
--- /dev/null
+++ b/.yarnclean
@@ -0,0 +1,45 @@
+# test directories
+__tests__
+test
+tests
+powered-test
+
+# asset directories
+docs
+doc
+website
+images
+assets
+
+# examples
+example
+examples
+
+# code coverage directories
+coverage
+.nyc_output
+
+# build scripts
+Makefile
+Gulpfile.js
+Gruntfile.js
+
+# configs
+appveyor.yml
+circle.yml
+codeship-services.yml
+codeship-steps.yml
+wercker.yml
+.tern-project
+.gitattributes
+.editorconfig
+.*ignore
+.eslintrc
+.jshintrc
+.flowconfig
+.documentup.json
+.yarn-metadata.json
+.travis.yml
+
+# misc
+*.md
diff --git a/package_old.json b/package_old.json
deleted file mode 100644
index 35f0f7f..0000000
--- a/package_old.json
+++ /dev/null
@@ -1,68 +0,0 @@
-{
- "name": "cs3099jhbe4",
- "description": "2017 Junior Honours Project - Backend Group 4",
- "main": "index.ts",
- "license": "UNLICENSED",
- "scripts": {
- "test": "mocha --reporter spec -r ts-node/register './test/**/*.test.ts'",
- "test:routes": "mocha --reporter spec -r ts-node/register './test/**/*.routes.test.ts'",
- "test:unit": "mocha --reporter spec -r ts-node/register './test/**/*.unit.test.ts'",
- "docs": "typedoc --out 'docs' --readme 'README.md' --name 'St Andrews Junior Honours 2017/18 - Team BE4' --module commonjs --target ES5 --exclude '*.test.ts' --includeDeclarations --excludeExternals 'src'",
- "start": "ts-node src/index.ts",
- "lint": "tslint --fix --force -p './tsconfig.json'",
- "_gl-lint": "tslint -p './tsconfig.json'"
- },
- "repository": {
- "type": "git",
- "url": "https://gitlab.cs.st-andrews.ac.uk/cs3099group-be-4/project-code.git"
- },
- "keywords": [
- "backend",
- "database",
- "university",
- "honours",
- "project"
- ],
- "author": "2017 Junior Honours Backend Group 4",
- "devDependencies": {
- "@types/chai": "^3.4.34",
- "@types/chai-http": "0.0.29",
- "@types/debug": "0.0.29",
- "@types/mocha": "^2.2.32",
- "@types/mocha-each": "^1.1.0",
- "chai": "^3.5.0",
- "chai-http": "^3.0.0",
- "mocha": "^5.0.1",
- "nodemon": "^1.14.10",
- "tslint": "^5.9.1",
- "typedoc": "^0.10.0"
- },
- "dependencies": {
- "@types/body-parser": "0.0.33",
- "@types/express": "^4.0.33",
- "@types/express-jwt": "^0.0.38",
- "@types/jsonwebtoken": "^7.2.5",
- "@types/morgan": "^1.7.32",
- "@types/node": "^6.0.46",
- "@types/passport": "^0.4.2",
- "@types/passport-local": "^1.0.32",
- "@types/sequelize": "^4.27.5",
- "body-parser": "^1.18.2",
- "express": "^4.16.2",
- "express-jwt": "^5.3.0",
- "jsonwebtoken": "^8.1.1",
- "mocha-each": "^1.1.0",
- "morgan": "^1.9.0",
- "passport": "^0.4.0",
- "passport-local": "^1.0.0",
- "pg": "^7.4.1",
- "pg-hstore": "^2.3.2",
- "reflect-metadata": "^0.1.12",
- "sequelize": "^4.32.7",
- "sequelize-typescript": "^0.6.2",
- "supertest": "^3.0.0",
- "ts-node": "^5.0.0",
- "typescript": "^2.7.1",
- "xlsx": "^0.11.19"
- }
-}
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);
+ }
};