Skip to content

Commit 178149a

Browse files
committed
Add support for authentication via @cryb/auth
1 parent 26b5d60 commit 178149a

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ APERTURE_WS_URL=ws://localhost:9001
1717
# This should be the same secure key in @cryb/aperture/.env under 'APERTURE_KEY'
1818
APERTURE_WS_KEY=api-aperture-key
1919

20+
# Optional: the base URL of @cryb/auth. This service usually runs on port 4500
21+
# AUTH_BASE_URL=http://localhost:4500
22+
2023
# The URI used for connecting to the MongoDB database
2124
MONGO_URI=
2225
# The URI used for connecting to Redis instance

src/config/passport.config.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from 'axios'
12
import passport from 'passport'
23
import { Strategy, ExtractJwt } from 'passport-jwt'
34
import { Request, Response, NextFunction } from 'express'
@@ -28,14 +29,34 @@ const BAN_SAFE_ENDPOINTS = [
2829

2930
const fetchEndpoint = (req: Request) => `${req.method} ${req.baseUrl}${req.route.path}`
3031

31-
export const authenticate = (req: Request, res: Response, next: NextFunction) =>
32-
passport.authenticate('jwt', { session: false }, async (err, user: User) => {
33-
if(err) return res.sendStatus(500)
34-
if(!user) return res.sendStatus(401)
35-
36-
const endpoint = fetchEndpoint(req)
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+
}
52+
})
3753

38-
const ban = await user.fetchBan()
54+
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+
3960
if(ban && BAN_SAFE_ENDPOINTS.indexOf(endpoint) > -1)
4061
return handleError('UserBanned', res)
4162

@@ -45,6 +66,9 @@ export const authenticate = (req: Request, res: Response, next: NextFunction) =>
4566
req.user = user
4667

4768
next()
48-
})(req, res, next)
69+
} catch(error) {
70+
handleError(error, res)
71+
}
72+
}
4973

5074
export default passport

src/models/user/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import IUser, { Role, DiscordCredentials } from './defs'
2+
import StoredUser from '../../schemas/user.schema'
23

34
import Ban from './ban'
45
import Room from '../room'
56

6-
import StoredUser from '../../schemas/user.schema'
7-
87
import StoredBan from '../../schemas/ban.schema'
98
import { createPortal } from '../../drivers/portals.driver'
109

0 commit comments

Comments
 (0)