forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeluxe.ts
69 lines (63 loc) · 3.2 KB
/
deluxe.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
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
/*
* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import { type Request, type Response, type NextFunction } from 'express'
import { UserModel } from '../models/user'
import { WalletModel } from '../models/wallet'
import { CardModel } from '../models/card'
import challengeUtils = require('../lib/challengeUtils')
import * as utils from '../lib/utils'
import { challenges } from '../data/datacache'
const security = require('../lib/insecurity')
module.exports.upgradeToDeluxe = function upgradeToDeluxe () {
return async (req: Request, res: Response, next: NextFunction) => {
try {
const user = await UserModel.findOne({ where: { id: req.body.UserId, role: security.roles.customer } })
if (user == null) {
res.status(400).json({ status: 'error', error: 'Something went wrong. Please try again!' })
return
}
if (req.body.paymentMode === 'wallet') {
const wallet = await WalletModel.findOne({ where: { UserId: req.body.UserId } })
if ((wallet != null) && wallet.balance < 49) {
res.status(400).json({ status: 'error', error: 'Insuffienct funds in Wallet' })
return
} else {
await WalletModel.decrement({ balance: 49 }, { where: { UserId: req.body.UserId } })
}
}
if (req.body.paymentMode === 'card') {
const card = await CardModel.findOne({ where: { id: req.body.paymentId, UserId: req.body.UserId } })
if ((card == null) || card.expYear < new Date().getFullYear() || (card.expYear === new Date().getFullYear() && card.expMonth - 1 < new Date().getMonth())) {
res.status(400).json({ status: 'error', error: 'Invalid Card' })
return
}
}
user.update({ role: security.roles.deluxe, deluxeToken: security.deluxeToken(user.email) })
.then(user => {
challengeUtils.solveIf(challenges.freeDeluxeChallenge, () => { return security.verify(utils.jwtFrom(req)) && req.body.paymentMode !== 'wallet' && req.body.paymentMode !== 'card' })
// @ts-expect-error FIXME some properties missing in user
user = utils.queryResultToJson(user)
const updatedToken = security.authorize(user)
security.authenticatedUsers.put(updatedToken, user)
res.status(200).json({ status: 'success', data: { confirmation: 'Congratulations! You are now a deluxe member!', token: updatedToken } })
}).catch(() => {
res.status(400).json({ status: 'error', error: 'Something went wrong. Please try again!' })
})
} catch (err: unknown) {
res.status(400).json({ status: 'error', error: 'Something went wrong: ' + utils.getErrorMessage(err) })
}
}
}
module.exports.deluxeMembershipStatus = function deluxeMembershipStatus () {
return (req: Request, res: Response, next: NextFunction) => {
if (security.isCustomer(req)) {
res.status(200).json({ status: 'success', data: { membershipCost: 49 } })
} else if (security.isDeluxe(req)) {
res.status(400).json({ status: 'error', error: 'You are already a deluxe member!' })
} else {
res.status(400).json({ status: 'error', error: 'You are not eligible for deluxe membership!' })
}
}
}