forked from purewave1989/nodebb-plugin-sso-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
121 lines (105 loc) · 2.85 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
(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': {
'icon': 'fa-github',
'route': '/github'
}
});
var GitHub = {};
GitHub.getStrategy = function(strategies, callback) {
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'
});
callback(null, 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: username, email: 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, callback) {
custom_header.authentication.push({
"route": constants.admin.route,
"icon": constants.admin.icon,
"name": constants.name
});
callback(null, custom_header);
};
function renderAdmin(req, res, callback) {
res.render('sso/github/admin', {});
}
GitHub.init = function(app, middleware, controllers) {
app.get('/admin/github', middleware.admin.buildHeader, renderAdmin);
app.get('/api/admin/github', renderAdmin);
};
module.exports = GitHub;
}(module));