Source: passport/strategies/strategyFactory.js

  1. 'use strict';
  2. /**
  3. * Gets an instance of a passport strategy.
  4. *
  5. * Have a look at [STRATEGIES]{@link module:passport/STRATEGIES~STRATEGIES} to find out which
  6. * passport strategies are supported.
  7. *
  8. * @module passport/strategyFactory
  9. */
  10. /* eslint node/no-sync: 0 */
  11. var fs = require('fs');
  12. var STRATEGIES = process.requireApi('lib/passport/strategies/strategies.js');
  13. /**
  14. * Gets an instance of a passport strategy.
  15. *
  16. * @example
  17. * // cas strategy configuration example
  18. * // {
  19. *
  20. * // // Application service
  21. * // "service": "https://my-application-service-host",
  22. *
  23. * // // CAS server url
  24. * // "url": "https://my-cas-server-host:8443/cas",
  25. *
  26. * // // CAS protocol version (could be 1, 2, 3)
  27. * // "version": "3",
  28. *
  29. * // // CAS full chain certificate if one of the CAs not in system well known CAs
  30. * // "certificate": "/home/test/cas.crt"
  31. *
  32. * // // URI to return to when logged out
  33. * // "logoutUri": "be"
  34. *
  35. * // }
  36. *
  37. * @example
  38. * // ldapauth strategy configuration example
  39. * // {
  40. *
  41. * // // The url of the LDAP server
  42. * // "url": "ldaps://my-ldap-server-host",
  43. *
  44. * // // The LDAP attribute used by "bindDn" (default to "dn")
  45. * // "bindAttribute": "dn",
  46. *
  47. * // // The value of the "bindAttribute" associated to the entry used to connect to the server
  48. * // "bindDn": "cn=my-user,dc=my-ldap,dc=test",
  49. *
  50. * // // The password of the entry used to connect to the server
  51. * // "bindPassword": "qT5gvobG2ZxYSiY2r4mt",
  52. *
  53. * // // The search base when looking for users
  54. * // "searchBase": "ou=user,dc=my-ldap,dc=test",
  55. *
  56. * // // The search scope when looking for users (default to "sub")
  57. * // "searchScope": "sub",
  58. *
  59. * // // The search filter to find user by name, use placeholder "{{username}}" which will be replaced
  60. * // // by the user name when searching
  61. * // "searchFilter": "(&(objectclass=person)(cn={{username}}))",
  62. *
  63. * // // The name of the LDAP attribute holding the group name of a user
  64. * // "userGroupAttribute": "group",
  65. *
  66. * // // The name of the LDAP attribute holding the name of a user
  67. * // "userNameAttribute": "cn",
  68. *
  69. * // // The name of the LDAP attribute holding the id of a user
  70. * // "userIdAttribute": "dn",
  71. *
  72. * // // The name of the LDAP attribute holding the email of a user
  73. * // "userEmailAttribute": "email",
  74. *
  75. * // // The absolute path of the LDAP server certificate full chain if root CA is not
  76. * // // in the Node.JS well known CAs
  77. * // "certificate": "/absolute/path/to/cert/ldap.crt",
  78. *
  79. * // // The name of the field in the authenticate request which will hold the user name
  80. * // "usernameField": "login",
  81. *
  82. * // // The name of the field in the authenticate request which will hold the user name
  83. * // "passwordField": "password"
  84. *
  85. * // }
  86. *
  87. * @example
  88. * // local strategy configuration example
  89. * // {
  90. *
  91. * // // The name of the field in the authenticate request which will hold the user name
  92. * // "usernameField": "login",
  93. *
  94. * // // The name of the field in the authenticate request which will hold the user password
  95. * // "passwordField": "password"
  96. *
  97. * // }
  98. *
  99. * @method get
  100. * @static
  101. * @param {String} id The id of the strategy, see require('@openveo/api').passport.STRATEGIES
  102. * to find out which strategies are supported
  103. * @param {Object} configuration Strategy configuration, it depends on the strategy
  104. * @param {Function} verify Passport verify callback to validate the user authenticated by the third party provider
  105. * - **Object** The user authenticated by the third party provider
  106. * - **Function** Function to call when verification has been performed
  107. * - **Error** An error occured during verification
  108. * - **Object** The verified user
  109. * - **String** Informative message about verification failure
  110. * @return {Object} A passport strategy
  111. */
  112. module.exports.get = function(id, configuration, verify) {
  113. if (id && configuration) {
  114. var Strategy;
  115. var strategy;
  116. switch (id) {
  117. // CAS strategy
  118. case STRATEGIES.CAS:
  119. Strategy = require('./cas/CasStrategy.js');
  120. strategy = new Strategy({
  121. service: configuration.service,
  122. url: configuration.url,
  123. version: configuration.version,
  124. certificate: configuration.certificate,
  125. logoutUri: configuration.logoutUri
  126. }, verify);
  127. strategy.internal = false;
  128. break;
  129. // LDAP strategy
  130. case STRATEGIES.LDAP: {
  131. Strategy = require('passport-ldapauth');
  132. var attributes = [];
  133. if (configuration.userIdAttribute) attributes.push(configuration.userIdAttribute);
  134. if (configuration.userNameAttribute) attributes.push(configuration.userNameAttribute);
  135. if (configuration.userEmailAttribute) attributes.push(configuration.userEmailAttribute);
  136. if (configuration.userGroupAttribute) attributes.push(configuration.userGroupAttribute);
  137. strategy = new Strategy({
  138. server: {
  139. url: configuration.url,
  140. bindDN: configuration.bindDn,
  141. bindCredentials: configuration.bindPassword,
  142. searchBase: configuration.searchBase,
  143. searchScope: configuration.searchScope,
  144. searchFilter: configuration.searchFilter,
  145. searchAttributes: attributes.length ? attributes : null,
  146. bindProperty: configuration.bindAttribute,
  147. tlsOptions: {
  148. ca: configuration.certificate ? fs.readFileSync(configuration.certificate) : null
  149. }
  150. },
  151. usernameField: configuration.usernameField,
  152. passwordField: configuration.passwordField
  153. }, verify);
  154. strategy.internal = true;
  155. break;
  156. }
  157. // Local strategy
  158. case STRATEGIES.LOCAL:
  159. Strategy = require('passport-local').Strategy;
  160. strategy = new Strategy({
  161. usernameField: configuration.usernameField,
  162. passwordField: configuration.passwordField
  163. }, verify);
  164. strategy.internal = true;
  165. break;
  166. default:
  167. throw new Error('Unknown passport strategy');
  168. }
  169. return strategy;
  170. }
  171. return null;
  172. };