forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrentUser.ts
32 lines (29 loc) · 1.11 KB
/
currentUser.ts
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
/*
* Copyright (c) 2014-2023 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import challengeUtils = require('../lib/challengeUtils')
import { type Request, type Response } from 'express'
const security = require('../lib/insecurity')
const cache = require('../data/datacache')
const challenges = cache.challenges
module.exports = function retrieveLoggedInUser () {
return (req: Request, res: Response) => {
let user
try {
if (security.verify(req.cookies.token)) {
user = security.authenticatedUsers.get(req.cookies.token)
}
} catch (err) {
user = undefined
} finally {
const response = { user: { id: (user?.data ? user.data.id : undefined), email: (user?.data ? user.data.email : undefined), lastLoginIp: (user?.data ? user.data.lastLoginIp : undefined), profileImage: (user?.data ? user.data.profileImage : undefined) } }
if (req.query.callback === undefined) {
res.json(response)
} else {
challengeUtils.solveIf(challenges.emailLeakChallenge, () => { return true })
res.jsonp(response)
}
}
}
}