1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
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 {
public localStrategy: Strategy;
public router: Router;
ensureAuth;
public constructor() {
this.localStrategy = Strategy();
this.ensureAuth = expressJwt({secret: 'Mr Secret'}); //protects routes
this.router();
this.init();
}
// 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 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) {
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
});
}
private authenticate(req: Request, res: Response, next: NextFunction) {
passport.authenticate('local', { session: false }, function(err, user) {
if (err) { console.log(err); }
if (!user) {
next(new RequestError(400, 'invalid_grant', 'Incorrect authentication details'));
}
});
next();
}
// 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) {
next(new RequestError(400, 'invalid_request', 'Missing parameters'));
}
if (req.body.grant_type !== 'refresh_token'
&& req.body.grant_type !== 'password') {
next(new RequestError(400, 'unsupported_grant_type', 'invalid grant type'));
}
next();
}
// Handle error if user is unauthorised
public unauthorisedErr(err, req: Request, res: Response, next: NextFunction) {
if (err.name === 'UnauthorizedError') {
next(new RequestError(401, 'not_authorised', 'user does not have correct authorisation to complete task'));
}
next();
}
// Configure local strategy to use with passport-js.
private configStrategy() {
passport.use(new localStrategy( (username, password, done) => {
const user = findByUsername(username);
if (!user || user.password != password) {
return done(null, false);
}
return done(null, user);
}));
}
init() {
this.configStrategy();
this.router.post('/login', this.checkErr, this.authenticate, this.genToken);
this.router.get('/logout', (req, res) => { req.logout(); });
}
}
const authRoutes = new AuthRouter();
authRoutes.init();
export default authRoutes.router;
///////////////////////////////////////////////////
app.use(passport.initialize());
app.use(unauthorisedErr); //currently does not handle userdefined error
/*
tasks left:
refreshing tokens
connect to db
revoke tokens/checking validity
logging out
*/
|