123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- "use strict";
- const LocalStrategy = require('passport-local').Strategy;
- const GoogleStrategy = require('passport-google-oauth20').Strategy;
- const WindowsLiveStrategy = require('passport-windowslive').Strategy;
- const FacebookStrategy = require('passport-facebook').Strategy;
- const _ = require('lodash');
- module.exports = function(passport, appconfig) {
- // Serialization user methods
- passport.serializeUser(function(user, done) {
- done(null, user._id);
- });
- passport.deserializeUser(function(id, done) {
- db.User.findById(id).then((user) => {
- if(user) {
- done(null, user);
- } else {
- done(new Error('User not found.'), null);
- }
- return true;
- }).catch((err) => {
- done(err, null);
- });
- });
- // Local Account
- if(appconfig.auth.local && appconfig.auth.local.enabled) {
- passport.use('local',
- new LocalStrategy({
- usernameField : 'email',
- passwordField : 'password'
- },
- (uEmail, uPassword, done) => {
- db.User.findOne({ email: uEmail, provider: 'local' }).then((user) => {
- if(user) {
- return user.validatePassword(uPassword).then(() => {
- return done(null, user) || true;
- }).catch((err) => {
- return done(err, null);
- });
- } else {
- return done(new Error('Invalid Login'), null);
- }
- }).catch((err) => {
- done(err, null) ;
- });
- }
- ));
- }
- // Google ID
- if(appconfig.auth.google && appconfig.auth.google.enabled) {
- passport.use('google',
- new GoogleStrategy({
- clientID: appconfig.auth.google.clientId,
- clientSecret: appconfig.auth.google.clientSecret,
- callbackURL: appconfig.host + '/login/google/callback'
- },
- (accessToken, refreshToken, profile, cb) => {
- db.User.processProfile(profile).then((user) => {
- return cb(null, user) || true;
- }).catch((err) => {
- return cb(err, null) || true;
- });
- }
- ));
- }
- // Microsoft Accounts
- if(appconfig.auth.microsoft && appconfig.auth.microsoft.enabled) {
- passport.use('windowslive',
- new WindowsLiveStrategy({
- clientID: appconfig.auth.microsoft.clientId,
- clientSecret: appconfig.auth.microsoft.clientSecret,
- callbackURL: appconfig.host + '/login/ms/callback'
- },
- function(accessToken, refreshToken, profile, cb) {
- db.User.processProfile(profile).then((user) => {
- return cb(null, user) || true;
- }).catch((err) => {
- return cb(err, null) || true;
- });
- }
- ));
- }
- // Facebook
- if(appconfig.auth.facebook && appconfig.auth.facebook.enabled) {
- passport.use('facebook',
- new FacebookStrategy({
- clientID: appconfig.auth.facebook.clientId,
- clientSecret: appconfig.auth.facebook.clientSecret,
- callbackURL: appconfig.host + '/login/facebook/callback',
- profileFields: ['id', 'displayName', 'email']
- },
- function(accessToken, refreshToken, profile, cb) {
- db.User.processProfile(profile).then((user) => {
- return cb(null, user) || true;
- }).catch((err) => {
- return cb(err, null) || true;
- });
- }
- ));
- }
- // Check for admin access
- db.onReady.then(() => {
- db.User.count().then((c) => {
- if(c < 1) {
- winston.info('[' + PROCNAME + '][AUTH] No administrator account found. Creating a new one...');
- db.User.hashPassword('admin123').then((pwd) => {
- return db.User.create({
- provider: 'local',
- email: appconfig.admin,
- name: "Administrator",
- password: pwd,
- rights: [{
- role: 'admin',
- path: '/',
- exact: false,
- deny: false
- }]
- });
- }).then(() => {
- winston.info('[' + PROCNAME + '][AUTH] Administrator account created successfully!');
- }).catch((err) => {
- winston.error('[' + PROCNAME + '][AUTH] An error occured while creating administrator account:');
- winston.error(err);
- });
- }
- });
-
- return true;
- });
- };
|