passport.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * the flag whether LocalStrategy is set up successfully
  16. */
  17. this.isLocalStrategySetup = false;
  18. /**
  19. * the flag whether LdapStrategy is set up successfully
  20. */
  21. this.isLdapStrategySetup = false;
  22. /**
  23. * the flag whether serializer/deserializer are set up successfully
  24. */
  25. this.isSerializerSetup = false;
  26. }
  27. /**
  28. * reset LocalStrategy
  29. *
  30. * @memberof PassportService
  31. */
  32. resetLocalStrategy() {
  33. debug('LocalStrategy: reset');
  34. passport.unuse('local');
  35. this.isLocalStrategySetup = false;
  36. }
  37. /**
  38. * setup LocalStrategy
  39. *
  40. * @memberof PassportService
  41. */
  42. setupLocalStrategy(isForce) {
  43. if (isForce === true) {
  44. this.resetLocalStrategy();
  45. }
  46. // check whether the strategy has already been set up
  47. if (this.isLocalStrategySetup) {
  48. throw new Error('LocalStrategy has already been set up');
  49. }
  50. debug('LocalStrategy: setting up..');
  51. const User = this.crowi.model('User');
  52. passport.use(new LocalStrategy(
  53. {
  54. usernameField: PassportService.USERNAME_FIELD,
  55. passwordField: PassportService.PASSWORD_FIELD,
  56. },
  57. (username, password, done) => {
  58. // find user
  59. User.findUserByUsernameOrEmail(username, password, (err, user) => {
  60. if (err) { return done(err); }
  61. // check existence and password
  62. if (!user || !user.isPasswordValid(password)) {
  63. return done(null, false, { message: 'Incorrect credentials.' });
  64. }
  65. return done(null, user);
  66. });
  67. }
  68. ));
  69. this.isLocalStrategySetup = true;
  70. debug('LocalStrategy: setup is done');
  71. }
  72. /**
  73. * reset LdapStrategy
  74. *
  75. * @memberof PassportService
  76. */
  77. resetLdapStrategy() {
  78. debug('LdapStrategy: reset');
  79. passport.unuse('ldapauth');
  80. this.isLdapStrategySetup = false;
  81. }
  82. /**
  83. * Asynchronous configuration retrieval
  84. *
  85. * @memberof PassportService
  86. */
  87. setupLdapStrategy(isForce) {
  88. if (isForce === true) {
  89. this.resetLdapStrategy();
  90. }
  91. // check whether the strategy has already been set up
  92. if (this.isLdapStrategySetup) {
  93. throw new Error('LdapStrategy has already been set up');
  94. }
  95. debug('LdapStrategy: setting up..');
  96. const config = this.crowi.config;
  97. // get configurations
  98. const isUserBind = config.crowi['security:passport-ldap:isUserBind'];
  99. const serverUrl = config.crowi['security:passport-ldap:serverUrl'];
  100. let bindDN = config.crowi['security:passport-ldap:bindDN'];
  101. let bindCredentials = config.crowi['security:passport-ldap:bindDNPassword'];
  102. const searchFilter = config.crowi['security:passport-ldap:searchFilter'] || '(uid={{username}})';
  103. // parse serverUrl
  104. // see: https://regex101.com/r/0tuYBB/1
  105. const match = serverUrl.match(/(ldaps?:\/\/[^\/]+)\/(.*)?/);
  106. if (match == null || match.length < 1) {
  107. debug('LdapStrategy: serverUrl is invalid');
  108. return;
  109. }
  110. const url = match[1];
  111. const searchBase = match[2] || '';
  112. debug(`LdapStrategy: url=${url}`);
  113. debug(`LdapStrategy: searchBase=${searchBase}`);
  114. debug(`LdapStrategy: isUserBind=${isUserBind}`);
  115. if (!isUserBind) {
  116. debug(`LdapStrategy: bindDN=${bindDN}`);
  117. debug(`LdapStrategy: bindCredentials=${bindCredentials}`);
  118. }
  119. debug(`LdapStrategy: searchFilter=${searchFilter}`);
  120. // Asynchronous configuration retrieval
  121. const getLDAPConfiguration = (req, callback) => {
  122. // get credentials from form data
  123. const loginForm = req.body.loginForm;
  124. if (!req.form.isValid) {
  125. return callback({ message: 'Incorrect credentials.' });
  126. }
  127. const username = loginForm.username;
  128. const password = loginForm.password;
  129. // user bind
  130. if (isUserBind) {
  131. bindDN = bindDN.replace(/{{username}}/, username);
  132. bindCredentials = password;
  133. }
  134. process.nextTick(() => {
  135. const opts = {
  136. usernameField: PassportService.USERNAME_FIELD,
  137. passwordField: PassportService.PASSWORD_FIELD,
  138. server: {
  139. url,
  140. bindDN,
  141. bindCredentials,
  142. searchBase,
  143. searchFilter,
  144. }
  145. };
  146. debug('ldap configuration: ', opts);
  147. callback(null, opts);
  148. });
  149. };
  150. passport.use(new LdapStrategy(getLDAPConfiguration,
  151. (user, done) => {
  152. debug("LDAP authentication has successed");
  153. return done(null, user);
  154. }
  155. ));
  156. this.isLdapStrategySetup = true;
  157. debug('LdapStrategy: setup is done');
  158. }
  159. /**
  160. * setup serializer and deserializer
  161. *
  162. * @memberof PassportService
  163. */
  164. setupSerializer() {
  165. // check whether the serializer/deserializer have already been set up
  166. if (this.isSerializerSetup) {
  167. throw new Error('serializer/deserializer have already been set up');
  168. }
  169. debug('setting up serializer and deserializer');
  170. const User = this.crowi.model('User');
  171. passport.serializeUser(function(user, done) {
  172. done(null, user.id);
  173. });
  174. passport.deserializeUser(function(id, done) {
  175. User.findById(id, function(err, user) {
  176. done(err, user);
  177. });
  178. });
  179. this.isSerializerSetup = true;
  180. }
  181. }
  182. module.exports = PassportService;