-
Notifications
You must be signed in to change notification settings - Fork 1
/
kivaoAuth.js
127 lines (108 loc) · 3.66 KB
/
kivaoAuth.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
var kivaOauth = "https://www.kiva.org/oauth";
var requesttokenurl = "https://api.kivaws.org/oauth/request_token";
var accesstokenurl = "https://api.kivaws.org/oauth/access_token";
exports.$ = function(app_id, client_id, client_secret, _scopes, _callbackURL){
var _ = require('./myapp.js');
this.oauth = require('oauth');
this.version = "1.0";
this.signatureMethod = "HMAC-SHA1";
this.appid = _.appid;
this.clientid=_.clientid;
this.clientsecret = _.clientsecret;
this.scopes = _.scopes;
this.callbackURL = _.callbackURL;
this.authurl = kivaOauth + "/authorize?" +
"scope=" + this.scopes.toString() +
"&response_type=code" +
"&oauth_callback=" + this.callbackURL +
"&client_id=" + this.clientid;
this._getProtectedResource =
function ( access, url, callback){
var oa = this.getNewOauth();
oa.getProtectedResource(
url,
"GET",
access.access_token,
access.access_token_secret,
function (error, data, response) {
callback(data);
});
}
this.getNewOauth = function(){
return new this.oauth.OAuth(
requesttokenurl,
accesstokenurl,
this.clientid,
this.clientsecret,
this.version,
this.callbackURL,
this.signatureMethod);
}
};
var kivaApi = "https://api.kivaws.org/v1";
exports.$.prototype.getLender = function (access, callback)
{
this._getProtectedResource(access, kivaApi + "/my/lender.json",function(data){
callback(data);
});
};
exports.$.prototype.getEmail = function (access, callback)
{
this._getProtectedResource(access, kivaApi + "/my/email.json",function(data){
callback(data);
});
};
exports.$.prototype.getBalance = function (access, callback)
{
this._getProtectedResource(access, kivaApi + "/my/balance.json",function(data){
callback(data);
});
};
exports.$.prototype.getAccount = function (access, callback)
{
this._getProtectedResource(access, kivaApi + "/my/account.json",function(data){
callback(data);
});
};
exports.$.prototype.getOAuthRequestToken =
function(req,resp){
var oa = this.getNewOauth();
var kivaUrl = this.authurl;
oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
if (error) {
//console.log(error);
resp.send(error.data, 500);
} else {
req.session.oa = oa;
req.session.oauth_token = oauth_token;
req.session.oauth_token_secret = oauth_token_secret;
kivaUrl += ("&oauth_token=" + oauth_token + "&state=" + req.sessionID);
resp.redirect(kivaUrl);
}
});
};
exports.$.prototype.getOAuthAccessToken =
function(req,resp, callback){
var oa = this.getNewOauth();
var kivaoauth = this;
oa.getOAuthAccessToken(
req.session.oauth_token,
req.session.oauth_token_secret,
req.param('oauth_verifier'),
function(error, oauth_access_token, oauth_access_token_secret, results) {
if(error) {
//console.log(error);
resp.send(error.data, error.statusCode)
}
else {
var access = {"access_token":oauth_access_token,"access_token_secret":oauth_access_token_secret};
kivaoauth.getAccount(access,function(data){
var account = JSON.parse(data).user_account;
kivaoauth.getEmail(access,function(data){
var email = JSON.parse(data).user_email;
callback({"access":access,"user_account":account,"user_email":email});
})
})
}
});
};