aboutsummaryrefslogtreecommitdiff
path: root/src/auth.ts
diff options
context:
space:
mode:
authorTom Harley2018-03-06 16:57:39 +0000
committerTom Harley2018-03-06 16:57:39 +0000
commitf38249b8c5c07b13a43f3b5dd9c3771881a1c9e0 (patch)
tree049fb20ad99498d3c12280019c91db3e70ceea56 /src/auth.ts
parentRemove console.log mentions in errorware.ts (diff)
parentfixed merge conflict (diff)
downloadmedicine-f38249b8c5c07b13a43f3b5dd9c3771881a1c9e0.tar.gz
medicine-f38249b8c5c07b13a43f3b5dd9c3771881a1c9e0.zip

Merge branch ‘trh/payload-checker’ of gitlab.cs.st-andrews.ac.uk:cs3099group-be-4/project-code into trh/payload-checker

Diffstat (limited to 'src/auth.ts')
-rw-r--r--src/auth.ts42
1 files changed, 25 insertions, 17 deletions
diff --git a/src/auth.ts b/src/auth.ts
index 083357e..5a1707c 100644
--- a/src/auth.ts
+++ b/src/auth.ts
@@ -6,6 +6,8 @@ import * as jwt from 'jsonwebtoken';
import * as passport from 'passport';
import { Strategy } from 'passport-local';
import { default as User } from './db/model/User';
+import { default as UserGroup } from './db/model/UserGroup';
+import { default as Project } from './db/model/Project';
import { Errorware } from './errors/errorware';
@@ -22,8 +24,9 @@ export class AuthRouter {
const user: User | null = await User.findOne({
where: {
username: req.body.username
- }
- });
+ },
+ include: [ UserGroup, Project]
+ });
if (user === null) {
// Whoa, what an error!
@@ -31,7 +34,7 @@ export class AuthRouter {
}
const token: string = jwt.sign(
- { id: user.username },
+ { object: user },
'Mr Secret',
{ expiresIn: 21600 }
);
@@ -76,18 +79,7 @@ export class AuthRouter {
next();
}
- // Middleware to check privileges - admin
- public isAdmin(req: Request, res: Response, next: NextFunction): void {
- if (!req.user.admin) {
- next(new RequestError(
- 401, 'not_authorised',
- 'user is not authorised to perform task'
- ));
- return;
- }
- next();
- }
-
+
// Configure local strategy to use with passport-js.
private configStrategy(): void {
// tslint:disable-next-line:typedef
@@ -95,12 +87,13 @@ export class AuthRouter {
const user: User | null = await User.findOne({
where: {
username
- }
+ },
+ include: [UserGroup, Project]
});
if (!user || user.password !== password) {
return done(null, false);
- }
+ }
return done(null, user);
}));
}
@@ -130,6 +123,21 @@ export const unauthorisedErr: Errorware =
next(err);
};
+// Middleware to check privileges - admin
+export const isAdmin: any =
+(req: Request, res: Response, next: NextFunction): void => {
+ var isAdmin: boolean = req.user.object.userGroups.some((x: UserGroup): boolean => x.name === 'admin');
+
+ if (!isAdmin) {
+ next(new RequestError(
+ 401, 'not_authorised',
+ 'user is not authorised to perform task'
+ ));
+ return;
+ }
+ next();
+}
+
const authRoutes: AuthRouter = new AuthRouter();
authRoutes.init();