PageRenderTime 9ms CodeModel.GetById 114ms app.highlight 166ms RepoModel.GetById 65ms app.codeStats 1ms

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