aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Harley2018-03-27 21:48:09 +0100
committerTom Harley2018-03-27 21:48:09 +0100
commit0ba694cf2a47188106326626f47b750ff750a6af (patch)
tree19465ac00ffc4c3f0b3478f9e2a40dedeeb53976
parentRemove index file for ORM models (diff)
downloadmedicine-0ba694cf2a47188106326626f47b750ff750a6af.tar.gz
medicine-0ba694cf2a47188106326626f47b750ff750a6af.zip

Fix some minor linting errors

-rw-r--r--src/app.ts2
-rw-r--r--src/auth.ts208
-rw-r--r--src/errors/errorware.ts7
3 files changed, 110 insertions, 107 deletions
diff --git a/src/app.ts b/src/app.ts
index 03f2cdb..ad00105 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 { default as authRouter, isAdmin, unauthorisedErr } from './auth';
+import { default as authRouter, unauthorisedErr } from './auth';
import { RequestError, errorHandler } from './errors/errorware';
import FileRouter from './FileRouter';
diff --git a/src/auth.ts b/src/auth.ts
index 5395728..368f130 100644
--- a/src/auth.ts
+++ b/src/auth.ts
@@ -1,5 +1,3 @@
-/* tslint:disable:prefer-function-over-method */
-
import { Errorware, RequestError } from './errors/errorware';
import { NextFunction, Request, Response, Router } from 'express';
import { Middleware } from './FileRouter';
@@ -15,134 +13,140 @@ import { default as UserGroup } from './db/model/UserGroup';
*/
/**
- * A class used to configure the authentication of the server.
+ * Used to configure Passport to use the custom authentication strategy.
*/
-export class AuthRouter {
- /**
- * The Express Router this authenticator is built on.
- */
- public router: Router;
-
- /**
- * A simple constructor that initialises the authenticator.
- */
- public constructor() {
- this.router = Router();
- this.init();
- }
-
- /**
- * A middleware function that generates an OAuth-like token, used for
- * authentication in future interactions with the client.
- *
- * @param req The Express http request.
- * @param res The Express http response.
- */
- public async genToken(req: Request, res: Response): Promise<void> {
+const configStrategy: () => void = (): void => {
+ // tslint:disable-next-line:typedef
+ passport.use(new Strategy( async(username, password, done) => {
const user: User | null = await User.findOne({
where: {
- username: req.body.username
+ username
},
- include: [ UserGroup, Project]
+ include: [UserGroup, Project]
});
- if (user === null) {
- // Whoa, what an error!
- return;
+ if (!user || user.password !== password) {
+ return done(null, false);
}
+ return done(null, user);
+ }));
+};
- const token: string = jwt.sign(
- { object: user },
- 'Mr Secret',
- { expiresIn: 21600 }
- );
-
- res.json({
- token_type: 'bearer',
- access_token: token,
- refresh_token: token,
- expires_in: 21600
- });
+/**
+ * Middleware that checks the incoming request for missing or
+ * inaccurate properties, before the authentication process.
+ *
+ * @param req The Express http request.
+ * @param res The Express http response.
+ */
+const checkErr: Middleware = (
+ req: Request, res: Response, next: NextFunction
+): void => {
+ if (!req.body.grant_type || !req.body.username || !req.body.password) {
+ return next(new RequestError(400, 'invalid_request', 'Missing parameters'));
}
- /**
- * Middleware used to authenticate the user, using previously-obtained
- * OAuth-like tokens.
- *
- * @param req The Express http request.
- * @param res The Express http response.
- */
- private authenticate(req: Request, res: Response, next: NextFunction): void {
- passport.authenticate(
- 'local',
- { session: false },
- (err: Error, user: User) => {
- // TODO: Implement
- if (err) { throw err; }
-
- if (!user) {
- next(new RequestError(
- 400, 'invalid_grant', 'Incorrect authentication details'
- ));
- }
- }
- );
- next();
+ if (req.body.grant_type !== 'refresh_token'
+ && req.body.grant_type !== 'password') {
+ return next(new RequestError(
+ 400, 'unsupported_grant_type', 'invalid grant type'
+ ));
}
- /**
- * Middleware that checks the incoming request for missing or
- * inaccurate properties, before the authentication process.
- *
- * @param req The Express http request.
- * @param res The Express http response.
- */
- public checkErr(req: Request, res: Response, next: NextFunction): void {
- if (!req.body.grant_type || !req.body.username || !req.body.password) {
- return next(new RequestError(400, 'invalid_request', 'Missing parameters'));
- }
+ next();
+};
- if (req.body.grant_type !== 'refresh_token'
- && req.body.grant_type !== 'password') {
- return next(new RequestError(
- 400, 'unsupported_grant_type', 'invalid grant type'
- ));
+/**
+ * Middleware used to authenticate the user, using previously-obtained
+ * OAuth-like tokens.
+ *
+ * @param req The Express http request.
+ * @param res The Express http response.
+ */
+const authenticate: Middleware = (
+ req: Request, res: Response, next: NextFunction
+): void => {
+ passport.authenticate(
+ 'local',
+ { session: false },
+ (err: Error, user: User) => {
+ // TODO: Implement
+ if (err) { throw err; }
+
+ if (!user) {
+ next(new RequestError(
+ 400, 'invalid_grant', 'Incorrect authentication details'
+ ));
+ }
}
+ );
+ next();
+};
- next();
+/**
+ * A middleware function that generates an OAuth-like token, used for
+ * authentication in future interactions with the client.
+ *
+ * @param req The Express http request.
+ * @param res The Express http response.
+ */
+const genToken: Middleware = async(
+ req: Request, res: Response
+): Promise<void> => {
+ const user: User | null = await User.findOne({
+ where: {
+ username: req.body.username
+ },
+ include: [ UserGroup, Project]
+ });
+
+ if (user === null) {
+ // Whoa, what an error!
+ return;
}
+ const token: string = jwt.sign(
+ { object: user },
+ 'Mr Secret',
+ { expiresIn: 21600 }
+ );
+
+ res.json({
+ token_type: 'bearer',
+ access_token: token,
+ refresh_token: token,
+ expires_in: 21600
+ });
+};
+
+/**
+ * A class used to configure the authentication of the server.
+ */
+export class AuthRouter {
/**
- * Used to configure Passport to use the custom authentication strategy.
+ * The Express Router this authenticator is built on.
*/
- private configStrategy(): void {
- // tslint:disable-next-line:typedef
- passport.use(new Strategy( async(username, password, done) => {
- 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);
- }));
+ public router: Router;
+
+ /**
+ * A simple constructor that initialises the authenticator.
+ */
+ public constructor() {
+ this.router = Router();
+ this.init();
}
/**
* Function used by the constructor to initialise the authenticator.
*/
public init(): void {
- this.configStrategy();
+ configStrategy();
this.router.post(
'/oauth/token',
- this.checkErr,
- this.authenticate,
- this.genToken
+ checkErr,
+ authenticate,
+ genToken
);
this.router.get(
'/logout',
diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts
index e28e6ae..05c714d 100644
--- a/src/errors/errorware.ts
+++ b/src/errors/errorware.ts
@@ -29,10 +29,9 @@ export class RequestError extends Error {
* otherwise.
*/
public static is(test: Object): test is RequestError {
- const err: RequestError = test as RequestError;
- return err.code !== undefined
- && err.name !== undefined
- && err.responseBlock !== undefined;
+ return (test as Partial<RequestError>).code !== undefined
+ && (test as Partial<RequestError>).name !== undefined
+ && (test as Partial<RequestError>).responseBlock !== undefined;
}
/**