blank-web-sdk
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -11,7 +11,21 @@ "use strict"; | ||
var _events = require("../utils/events"); | ||
var _events2 = _interopRequireDefault(_events); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var BaseTokenProvider = function () { | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var BaseTokenProvider = function (_EventEmitter) { | ||
_inherits(BaseTokenProvider, _EventEmitter); | ||
function BaseTokenProvider() { | ||
_classCallCheck(this, BaseTokenProvider); | ||
return _possibleConstructorReturn(this, (BaseTokenProvider.__proto__ || Object.getPrototypeOf(BaseTokenProvider)).call(this)); | ||
} | ||
@@ -37,4 +51,4 @@ | ||
return BaseTokenProvider; | ||
}(); | ||
}(_events2.default); | ||
exports.default = BaseTokenProvider; |
@@ -60,10 +60,9 @@ "use strict"; | ||
_this.accessTokenProvider = new _LsTokenProvider2.default(blankUri); | ||
_this.accessTokenProvider = _this.__isSameOrigin(_this._blankUri) ? new _LsTokenProvider2.default(blankUri) : new _IframeTokenProvider2.default(blankUri); | ||
_this.accessTokenProvider.on("change", function (token) { | ||
// console.log("TOKEN UPDATE:", token); | ||
_this.__setToken(token); | ||
}); | ||
var initPromise = _this.accessTokenProvider.get().then(function (token) { | ||
_this._accessToken = token; | ||
if (token) { | ||
_this._ws ? _this.__openWS() : _this.__setState(_const.CLIENT_STATES.ready); | ||
} else { | ||
_this.__setState(_const.CLIENT_STATES.unauthorized); | ||
} | ||
_this.__setToken(token); | ||
_this.emit("init"); | ||
@@ -86,3 +85,5 @@ }); | ||
} | ||
return (0, _jwt.decodeToken)(this._accessToken); | ||
var tokenInfo = (0, _jwt.decodeToken)(this._accessToken); | ||
tokenInfo.RAW = this._accessToken; | ||
return tokenInfo; | ||
} | ||
@@ -148,2 +149,13 @@ }, { | ||
}, { | ||
key: "__isSameOrigin", | ||
value: function __isSameOrigin(url) { | ||
if (!url) { | ||
return true; | ||
} | ||
var loc = window.location, | ||
a = document.createElement("a"); | ||
a.href = url; | ||
return a.hostname == loc.hostname && a.port == loc.port && a.protocol == loc.protocol; | ||
} | ||
}, { | ||
key: "__checkAccessToken", | ||
@@ -178,2 +190,12 @@ value: function __checkAccessToken() { | ||
}, { | ||
key: "__setToken", | ||
value: function __setToken(token) { | ||
this._accessToken = token; | ||
if (token) { | ||
this._ws ? this.__openWS() : this.__setState(_const.CLIENT_STATES.ready); | ||
} else { | ||
this.__setState(_const.CLIENT_STATES.unauthorized); | ||
} | ||
} | ||
}, { | ||
key: "__reset", | ||
@@ -180,0 +202,0 @@ value: function __reset() { |
@@ -13,5 +13,5 @@ "use strict"; | ||
var _const = require("../const"); | ||
var _BaseTokenProvider2 = require("./BaseTokenProvider"); | ||
var _jwt = require("../jwt"); | ||
var _BaseTokenProvider3 = _interopRequireDefault(_BaseTokenProvider2); | ||
@@ -22,17 +22,31 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var AccessTokenProvider = function () { | ||
function AccessTokenProvider(uri) { | ||
_classCallCheck(this, AccessTokenProvider); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
this.store = "ls"; | ||
if (uri) { | ||
this._blankUri = uri; | ||
this.store = "iframe"; | ||
this.__prepareFrame; | ||
} | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var IframeTokenProvider = function (_BaseTokenProvider) { | ||
_inherits(IframeTokenProvider, _BaseTokenProvider); | ||
function IframeTokenProvider(uri) { | ||
_classCallCheck(this, IframeTokenProvider); | ||
var _this = _possibleConstructorReturn(this, (IframeTokenProvider.__proto__ || Object.getPrototypeOf(IframeTokenProvider)).call(this)); | ||
_this._mID = 0; | ||
_this._blankUri = uri; | ||
_this._requests = {}; | ||
var loadFramePromise = new Promise(function (resolve) { | ||
_this.__prepareFrame(resolve); | ||
}); | ||
_this._waitForLoad = function () { | ||
return loadFramePromise; | ||
}; | ||
return _this; | ||
} | ||
_createClass(AccessTokenProvider, [{ | ||
_createClass(IframeTokenProvider, [{ | ||
key: "get", | ||
value: function get(_cb) { | ||
var _this2 = this; | ||
var _doubleApi = (0, _doubleApi4.default)(_cb); | ||
@@ -43,13 +57,13 @@ | ||
var token = void 0; | ||
switch (this.store) { | ||
case "ls": | ||
token = localStorage.getItem(_const.TOKEN_LS_KEY) || null; | ||
cb(null, this.__validateToken(token)); | ||
break; | ||
case "iframe": | ||
token = localStorage.getItem(_const.TOKEN_LS_KEY) || null; | ||
cb(null, this.__validateToken(token)); | ||
break; | ||
} | ||
this._waitForLoad().then(function () { | ||
return _this2.__rpc({ method: "GET" }); | ||
}).then(function (token) { | ||
if (_this2.__isValidToken(token)) { | ||
cb(null, token); | ||
} else { | ||
console.log("Invalid token in iframe storage, will be cleared"); | ||
_this2.__rpc({ method: "REMOVE" }); | ||
cb(null, null); | ||
} | ||
}); | ||
return promise; | ||
@@ -60,2 +74,4 @@ } | ||
value: function set(token, _cb) { | ||
var _this3 = this; | ||
var _doubleApi2 = (0, _doubleApi4.default)(_cb); | ||
@@ -66,47 +82,62 @@ | ||
switch (this.store) { | ||
case "ls": | ||
if (token) { | ||
localStorage.setItem(_const.TOKEN_LS_KEY, token); | ||
} else { | ||
localStorage.removeItem(_const.TOKEN_LS_KEY); | ||
} | ||
cb(null); | ||
break; | ||
} | ||
this._waitForLoad().then(function () { | ||
return _this3.__rpc({ method: "SET", token: token }); | ||
}).then(function () { | ||
cb(null, null); | ||
}); | ||
return promise; | ||
} | ||
}, { | ||
key: "__validateToken", | ||
value: function __validateToken(token) { | ||
if (token) { | ||
try { | ||
var tokenInfo = (0, _jwt.decodeToken)(token); | ||
if (tokenInfo.exp > Math.floor(Date.now() / 1000)) { | ||
return token; | ||
} | ||
} catch (e) { | ||
console.log("Invalid token in localStorage, will be cleared"); | ||
localStorage.removeItem(_const.TOKEN_LS_KEY); | ||
} | ||
} | ||
return null; | ||
key: "__getMessageId", | ||
value: function __getMessageId() { | ||
return "a" + ++this._mID; | ||
} | ||
}, { | ||
key: "__rpc", | ||
value: function __rpc(data) { | ||
var _this4 = this; | ||
return new Promise(function (resolve, reject) { | ||
data.id = _this4.__getMessageId(); | ||
var timer = setTimeout(function () { | ||
reject("timeout"); | ||
}, 2000); | ||
_this4._requests[data.id] = function (_d) { | ||
clearTimeout(timer); | ||
delete _this4._requests[data.id]; | ||
resolve(_d); | ||
}; | ||
_this4.iframeWindow.postMessage(data, _this4._blankUri); | ||
}); | ||
} | ||
}, { | ||
key: "__prepareFrame", | ||
value: function __prepareFrame(cb) { | ||
var _this = this; | ||
value: function __prepareFrame(_cb) { | ||
var _this5 = this; | ||
var frame = this.ifrm = document.createElement("iframe"); | ||
var frame = document.createElement("iframe"); | ||
frame.style.width = "0"; | ||
frame.style.height = "0"; | ||
frame.setAttribute("src", this._blankUri + "/sso-frame"); | ||
frame.setAttribute("src", this._blankUri + "/hooks/cd/frame"); | ||
frame.addEventListener("load", function () { | ||
var w = frame.contentWindow; | ||
w.postMessage("", _this._blankUri); | ||
_this5.iframeWindow = frame.contentWindow; | ||
_this5.iframeWindow.postMessage({ method: "SUBSCRIBE", id: _this5.__getMessageId() }, _this5._blankUri); | ||
_cb(); | ||
}); | ||
window.addEventListener("message", function (event) { | ||
if (event.origin !== _this._blankUri) { | ||
console.log("EVENT:", event.origin, event.data); | ||
if (event.origin !== _this5._blankUri || event.data == null) { | ||
return; | ||
} | ||
if (event.data.requestId) { | ||
var requestCb = _this5._requests[event.data.requestId]; | ||
if (typeof requestCb === "function") { | ||
requestCb(event.data.result); | ||
} | ||
} else { | ||
if (typeof event.data.token !== "undefined") { | ||
_this5.emit("change", event.data.token); | ||
} | ||
} | ||
}, false); | ||
@@ -117,5 +148,5 @@ document.body.appendChild(frame); | ||
return AccessTokenProvider; | ||
}(); | ||
return IframeTokenProvider; | ||
}(_BaseTokenProvider3.default); | ||
exports.default = AccessTokenProvider; | ||
exports.default = IframeTokenProvider; |
@@ -27,12 +27,19 @@ "use strict"; | ||
var AccessTokenProvider = function (_BaseTokenProvider) { | ||
_inherits(AccessTokenProvider, _BaseTokenProvider); | ||
var LsTokenProvider = function (_BaseTokenProvider) { | ||
_inherits(LsTokenProvider, _BaseTokenProvider); | ||
function AccessTokenProvider() { | ||
_classCallCheck(this, AccessTokenProvider); | ||
function LsTokenProvider() { | ||
_classCallCheck(this, LsTokenProvider); | ||
return _possibleConstructorReturn(this, (AccessTokenProvider.__proto__ || Object.getPrototypeOf(AccessTokenProvider)).apply(this, arguments)); | ||
var _this = _possibleConstructorReturn(this, (LsTokenProvider.__proto__ || Object.getPrototypeOf(LsTokenProvider)).call(this)); | ||
window.addEventListener("storage", function (e) { | ||
if (e.key === _const.TOKEN_LS_KEY) { | ||
this.emit("change", localStorage.getItem(_const.TOKEN_LS_KEY)); | ||
} | ||
}); | ||
return _this; | ||
} | ||
_createClass(AccessTokenProvider, [{ | ||
_createClass(LsTokenProvider, [{ | ||
key: "get", | ||
@@ -93,5 +100,5 @@ value: function get(_cb) { | ||
return AccessTokenProvider; | ||
return LsTokenProvider; | ||
}(_BaseTokenProvider3.default); | ||
exports.default = AccessTokenProvider; | ||
exports.default = LsTokenProvider; |
{ | ||
"name": "blank-web-sdk", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "SDK for web applications created with Blank back-end", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -44,2 +44,5 @@ var WebpackDevServer = require("webpack-dev-server"); | ||
}); | ||
exec("./node_modules/.bin/babel src --out-dir ../bf-config/hugo/src/node_modules/blank-web-sdk/lib", (e) => { | ||
console.log(`Babel 2 ${e ? "error:" + e : "done"}`); | ||
}); | ||
}); | ||
@@ -46,0 +49,0 @@ |
Sorry, the diff of this file is too big to display
402273
10191