-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
207 lines (187 loc) · 5.33 KB
/
auth.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
mbtweet =
{
debug : false ,
build : 00001 ,
version : "1.0" ,
bitly_token : "",
database:
{
name : "mbtweet",
version : "1.0"
},
user :
{
language : navigator.language.substr(0,2),
conv_length : 3,
}
}
mbtweetOAuth =
{
consumerKey : "qemYQrHH04a3OqYTvBRVhA",
consumerSecret : "rouEipefKjITze8qZSqi6jev1T7D6bHt3q7ZSj63A8",
serviceProvider : {signatureMethod : "HMAC-SHA1" }
};
init_auth = function()
{
var sections = document.querySelectorAll("section.localize");
for( var j = 0 ; j < sections.length ; j++ )
{
var translation_parts = sections[j].querySelectorAll("*[lang]");
var removed_parts_number = translation_parts.length;
for( var i = 0 ; i < translation_parts.length ; i++ )
{
if( translation_parts[i].lang != mbtweet.user.language )
{
translation_parts[i].style.display = "none";
removed_parts_number--;
}
if( removed_parts_number == 0)
{
translation_parts[0].style.display = "";
}
}
}
var captured_oauth_token = "";
if( location.search.match(/\?oauth_token/) )
{
captured_oauth_token = location.search.match(/\?oauth_token\=(.+)/)[1];
restore_mb_settings();
mbtweetOAuthActivate.oauth_token_secret = mbtweetOAuth.oauth_token_secret;
document.querySelector("#start").style.display = "none";
document.querySelector("#post_key").style.display = "none";
document.querySelector("#go_pbtweet").style.display = "block";
mbtweetOAuthActivate.callAPI( "http://twitter.com/oauth/access_token" ,
"POST",
[
["oauth_token" , captured_oauth_token],
]
);
}
}
jump_to_auth_page = function()
{
var key_list = document.forms[0].keys.value.match(/^oauth_token\=(.+)\&oauth_token_secret\=(.+)$/);
if( key_list.length != 0 )
{
mbtweetOAuth.accessToken = key_list[1];
mbtweetOAuth.accessTokenSecret = key_list[2];
save_storage_Changes();
location.href = "http://twitter.com/oauth/authorize?" + key_list[0];
}
else
{
alert( "Please copy all text and paste to OAuth token and token secret keys." );
}
}
open_pbtweet = function()
{
var key_list = document.forms[0].pbkey.value.split("&");
if( key_list.length != 0 )
{
mbtweetOAuth.accessToken = key_list[0].match(/oauth_token\=(.+)/)[1];
mbtweetOAuth.accessTokenSecret = key_list[1].match(/oauth_token_secret\=(.+)/)[1];
mbtweet.user.screen_name = key_list[3].match(/screen_name\=(.+)/)[1];
save_storage_Changes();
location.href = "http://taiyolab.com/mbtweet/";
}
else
{
alert( "Please copy all text and paste to activate keys." );
}
}
mbtweetOAuthActivate =
{
consumerKey : mbtweetOAuth.consumerKey,
consumerSecret : mbtweetOAuth.consumerSecret,
serviceProvider : mbtweetOAuth.serviceProvider,
};
request_token = function()
{
mbtweetOAuthActivate.callAPI( "http://twitter.com/oauth/request_token" ,
"POST",
[
["callback" , "retreveRequestToken"],
]
);
}
retreveRequestToken = function(data)
{
window.console.log( data );
}
mbtweetOAuthActivate.callAPI = function( action , method, parameter )
{
var accessor = { consumerSecret: mbtweetOAuthActivate.consumerSecret };
var message = { action: action
, method: method
, parameters:
[
[ "oauth_consumer_key" , mbtweetOAuthActivate.consumerKey ],
[ "oauth_signature_method" , mbtweetOAuthActivate.signatureMethod ],
]
};
for (var i = 0; i < parameter.length; i++ )
{
var input = parameter[i];
message.parameters.push([ parameter[i][0], parameter[i][1] ]);
}
OAuth.completeRequest( message, accessor );
OAuth.SignatureMethod.sign( message, accessor );
var access_URL = action + "?" + OAuth.formEncode( message.parameters );
if( method == "GET" )
{
jsonp_fetch( access_URL );
}
else
{
post_activate( access_URL );
}
return true;
}
post_activate = function( url )
{
if( document.querySelector("#proxy_frame") )
{
document.querySelector("#post").removeChild( document.querySelector("#proxy_frame") );
}
var proxy_token = guid();
var pxcatch_frame = document.createElement("IFRAME");
pxcatch_frame.id = proxy_token;
pxcatch_frame.name = proxy_token;
//pxcatch_frame.style.display = "none";
document.querySelector("#post").appendChild( pxcatch_frame );
if( location.href.match(/\?oauth_token\=/) )
{
pxcatch_frame.addEventListener(
"load",
function(event)
{
document.querySelector("#start").style.display = "none";
document.querySelector("#post_key").style.display = "none";
document.querySelector("#go_pbtweet").style.display = "block";
},
true
);
}
else
{
pxcatch_frame.addEventListener(
"load",
function(event)
{
document.querySelector("#start").style.display = "none";
document.querySelector("#post_key").style.display = "block";
},
true
);
}
var proxy_frame = document.createElement("IFRAME");
proxy_frame.id = "proxy_frame";
proxy_frame.style.display = "none";
document.querySelector("#post").appendChild( proxy_frame );
var post_process = proxy_frame.contentWindow.document;
post_process.open();
post_process.write('<form method="POST" action="'+ url +'" target="' + proxy_token + '">');
post_process.write('</form>');
post_process.write('<script>window.onload = function(){document.forms[0].submit();}</script>');
post_process.close();
}