|
1 | 1 | import axios from 'axios' |
| 2 | +import { NextFunction, Request, Response } from 'express' |
2 | 3 | import passport from 'passport' |
3 | | -import { Strategy, ExtractJwt } from 'passport-jwt' |
4 | | -import { Request, Response, NextFunction } from 'express' |
5 | | - |
| 4 | +import { ExtractJwt, Strategy } from 'passport-jwt' |
6 | 5 | import User from '../models/user' |
7 | 6 |
|
8 | | -import { handleError, UserNoAuth } from '../utils/errors.utils' |
| 7 | +import { handleError, UserBanned, UserNoAuth } from '../utils/errors.utils' |
9 | 8 |
|
10 | 9 | passport.serializeUser((user, done) => done(null, user)) |
11 | 10 | passport.deserializeUser((id, done) => done(null, id)) |
12 | 11 |
|
13 | 12 | passport.use(new Strategy({ |
14 | | - jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), |
15 | | - secretOrKey: process.env.JWT_KEY |
| 13 | + jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), |
| 14 | + secretOrKey: process.env.JWT_KEY |
16 | 15 | }, async ({ id }, done) => { |
17 | | - try { |
18 | | - const user = await new User().load(id) |
| 16 | + try { |
| 17 | + const user = await new User().load(id) |
19 | 18 |
|
20 | | - return done(null, user) |
21 | | - } catch(error) { |
22 | | - done(error) |
23 | | - } |
| 19 | + return done(null, user) |
| 20 | + } catch (error) { |
| 21 | + done(error) |
| 22 | + } |
24 | 23 | })) |
25 | 24 |
|
26 | 25 | const BAN_SAFE_ENDPOINTS = [ |
27 | | - 'GET /user/me' |
| 26 | + 'GET /user/me' |
28 | 27 | ] |
29 | 28 |
|
30 | 29 | const fetchEndpoint = (req: Request) => `${req.method} ${req.baseUrl}${req.route.path}` |
31 | 30 |
|
32 | | -const fetchUser = async (req: Request, res: Response, next: NextFunction) => new Promise<User>(async (resolve, reject) => { |
33 | | - try { |
34 | | - if(process.env.AUTH_BASE_URL) { |
35 | | - const { authorization } = req.headers, |
36 | | - token = authorization.split(' ')[1], |
37 | | - { data } = await axios.post(process.env.AUTH_BASE_URL, { token }), |
38 | | - user = new User(data) |
39 | | - |
40 | | - resolve(user) |
41 | | - } else { |
42 | | - passport.authenticate('jwt', { session: false }, async (err, user: User) => { |
43 | | - if(err) return res.sendStatus(500) |
44 | | - if(!user) return res.sendStatus(401) |
45 | | - |
46 | | - resolve(user) |
47 | | - })(req, res, next) |
48 | | - } |
49 | | - } catch(error) { |
50 | | - reject(error) |
51 | | - } |
| 31 | +const fetchUser = async ( |
| 32 | + req: Request, |
| 33 | + res: Response, |
| 34 | + next: NextFunction |
| 35 | +) => new Promise<User>(async (resolve, reject) => { |
| 36 | + try { |
| 37 | + if (process.env.AUTH_BASE_URL) { |
| 38 | + const { authorization } = req.headers, |
| 39 | + token = authorization.split(' ')[1], |
| 40 | + { data } = await axios.post(process.env.AUTH_BASE_URL, { token }), |
| 41 | + user = new User(data) |
| 42 | + |
| 43 | + resolve(user) |
| 44 | + } else |
| 45 | + passport.authenticate('jwt', { session: false }, async (err, user: User) => { |
| 46 | + if (err) return res.sendStatus(500) |
| 47 | + if (!user) return handleError(UserNoAuth, res) |
| 48 | + |
| 49 | + resolve(user) |
| 50 | + })(req, res, next) |
| 51 | + } catch (error) { |
| 52 | + reject(error) |
| 53 | + } |
52 | 54 | }) |
53 | 55 |
|
54 | 56 | export const authenticate = async (req: Request, res: Response, next: NextFunction) => { |
55 | | - try { |
56 | | - const user = await fetchUser(req, res, next), |
57 | | - endpoint = fetchEndpoint(req), |
58 | | - ban = await user.fetchBan() |
59 | | - |
60 | | - if(ban && BAN_SAFE_ENDPOINTS.indexOf(endpoint) > -1) |
61 | | - return handleError('UserBanned', res) |
| 57 | + try { |
| 58 | + const user = await fetchUser(req, res, next), |
| 59 | + endpoint = fetchEndpoint(req), |
| 60 | + ban = await user.fetchBan() |
| 61 | + |
| 62 | + if (ban && BAN_SAFE_ENDPOINTS.indexOf(endpoint) > -1) |
| 63 | + return handleError(UserBanned, res) |
62 | 64 |
|
63 | | - if(req.baseUrl === '/admin' && user.roles.indexOf('admin') === -1) |
64 | | - return handleError(UserNoAuth, res) |
| 65 | + if (req.baseUrl === '/admin' && user.roles.indexOf('admin') === -1) |
| 66 | + return handleError(UserNoAuth, res) |
65 | 67 |
|
66 | | - req.user = user |
| 68 | + req.user = user |
67 | 69 |
|
68 | | - next() |
69 | | - } catch(error) { |
70 | | - handleError(error, res) |
71 | | - } |
| 70 | + next() |
| 71 | + } catch (error) { |
| 72 | + handleError(error, res) |
| 73 | + } |
72 | 74 | } |
73 | 75 |
|
74 | 76 | export default passport |
0 commit comments