mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Initial Commit
This commit is contained in:
36
config/auth.js
Normal file
36
config/auth.js
Normal file
@ -0,0 +1,36 @@
|
||||
module.exports = function(app, passport) {
|
||||
app.get('/auth', passport.authenticate('google', {
|
||||
accessType: 'offline',
|
||||
scope: [
|
||||
'https://www.googleapis.com/auth/userinfo.profile',
|
||||
'https://www.googleapis.com/auth/userinfo.email',
|
||||
'https://www.googleapis.com/auth/calendar'
|
||||
]}));
|
||||
|
||||
app.get('/auth/callback', function(req, res, next) {
|
||||
passport.authenticate('google', function(err, user, info) {
|
||||
if (err) { return next(err); }
|
||||
if (!user) { return res.redirect('/login/error'); }
|
||||
|
||||
req.logIn(user, function(err) {
|
||||
if (err) { return next(err); }
|
||||
return res.redirect('/');
|
||||
});
|
||||
})(req, res, next);
|
||||
});
|
||||
|
||||
return {
|
||||
requiresUiLogin: function(req, res, next) {
|
||||
if (!req.isAuthenticated()) {
|
||||
return res.redirect('/login');
|
||||
}
|
||||
next();
|
||||
},
|
||||
requiresApiAccess: function(req, res, next) {
|
||||
if (!req.isAuthenticated()) {
|
||||
return res.send(403);
|
||||
}
|
||||
next();
|
||||
}
|
||||
};
|
||||
};
|
96
config/calendar.js
Normal file
96
config/calendar.js
Normal file
@ -0,0 +1,96 @@
|
||||
var googleapis = require('googleapis'),
|
||||
sprintf = require('sprintf'),
|
||||
OAuth2Client = googleapis.OAuth2Client;
|
||||
|
||||
|
||||
module.exports = function(config) {
|
||||
|
||||
var oauth2Client = new OAuth2Client(
|
||||
config.auth.clientId, config.auth.clientSecret, config.auth.callback);
|
||||
|
||||
oauth2Client.credentials = {
|
||||
access_token: config.auth.accessToken,
|
||||
refresh_token: config.auth.refreshToken
|
||||
};
|
||||
|
||||
var opts = { baseDiscoveryUrl: 'https://www.googleapis.com/discovery/v1/apis/' };
|
||||
|
||||
|
||||
return {
|
||||
scheduleEvent: function(event, callback) {
|
||||
console.log("schedule event");
|
||||
|
||||
api(function(client, callback) {
|
||||
|
||||
var resource = buildResource(event);
|
||||
|
||||
var request = client.calendar.events.insert({
|
||||
calendarId: 'primary',
|
||||
resource: resource
|
||||
});
|
||||
|
||||
request.withAuthClient(oauth2Client).execute(function(err, result) {
|
||||
callback(err, result);
|
||||
});
|
||||
}, callback);
|
||||
},
|
||||
|
||||
updateEvent: function(event, callback) {
|
||||
api(function(client, callback) {
|
||||
|
||||
var resource = buildResource(event);
|
||||
|
||||
var request = client.calendar.events.update({
|
||||
calendarId: 'primary',
|
||||
eventId: event.eventId,
|
||||
resource: resource
|
||||
});
|
||||
|
||||
request.withAuthClient(oauth2Client).execute(function(err, result) {
|
||||
callback(err, result);
|
||||
});
|
||||
}, callback);
|
||||
}
|
||||
};
|
||||
|
||||
function buildResource(event) {
|
||||
var resource = {
|
||||
summary: event.summary,
|
||||
description: event.description,
|
||||
location: event.location,
|
||||
start: {
|
||||
dateTime: event.start
|
||||
},
|
||||
end: {
|
||||
dateTime: event.end
|
||||
},
|
||||
attendees: []
|
||||
};
|
||||
|
||||
event.attendees.forEach(function(attendee) {
|
||||
resource.attendees.push({
|
||||
email: attendee
|
||||
});
|
||||
})
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
function api(workorder, callback) {
|
||||
googleapis
|
||||
.discover('calendar', 'v3')
|
||||
.execute(function(err, client) {
|
||||
if (err) return callback(err);
|
||||
|
||||
workorder(client, function(err, result) {
|
||||
if (oauth2Client.credentials.access_token != config.auth.accessToken) {
|
||||
console.log("Updating access token");
|
||||
config.auth.accessToken = oauth2Client.credentials.access_token;
|
||||
}
|
||||
|
||||
callback(err, result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
};
|
46
config/config.js
Normal file
46
config/config.js
Normal file
@ -0,0 +1,46 @@
|
||||
module.exports = {
|
||||
development: {
|
||||
root: require('path').normalize(__dirname + '/..'),
|
||||
debug: true,
|
||||
database: 'mongodb://biomed.akira.gs/biomed_devel2',
|
||||
auth: {
|
||||
clientId: '223145213165.apps.googleusercontent.com',
|
||||
clientSecret: '8MRNar9E_pRTOGTQonPzYOW_',
|
||||
callback: 'http://devel.portal.atlanticbiomedical.com/auth/callback',
|
||||
accessToken: 'ya29.AHES6ZR-vUVEh7CZzsEeGFSHqFfXtU1-LHyEAidi0CKhDGQ',
|
||||
refreshToken: '1/exRXjTaGNlWEo-HZZWyn4NTwJ4TY3wKb-_npce21c50',
|
||||
},
|
||||
email: {
|
||||
user: 'api@atlanticbiomedical.com',
|
||||
password: 'success4'
|
||||
},
|
||||
mysql: {
|
||||
host: 'biomed.akira.gs',
|
||||
user: 'biomed_prod',
|
||||
password: 'wUw3RB8rrXX4HwKj',
|
||||
database: 'biomed_prod',
|
||||
}
|
||||
},
|
||||
prod: {
|
||||
root: require('path').normalize(__dirname + '/..'),
|
||||
debug: true,
|
||||
database: 'mongodb://localhost/biomed_prod',
|
||||
auth: {
|
||||
clientId: '333768673996-8epedo3je5h59n4l97v4dv8nofs7qnee.apps.googleusercontent.com',
|
||||
clientSecret: 'afu9KhKxckWJ3Tk6uxzp9Pg6',
|
||||
callback: 'http://portal.atlanticbiomedical.com/auth/callback',
|
||||
accessToken: 'ya29.AHES6ZT1Sj1vpgidR2I_ksLdlV_VeZUjkitnZ01cP6VRrknjUEVbuw',
|
||||
refreshToken: '1/XQW9P9FNYm6jikTsV8HOIuPAo1APYhwTH5CLhq9263g'
|
||||
},
|
||||
email: {
|
||||
user: 'api@atlanticbiomedical.com',
|
||||
password: 'success4',
|
||||
},
|
||||
mysql: {
|
||||
host: 'localhost',
|
||||
user: 'biomed_prod',
|
||||
password: 'wUw3RB8rrXX4HwKj',
|
||||
database: 'biomed_prod'
|
||||
}
|
||||
}
|
||||
};
|
40
config/express.js
Normal file
40
config/express.js
Normal file
@ -0,0 +1,40 @@
|
||||
var express = require('express');
|
||||
|
||||
module.exports = function(app, config, passport, piler) {
|
||||
app.set('showStackError', true);
|
||||
|
||||
app.use(express.static(config.root + '/public'))
|
||||
app.use(express.logger('dev'));
|
||||
app.set('views', config.root + '/app/views');
|
||||
app.set('view engine', 'jade');
|
||||
|
||||
app.configure(function() {
|
||||
// cookieParser should be above session
|
||||
app.use(express.cookieParser());
|
||||
|
||||
// bodyParser should be above methodOverride
|
||||
app.use(express.bodyParser());
|
||||
app.use(express.methodOverride());
|
||||
|
||||
app.use(express.session({
|
||||
secret: 'atlantic_biomed_server_secret'
|
||||
}));
|
||||
|
||||
// use passport session
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
// use piler for asset management
|
||||
piler.bind();
|
||||
|
||||
app.use(express.favicon());
|
||||
|
||||
// routes should be last
|
||||
app.use(app.router);
|
||||
});
|
||||
|
||||
// app.configure('development', function() {
|
||||
// // enable live update in development mode.
|
||||
// piler.liveUpdate();
|
||||
// });
|
||||
}
|
55
config/passport.js
Normal file
55
config/passport.js
Normal file
@ -0,0 +1,55 @@
|
||||
var mongoose = require('mongoose')
|
||||
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
|
||||
User = mongoose.model('User');
|
||||
|
||||
module.exports = function(passport, config) {
|
||||
passport.serializeUser(function(user, done) {
|
||||
done(null, user._id);
|
||||
});
|
||||
|
||||
passport.deserializeUser(function(id, done) {
|
||||
User.findById(id, function(err, user) {
|
||||
done(err, user);
|
||||
});
|
||||
});
|
||||
|
||||
passport.use(new GoogleStrategy({
|
||||
clientID: config.auth.clientId,
|
||||
clientSecret: config.auth.clientSecret,
|
||||
callbackURL: config.auth.callback
|
||||
},
|
||||
function(accessToken, refreshToken, profile, done) {
|
||||
console.log(profile);
|
||||
console.log(accessToken);
|
||||
console.log(refreshToken);
|
||||
|
||||
profile = profile._json;
|
||||
User.findOne({ email: profile.email.toLowerCase() }, function(err, user) {
|
||||
if (err) { return done(err); }
|
||||
if (!user || !user.hasPermission("system.login")) {
|
||||
return done(null, false, { message: "You are not authorized to access this portal." });
|
||||
}
|
||||
|
||||
user.accessToken = accessToken;
|
||||
|
||||
if (refreshToken) {
|
||||
user.refreshToken = refreshToken;
|
||||
}
|
||||
if (profile.given_name) {
|
||||
user.name.first = profile.given_name;
|
||||
}
|
||||
if (profile.family_name) {
|
||||
user.name.last = profile.family_name;
|
||||
}
|
||||
if (profile.picture) {
|
||||
user.picture = profile.picture;
|
||||
}
|
||||
|
||||
user.save(function(err) {
|
||||
if (err) console.log(err);
|
||||
|
||||
return done(err, user);
|
||||
});
|
||||
});
|
||||
}));
|
||||
}
|
38
config/piler.js
Normal file
38
config/piler.js
Normal file
@ -0,0 +1,38 @@
|
||||
var pile = require('piler');
|
||||
|
||||
module.exports = function(app, server, io, config) {
|
||||
var js = pile.createJSManager();
|
||||
var css = pile.createCSSManager();
|
||||
|
||||
var root = config.root + "/public";
|
||||
|
||||
return {
|
||||
bind: function() {
|
||||
js.bind(app, server);
|
||||
css.bind(app, server);
|
||||
},
|
||||
|
||||
liveUpdate: function() {
|
||||
js.liveUpdate(css, io);
|
||||
},
|
||||
|
||||
addCssUrl: function(url) {
|
||||
css.addUrl(url);
|
||||
},
|
||||
|
||||
addCssFile: function(path) {
|
||||
css.addFile(root + path);
|
||||
},
|
||||
|
||||
addJsUrl: function(url) {
|
||||
js.addUrl(url);
|
||||
},
|
||||
|
||||
addJsFile: function(path) {
|
||||
js.addFile(root + path);
|
||||
},
|
||||
|
||||
js: js,
|
||||
css: css
|
||||
};
|
||||
};
|
59
config/routes.js
Normal file
59
config/routes.js
Normal file
@ -0,0 +1,59 @@
|
||||
|
||||
module.exports = function(app, auth, piler, calendar, config) {
|
||||
|
||||
piler.addCssUrl("//fonts.googleapis.com/css?family=Open+Sans:400,300");
|
||||
piler.addCssFile("/css/biomed.less");
|
||||
|
||||
piler.addJsUrl("//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
|
||||
piler.addJsUrl("//ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.js");
|
||||
piler.addJsUrl("//ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular-resource.js");
|
||||
piler.addJsUrl("http://d3js.org/d3.v2.js");
|
||||
piler.addJsFile("/js/lib/moment.js");
|
||||
piler.addJsFile("/js/lib/bootstrap-datepicker.js");
|
||||
piler.addJsFile("/js/lib/dialog.js");
|
||||
piler.addJsFile("/js/lib/select2.js");
|
||||
piler.addJsFile("/js/app.js");
|
||||
piler.addJsFile("/js/controllers.js");
|
||||
piler.addJsFile("/js/directives.js");
|
||||
piler.addJsFile("/js/filters.js");
|
||||
piler.addJsFile("/js/services.js");
|
||||
|
||||
app.all('/api/*', auth.requiresApiAccess);
|
||||
|
||||
var clients = require('../app/controllers/clients');
|
||||
app.get('/api/clients', clients.index);
|
||||
app.get('/api/clients/frequencies', clients.frequencies);
|
||||
app.get('/api/clients/:client_id', clients.get);
|
||||
app.get('/api/clients/:client_id/workorders', clients.workorders);
|
||||
app.post('/api/clients', clients.create);
|
||||
app.post('/api/clients/:client_id', clients.update);
|
||||
app.del('/api/clients/:client_id', clients.destroy);
|
||||
|
||||
var workorders = require('../app/controllers/workorders')(calendar);
|
||||
app.get('/api/workorders', workorders.index);
|
||||
app.get('/api/workorders/:workorder_id', workorders.get);
|
||||
app.post('/api/workorders', workorders.create);
|
||||
app.post('/api/workorders/:workorder_id', workorders.update);
|
||||
app.del('/api/workorders/:workorder_id', workorders.destroy);
|
||||
|
||||
var schedule = require('../app/controllers/schedule');
|
||||
app.get('/api/schedule', schedule.index);
|
||||
|
||||
var users = require('../app/controllers/users');
|
||||
app.get('/api/users', users.index);
|
||||
|
||||
var account = require('../app/controllers/account');
|
||||
app.get('/api/account', account.profile);
|
||||
|
||||
var messages = require('../app/controllers/messages')(config);
|
||||
app.post('/api/messages/send', messages.send);
|
||||
|
||||
var login = require('../app/controllers/login')(piler);
|
||||
app.get('/login', login.login);
|
||||
app.get('/login/error', login.error);
|
||||
app.get('/logout', login.logout);
|
||||
|
||||
var home = require('../app/controllers/home')(piler);
|
||||
app.get('/', auth.requiresUiLogin, home.index);
|
||||
app.get('*', auth.requiresUiLogin, home.index);
|
||||
};
|
Reference in New Issue
Block a user