-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoauth.utils.js
121 lines (101 loc) · 2.9 KB
/
oauth.utils.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
var document = {getElementsByTagName:function(value) { return null; }};
y.include("http://oauth.googlecode.com/svn/code/javascript/oauth.js");
y.include("http://oauth.googlecode.com/svn/code/javascript/sha1.js");
function sendRequest(args)
{
switch(args.method) {
case 'POST':
return postRequest(args);
case 'PUT':
return putRequest(args);
case 'DELETE':
return deleteRequest(args);
case 'GET':
default:
return getRequest(args);
}
return null;
}
function getRequest(args)
{
var auth = (args.accessor) ? oAuthSignRequest(args) : null;
var rsp = null;
try {
if(auth) request.header('Authorization', auth.header);
rsp = request.get().response;
} catch(err) {
rsp = {'result':'failure', 'error': err};
}
return rsp;
}
function postRequest(args)
{
var data = oAuthBuildContent(args.parameters);
var auth = (args.accessor) ? oAuthSignRequest(args) : null;
var rsp = null;
try {
if(auth) request.header('Authorization', auth.header);
rsp = request.post( data ).response;
} catch(err) {
rsp = {'result':'failure', 'error': err};
}
return rsp;
}
function putRequest(args)
{
var data = oAuthBuildContent(args.parameters);
var auth = (args.accessor) ? oAuthSignRequest(args) : null;
var rsp = null;
try {
if(auth) request.header('Authorization', auth.header);
rsp = request.put( data ).response;
} catch(err) {
rsp = {'result':'failure', 'error': err};
}
return rsp;
}
function deleteRequest(args)
{
var auth = (args.accessor) ? oAuthSignRequest(args) : null;
var rsp = null;
try {
if(auth) request.header('Authorization', auth.header);
rsp = request.del().response;
} catch(err) {
rsp = {'result':'failure', 'error': err};
}
return rsp;
}
function oAuthBuildContent(content)
{
var components = [];
for(var i=0, count=content.length; i<count; i++) {
var item = content[i];
var key = item[0];
var value = encodeURIComponent(item[1]);
components.push(key+'='+value);
}
return components.join('&');
}
function oAuthSignRequest(args)
{
var accessor = args.accessor;
var message = {};
message.action = args.action || request.url;
message.method = args.method || 'GET';
message.parameters = args.parameters || null;
OAuth.setTimestampAndNonce(message);
OAuth.setParameter(message, "oauth_consumer_key", accessor.consumerKey);
OAuth.setParameter(message, "oauth_version", '1.0');
if(accessor.token) {
OAuth.setParameter(message, "oauth_token", accessor.token);
}
OAuth.SignatureMethod.sign(message, accessor);
var realm = accessor.realm || '';
var header = OAuth.getAuthorizationHeader(realm, message.parameters);
var auth = {message:message, accessor:accessor, header:header};
if(args.debug==true) {
y.log(auth);
}
return auth;
}