Skip to content

Commit e4a7fbb

Browse files
qiushi2012flovilmart
authored andcommitted
add login with qq, wechat and weibo (#3069)
* add login with qq, wechat and weibo * modify the code style, etc
1 parent edb7b70 commit e4a7fbb

File tree

4 files changed

+155
-1
lines changed

4 files changed

+155
-1
lines changed

src/authDataManager/index.js

100644100755
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ let digits = require("./twitter"); // digits tokens are validated by twitter
1010
let janrainengage = require("./janrainengage");
1111
let janraincapture = require("./janraincapture");
1212
let vkontakte = require("./vkontakte");
13+
let qq = require("./qq");
14+
let wechat = require("./wechat");
15+
let weibo = require("./weibo");
1316

1417
let anonymous = {
1518
validateAuthData: () => {
@@ -33,7 +36,10 @@ let providers = {
3336
digits,
3437
janrainengage,
3538
janraincapture,
36-
vkontakte
39+
vkontakte,
40+
qq,
41+
wechat,
42+
weibo
3743
}
3844

3945
module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) {

src/authDataManager/qq.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Helper functions for accessing the qq Graph API.
2+
var https = require('https');
3+
var Parse = require('parse/node').Parse;
4+
5+
// Returns a promise that fulfills iff this user id is valid.
6+
function validateAuthData(authData) {
7+
return graphRequest('me?access_token=' + authData.access_token).then(function (data) {
8+
if (data && data.openid == authData.id) {
9+
return;
10+
}
11+
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'qq auth is invalid for this user.');
12+
});
13+
}
14+
15+
// Returns a promise that fulfills if this app id is valid.
16+
function validateAppId(appIds, authData) {
17+
return Promise.resolve();
18+
}
19+
20+
// A promisey wrapper for qq graph requests.
21+
function graphRequest(path) {
22+
return new Promise(function (resolve, reject) {
23+
https.get('https://graph.qq.com/oauth2.0/' + path, function (res) {
24+
var data = '';
25+
res.on('data', function (chunk) {
26+
data += chunk;
27+
});
28+
res.on('end', function () {
29+
var starPos=data.indexOf("(");
30+
var endPos=data.indexOf(")");
31+
if(starPos==-1||endPos==-1){
32+
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'qq auth is invalid for this user.');
33+
}
34+
data=data.substring(starPos+1,endPos-1);
35+
data = JSON.parse(data);
36+
resolve(data);
37+
});
38+
}).on('error', function (e) {
39+
reject('Failed to validate this access token with qq.');
40+
});
41+
});
42+
}
43+
44+
module.exports = {
45+
validateAppId,
46+
validateAuthData
47+
};

src/authDataManager/wechat.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Helper functions for accessing the WeChat Graph API.
2+
var https = require('https');
3+
var Parse = require('parse/node').Parse;
4+
5+
// Returns a promise that fulfills iff this user id is valid.
6+
function validateAuthData(authData) {
7+
return graphRequest('auth?access_token=' + authData.access_token +'&openid=' +authData.id).then(function (data) {
8+
if (data.errcode == 0) {
9+
return;
10+
}
11+
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'qq auth is invalid for this user.');
12+
});
13+
}
14+
15+
// Returns a promise that fulfills if this app id is valid.
16+
function validateAppId(appIds, authData) {
17+
return Promise.resolve();
18+
}
19+
20+
// A promisey wrapper for WeChat graph requests.
21+
function graphRequest(path) {
22+
return new Promise(function (resolve, reject) {
23+
https.get('https://api.weixin.qq.com/sns/' + path, function (res) {
24+
var data = '';
25+
res.on('data', function (chunk) {
26+
data += chunk;
27+
});
28+
res.on('end', function () {
29+
data = JSON.parse(data);
30+
resolve(data);
31+
});
32+
}).on('error', function (e) {
33+
reject('Failed to validate this access token with weixin.');
34+
});
35+
});
36+
}
37+
38+
module.exports = {
39+
validateAppId,
40+
validateAuthData
41+
};

src/authDataManager/weibo.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Helper functions for accessing the weibo Graph API.
2+
var https = require('https');
3+
var Parse = require('parse/node').Parse;
4+
var querystring = require('querystring');
5+
6+
// Returns a promise that fulfills iff this user id is valid.
7+
function validateAuthData(authData) {
8+
return graphRequest(authData.access_token).then(function (data) {
9+
if (data && data.uid == authData.id) {
10+
return;
11+
}
12+
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'weibo auth is invalid for this user.');
13+
});
14+
}
15+
16+
// Returns a promise that fulfills if this app id is valid.
17+
function validateAppId(appIds, authData) {
18+
return Promise.resolve();
19+
}
20+
21+
// A promisey wrapper for weibo graph requests.
22+
function graphRequest(access_token) {
23+
return new Promise(function (resolve, reject) {
24+
var postData = querystring.stringify({
25+
"access_token":access_token
26+
});
27+
var options = {
28+
hostname: 'api.weibo.com',
29+
path: '/oauth2/get_token_info',
30+
method: 'POST',
31+
headers: {
32+
'Content-Type': 'application/x-www-form-urlencoded',
33+
'Content-Length': Buffer.byteLength(postData)
34+
}
35+
};
36+
var req = https.request(options, function(res){
37+
var data = '';
38+
res.on('data', function (chunk) {
39+
data += chunk;
40+
});
41+
res.on('end', function () {
42+
data = JSON.parse(data);
43+
resolve(data);
44+
});
45+
res.on('error', function (err) {
46+
reject('Failed to validate this access token with weibo.');
47+
});
48+
});
49+
req.on('error', function (e){
50+
reject('Failed to validate this access token with weibo.');
51+
});
52+
req.write(postData);
53+
req.end();
54+
});
55+
}
56+
57+
module.exports = {
58+
validateAppId,
59+
validateAuthData
60+
};

0 commit comments

Comments
 (0)