You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

  1. const Promise = require('bluebird')
  2. const crypto = require('crypto')
  3. const passportJWT = require('passport-jwt')
  4. module.exports = {
  5. sanitizeCommitUser (user) {
  6. // let wlist = new RegExp('[^a-zA-Z0-9-_.\',& ' + appdata.regex.cjk + appdata.regex.arabic + ']', 'g')
  7. // return {
  8. // name: _.chain(user.name).replace(wlist, '').trim().value(),
  9. // email: appconfig.git.showUserEmail ? user.email : appconfig.git.serverEmail
  10. // }
  11. },
  12. /**
  13. * Generate a random token
  14. *
  15. * @param {any} length
  16. * @returns
  17. */
  18. async generateToken (length) {
  19. return Promise.fromCallback(clb => {
  20. crypto.randomBytes(length, clb)
  21. }).then(buf => {
  22. return buf.toString('hex')
  23. })
  24. },
  25. extractJWT: passportJWT.ExtractJwt.fromExtractors([
  26. passportJWT.ExtractJwt.fromAuthHeaderAsBearerToken(),
  27. (req) => {
  28. let token = null
  29. if (req && req.cookies) {
  30. token = req.cookies['jwt']
  31. }
  32. // Force uploads to use Auth headers
  33. if (req.path.toLowerCase() === '/u') {
  34. return null
  35. }
  36. return token
  37. }
  38. ])
  39. }