PageRenderTime 69ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/dist/oauth.js

https://github.com/javascript-forks/oauth-io.oauth-js
JavaScript | 1186 lines | 1158 code | 26 blank | 2 comment | 304 complexity | 825ee21a8916aa4fe0764335b45b7935 MD5 | raw file
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. module.exports = {
  3. oauthd_url: "https://oauth.io",
  4. oauthd_api: "https://oauth.io/api",
  5. version: "web-0.2.0",
  6. options: {}
  7. };
  8. },{}],2:[function(require,module,exports){
  9. "use strict";
  10. var Url, cache, config, cookies, sha1;
  11. config = require("../config");
  12. cookies = require("../tools/cookies");
  13. cache = require("../tools/cache");
  14. Url = require("../tools/url");
  15. sha1 = require("../tools/sha1");
  16. module.exports = function(window, document, jQuery, navigator) {
  17. var $, client_states, oauth_result, oauthio, parse_urlfragment, providers_api, providers_cb, providers_desc;
  18. $ = jQuery;
  19. Url = Url(document);
  20. cookies.init(config, document);
  21. cache.init(cookies, config);
  22. oauthio = {
  23. request: {}
  24. };
  25. providers_desc = {};
  26. providers_cb = {};
  27. providers_api = {
  28. execProvidersCb: function(provider, e, r) {
  29. var cbs, i;
  30. if (providers_cb[provider]) {
  31. cbs = providers_cb[provider];
  32. delete providers_cb[provider];
  33. for (i in cbs) {
  34. cbs[i](e, r);
  35. }
  36. }
  37. },
  38. getDescription: function(provider, opts, callback) {
  39. opts = opts || {};
  40. if (typeof providers_desc[provider] === "object") {
  41. return callback(null, providers_desc[provider]);
  42. }
  43. if (!providers_desc[provider]) {
  44. providers_api.fetchDescription(provider);
  45. }
  46. if (!opts.wait) {
  47. return callback(null, {});
  48. }
  49. providers_cb[provider] = providers_cb[provider] || [];
  50. providers_cb[provider].push(callback);
  51. }
  52. };
  53. config.oauthd_base = Url.getAbsUrl(config.oauthd_url).match(/^.{2,5}:\/\/[^/]+/)[0];
  54. client_states = [];
  55. oauth_result = void 0;
  56. (parse_urlfragment = function() {
  57. var cookie_state, results;
  58. results = /[\\#&]oauthio=([^&]*)/.exec(document.location.hash);
  59. if (results) {
  60. document.location.hash = document.location.hash.replace(/&?oauthio=[^&]*/, "");
  61. oauth_result = decodeURIComponent(results[1].replace(/\+/g, " "));
  62. cookie_state = cookies.readCookie("oauthio_state");
  63. if (cookie_state) {
  64. client_states.push(cookie_state);
  65. cookies.eraseCookie("oauthio_state");
  66. }
  67. }
  68. })();
  69. window.location_operations = {
  70. reload: function() {
  71. return document.location.reload();
  72. },
  73. getHash: function() {
  74. return document.location.hash;
  75. },
  76. setHash: function(newHash) {
  77. return document.location.hash = newHash;
  78. },
  79. changeHref: function(newLocation) {
  80. return document.location.href = newLocation;
  81. }
  82. };
  83. return function(exports) {
  84. var delayedFunctions, delayfn, e, _preloadcalls;
  85. delayedFunctions = function($) {
  86. oauthio.request = require("./oauthio_requests")($, config, client_states, cache, providers_api);
  87. providers_api.fetchDescription = function(provider) {
  88. if (providers_desc[provider]) {
  89. return;
  90. }
  91. providers_desc[provider] = true;
  92. $.ajax({
  93. url: config.oauthd_api + "/providers/" + provider,
  94. data: {
  95. extend: true
  96. },
  97. dataType: "json"
  98. }).done(function(data) {
  99. providers_desc[provider] = data.data;
  100. providers_api.execProvidersCb(provider, null, data.data);
  101. }).always(function() {
  102. if (typeof providers_desc[provider] !== "object") {
  103. delete providers_desc[provider];
  104. providers_api.execProvidersCb(provider, new Error("Unable to fetch request description"));
  105. }
  106. });
  107. };
  108. };
  109. if (exports.OAuth == null) {
  110. exports.OAuth = {
  111. initialize: function(public_key, options) {
  112. var i;
  113. config.key = public_key;
  114. if (options) {
  115. for (i in options) {
  116. config.options[i] = options[i];
  117. }
  118. }
  119. },
  120. setOAuthdURL: function(url) {
  121. config.oauthd_url = url;
  122. config.oauthd_base = Url.getAbsUrl(config.oauthd_url).match(/^.{2,5}:\/\/[^/]+/)[0];
  123. },
  124. getVersion: function() {
  125. return config.version;
  126. },
  127. create: function(provider, tokens, request) {
  128. var i, make_res, make_res_endpoint, res;
  129. if (!tokens) {
  130. return cache.tryCache(exports.OAuth, provider, true);
  131. }
  132. if (typeof request !== "object") {
  133. providers_api.fetchDescription(provider);
  134. }
  135. make_res = function(method) {
  136. return oauthio.request.mkHttp(provider, tokens, request, method);
  137. };
  138. make_res_endpoint = function(method, url) {
  139. return oauthio.request.mkHttpEndpoint(data.provider, tokens, request, method, url);
  140. };
  141. res = {};
  142. for (i in tokens) {
  143. res[i] = tokens[i];
  144. }
  145. res.get = make_res("GET");
  146. res.post = make_res("POST");
  147. res.put = make_res("PUT");
  148. res.patch = make_res("PATCH");
  149. res.del = make_res("DELETE");
  150. res.me = function(opts) {
  151. oauthio.request.mkHttpMe(data.provider, tokens, request, "GET");
  152. };
  153. return res;
  154. },
  155. popup: function(provider, opts, callback) {
  156. var defer, frm, getMessage, res, url, wnd, wndTimeout, wnd_options, wnd_settings, _ref;
  157. getMessage = function(e) {
  158. if (e.origin !== config.oauthd_base) {
  159. return;
  160. }
  161. try {
  162. wnd.close();
  163. } catch (_error) {}
  164. opts.data = e.data;
  165. return oauthio.request.sendCallback(opts, defer);
  166. };
  167. wnd = void 0;
  168. frm = void 0;
  169. wndTimeout = void 0;
  170. defer = (_ref = window.jQuery) != null ? _ref.Deferred() : void 0;
  171. opts = opts || {};
  172. if (!config.key) {
  173. if (defer != null) {
  174. defer.reject(new Error("OAuth object must be initialized"));
  175. }
  176. return callback(new Error("OAuth object must be initialized"));
  177. }
  178. if (arguments.length === 2 && typeof opts === 'function') {
  179. callback = opts;
  180. opts = {};
  181. }
  182. if (cache.cacheEnabled(opts.cache)) {
  183. res = cache.tryCache(exports.OAuth, provider, opts.cache);
  184. if (res) {
  185. if (defer != null) {
  186. defer.resolve(res);
  187. }
  188. if (callback) {
  189. return callback(null, res);
  190. } else {
  191. return;
  192. }
  193. }
  194. }
  195. if (!opts.state) {
  196. opts.state = sha1.create_hash();
  197. opts.state_type = "client";
  198. }
  199. client_states.push(opts.state);
  200. url = config.oauthd_url + "/auth/" + provider + "?k=" + config.key;
  201. url += "&d=" + encodeURIComponent(Url.getAbsUrl("/"));
  202. if (opts) {
  203. url += "&opts=" + encodeURIComponent(JSON.stringify(opts));
  204. }
  205. wnd_settings = {
  206. width: Math.floor(window.outerWidth * 0.8),
  207. height: Math.floor(window.outerHeight * 0.5)
  208. };
  209. if (wnd_settings.height < 350) {
  210. wnd_settings.height = 350;
  211. }
  212. if (wnd_settings.width < 800) {
  213. wnd_settings.width = 800;
  214. }
  215. wnd_settings.left = window.screenX + (window.outerWidth - wnd_settings.width) / 2;
  216. wnd_settings.top = window.screenY + (window.outerHeight - wnd_settings.height) / 8;
  217. wnd_options = "width=" + wnd_settings.width + ",height=" + wnd_settings.height;
  218. wnd_options += ",toolbar=0,scrollbars=1,status=1,resizable=1,location=1,menuBar=0";
  219. wnd_options += ",left=" + wnd_settings.left + ",top=" + wnd_settings.top;
  220. opts = {
  221. provider: provider,
  222. cache: opts.cache
  223. };
  224. opts.callback = function(e, r) {
  225. if (window.removeEventListener) {
  226. window.removeEventListener("message", getMessage, false);
  227. } else if (window.detachEvent) {
  228. window.detachEvent("onmessage", getMessage);
  229. } else {
  230. if (document.detachEvent) {
  231. document.detachEvent("onmessage", getMessage);
  232. }
  233. }
  234. opts.callback = function() {};
  235. if (wndTimeout) {
  236. clearTimeout(wndTimeout);
  237. wndTimeout = undefined;
  238. }
  239. if (callback) {
  240. return callback(e, r);
  241. } else {
  242. return undefined;
  243. }
  244. };
  245. if (window.attachEvent) {
  246. window.attachEvent("onmessage", getMessage);
  247. } else if (document.attachEvent) {
  248. document.attachEvent("onmessage", getMessage);
  249. } else {
  250. if (window.addEventListener) {
  251. window.addEventListener("message", getMessage, false);
  252. }
  253. }
  254. if (typeof chrome !== "undefined" && chrome.runtime && chrome.runtime.onMessageExternal) {
  255. chrome.runtime.onMessageExternal.addListener(function(request, sender, sendResponse) {
  256. request.origin = sender.url.match(/^.{2,5}:\/\/[^/]+/)[0];
  257. if (defer != null) {
  258. defer.resolve();
  259. }
  260. return getMessage(request);
  261. });
  262. }
  263. if (!frm && (navigator.userAgent.indexOf("MSIE") !== -1 || navigator.appVersion.indexOf("Trident/") > 0)) {
  264. frm = document.createElement("iframe");
  265. frm.src = config.oauthd_url + "/auth/iframe?d=" + encodeURIComponent(Url.getAbsUrl("/"));
  266. frm.width = 0;
  267. frm.height = 0;
  268. frm.frameBorder = 0;
  269. frm.style.visibility = "hidden";
  270. document.body.appendChild(frm);
  271. }
  272. wndTimeout = setTimeout(function() {
  273. if (defer != null) {
  274. defer.reject(new Error("Authorization timed out"));
  275. }
  276. if (opts.callback && typeof opts.callback === "function") {
  277. opts.callback(new Error("Authorization timed out"));
  278. }
  279. try {
  280. wnd.close();
  281. } catch (_error) {}
  282. }, 1200 * 1000);
  283. wnd = window.open(url, "Authorization", wnd_options);
  284. if (wnd) {
  285. wnd.focus();
  286. } else {
  287. if (defer != null) {
  288. defer.reject(new Error("Could not open a popup"));
  289. }
  290. if (opts.callback && typeof opts.callback === "function") {
  291. opts.callback(new Error("Could not open a popup"));
  292. }
  293. }
  294. return defer != null ? defer.promise() : void 0;
  295. },
  296. redirect: function(provider, opts, url) {
  297. var redirect_uri, res;
  298. if (arguments.length === 2) {
  299. url = opts;
  300. opts = {};
  301. }
  302. if (cache.cacheEnabled(opts.cache)) {
  303. res = cache.tryCache(exports.OAuth, provider, opts.cache);
  304. if (res) {
  305. url = Url.getAbsUrl(url) + (url.indexOf("#") === -1 ? "#" : "&") + "oauthio=cache";
  306. window.location_operations.changeHref(url);
  307. window.location_operations.reload();
  308. return;
  309. }
  310. }
  311. if (!opts.state) {
  312. opts.state = sha1.create_hash();
  313. opts.state_type = "client";
  314. }
  315. cookies.createCookie("oauthio_state", opts.state);
  316. redirect_uri = encodeURIComponent(Url.getAbsUrl(url));
  317. url = config.oauthd_url + "/auth/" + provider + "?k=" + config.key;
  318. url += "&redirect_uri=" + redirect_uri;
  319. if (opts) {
  320. url += "&opts=" + encodeURIComponent(JSON.stringify(opts));
  321. }
  322. window.location_operations.changeHref(url);
  323. },
  324. callback: function(provider, opts, callback) {
  325. var defer, res, _ref;
  326. defer = (_ref = window.jQuery) != null ? _ref.Deferred() : void 0;
  327. if (arguments.length === 1 && typeof provider === "function") {
  328. callback = provider;
  329. provider = undefined;
  330. opts = {};
  331. }
  332. if (arguments.length === 1 && typeof provider === "string") {
  333. opts = {};
  334. }
  335. if (arguments.length === 2 && typeof opts === "function") {
  336. callback = opts;
  337. opts = {};
  338. }
  339. if (cache.cacheEnabled(opts.cache) || oauth_result === "cache") {
  340. res = cache.tryCache(exports.OAuth, provider, opts.cache);
  341. if (oauth_result === "cache" && (typeof provider !== "string" || !provider)) {
  342. if (defer != null) {
  343. defer.reject(new Error("You must set a provider when using the cache"));
  344. }
  345. if (callback) {
  346. return callback(new Error("You must set a provider when using the cache"));
  347. } else {
  348. return defer != null ? defer.promise() : void 0;
  349. }
  350. }
  351. if (res) {
  352. if (callback) {
  353. if (res) {
  354. return callback(null, res);
  355. }
  356. } else {
  357. if (defer != null) {
  358. defer.resolve(res);
  359. }
  360. return defer != null ? defer.promise() : void 0;
  361. }
  362. }
  363. }
  364. if (!oauth_result) {
  365. return;
  366. }
  367. oauthio.request.sendCallback({
  368. data: oauth_result,
  369. provider: provider,
  370. cache: opts.cache,
  371. callback: callback
  372. }, defer);
  373. return defer != null ? defer.promise() : void 0;
  374. },
  375. clearCache: function(provider) {
  376. cookies.eraseCookie("oauthio_provider_" + provider);
  377. },
  378. http_me: function(opts) {
  379. if (oauthio.request.http_me) {
  380. oauthio.request.http_me(opts);
  381. }
  382. },
  383. http: function(opts) {
  384. if (oauthio.request.http) {
  385. oauthio.request.http(opts);
  386. }
  387. }
  388. };
  389. if (typeof window.jQuery === "undefined") {
  390. _preloadcalls = [];
  391. delayfn = void 0;
  392. if (typeof chrome !== "undefined" && chrome.extension) {
  393. delayfn = function() {
  394. return function() {
  395. throw new Error("Please include jQuery before oauth.js");
  396. };
  397. };
  398. } else {
  399. e = document.createElement("script");
  400. e.src = "//code.jquery.com/jquery.min.js";
  401. e.type = "text/javascript";
  402. e.onload = function() {
  403. var i;
  404. delayedFunctions(window.jQuery);
  405. for (i in _preloadcalls) {
  406. _preloadcalls[i].fn.apply(null, _preloadcalls[i].args);
  407. }
  408. };
  409. document.getElementsByTagName("head")[0].appendChild(e);
  410. delayfn = function(f) {
  411. return function() {
  412. var arg, args_copy;
  413. args_copy = [];
  414. for (arg in arguments) {
  415. args_copy[arg] = arguments[arg];
  416. }
  417. _preloadcalls.push({
  418. fn: f,
  419. args: args_copy
  420. });
  421. };
  422. };
  423. }
  424. oauthio.request.http = delayfn(function() {
  425. oauthio.request.http.apply(exports.OAuth, arguments);
  426. });
  427. providers_api.fetchDescription = delayfn(function() {
  428. providers_api.fetchDescription.apply(providers_api, arguments);
  429. });
  430. oauthio.request = require("./oauthio_requests")(window.jQuery, config, client_states, cache, providers_api);
  431. } else {
  432. delayedFunctions(window.jQuery);
  433. }
  434. }
  435. };
  436. };
  437. },{"../config":1,"../tools/cache":5,"../tools/cookies":6,"../tools/sha1":7,"../tools/url":8,"./oauthio_requests":3}],3:[function(require,module,exports){
  438. var Url,
  439. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  440. Url = require('../tools/url')();
  441. module.exports = function($, config, client_states, cache) {
  442. return {
  443. http: function(opts) {
  444. var defer, desc_opts, doRequest, i, options;
  445. doRequest = function() {
  446. var i, k, qs, request;
  447. request = options.oauthio.request || {};
  448. if (!request.cors) {
  449. options.url = encodeURIComponent(options.url);
  450. if (options.url[0] !== "/") {
  451. options.url = "/" + options.url;
  452. }
  453. options.url = config.oauthd_url + "/request/" + options.oauthio.provider + options.url;
  454. options.headers = options.headers || {};
  455. options.headers.oauthio = "k=" + config.key;
  456. if (options.oauthio.tokens.oauth_token && options.oauthio.tokens.oauth_token_secret) {
  457. options.headers.oauthio += "&oauthv=1";
  458. }
  459. for (k in options.oauthio.tokens) {
  460. options.headers.oauthio += "&" + encodeURIComponent(k) + "=" + encodeURIComponent(options.oauthio.tokens[k]);
  461. }
  462. delete options.oauthio;
  463. return $.ajax(options);
  464. }
  465. if (options.oauthio.tokens) {
  466. if (options.oauthio.tokens.access_token) {
  467. options.oauthio.tokens.token = options.oauthio.tokens.access_token;
  468. }
  469. if (!options.url.match(/^[a-z]{2,16}:\/\//)) {
  470. if (options.url[0] !== "/") {
  471. options.url = "/" + options.url;
  472. }
  473. options.url = request.url + options.url;
  474. }
  475. options.url = Url.replaceParam(options.url, options.oauthio.tokens, request.parameters);
  476. if (request.query) {
  477. qs = [];
  478. for (i in request.query) {
  479. qs.push(encodeURIComponent(i) + "=" + encodeURIComponent(Url.replaceParam(request.query[i], options.oauthio.tokens, request.parameters)));
  480. }
  481. if (__indexOf.call(options.url, "?") >= 0) {
  482. options.url += "&" + qs;
  483. } else {
  484. options.url += "?" + qs;
  485. }
  486. }
  487. if (request.headers) {
  488. options.headers = options.headers || {};
  489. for (i in request.headers) {
  490. options.headers[i] = Url.replaceParam(request.headers[i], options.oauthio.tokens, request.parameters);
  491. }
  492. }
  493. delete options.oauthio;
  494. return $.ajax(options);
  495. }
  496. };
  497. options = {};
  498. i = void 0;
  499. for (i in opts) {
  500. options[i] = opts[i];
  501. }
  502. if (!options.oauthio.request || options.oauthio.request === true) {
  503. desc_opts = {
  504. wait: !!options.oauthio.request
  505. };
  506. defer = $ != null ? $.Deferred() : void 0;
  507. providers_api.getDescription(options.oauthio.provider, desc_opts, function(e, desc) {
  508. if (e) {
  509. return defer != null ? defer.reject(e) : void 0;
  510. }
  511. if (options.oauthio.tokens.oauth_token && options.oauthio.tokens.oauth_token_secret) {
  512. options.oauthio.request = desc.oauth1 && desc.oauth1.request;
  513. } else {
  514. options.oauthio.request = desc.oauth2 && desc.oauth2.request;
  515. }
  516. if (defer != null) {
  517. defer.resolve();
  518. }
  519. });
  520. return defer != null ? defer.then(doRequest) : void 0;
  521. } else {
  522. return doRequest();
  523. }
  524. },
  525. http_me: function(opts) {
  526. var defer, desc_opts, doRequest, k, options;
  527. doRequest = function() {
  528. var defer, k, promise, request;
  529. defer = $ != null ? $.Deferred() : void 0;
  530. request = options.oauthio.request || {};
  531. options.url = config.oauthd_url + "/auth/" + options.oauthio.provider + "/me";
  532. options.headers = options.headers || {};
  533. options.headers.oauthio = "k=" + config.key;
  534. if (options.oauthio.tokens.oauth_token && options.oauthio.tokens.oauth_token_secret) {
  535. options.headers.oauthio += "&oauthv=1";
  536. }
  537. for (k in options.oauthio.tokens) {
  538. options.headers.oauthio += "&" + encodeURIComponent(k) + "=" + encodeURIComponent(options.oauthio.tokens[k]);
  539. }
  540. delete options.oauthio;
  541. promise = $.ajax(options);
  542. $.when(promise).done(function(data) {
  543. if (defer != null) {
  544. defer.resolve(data.data);
  545. }
  546. }).fail(function(data) {
  547. if (data.responseJSON) {
  548. if (defer != null) {
  549. defer.reject(data.responseJSON.data);
  550. }
  551. } else {
  552. if (defer != null) {
  553. defer.reject(new Error("An error occured while trying to access the resource"));
  554. }
  555. }
  556. });
  557. return defer != null ? defer.promise() : void 0;
  558. };
  559. options = {};
  560. for (k in opts) {
  561. options[k] = opts[k];
  562. }
  563. if (!options.oauthio.request || options.oauthio.request === true) {
  564. desc_opts = {
  565. wait: !!options.oauthio.request
  566. };
  567. defer = $ != null ? $.Deferred() : void 0;
  568. providers_api.getDescription(options.oauthio.provider, desc_opts, function(e, desc) {
  569. if (e) {
  570. return defer != null ? defer.reject(e) : void 0;
  571. }
  572. if (options.oauthio.tokens.oauth_token && options.oauthio.tokens.oauth_token_secret) {
  573. options.oauthio.request = desc.oauth1 && desc.oauth1.request;
  574. } else {
  575. options.oauthio.request = desc.oauth2 && desc.oauth2.request;
  576. }
  577. if (defer != null) {
  578. defer.resolve();
  579. }
  580. });
  581. return defer != null ? defer.then(doRequest) : void 0;
  582. } else {
  583. return doRequest();
  584. }
  585. },
  586. mkHttp: function(provider, tokens, request, method) {
  587. var base;
  588. base = this;
  589. return function(opts, opts2) {
  590. var i, options;
  591. options = {};
  592. if (typeof opts === "string") {
  593. if (typeof opts2 === "object") {
  594. for (i in opts2) {
  595. options[i] = opts2[i];
  596. }
  597. }
  598. options.url = opts;
  599. } else if (typeof opts === "object") {
  600. for (i in opts) {
  601. options[i] = opts[i];
  602. }
  603. }
  604. options.type = options.type || method;
  605. options.oauthio = {
  606. provider: provider,
  607. tokens: tokens,
  608. request: request
  609. };
  610. return base.http(options);
  611. };
  612. },
  613. mkHttpMe: function(provider, tokens, request, method) {
  614. var base;
  615. base = this;
  616. return function(filter) {
  617. var options;
  618. options = {};
  619. options.type = options.type || method;
  620. options.oauthio = {
  621. provider: provider,
  622. tokens: tokens,
  623. request: request
  624. };
  625. options.data = options.data || {};
  626. options.data.filter = (filter ? filter.join(",") : undefined);
  627. return base.http_me(options);
  628. };
  629. },
  630. sendCallback: function(opts, defer) {
  631. var base, data, e, err, i, make_res, request, res, tokens;
  632. base = this;
  633. data = void 0;
  634. err = void 0;
  635. try {
  636. data = JSON.parse(opts.data);
  637. } catch (_error) {
  638. e = _error;
  639. if (defer != null) {
  640. defer.reject(new Error("Error while parsing result"));
  641. }
  642. return opts.callback(new Error("Error while parsing result"));
  643. }
  644. if (!data || !data.provider) {
  645. return;
  646. }
  647. if (opts.provider && data.provider.toLowerCase() !== opts.provider.toLowerCase()) {
  648. err = new Error("Returned provider name does not match asked provider");
  649. if (defer != null) {
  650. defer.reject(err);
  651. }
  652. if (opts.callback && typeof opts.callback === "function") {
  653. return opts.callback(err);
  654. } else {
  655. return;
  656. }
  657. }
  658. if (data.status === "error" || data.status === "fail") {
  659. err = new Error(data.message);
  660. err.body = data.data;
  661. if (defer != null) {
  662. defer.reject(err);
  663. }
  664. if (opts.callback && typeof opts.callback === "function") {
  665. return opts.callback(err);
  666. } else {
  667. return;
  668. }
  669. }
  670. if (data.status !== "success" || !data.data) {
  671. err = new Error();
  672. err.body = data.data;
  673. if (defer != null) {
  674. defer.reject(err);
  675. }
  676. if (opts.callback && typeof opts.callback === "function") {
  677. return opts.callback(err);
  678. } else {
  679. return;
  680. }
  681. }
  682. if (!data.state || client_states.indexOf(data.state) === -1) {
  683. if (defer != null) {
  684. defer.reject(new Error("State is not matching"));
  685. }
  686. if (opts.callback && typeof opts.callback === "function") {
  687. return opts.callback(new Error("State is not matching"));
  688. } else {
  689. return;
  690. }
  691. }
  692. if (!opts.provider) {
  693. data.data.provider = data.provider;
  694. }
  695. res = data.data;
  696. if (cache.cacheEnabled(opts.cache) && res) {
  697. cache.storeCache(data.provider, res);
  698. }
  699. request = res.request;
  700. delete res.request;
  701. tokens = void 0;
  702. if (res.access_token) {
  703. tokens = {
  704. access_token: res.access_token
  705. };
  706. } else if (res.oauth_token && res.oauth_token_secret) {
  707. tokens = {
  708. oauth_token: res.oauth_token,
  709. oauth_token_secret: res.oauth_token_secret
  710. };
  711. }
  712. if (!request) {
  713. if (defer != null) {
  714. defer.resolve(res);
  715. }
  716. if (opts.callback && typeof opts.callback === "function") {
  717. return opts.callback(null, res);
  718. } else {
  719. return;
  720. }
  721. }
  722. if (request.required) {
  723. for (i in request.required) {
  724. tokens[request.required[i]] = res[request.required[i]];
  725. }
  726. }
  727. make_res = function(method) {
  728. return base.mkHttp(data.provider, tokens, request, method);
  729. };
  730. res.get = make_res("GET");
  731. res.post = make_res("POST");
  732. res.put = make_res("PUT");
  733. res.patch = make_res("PATCH");
  734. res.del = make_res("DELETE");
  735. res.me = base.mkHttpMe(data.provider, tokens, request, "GET");
  736. if (defer != null) {
  737. defer.resolve(res);
  738. }
  739. if (opts.callback && typeof opts.callback === "function") {
  740. return opts.callback(null, res);
  741. } else {
  742. }
  743. }
  744. };
  745. };
  746. },{"../tools/url":8}],4:[function(require,module,exports){
  747. var OAuth_creator, jquery;
  748. if (typeof jQuery !== "undefined" && jQuery !== null) {
  749. jquery = jQuery;
  750. } else {
  751. jquery = void 0;
  752. }
  753. OAuth_creator = require('./lib/oauth')(window, document, jquery, navigator);
  754. OAuth_creator(window || this);
  755. },{"./lib/oauth":2}],5:[function(require,module,exports){
  756. module.exports = {
  757. init: function(cookies_module, config) {
  758. this.config = config;
  759. return this.cookies = cookies_module;
  760. },
  761. tryCache: function(OAuth, provider, cache) {
  762. var e, i, res;
  763. if (this.cacheEnabled(cache)) {
  764. cache = this.cookies.readCookie("oauthio_provider_" + provider);
  765. if (!cache) {
  766. return false;
  767. }
  768. cache = decodeURIComponent(cache);
  769. }
  770. if (typeof cache === "string") {
  771. try {
  772. cache = JSON.parse(cache);
  773. } catch (_error) {
  774. e = _error;
  775. return false;
  776. }
  777. }
  778. if (typeof cache === "object") {
  779. res = {};
  780. for (i in cache) {
  781. if (i !== "request" && typeof cache[i] !== "function") {
  782. res[i] = cache[i];
  783. }
  784. }
  785. return OAuth.create(provider, res, cache.request);
  786. }
  787. return false;
  788. },
  789. storeCache: function(provider, cache) {
  790. this.cookies.createCookie("oauthio_provider_" + provider, encodeURIComponent(JSON.stringify(cache)), cache.expires_in - 10 || 3600);
  791. },
  792. cacheEnabled: function(cache) {
  793. if (typeof cache === "undefined") {
  794. return this.config.options.cache;
  795. }
  796. return cache;
  797. }
  798. };
  799. },{}],6:[function(require,module,exports){
  800. /* istanbul ignore next */
  801. module.exports = {
  802. init: function(config, document) {
  803. this.config = config;
  804. return this.document = document;
  805. },
  806. createCookie: function(name, value, expires) {
  807. var date;
  808. this.eraseCookie(name);
  809. date = new Date();
  810. date.setTime(date.getTime() + (expires || 1200) * 1000);
  811. expires = "; expires=" + date.toGMTString();
  812. this.document.cookie = name + "=" + value + expires + "; path=/";
  813. },
  814. readCookie: function(name) {
  815. var c, ca, i, nameEQ;
  816. nameEQ = name + "=";
  817. ca = this.document.cookie.split(";");
  818. i = 0;
  819. while (i < ca.length) {
  820. c = ca[i];
  821. while (c.charAt(0) === " ") {
  822. c = c.substring(1, c.length);
  823. }
  824. if (c.indexOf(nameEQ) === 0) {
  825. return c.substring(nameEQ.length, c.length);
  826. }
  827. i++;
  828. }
  829. return null;
  830. },
  831. eraseCookie: function(name) {
  832. var date;
  833. date = new Date();
  834. date.setTime(date.getTime() - 86400000);
  835. this.document.cookie = name + "=; expires=" + date.toGMTString() + "; path=/";
  836. }
  837. };
  838. },{}],7:[function(require,module,exports){
  839. var b64pad, hexcase;
  840. hexcase = 0;
  841. b64pad = "";
  842. /* istanbul ignore next */
  843. module.exports = {
  844. hex_sha1: function(s) {
  845. return this.rstr2hex(this.rstr_sha1(this.str2rstr_utf8(s)));
  846. },
  847. b64_sha1: function(s) {
  848. return this.rstr2b64(this.rstr_sha1(this.str2rstr_utf8(s)));
  849. },
  850. any_sha1: function(s, e) {
  851. return this.rstr2any(this.rstr_sha1(this.str2rstr_utf8(s)), e);
  852. },
  853. hex_hmac_sha1: function(k, d) {
  854. return this.rstr2hex(this.rstr_hmac_sha1(this.str2rstr_utf8(k), this.str2rstr_utf8(d)));
  855. },
  856. b64_hmac_sha1: function(k, d) {
  857. return this.rstr2b64(this.rstr_hmac_sha1(this.str2rstr_utf8(k), this.str2rstr_utf8(d)));
  858. },
  859. any_hmac_sha1: function(k, d, e) {
  860. return this.rstr2any(this.rstr_hmac_sha1(this.str2rstr_utf8(k), this.str2rstr_utf8(d)), e);
  861. },
  862. sha1_vm_test: function() {
  863. return thishex_sha1("abc").toLowerCase() === "a9993e364706816aba3e25717850c26c9cd0d89d";
  864. },
  865. rstr_sha1: function(s) {
  866. return this.binb2rstr(this.binb_sha1(this.rstr2binb(s), s.length * 8));
  867. },
  868. rstr_hmac_sha1: function(key, data) {
  869. var bkey, hash, i, ipad, opad;
  870. bkey = this.rstr2binb(key);
  871. if (bkey.length > 16) {
  872. bkey = this.binb_sha1(bkey, key.length * 8);
  873. }
  874. ipad = Array(16);
  875. opad = Array(16);
  876. i = 0;
  877. while (i < 16) {
  878. ipad[i] = bkey[i] ^ 0x36363636;
  879. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  880. i++;
  881. }
  882. hash = this.binb_sha1(ipad.concat(this.rstr2binb(data)), 512 + data.length * 8);
  883. return this.binb2rstr(this.binb_sha1(opad.concat(hash), 512 + 160));
  884. },
  885. rstr2hex: function(input) {
  886. var e, hex_tab, i, output, x;
  887. try {
  888. hexcase;
  889. } catch (_error) {
  890. e = _error;
  891. hexcase = 0;
  892. }
  893. hex_tab = (hexcase ? "0123456789ABCDEF" : "0123456789abcdef");
  894. output = "";
  895. x = void 0;
  896. i = 0;
  897. while (i < input.length) {
  898. x = input.charCodeAt(i);
  899. output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt(x & 0x0F);
  900. i++;
  901. }
  902. return output;
  903. },
  904. rstr2b64: function(input) {
  905. var e, i, j, len, output, tab, triplet;
  906. try {
  907. b64pad;
  908. } catch (_error) {
  909. e = _error;
  910. b64pad = "";
  911. }
  912. tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  913. output = "";
  914. len = input.length;
  915. i = 0;
  916. while (i < len) {
  917. triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i + 2) : 0);
  918. j = 0;
  919. while (j < 4) {
  920. if (i * 8 + j * 6 > input.length * 8) {
  921. output += b64pad;
  922. } else {
  923. output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
  924. }
  925. j++;
  926. }
  927. i += 3;
  928. }
  929. return output;
  930. },
  931. rstr2any: function(input, encoding) {
  932. var dividend, divisor, full_length, i, output, q, quotient, remainders, x;
  933. divisor = encoding.length;
  934. remainders = Array();
  935. i = void 0;
  936. q = void 0;
  937. x = void 0;
  938. quotient = void 0;
  939. dividend = Array(Math.ceil(input.length / 2));
  940. i = 0;
  941. while (i < dividend.length) {
  942. dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  943. i++;
  944. }
  945. while (dividend.length > 0) {
  946. quotient = Array();
  947. x = 0;
  948. i = 0;
  949. while (i < dividend.length) {
  950. x = (x << 16) + dividend[i];
  951. q = Math.floor(x / divisor);
  952. x -= q * divisor;
  953. if (quotient.length > 0 || q > 0) {
  954. quotient[quotient.length] = q;
  955. }
  956. i++;
  957. }
  958. remainders[remainders.length] = x;
  959. dividend = quotient;
  960. }
  961. output = "";
  962. i = remainders.length - 1;
  963. while (i >= 0) {
  964. output += encoding.charAt(remainders[i]);
  965. i--;
  966. }
  967. full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2)));
  968. i = output.length;
  969. while (i < full_length) {
  970. output = encoding[0] + output;
  971. i++;
  972. }
  973. return output;
  974. },
  975. str2rstr_utf8: function(input) {
  976. var i, output, x, y;
  977. output = "";
  978. i = -1;
  979. x = void 0;
  980. y = void 0;
  981. while (++i < input.length) {
  982. x = input.charCodeAt(i);
  983. y = (i + 1 < input.length ? input.charCodeAt(i + 1) : 0);
  984. if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
  985. x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  986. i++;
  987. }
  988. if (x <= 0x7F) {
  989. output += String.fromCharCode(x);
  990. } else if (x <= 0x7FF) {
  991. output += String.fromCharCode(0xC0 | ((x >>> 6) & 0x1F), 0x80 | (x & 0x3F));
  992. } else if (x <= 0xFFFF) {
  993. output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F), 0x80 | ((x >>> 6) & 0x3F), 0x80 | (x & 0x3F));
  994. } else {
  995. if (x <= 0x1FFFFF) {
  996. output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07), 0x80 | ((x >>> 12) & 0x3F), 0x80 | ((x >>> 6) & 0x3F), 0x80 | (x & 0x3F));
  997. }
  998. }
  999. }
  1000. return output;
  1001. },
  1002. str2rstr_utf16le: function(input) {
  1003. var i, output;
  1004. output = "";
  1005. i = 0;
  1006. while (i < input.length) {
  1007. output += String.fromCharCode(input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF);
  1008. i++;
  1009. }
  1010. return output;
  1011. },
  1012. str2rstr_utf16be: function(input) {
  1013. var i, output;
  1014. output = "";
  1015. i = 0;
  1016. while (i < input.length) {
  1017. output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF);
  1018. i++;
  1019. }
  1020. return output;
  1021. },
  1022. rstr2binb: function(input) {
  1023. var i, output;
  1024. output = Array(input.length >> 2);
  1025. i = 0;
  1026. while (i < output.length) {
  1027. output[i] = 0;
  1028. i++;
  1029. }
  1030. i = 0;
  1031. while (i < input.length * 8) {
  1032. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
  1033. i += 8;
  1034. }
  1035. return output;
  1036. },
  1037. binb2rstr: function(input) {
  1038. var i, output;
  1039. output = "";
  1040. i = 0;
  1041. while (i < input.length * 32) {
  1042. output += String.fromCharCode((input[i >> 5] >>> (24 - i % 32)) & 0xFF);
  1043. i += 8;
  1044. }
  1045. return output;
  1046. },
  1047. binb_sha1: function(x, len) {
  1048. var a, b, c, d, e, i, j, olda, oldb, oldc, oldd, olde, t, w;
  1049. x[len >> 5] |= 0x80 << (24 - len % 32);
  1050. x[((len + 64 >> 9) << 4) + 15] = len;
  1051. w = Array(80);
  1052. a = 1732584193;
  1053. b = -271733879;
  1054. c = -1732584194;
  1055. d = 271733878;
  1056. e = -1009589776;
  1057. i = 0;
  1058. while (i < x.length) {
  1059. olda = a;
  1060. oldb = b;
  1061. oldc = c;
  1062. oldd = d;
  1063. olde = e;
  1064. j = 0;
  1065. while (j < 80) {
  1066. if (j < 16) {
  1067. w[j] = x[i + j];
  1068. } else {
  1069. w[j] = this.bit_rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
  1070. }
  1071. t = this.safe_add(this.safe_add(this.bit_rol(a, 5), this.sha1_ft(j, b, c, d)), this.safe_add(this.safe_add(e, w[j]), this.sha1_kt(j)));
  1072. e = d;
  1073. d = c;
  1074. c = this.bit_rol(b, 30);
  1075. b = a;
  1076. a = t;
  1077. j++;
  1078. }
  1079. a = this.safe_add(a, olda);
  1080. b = this.safe_add(b, oldb);
  1081. c = this.safe_add(c, oldc);
  1082. d = this.safe_add(d, oldd);
  1083. e = this.safe_add(e, olde);
  1084. i += 16;
  1085. }
  1086. return Array(a, b, c, d, e);
  1087. },
  1088. sha1_ft: function(t, b, c, d) {
  1089. if (t < 20) {
  1090. return (b & c) | ((~b) & d);
  1091. }
  1092. if (t < 40) {
  1093. return b ^ c ^ d;
  1094. }
  1095. if (t < 60) {
  1096. return (b & c) | (b & d) | (c & d);
  1097. }
  1098. return b ^ c ^ d;
  1099. },
  1100. sha1_kt: function(t) {
  1101. if (t < 20) {
  1102. return 1518500249;
  1103. } else {
  1104. if (t < 40) {
  1105. return 1859775393;
  1106. } else {
  1107. if (t < 60) {
  1108. return -1894007588;
  1109. } else {
  1110. return -899497514;
  1111. }
  1112. }
  1113. }
  1114. },
  1115. safe_add: function(x, y) {
  1116. var lsw, msw;
  1117. lsw = (x & 0xFFFF) + (y & 0xFFFF);
  1118. msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  1119. return (msw << 16) | (lsw & 0xFFFF);
  1120. },
  1121. bit_rol: function(num, cnt) {
  1122. return (num << cnt) | (num >>> (32 - cnt));
  1123. },
  1124. create_hash: function() {
  1125. var hash;
  1126. hash = this.b64_sha1((new Date()).getTime() + ":" + Math.floor(Math.random() * 9999999));
  1127. return hash.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=+$/, "");
  1128. }
  1129. };
  1130. },{}],8:[function(require,module,exports){
  1131. module.exports = function(document) {
  1132. return {
  1133. getAbsUrl: function(url) {
  1134. var base_url;
  1135. if (url.match(/^.{2,5}:\/\//)) {
  1136. return url;
  1137. }
  1138. if (url[0] === "/") {
  1139. return document.location.protocol + "//" + document.location.host + url;
  1140. }
  1141. base_url = document.location.protocol + "//" + document.location.host + document.location.pathname;
  1142. if (base_url[base_url.length - 1] !== "/" && url[0] !== "#") {
  1143. return base_url + "/" + url;
  1144. }
  1145. return base_url + url;
  1146. },
  1147. replaceParam: function(param, rep, rep2) {
  1148. param = param.replace(/\{\{(.*?)\}\}/g, function(m, v) {
  1149. return rep[v] || "";
  1150. });
  1151. if (rep2) {
  1152. param = param.replace(/\{(.*?)\}/g, function(m, v) {
  1153. return rep2[v] || "";
  1154. });
  1155. }
  1156. return param;
  1157. }
  1158. };
  1159. };
  1160. },{}]},{},[4])