passport.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const debug = require('debug')('crowi:service:PassportService');
  2. const passport = require('passport');
  3. const LocalStrategy = require('passport-local').Strategy;
  4. const LdapStrategy = require('passport-ldapauth');
  5. /**
  6. * the service class of Passport
  7. */
  8. class PassportService {
  9. // see '/lib/form/login.js'
  10. static get USERNAME_FIELD() { return 'loginForm[username]' }
  11. static get PASSWORD_FIELD() { return 'loginForm[password]' }
  12. constructor(crowi) {
  13. this.crowi = crowi;
  14. }
  15. /**
  16. * setup LocalStrategy
  17. *
  18. * @memberof PassportService
  19. */
  20. setupLocalStrategy() {
  21. debug('setup LocalStrategy');
  22. const User = this.crowi.model('User');
  23. passport.use(new LocalStrategy(
  24. {
  25. usernameField: PassportService.USERNAME_FIELD,
  26. passwordField: PassportService.PASSWORD_FIELD,
  27. },
  28. (username, password, done) => {
  29. // find user
  30. User.findUserByUsernameOrEmail(username, password, (err, user) => {
  31. if (err) { return done(err); }
  32. // check existence and password
  33. if (!user || !user.isPasswordValid(password)) {
  34. return done(null, false, { message: 'Incorrect credentials.' });
  35. }
  36. return done(null, user);
  37. });
  38. }
  39. ));
  40. }
  41. /*
  42. * Asynchronous configuration retrieval
  43. */
  44. setupLdapStrategy() {
  45. debug('setup LdapStrategy');
  46. const config = this.crowi.config;
  47. // get configurations
  48. const isUserBind = config.crowi['security:passport-ldap:isUserBind'];
  49. const serverUrl = config.crowi['security:passport-ldap:serverUrl'];
  50. let bindDN = config.crowi['security:passport-ldap:bindDN'];
  51. let bindCredentials = config.crowi['security:passport-ldap:bindDNPassword'];
  52. const searchFilter = config.crowi['security:passport-ldap:searchFilter'] || '(uid={{username}})';
  53. // parse serverUrl
  54. // see: https://regex101.com/r/0tuYBB/1
  55. const match = serverUrl.match(/(ldaps?:\/\/[^\/]+)\/(.*)?/);
  56. if (match == null || match.length < 1) {
  57. debug('serverUrl is invalid');
  58. return;
  59. }
  60. const url = match[1];
  61. const searchBase = match[2] || '';
  62. debug(`LDAP url: ${url}`);
  63. debug(`LDAP searchBase: ${searchBase}`);
  64. debug(`LDAP isUserBind: ${isUserBind}`);
  65. debug(`LDAP bindDN: ${bindDN}`);
  66. debug(`LDAP bindCredentials: ${bindCredentials}`);
  67. debug(`LDAP searchFilter: ${searchFilter}`);
  68. // Asynchronous configuration retrieval
  69. var getLDAPConfiguration = (req, callback) => {
  70. // get credentials from form data
  71. const loginForm = req.body.loginForm;
  72. if (!req.form.isValid) {
  73. return callback({ message: 'Incorrect credentials.' });
  74. }
  75. const username = loginForm.username;
  76. const password = loginForm.password;
  77. // user bind
  78. if (isUserBind) {
  79. bindDN = bindDN.replace(/{{username}}/, username);
  80. bindCredentials = password;
  81. }
  82. process.nextTick(() => {
  83. const opts = {
  84. usernameField: PassportService.USERNAME_FIELD,
  85. passwordField: PassportService.PASSWORD_FIELD,
  86. server: {
  87. url,
  88. bindDN,
  89. bindCredentials,
  90. searchBase,
  91. searchFilter,
  92. }
  93. };
  94. debug('ldap configuration: ', opts);
  95. callback(null, opts);
  96. });
  97. };
  98. passport.use(new LdapStrategy(getLDAPConfiguration,
  99. (user, done) => {
  100. debug("LDAP authentication has successed");
  101. return done(null, user);
  102. }
  103. ));
  104. }
  105. /**
  106. * setup serializer and deserializer
  107. *
  108. * @memberof PassportService
  109. */
  110. setupSerializer() {
  111. debug('setup serializer and deserializer');
  112. const User = this.crowi.model('User');
  113. passport.serializeUser(function(user, done) {
  114. done(null, user.id);
  115. });
  116. passport.deserializeUser(function(id, done) {
  117. User.findById(id, function(err, user) {
  118. done(err, user);
  119. });
  120. });
  121. }
  122. }
  123. module.exports = PassportService;