blob: cfc399f900488c8f2593ba6683b4eda0982774d6 [file] [log] [blame]
avm9996304def3e2016-11-27 22:53:05 +01001(function() {
2 const IMGUR_APP_KEY = '90922ae86b91b09541d74134a5bb0f6404e0c378f';
3 const IMGUR_APP_SECRET = 'b100656ebf595b7464e428615b9bc703';
4 const IMGUR_REQUEST_TOKEN_URL = 'https://api.imgur.com/oauth/request_token';
5 const IMGUR_USER_AUTHENTICATION_URL = 'https://api.imgur.com/oauth/authorize';
6 const IMGUR_ACCESS_TOKEN_URL = 'https://api.imgur.com/oauth/access_token';
7 const IMGUR_USER_INFO_URL = 'http://api.imgur.com/2/account.json';
8 const IMGUR_PHOTO_UPLOAD_URL = 'http://api.imgur.com/2/account/images.json';
9 const IMGUR_LOGOUT_URL = 'http://imgur.com/logout';
10 const OAUTH_SIGNATURE_METHOD = 'HMAC-SHA1';
11 const OAUTH_VERSION = '1.0';
12
13 var Imgur = window.Imgur = {
14 siteId: 'imgur',
15 currentUserId: null,
16 currentUserOauthToken: '',
17 currentUserOauthTokenSecret: '',
18 accessTokenCallback: null,
19
20 isRedirectUrl: function() {},
21
22 getAuthorizationHeader: function(message, accessor) {
23 OAuth.setTimestampAndNonce(message);
24 OAuth.SignatureMethod.sign(message, accessor);
25 return OAuth.getAuthorizationHeader("", message.parameters);
26 },
27
28 getRequestToken: function(callback) {
29 Imgur.accessTokenCallback = callback;
30 var message = {
31 action: IMGUR_REQUEST_TOKEN_URL,
32 method: 'POST',
33 parameters: {
34 'oauth_consumer_key': IMGUR_APP_KEY,
35 'oauth_signature_method': OAUTH_SIGNATURE_METHOD,
36 'oauth_version': OAUTH_VERSION
37 }
38 };
39 var accessor = {
40 consumerKey: IMGUR_APP_KEY,
41 consumerSecret: IMGUR_APP_SECRET
42 };
43
44 // Get oauth signature header
45 var header = Imgur.getAuthorizationHeader(message, accessor);
46
47 ajax({
48 url: IMGUR_REQUEST_TOKEN_URL,
49 method: 'POST',
50 headers: {
51 'Authorization': header
52 },
53 success: function(response) {
54 parameters = OAuth.getParameterMap(response);
55 var oauth_token = parameters['oauth_token'];
56 var oauth_token_secret = parameters['oauth_token_secret'];
57 Imgur.currentUserOauthToken = oauth_token;
58 Imgur.currentUserOauthTokenSecret = oauth_token_secret;
59 Imgur.getUserAuthentication(oauth_token);
60 },
61 status: {
62 others: function() {
63 callback('failure', 'imgur_failed_to_get_request_token');
64 }
65 }
66 });
67 },
68
69 getUserAuthentication: function(oauth_token) {
70 var url = IMGUR_USER_AUTHENTICATION_URL + '?oauth_token=' + oauth_token +
71 '&oauth_callback=ready';
72 chrome.tabs.create({url: url}, function(tab) {
73 chrome.tabs.onUpdated.addListener(
74 function(tabId, changeInfo, _tab) {
75 if (tabId == tab.id && changeInfo.url
76 && changeInfo.url.indexOf('oauth_verifier=') > 0) {
77 chrome.tabs.remove(tabId);
78 Imgur.parseAccessToken(changeInfo.url);
79 }
80 });
81 });
82 },
83
84 parseAccessToken: function(url) {
85 var oauth_verifier = OAuth.getParameter(url, 'oauth_verifier');
86 Imgur.getAccessToken(Imgur.accessTokenCallback, oauth_verifier);
87 Imgur.accessTokenCallback = null;
88 },
89
90 getAccessToken: function(callback, oauth_verifier) {
91 if (!oauth_verifier) {
92 Imgur.getRequestToken(callback);
93 return;
94 }
95 var message = {
96 action: IMGUR_ACCESS_TOKEN_URL,
97 method: 'POST',
98 parameters: {
99 'oauth_consumer_key': IMGUR_APP_KEY,
100 'oauth_token': Imgur.currentUserOauthToken,
101 'oauth_token_secret': Imgur.currentUserOauthTokenSecret,
102 'oauth_signature_method': OAUTH_SIGNATURE_METHOD,
103 'oauth_verifier': oauth_verifier,
104 'oauth_version': OAUTH_VERSION
105 }
106 };
107 var accessor = {
108 consumerKey: IMGUR_APP_KEY,
109 consumerSecret: IMGUR_APP_SECRET,
110 tokenSecret: Imgur.currentUserOauthTokenSecret
111 };
112 var header = Imgur.getAuthorizationHeader(message, accessor);
113
114 ajax({
115 url: IMGUR_ACCESS_TOKEN_URL,
116 method: 'POST',
117 headers: {
118 'Authorization': header
119 },
120 success: function(response) {
121 responseMap = OAuth.getParameterMap(response);
122 var accessToken = responseMap.oauth_token;
123 var accessTokenSecret = responseMap.oauth_token_secret;
124 var user = new User({
125 id: null,
126 accessToken: accessToken,
127 accessTokenSecret: accessTokenSecret
128 });
129
130 callback('success', user);
131 },
132 status: {
133 others: function(data) {
134 callback('failure', 'imgur_failed_to_get_access_token');
135 }
136 }
137 });
138 },
139
140 getUserInfo: function(user, callback) {
141 var url = IMGUR_USER_INFO_URL;
142 var message = {
143 action: url,
144 method: 'GET',
145 parameters: {
146 'oauth_consumer_key': IMGUR_APP_KEY,
147 'oauth_token': user.accessToken,
148 'oauth_signature_method': OAUTH_SIGNATURE_METHOD,
149 'oauth_version': OAUTH_VERSION
150 }
151 };
152
153 var accessor = {
154 consumerSecret: IMGUR_APP_SECRET,
155 tokenSecret: user.accessTokenSecret
156 };
157
158 var header = Imgur.getAuthorizationHeader(message, accessor);
159 ajax({
160 url: url,
161 method: 'GET',
162 headers: {
163 'Authorization': header
164 },
165 success: function(data) {
166 if (callback) {
167 user.id = data.account.url;
168 user.name = data.account.url;
169 callback('success', user);
170 }
171 },
172 status: {
173 others: function(data) {
174 callback('failure', 'failed_to_get_user_info');
175 }
176 }
177 });
178 },
179
180 upload: function(user, caption, imageData, callback) {
181 caption = encodeURIComponent(caption);
182 var message = {
183 action: IMGUR_PHOTO_UPLOAD_URL,
184 method: 'POST',
185 parameters: {
186 'oauth_consumer_key': IMGUR_APP_KEY,
187 'oauth_token': user.accessToken,
188 'oauth_signature_method': OAUTH_SIGNATURE_METHOD,
189 'oauth_version': OAUTH_VERSION
190 }
191 };
192 var accessor = {
193 consumerSecret: IMGUR_APP_SECRET,
194 tokenSecret: user.accessTokenSecret
195 };
196 var header = Imgur.getAuthorizationHeader(message, accessor);
197
198 var binaryData = {
199 boundary: MULTIPART_FORMDATA_BOUNDARY,
200 name: 'image',
201 value: 'screencapture.png',
202 data: imageData,
203 type: 'image/png'
204 };
205
206 ajax({
207 url: IMGUR_PHOTO_UPLOAD_URL,
208 method: 'POST',
209 multipartData: binaryData,
210 headers: {
211 'Authorization': header
212 },
213 success: function(response) {
214 callback('success', response.images.links.original);
215 },
216 status: {
217 others: function(err, statusCode) {
218 if (statusCode == 401) {
219 callback('failure', 'bad_access_token');
220 } else {
221 callback('failure', 'failed_to_upload_image');
222 };
223 }
224 }
225 });
226 },
227
228 getPhotoLink: function(user, photoLink, callback) {
229 callback('success', photoLink);
230 },
231
232 logout: function(callback) {
233 ajax({
234 url: IMGUR_LOGOUT_URL,
235 success: function() {
236 callback();
237 }
238 });
239 }
240 };
241})();