You need to sign in or sign up before continuing.
security.js 1.07 KB
Newer Older
1 2
const Promise = require('bluebird')
const crypto = require('crypto')
3
const passportJWT = require('passport-jwt')
4

NGPixel's avatar
NGPixel committed
5 6
module.exports = {
  sanitizeCommitUser (user) {
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    // let wlist = new RegExp('[^a-zA-Z0-9-_.\',& ' + appdata.regex.cjk + appdata.regex.arabic + ']', 'g')
    // return {
    //   name: _.chain(user.name).replace(wlist, '').trim().value(),
    //   email: appconfig.git.showUserEmail ? user.email : appconfig.git.serverEmail
    // }
  },
  /**
   * Generate a random token
   *
   * @param {any} length
   * @returns
   */
  async generateToken (length) {
    return Promise.fromCallback(clb => {
      crypto.randomBytes(length, clb)
    }).then(buf => {
      return buf.toString('hex')
    })
25 26
  },

Nicolas Giard's avatar
Nicolas Giard committed
27 28 29 30 31 32
  extractJWT: passportJWT.ExtractJwt.fromExtractors([
    passportJWT.ExtractJwt.fromAuthHeaderAsBearerToken(),
    (req) => {
      let token = null
      if (req && req.cookies) {
        token = req.cookies['jwt']
33
      }
34
      // Force uploads to use Auth headers
35
      if (req.path.toLowerCase() === '/u') {
36 37
        return null
      }
Nicolas Giard's avatar
Nicolas Giard committed
38 39 40
      return token
    }
  ])
NGPixel's avatar
NGPixel committed
41
}