juice-shop

Форк
0
/
loginAdminChallenge_1.ts 
41 строка · 1.7 Кб
1
import {BasketModel} from "../../../models/basket";
2

3
module.exports = function login () {
4
  function afterLogin (user: { data: User, bid: number }, res: Response, next: NextFunction) {
5
    BasketModel.findOrCreate({ where: { UserId: user.data.id } })
6
      .then(([basket]: [BasketModel, boolean]) => {
7
        const token = security.authorize(user)
8
        user.bid = basket.id // keep track of original basket
9
        security.authenticatedUsers.put(token, user)
10
        res.json({ authentication: { token, bid: basket.id, umail: user.data.email } })
11
      }).catch((error: Error) => {
12
        next(error)
13
      })
14
  }
15

16
  return (req: Request, res: Response, next: NextFunction) => {
17
    if (req.body.email.match(/.*['-;].*/) || req.body.password.match(/.*['-;].*/)) {
18
      res.status(451).send(res.__('SQL Injection detected.'))
19
    }
20
    models.sequelize.query(`SELECT * FROM Users WHERE email = '${req.body.email || ''}' AND password = '${security.hash(req.body.password || '')}' AND deletedAt IS NULL`, { model: models.User, plain: true })
21
      .then((authenticatedUser) => {
22
        const user = utils.queryResultToJson(authenticatedUser)
23
        if (user.data?.id && user.data.totpSecret !== '') {
24
          res.status(401).json({
25
            status: 'totp_token_required',
26
            data: {
27
              tmpToken: security.authorize({
28
                userId: user.data.id,
29
                type: 'password_valid_needs_second_factor_token'
30
              })
31
            }
32
          })
33
        } else if (user.data?.id) {
34
          afterLogin(user, res, next)
35
        } else {
36
          res.status(401).send(res.__('Invalid email or password.'))
37
        }
38
      }).catch((error: Error) => {
39
        next(error)
40
      })
41
  }

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.