diff options
| author | Tom Harley | 2018-02-20 16:21:30 +0000 |
|---|---|---|
| committer | Tom Harley | 2018-02-20 16:21:30 +0000 |
| commit | eba2d2c0b0293e9dd146a377a85ff6c31e4b96cb (patch) | |
| tree | 8351e174ee8ac7249f61286dd52bccd46f147a31 /src/auth.ts | |
| parent | minor fixes (diff) | |
| parent | Merge branch 'trh/yarn' into 'master' (diff) | |
| download | medicine-eba2d2c0b0293e9dd146a377a85ff6c31e4b96cb.tar.gz medicine-eba2d2c0b0293e9dd146a377a85ff6c31e4b96cb.zip | |
Merge branch ‘master’ into trh/payload-checker
Diffstat (limited to 'src/auth.ts')
| -rw-r--r-- | src/auth.ts | 120 |
1 files changed, 61 insertions, 59 deletions
diff --git a/src/auth.ts b/src/auth.ts index 3e2dafe..a59e417 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,67 +1,70 @@ -import * as passport from 'passport'; -import * as passportLocal from 'passport-local'; -import * as jwt from 'jsonwebtoken'; -import {Router, Request, Response, NextFunction} from 'express'; +import { NextFunction, Request, Response, Router } from 'express'; import * as expressJwt from 'express-jwt'; +import * as jwt from 'jsonwebtoken'; +import * as passport from 'passport'; +import { Strategy } from 'passport-local'; +import { default as User } from './db/model/User'; export class AuthRouter { - LocalStrategy: any - router: Router + public localStrategy: Strategy; + public router: Router; - constructor() { - this.LocalStrategy = passportLocal.Strategy(); + public constructor() { + this.localStrategy = Strategy(); this.router(); this.init(); } - //Used by passport-js to locate a user object from the database by username - public findByUsername(username) { - if (username == user.username) { + // Used by passport-js to locate a user object from the database by username + public findByUsername(username: string): User { + if (username === user.username) { return user; } return false; } - //Find user obejct from database by user ID - public findUserByID(id) { - if (id == user.id) { + // Find user object from database by user ID + public findUserByID(id): User { + if (id === user.id) { return user; } return false; } - //middleware which generates token and sends it as response - public genToken(req: Request, res: Response) { - var userid = findUserByID(req.body.username); - - var token = jwt.sign({ - id: userid - }, 'Mr Secret', { - expiresIn: 21600 - }); + // Middleware which generates token and sends it as response + public genToken(req: Request, res: Response) { + const userid = findUserByID(req.body.username); + const token = jwt.sign( + { id: userid }, + 'Mr Secret', + { expiresIn: 21600 } + ); res.json({ - "token_type": "bearer", - "access_token": token, - "refresh_token": token, - "expires_in": 21600 + token_type: 'bearer', + access_token: token, + refresh_token: token, + expires_in: 21600 }); } - //checks for errors within request before authentication - public checkErr(req: Request, res: Response, next: NextFunction) { + // Checks for errors within request before authentication + public checkErr(req: Request, res: Response, next: NextFunction) { if (!req.body.grant_type || !req.body.username || !req.body.password) { - res.status(400).json({ - "error": "invalid_request", - "error_description": "Missing parameters" - }); - res.end(); + res.status(400) + .json({ + error: 'invalid_request', + error_description: 'Missing parameters' + }); + res.end(); } - if (req.body.grant_type != "refresh_token" && req.body.grant_type != "password") { - res.status(400).json({ - "error": "unsupported_grant_type", - "error_description": "invalid grant type" + if (req.body.grant_type !== 'refresh_token' + && req.body.grant_type !== 'password') { + res.status(400) + .json({ + error: 'unsupported_grant_type', + error_description: 'invalid grant type' }); res.end(); } @@ -69,26 +72,26 @@ export class AuthRouter { next(); } - //handle error if user is unauthorised + // Handle error if user is unauthorised public unauthorisedErr(err, req: Request, res: Response, next: NextFunction) { - console.log(err); if (err.name === 'UnauthorizedError') { res.status(401).json({ - "status": "error", - "error": "not_authorised", - "error_description": "user does not have correct authorisation to complete task" - }); + status: 'error', + error: 'not_authorised', + error_description: + 'user does not have correct authorisation to complete task' + }); } next(); } - //Configure local strategy to use with passport-js. + // Configure local strategy to use with passport-js. private configStrategy() { - passport.use(new LocalStrategy( (username, password, done) => { - var user = findByUsername(username); + passport.use(new localStrategy( (username, password, done) => { + const user = findByUsername(username); - if (!user || user.password != password) { - return done(null, false); + if (!user || user.password != password) { + return done(null, false); } return done(null, user); })); @@ -96,35 +99,34 @@ export class AuthRouter { } /////////////////////////////////////////////////// -var app = express(); -var ensureAuth = expressJwt({secret: 'Mr Secret'}); //protect routes +const app = express(); +const ensureAuth = expressJwt({secret: 'Mr Secret'}); //protect routes app.use(passport.initialize()); app.use(unauthorisedErr); //currently does not handle userdefined error -app.post('/login', checkErr, function(req, res, next) { +app.post('/login', checkErr, function(req, res, next) { passport.authenticate('local', { session: false }, function(err, user) { - if (err) { console.log(err); } + if (err) { console.log(err); } if (!user) { res.status(400).json({ - "error": "invalid_grant", - "error_description": "Incorrect authentication details" + error: 'invalid_grant', + error_description: 'Incorrect authentication details' }); - } + } }); next(); -}, genToken); +}, genToken); app.get('/logout', function(req, res) { req.logout(); }); app.get('/test', ensureAuth, function(req, res) { - res.send("you are authenticated!"); + res.send('you are authenticated!'); }); - /* tasks left: refreshing tokens |
