forked from mani95lisa/nodebb-plugin-sso-weibo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
140 lines (120 loc) · 3.71 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
129
130
131
132
133
134
135
136
137
138
139
140
(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'),
passportWeibo = require('passport-weibo').Strategy,
nconf = module.parent.require('nconf'),
async = module.parent.require('async'),
winston = module.parent.require('winston');
var constants = Object.freeze({
'name': "Weibo",
'admin': {
'icon': 'fa-weibo',
'route': '/plugins/sso-weibo'
}
});
var Weibo = {};
Weibo.init = function(params, callback) {
function render(req, res) {
res.render('admin/plugins/sso-weibo', {});
}
params.router.get('/admin/plugins/sso-weibo', params.middleware.admin.buildHeader, render);
params.router.get('/api/admin/plugins/sso-weibo', render);
callback();
};
Weibo.getStrategy = function(strategies, callback) {
if (meta.config['social:weibo:app_id'] && meta.config['social:weibo:secret']) {
passport.use(new passportWeibo(
{
clientID: meta.config['social:weibo:app_id'],
clientSecret: meta.config['social:weibo:secret'],
callbackURL: nconf.get('url') + '/auth/weibo/callback'
},
function(accessToken, refreshtoken, profile, done) {
var avatar = profile._json.avatar_large;
Weibo.login(profile.id, profile.displayName, avatar, function(err, user) {
if (err) {
return done(err);
}
done(null, user);
});
}
));
strategies.push({
name: 'weibo',
url: '/auth/weibo',
callbackURL: '/auth/weibo/callback',
icon: constants.admin.icon,
scope: ''
});
}
callback(null, strategies);
};
Weibo.login = function(weiboid, displayName, avatar, callback) {
Weibo.getUidByWeiboId(weiboid, function(err, uid) {
if (err) {
return callback(err);
}
if (uid !== null) {
// Existing User
callback(null, {
uid: uid
});
} else {
// New User
user.create({username: displayName}, function(err, uid) {
if (err) {
return callback(err);
}
// Save weibo-specific information to the user
user.setUserField(uid, 'weiboid', weiboid);
db.setObjectField('weiboid:uid', weiboid, uid);
// Save their avatar, if present
if (avatar) {
user.setUserField(uid, 'uploadedpicture', avatar);
user.setUserField(uid, 'picture', avatar);
}
callback(null, {
uid: uid
});
});
}
});
};
Weibo.getUidByWeiboId = function(weiboid, callback) {
db.getObjectField('weiboid:uid', weiboid, function(err, uid) {
if (err) {
return callback(err);
}
callback(null, uid);
});
};
Weibo.addMenuItem = function(custom_header, callback) {
custom_header.authentication.push({
"route": constants.admin.route,
"icon": constants.admin.icon,
"name": constants.name
});
callback(null, custom_header);
};
Weibo.deleteUserData = function(uid, callback) {
async.waterfall([
async.apply(user.getUserField, uid, 'weiboid'),
function (oAuthIdToDelete, next) {
db.deleteObjectField('weiboid:uid', oAuthIdToDelete, next);
}
],
function (err) {
if (err) {
winston.error('[sso-weibo] Could not remove OAuthId data for uid '
+ '.Error: ' + err);
return callback(err);
}
callback(null, uid);
}
);
};
module.exports = Weibo;
}(module));