auth.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. 'use strict'
  2. /* global wiki */
  3. const fs = require('fs')
  4. module.exports = function (passport) {
  5. // Serialization user methods
  6. passport.serializeUser(function (user, done) {
  7. done(null, user._id)
  8. })
  9. passport.deserializeUser(function (id, done) {
  10. wiki.db.User.findById(id).then((user) => {
  11. if (user) {
  12. done(null, user)
  13. } else {
  14. done(new Error(wiki.lang.t('auth:errors:usernotfound')), null)
  15. }
  16. return true
  17. }).catch((err) => {
  18. done(err, null)
  19. })
  20. })
  21. // Local Account
  22. if (wiki.config.auth.local && wiki.config.auth.local.enabled) {
  23. const LocalStrategy = require('passport-local').Strategy
  24. passport.use('local',
  25. new LocalStrategy({
  26. usernameField: 'email',
  27. passwordField: 'password'
  28. }, (uEmail, uPassword, done) => {
  29. wiki.db.User.findOne({ email: uEmail, provider: 'local' }).then((user) => {
  30. if (user) {
  31. return user.validatePassword(uPassword).then(() => {
  32. return done(null, user) || true
  33. }).catch((err) => {
  34. return done(err, null)
  35. })
  36. } else {
  37. return done(new Error('INVALID_LOGIN'), null)
  38. }
  39. }).catch((err) => {
  40. done(err, null)
  41. })
  42. }
  43. ))
  44. }
  45. // Google ID
  46. if (wiki.config.auth.google && wiki.config.auth.google.enabled) {
  47. const GoogleStrategy = require('passport-google-oauth20').Strategy
  48. passport.use('google',
  49. new GoogleStrategy({
  50. clientID: wiki.config.auth.google.clientId,
  51. clientSecret: wiki.config.auth.google.clientSecret,
  52. callbackURL: wiki.config.host + '/login/google/callback'
  53. }, (accessToken, refreshToken, profile, cb) => {
  54. wiki.db.User.processProfile(profile).then((user) => {
  55. return cb(null, user) || true
  56. }).catch((err) => {
  57. return cb(err, null) || true
  58. })
  59. }
  60. ))
  61. }
  62. // Microsoft Accounts
  63. if (wiki.config.auth.microsoft && wiki.config.auth.microsoft.enabled) {
  64. const WindowsLiveStrategy = require('passport-windowslive').Strategy
  65. passport.use('windowslive',
  66. new WindowsLiveStrategy({
  67. clientID: wiki.config.auth.microsoft.clientId,
  68. clientSecret: wiki.config.auth.microsoft.clientSecret,
  69. callbackURL: wiki.config.host + '/login/ms/callback'
  70. }, function (accessToken, refreshToken, profile, cb) {
  71. wiki.db.User.processProfile(profile).then((user) => {
  72. return cb(null, user) || true
  73. }).catch((err) => {
  74. return cb(err, null) || true
  75. })
  76. }
  77. ))
  78. }
  79. // Facebook
  80. if (wiki.config.auth.facebook && wiki.config.auth.facebook.enabled) {
  81. const FacebookStrategy = require('passport-facebook').Strategy
  82. passport.use('facebook',
  83. new FacebookStrategy({
  84. clientID: wiki.config.auth.facebook.clientId,
  85. clientSecret: wiki.config.auth.facebook.clientSecret,
  86. callbackURL: wiki.config.host + '/login/facebook/callback',
  87. profileFields: ['id', 'displayName', 'email']
  88. }, function (accessToken, refreshToken, profile, cb) {
  89. wiki.db.User.processProfile(profile).then((user) => {
  90. return cb(null, user) || true
  91. }).catch((err) => {
  92. return cb(err, null) || true
  93. })
  94. }
  95. ))
  96. }
  97. // GitHub
  98. if (wiki.config.auth.github && wiki.config.auth.github.enabled) {
  99. const GitHubStrategy = require('passport-github2').Strategy
  100. passport.use('github',
  101. new GitHubStrategy({
  102. clientID: wiki.config.auth.github.clientId,
  103. clientSecret: wiki.config.auth.github.clientSecret,
  104. callbackURL: wiki.config.host + '/login/github/callback',
  105. scope: ['user:email']
  106. }, (accessToken, refreshToken, profile, cb) => {
  107. wiki.db.User.processProfile(profile).then((user) => {
  108. return cb(null, user) || true
  109. }).catch((err) => {
  110. return cb(err, null) || true
  111. })
  112. }
  113. ))
  114. }
  115. // Slack
  116. if (wiki.config.auth.slack && wiki.config.auth.slack.enabled) {
  117. const SlackStrategy = require('passport-slack').Strategy
  118. passport.use('slack',
  119. new SlackStrategy({
  120. clientID: wiki.config.auth.slack.clientId,
  121. clientSecret: wiki.config.auth.slack.clientSecret,
  122. callbackURL: wiki.config.host + '/login/slack/callback'
  123. }, (accessToken, refreshToken, profile, cb) => {
  124. wiki.db.User.processProfile(profile).then((user) => {
  125. return cb(null, user) || true
  126. }).catch((err) => {
  127. return cb(err, null) || true
  128. })
  129. }
  130. ))
  131. }
  132. // LDAP
  133. if (wiki.config.auth.ldap && wiki.config.auth.ldap.enabled) {
  134. const LdapStrategy = require('passport-ldapauth').Strategy
  135. passport.use('ldapauth',
  136. new LdapStrategy({
  137. server: {
  138. url: wiki.config.auth.ldap.url,
  139. bindDn: wiki.config.auth.ldap.bindDn,
  140. bindCredentials: wiki.config.auth.ldap.bindCredentials,
  141. searchBase: wiki.config.auth.ldap.searchBase,
  142. searchFilter: wiki.config.auth.ldap.searchFilter,
  143. searchAttributes: ['displayName', 'name', 'cn', 'mail'],
  144. tlsOptions: (wiki.config.auth.ldap.tlsEnabled) ? {
  145. ca: [
  146. fs.readFileSync(wiki.config.auth.ldap.tlsCertPath)
  147. ]
  148. } : {}
  149. },
  150. usernameField: 'email',
  151. passReqToCallback: false
  152. }, (profile, cb) => {
  153. profile.provider = 'ldap'
  154. profile.id = profile.dn
  155. wiki.db.User.processProfile(profile).then((user) => {
  156. return cb(null, user) || true
  157. }).catch((err) => {
  158. return cb(err, null) || true
  159. })
  160. }
  161. ))
  162. }
  163. // AZURE AD
  164. if (wiki.config.auth.azure && wiki.config.auth.azure.enabled) {
  165. const AzureAdOAuth2Strategy = require('passport-azure-ad-oauth2').Strategy
  166. const jwt = require('jsonwebtoken')
  167. passport.use('azure_ad_oauth2',
  168. new AzureAdOAuth2Strategy({
  169. clientID: wiki.config.auth.azure.clientId,
  170. clientSecret: wiki.config.auth.azure.clientSecret,
  171. callbackURL: wiki.config.host + '/login/azure/callback',
  172. resource: wiki.config.auth.azure.resource,
  173. tenant: wiki.config.auth.azure.tenant
  174. }, (accessToken, refreshToken, params, profile, cb) => {
  175. let waadProfile = jwt.decode(params.id_token)
  176. waadProfile.id = waadProfile.oid
  177. waadProfile.provider = 'azure'
  178. wiki.db.User.processProfile(waadProfile).then((user) => {
  179. return cb(null, user) || true
  180. }).catch((err) => {
  181. return cb(err, null) || true
  182. })
  183. }
  184. ))
  185. }
  186. // Create users for first-time
  187. wiki.db.onReady.then(() => {
  188. return wiki.db.User.findOne({ provider: 'local', email: 'guest' }).then((c) => {
  189. if (c < 1) {
  190. // Create guest account
  191. return wiki.db.User.create({
  192. provider: 'local',
  193. email: 'guest@example.com',
  194. name: 'Guest',
  195. password: '',
  196. role: 'guest'
  197. }).then(() => {
  198. wiki.logger.info('[AUTH] Guest account created successfully!')
  199. return true
  200. }).catch((err) => {
  201. wiki.logger.error('[AUTH] An error occured while creating guest account:')
  202. wiki.logger.error(err)
  203. return err
  204. })
  205. }
  206. }).then(() => {
  207. if (process.env.WIKI_JS_HEROKU) {
  208. return wiki.db.User.findOne({ provider: 'local', email: process.env.WIKI_ADMIN_EMAIL }).then((c) => {
  209. if (c < 1) {
  210. // Create root admin account (HEROKU ONLY)
  211. return wiki.db.User.create({
  212. provider: 'local',
  213. email: process.env.WIKI_ADMIN_EMAIL,
  214. name: 'Administrator',
  215. password: '$2a$04$MAHRw785Xe/Jd5kcKzr3D.VRZDeomFZu2lius4gGpZZ9cJw7B7Mna', // admin123 (default)
  216. role: 'admin'
  217. }).then(() => {
  218. wiki.logger.info('[AUTH] Root admin account created successfully!')
  219. return true
  220. }).catch((err) => {
  221. wiki.logger.error('[AUTH] An error occured while creating root admin account:')
  222. wiki.logger.error(err)
  223. return err
  224. })
  225. } else { return true }
  226. })
  227. } else { return true }
  228. })
  229. })
  230. }