ukey1-jquery-sdk
Version:
Ukey1 SDK plugin for jQuery
6 lines • 4.74 kB
JavaScript
/*!
* Ukey1 Jquery plugin (git@github.com:asaritech/ukey1-jquery-sdk.git)
* Copyright 2017 Ukey1 Community (https://github.com/asaritech/ukey1-jquery-sdk/contributors)
* Licensed under MIT
*/
!function(e){"function"==typeof define&&define.amd?define(["jquery"],e):"object"==typeof module&&module.exports?module.exports=function(o,t){return void 0===t&&(t="undefined"!=typeof window?require("jquery"):require("jquery")(o)),e(t),t}:e(jQuery)}(function(e){"use strict";function o(e){n("options",e,"object",!0),n("options.appId",e.appId,"string",!0),n("options.host",e.host,"string",!0),n("options.method",e.method,"string",!0,["GET","POST"]),n("options.endpoint",e.endpoint,"string",!0),n("options.accessToken",e.accessToken,"string",!1),this.o=e}function t(o){return n("options",o,"object",!0),n("options.appId",o.appId,"string",!0),o.host||(o.host=e.ukey1.defaults.host),o}function n(e,o,t,s,r){if("array"===t){if(s&&!o||o&&"[object Array]"!==Object.prototype.toString.call(o))throw new Error("Unknown/invalid `"+e+"` ("+t+" required)")}else if("function"===t){if(s&&!o||o&&"[object Function]"!==Object.prototype.toString.call(o))throw new Error("Unknown/invalid `"+e+"` ("+t+" required)")}else if(s&&!o||o&&typeof o!==t)throw new Error("Unknown/invalid `"+e+"` ("+t+" required)");if("string"===t&&r&&(n("checkOption(attribute a)",r,"array",!1),r.indexOf(o)<0))throw new Error("Unknown/invalid `"+e+"` (possible values are ["+r.join(",")+"])")}function s(e,o){var t,n;return o||(o=window.location.href),o=decodeURIComponent(o),e=e.replace(/[\[\]]/g,"\\$&"),t=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)"),n=t.exec(o),n?n[2]?decodeURIComponent(n[2].replace(/\+/g," ")):"":null}function r(){return Math.random().toString(36).substr(2,10)+"-"+(new Date).getTime()}function i(e,o){var t=e.gateway;o.signup&&(t+=(t.search(/\?/)>=0?"&":"?")+"signup=1"),d?(d.setItem(a+"requestId",o.requestId),d.setItem(a+"connectId",e.connect_id)):(Cookies.set(a+"requestId",o.requestId),Cookies.set(a+"connectId",e.connect_id)),window.location=t}function c(e,t){var n;t.accessToken=e.access_token,t.method="GET",t.endpoint="/auth/v2/me",n=new o(t),n.send("",function(e){u(e,t)})}function u(e,o){o.success(e.user,e.scope),o.finished(!0)}var d,a="uk1_jq_",p="localStorage"in window,l="sessionStorage"in window,f=Cookies&&"noConflict"in Cookies;if(l)d=sessionStorage;else if(p)d=localStorage;else if(!f)throw console.log("Web Storage is not supported!","Cookies plugin is not supported!"),new Error("There is a problem with browser storage!");e.ukey1=function(){};e.ukey1.defaults={host:"https://api.ukey.one"},e.ukey1.prototype.connect=function(e){var s,c,u=t(e);u.requestId=r(),n("options.requestId",u.requestId,"string",!0),n("options.returnUrl",u.returnUrl,"string",!0),n("options.scope",u.scope,"array",!1),u.method="POST",u.endpoint="/auth/v2/connect",s=new o(u),c={request_id:u.requestId,scope:u.scope,return_url:u.returnUrl},s.send(c,function(e){i(e,u)})},e.ukey1.prototype.accessToken=function(e){var r,i,u,p,l,f,h=t(e);return n("options.success",h.success,"function",!0),n("options.finished",h.finished,"function",!0),d?(h.requestId=d.getItem(a+"requestId")+"",h.connectId=d.getItem(a+"connectId")+"",d.removeItem(a+"requestId"),d.removeItem(a+"connectId")):(h.requestId=Cookies.get(a+"requestId")+"",h.connectId=Cookies.get(a+"connectId")+"",Cookies.remove(a+"requestId"),Cookies.remove(a+"connectId")),h.requestId&&h.connectId?(u=s("_ukey1[request_id]"),p=s("_ukey1[connect_id]"),l=s("_ukey1[result]"),f=s("_ukey1[code]"),u&&u===h.requestId?p&&p===h.connectId?"authorized"!==l?(console.log("User has canceled the request"),h.finished(!1),!1):(h.method="POST",h.endpoint="/auth/v2/token",r=new o(h),i={request_id:h.requestId,connect_id:h.connectId,auth_code:f},void r.send(i,function(e){c(e,h)})):(console.log("Invalid connectId"),h.finished(!1),!1):(console.log("Invalid requestId"),h.finished(!1),!1)):(console.log("No requestId and connectId in device storage"),h.finished(!1),!1)},o.prototype.prepareHeaders=function(e){return e["x-ukey1-app"]=this.o.appId,this.o.accessToken&&(e.Authorization="Bearer "+this.o.accessToken),e},o.prototype.send=function(o,t){var n={},s={};s=this.prepareHeaders(s),n.url=this.o.host+this.o.endpoint,n.method=n.type=this.o.method,n.timeout=5e3,n.jsonp=!1,n.dataType="json",n.success=function(e,o,n){var s={};if(!e.result)throw s=new Error("Invalid response structure"),s.response=e,s;if(e.result!==n.status)throw s=new Error("Unexpected HTTP status "+n.status),s.response=e,s;if(!(e.result>=200&&e.result<300))throw new Error(e.debug.message);t(e)},n.error=function(e,o,t){throw console.log(e.responseJSON.debug.message),t},o&&(s["Content-Type"]="application/json",n.data=JSON.stringify(o)),n.headers=s,e.ajax(n)}});