-
Notifications
You must be signed in to change notification settings - Fork 18
/
library.js
128 lines (112 loc) · 2.99 KB
/
library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(function(module) {
"use strict";
var User = module.parent.require('./user'),
db = module.parent.require('../src/database'),
meta = module.parent.require('./meta'),
passport = module.parent.require('passport'),
passportGithub = require('passport-github').Strategy,
fs = module.parent.require('fs'),
path = module.parent.require('path');
var constants = Object.freeze({
'name': "GitHub",
'admin': {
'route': '/github',
'icon': 'fa-github'
}
});
var GitHub = {};
GitHub.getStrategy = function(strategies) {
if (meta.config['social:github:id'] && meta.config['social:github:secret']) {
passport.use(new passportGithub({
clientID: meta.config['social:github:id'],
clientSecret: meta.config['social:github:secret'],
callbackURL: module.parent.require('nconf').get('url') + 'auth/github/callback'
}, function(token, tokenSecret, profile, done) {
GitHub.login(profile.id, profile.username, profile.emails[0].value, function(err, user) {
if (err) {
return done(err);
}
done(null, user);
});
}));
strategies.push({
name: 'github',
url: '/auth/github',
callbackURL: '/auth/github/callback',
icon: 'github',
scope: 'user:email'
});
}
return strategies;
};
GitHub.login = function(githubID, username, email, callback) {
if (!email) {
email = username + '@users.noreply.github.com';
}
GitHub.getUidByGitHubID(githubID, function(err, uid) {
if (err) {
return callback(err);
}
if (uid) {
// Existing User
callback(null, {
uid: uid
});
} else {
// New User
var success = function(uid) {
User.setUserField(uid, 'githubid', githubID);
db.setObjectField('githubid:uid', githubID, uid);
callback(null, {
uid: uid
});
}
User.getUidByEmail(email, function(err, uid) {
if (!uid) {
User.create(username, undefined, email, function(err, uid) {
if (err !== null) {
callback(err);
} else success(uid);
});
} else success(uid); // Existing account -- merge
});
}
});
}
GitHub.getUidByGitHubID = function(githubID, callback) {
db.getObjectField('githubid:uid', githubID, function(err, uid) {
if (err) {
callback(err);
} else {
callback(null, uid);
}
});
};
GitHub.addMenuItem = function(custom_header) {
custom_header.authentication.push({
"route": constants.admin.route,
"icon": constants.admin.icon,
"name": constants.name
});
return custom_header;
}
GitHub.addAdminRoute = function(custom_routes, callback) {
fs.readFile(path.resolve(__dirname, './static/admin.tpl'), function (err, template) {
custom_routes.routes.push({
"route": constants.admin.route,
"method": "get",
"options": function(req, res, callback) {
callback({
req: req,
res: res,
route: constants.admin.route,
name: constants.name,
content: template
});
}
});
callback(null, custom_routes);
});
};
module.exports = GitHub;
}(module));