auth.js 3.72 KB
Newer Older
1
/* global WIKI */
2

3
const express = require('express')
NGPixel's avatar
NGPixel committed
4
const ExpressBrute = require('express-brute')
NGPixel's avatar
NGPixel committed
5
const BruteKnex = require('../helpers/brute-knex')
6
const router = express.Router()
7
const moment = require('moment')
8
const _ = require('lodash')
NGPixel's avatar
NGPixel committed
9 10 11 12 13 14

const bruteforce = new ExpressBrute(new BruteKnex({
  createTable: true,
  knex: WIKI.models.knex
}), {
  freeRetries: 5,
15 16
  minWait: 5 * 60 * 1000, // 5 minutes
  maxWait: 60 * 60 * 1000, // 1 hour
NGPixel's avatar
NGPixel committed
17 18 19 20
  failCallback: (req, res, next) => {
    res.status(401).send('Too many failed attempts. Try again later.')
  }
})
NGPixel's avatar
NGPixel committed
21 22 23 24

/**
 * Login form
 */
NGPixel's avatar
NGPixel committed
25
router.get('/login', async (req, res, next) => {
26
  _.set(res.locals, 'pageMeta.title', 'Login')
NGPixel's avatar
NGPixel committed
27

28
  if (req.query.legacy || (req.get('user-agent') && req.get('user-agent').indexOf('Trident') >= 0)) {
29
    const { formStrategies, socialStrategies } = await WIKI.models.authentication.getStrategiesForLegacyClient()
NGPixel's avatar
NGPixel committed
30
    res.render('legacy/login', {
31
      err: false,
NGPixel's avatar
NGPixel committed
32 33 34 35 36 37
      formStrategies,
      socialStrategies
    })
  } else {
    res.render('login')
  }
38
})
NGPixel's avatar
NGPixel committed
39 40 41 42

/**
 * Social Strategies Login
 */
Nick's avatar
Nick committed
43 44
router.get('/login/:strategy', async (req, res, next) => {
  try {
Nick's avatar
Nick committed
45
    await WIKI.models.users.login({
Nick's avatar
Nick committed
46 47 48 49 50 51
      strategy: req.params.strategy
    }, { req, res })
  } catch (err) {
    next(err)
  }
})
NGPixel's avatar
NGPixel committed
52 53 54 55

/**
 * Social Strategies Callback
 */
Nick's avatar
Nick committed
56 57 58
router.all('/login/:strategy/callback', async (req, res, next) => {
  if (req.method !== 'GET' && req.method !== 'POST') { return next() }

Nick's avatar
Nick committed
59 60 61 62
  try {
    const authResult = await WIKI.models.users.login({
      strategy: req.params.strategy
    }, { req, res })
63 64
    res.cookie('jwt', authResult.jwt, { expires: moment().add(1, 'y').toDate() })
    res.redirect('/')
Nick's avatar
Nick committed
65 66 67 68
  } catch (err) {
    next(err)
  }
})
NGPixel's avatar
NGPixel committed
69

NGPixel's avatar
NGPixel committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/**
 * LEGACY - Login form handling
 */
router.post('/login', bruteforce.prevent, async (req, res, next) => {
  _.set(res.locals, 'pageMeta.title', 'Login')

  if (req.query.legacy || req.get('user-agent').indexOf('Trident') >= 0) {
    try {
      const authResult = await WIKI.models.users.login({
        strategy: req.body.strategy,
        username: req.body.user,
        password: req.body.pass
      }, { req, res })
      req.brute.reset()
      res.cookie('jwt', authResult.jwt, { expires: moment().add(1, 'y').toDate() })
      res.redirect('/')
    } catch (err) {
87 88 89 90 91 92
      const { formStrategies, socialStrategies } = await WIKI.models.authentication.getStrategiesForLegacyClient()
      res.render('legacy/login', {
        err,
        formStrategies,
        socialStrategies
      })
NGPixel's avatar
NGPixel committed
93 94 95 96 97 98
    }
  } else {
    res.redirect('/login')
  }
})

NGPixel's avatar
NGPixel committed
99 100 101
/**
 * Logout
 */
102 103
router.get('/logout', function (req, res) {
  req.logout()
104
  res.clearCookie('jwt')
105 106
  res.redirect('/')
})
NGPixel's avatar
NGPixel committed
107

108 109 110
/**
 * Register form
 */
111
router.get('/register', async (req, res, next) => {
112
  _.set(res.locals, 'pageMeta.title', 'Register')
113 114 115 116 117 118
  const localStrg = await WIKI.models.authentication.getStrategy('local')
  if (localStrg.selfRegistration) {
    res.render('register')
  } else {
    next(new WIKI.Error.AuthRegistrationDisabled())
  }
119 120
})

121 122 123
/**
 * Verify
 */
NGPixel's avatar
NGPixel committed
124
router.get('/verify/:token', bruteforce.prevent, async (req, res, next) => {
125 126 127 128 129 130 131 132 133 134
  try {
    const usr = await WIKI.models.userKeys.validateToken({ kind: 'verify', token: req.params.token })
    await WIKI.models.users.query().patch({ isVerified: true }).where('id', usr.id)
    const result = await WIKI.models.users.refreshToken(usr)
    req.brute.reset()
    res.cookie('jwt', result.token, { expires: moment().add(1, 'years').toDate() })
    res.redirect('/')
  } catch (err) {
    next(err)
  }
135 136
})

137 138 139 140 141 142 143 144 145 146
/**
 * JWT Public Endpoints
 */
router.get('/.well-known/jwk.json', function (req, res, next) {
  res.json(WIKI.config.certs.jwk)
})
router.get('/.well-known/jwk.pem', function (req, res, next) {
  res.send(WIKI.config.certs.public)
})

147
module.exports = router