auth.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. const LocalStrategy = require('passport-local').Strategy;
  3. const GoogleStrategy = require('passport-google-oauth20').Strategy;
  4. const WindowsLiveStrategy = require('passport-windowslive').Strategy;
  5. const FacebookStrategy = require('passport-facebook').Strategy;
  6. const _ = require('lodash');
  7. module.exports = function(passport, appconfig) {
  8. // Serialization user methods
  9. passport.serializeUser(function(user, done) {
  10. done(null, user._id);
  11. });
  12. passport.deserializeUser(function(id, done) {
  13. db.User.findById(id).then((user) => {
  14. if(user) {
  15. done(null, user);
  16. } else {
  17. done(new Error('User not found.'), null);
  18. }
  19. return true;
  20. }).catch((err) => {
  21. done(err, null);
  22. });
  23. });
  24. // Local Account
  25. if(appconfig.auth.local && appconfig.auth.local.enabled) {
  26. passport.use('local',
  27. new LocalStrategy({
  28. usernameField : 'email',
  29. passwordField : 'password',
  30. passReqToCallback : true
  31. },
  32. function(req, uEmail, uPassword, done) {
  33. db.User.findOne({ 'email' : uEmail }).then((user) => {
  34. if (user) {
  35. user.validatePassword(uPassword).then((isValid) => {
  36. return (isValid) ? done(null, user) : done(null, false);
  37. });
  38. } else {
  39. return done(null, false);
  40. }
  41. }).catch((err) => {
  42. done(err);
  43. });
  44. })
  45. );
  46. }
  47. // Google ID
  48. if(appconfig.auth.google && appconfig.auth.google.enabled) {
  49. passport.use('google',
  50. new GoogleStrategy({
  51. clientID: appconfig.auth.google.clientId,
  52. clientSecret: appconfig.auth.google.clientSecret,
  53. callbackURL: appconfig.host + '/login/google/callback'
  54. },
  55. (accessToken, refreshToken, profile, cb) => {
  56. db.User.processProfile(profile).then((user) => {
  57. return cb(null, user) || true;
  58. }).catch((err) => {
  59. return cb(err, null) || true;
  60. });
  61. }
  62. ));
  63. }
  64. // Microsoft Accounts
  65. if(appconfig.auth.microsoft && appconfig.auth.microsoft.enabled) {
  66. passport.use('windowslive',
  67. new WindowsLiveStrategy({
  68. clientID: appconfig.auth.microsoft.clientId,
  69. clientSecret: appconfig.auth.microsoft.clientSecret,
  70. callbackURL: appconfig.host + '/login/ms/callback'
  71. },
  72. function(accessToken, refreshToken, profile, cb) {
  73. db.User.processProfile(profile).then((user) => {
  74. return cb(null, user) || true;
  75. }).catch((err) => {
  76. return cb(err, null) || true;
  77. });
  78. }
  79. ));
  80. }
  81. // Facebook
  82. if(appconfig.auth.facebook && appconfig.auth.facebook.enabled) {
  83. passport.use('facebook',
  84. new FacebookStrategy({
  85. clientID: appconfig.auth.facebook.clientId,
  86. clientSecret: appconfig.auth.facebook.clientSecret,
  87. callbackURL: appconfig.host + '/login/facebook/callback',
  88. profileFields: ['id', 'displayName', 'email']
  89. },
  90. function(accessToken, refreshToken, profile, cb) {
  91. db.User.processProfile(profile).then((user) => {
  92. return cb(null, user) || true;
  93. }).catch((err) => {
  94. return cb(err, null) || true;
  95. });
  96. }
  97. ));
  98. }
  99. // Check for admin access
  100. db.onReady.then(() => {
  101. db.User.count().then((c) => {
  102. if(c < 1) {
  103. winston.info('[' + PROCNAME + '][AUTH] No administrator account found. Creating a new one...');
  104. db.User.hashPassword('admin123').then((pwd) => {
  105. return db.User.create({
  106. provider: 'local',
  107. email: appconfig.admin,
  108. name: "Administrator",
  109. password: pwd
  110. });
  111. }).then(() => {
  112. winston.info('[' + PROCNAME + '][AUTH] Administrator account created successfully!');
  113. }).catch((err) => {
  114. winston.error('[' + PROCNAME + '][AUTH] An error occured while creating administrator account:');
  115. winston.error(err);
  116. });
  117. }
  118. });
  119. return true;
  120. });
  121. };