2
0

security.js 1.0 KB

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