transposit
Advanced tools
Comparing version 0.7.6 to 0.7.7
@@ -177,2 +177,7 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
}; | ||
Transposit.prototype.areClientClaimsValid = function (clientClaims) { | ||
var expiration = clientClaims.exp * 1000; | ||
var now = Date.now(); | ||
return expiration > now; | ||
}; | ||
Transposit.prototype.persistClientClaims = function (clientClaimsJSON) { | ||
@@ -299,3 +304,3 @@ localStorage.setItem(this.getConsumeKey(), clientClaimsJSON); | ||
var clientClaims = this.retrieveClientClaims(); | ||
return !!clientClaims; | ||
return clientClaims !== null && this.areClientClaimsValid(clientClaims); | ||
}; | ||
@@ -302,0 +307,0 @@ Transposit.prototype.runOperation = function (operationId, params) { |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Transposit=t():e.Transposit=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";n.r(t);var r=function(e,t,n,r){return new(n||(n=Promise))(function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function l(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){e.done?o(e.value):new n(function(t){t(e.value)}).then(a,l)}c((r=r.apply(e,t||[])).next())})},o=function(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function l(i){return function(l){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,l])}}};function i(e){var t=window.location.href;e=e.replace(/[\[\]]/g,"\\$&");var n=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)").exec(t);return n?n[2]?decodeURIComponent(n[2].replace(/\+/g," ")):"":null}var a=function(){function e(e,t,n){void 0===n&&(n="https://api.transposit.com"),this.serviceMaintainer=e,this.serviceName=t,this.tpsHostedAppApiUrl=n}return e.prototype.getConsumeKey=function(){return"TRANSPOSIT_CONSUME_KEY/"+this.serviceMaintainer+"/"+this.serviceName},e.prototype.retrieveClientClaims=function(){var e=localStorage.getItem(this.getConsumeKey());return e?JSON.parse(e):null},e.prototype.persistClientClaims=function(e){localStorage.setItem(this.getConsumeKey(),e)},e.prototype.clearClientClaims=function(){localStorage.removeItem(this.getConsumeKey())},e.prototype.apiUrl=function(e){return void 0===e&&(e=""),this.tpsHostedAppApiUrl+"/app/"+this.serviceMaintainer+"/"+this.serviceName+e},e.prototype.handleLogin=function(e){var t=i("clientJwt");if(null===t)throw new Error("clientJwt query parameter could not be found. This method should only be called after redirection during login.");var n=t,r=i("needsKeys");if(null===r)throw new Error("needsKeys query parameter could not be found. This is unexpected.");var o,a="true"===r,l=n.split(".");if(3!==l.length)throw new Error("clientJwt query parameter does not appear to be a valid JWT string. This method should only be called after redirection during login.");try{o=atob(l[1])}catch(e){throw new Error("clientJwt query parameter does not appear to be a valid JWT string. This method should only be called after redirection during login.")}try{JSON.parse(o)}catch(e){throw new Error("clientJwt query parameter does not appear to be a valid JWT string. This method should only be called after redirection during login.")}if(this.persistClientClaims(o),e){if("function"!=typeof e)throw new Error("Provided callback is not a function.");e({needsKeys:a})}else if(a){var c=window.location.protocol+"//"+window.location.host+window.location.pathname;window.location.href=this.getConnectLocation(c)}else window.history.replaceState({},document.title,window.location.pathname)},e.prototype.logOut=function(){return r(this,void 0,void 0,function(){var e;return o(this,function(t){switch(t.label){case 0:if(!(e=this.retrieveClientClaims()))return[2];t.label=1;case 1:return t.trys.push([1,3,,4]),[4,fetch(this.apiUrl("/api/v1/logout"),{credentials:"include",method:"POST",headers:{"content-type":"application/json","X-PUBLIC-TOKEN":e.publicToken}})];case 2:return t.sent(),[3,4];case 3:return t.sent(),[3,4];case 4:return this.clearClientClaims(),[2]}})})},e.prototype.getConnectLocation=function(e){return this.apiUrl("/connect?redirectUri="+encodeURIComponent(e||window.location.href))},e.prototype.getGoogleLoginLocation=function(e){return this.apiUrl("/login/google?redirectUri="+encodeURIComponent(e||window.location.href))},e.prototype.getLoginLocation=function(){return this.apiUrl("/login")},e.prototype.getUserEmail=function(){var e=this.retrieveClientClaims();return e?e.email:null},e.prototype.getUserName=function(){var e=this.retrieveClientClaims();return e?e.name:null},e.prototype.isLoggedIn=function(){return!!this.retrieveClientClaims()},e.prototype.runOperation=function(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var n,r,i;return o(this,function(o){switch(o.label){case 0:n={"content-type":"application/json"},(r=this.retrieveClientClaims())&&(n["X-PUBLIC-TOKEN"]=r.publicToken),o.label=1;case 1:return o.trys.push([1,6,,7]),[4,fetch(this.apiUrl("/api/v1/execute/"+e),{credentials:"include",method:"POST",headers:n,body:JSON.stringify({parameters:t})})];case 2:return(i=o.sent()).status>=200&&i.status<300?[4,i.json()]:[3,4];case 3:return[2,o.sent()];case 4:throw i;case 5:return[3,7];case 6:throw o.sent();case 7:return[2]}})})},e}();n.d(t,"TRANSPOSIT_CONSUME_KEY_PREFIX",function(){return"TRANSPOSIT_CONSUME_KEY"}),n.d(t,"Transposit",function(){return a})}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Transposit=t():e.Transposit=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";n.r(t);var r=function(e,t,n,r){return new(n||(n=Promise))(function(o,i){function a(e){try{s(r.next(e))}catch(e){i(e)}}function l(e){try{s(r.throw(e))}catch(e){i(e)}}function s(e){e.done?o(e.value):new n(function(t){t(e.value)}).then(a,l)}s((r=r.apply(e,t||[])).next())})},o=function(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function l(i){return function(l){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,l])}}};function i(e){var t=window.location.href;e=e.replace(/[\[\]]/g,"\\$&");var n=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)").exec(t);return n?n[2]?decodeURIComponent(n[2].replace(/\+/g," ")):"":null}var a=function(){function e(e,t,n){void 0===n&&(n="https://api.transposit.com"),this.serviceMaintainer=e,this.serviceName=t,this.tpsHostedAppApiUrl=n}return e.prototype.getConsumeKey=function(){return"TRANSPOSIT_CONSUME_KEY/"+this.serviceMaintainer+"/"+this.serviceName},e.prototype.retrieveClientClaims=function(){var e=localStorage.getItem(this.getConsumeKey());return e?JSON.parse(e):null},e.prototype.areClientClaimsValid=function(e){return 1e3*e.exp>Date.now()},e.prototype.persistClientClaims=function(e){localStorage.setItem(this.getConsumeKey(),e)},e.prototype.clearClientClaims=function(){localStorage.removeItem(this.getConsumeKey())},e.prototype.apiUrl=function(e){return void 0===e&&(e=""),this.tpsHostedAppApiUrl+"/app/"+this.serviceMaintainer+"/"+this.serviceName+e},e.prototype.handleLogin=function(e){var t=i("clientJwt");if(null===t)throw new Error("clientJwt query parameter could not be found. This method should only be called after redirection during login.");var n=t,r=i("needsKeys");if(null===r)throw new Error("needsKeys query parameter could not be found. This is unexpected.");var o,a="true"===r,l=n.split(".");if(3!==l.length)throw new Error("clientJwt query parameter does not appear to be a valid JWT string. This method should only be called after redirection during login.");try{o=atob(l[1])}catch(e){throw new Error("clientJwt query parameter does not appear to be a valid JWT string. This method should only be called after redirection during login.")}try{JSON.parse(o)}catch(e){throw new Error("clientJwt query parameter does not appear to be a valid JWT string. This method should only be called after redirection during login.")}if(this.persistClientClaims(o),e){if("function"!=typeof e)throw new Error("Provided callback is not a function.");e({needsKeys:a})}else if(a){var s=window.location.protocol+"//"+window.location.host+window.location.pathname;window.location.href=this.getConnectLocation(s)}else window.history.replaceState({},document.title,window.location.pathname)},e.prototype.logOut=function(){return r(this,void 0,void 0,function(){var e;return o(this,function(t){switch(t.label){case 0:if(!(e=this.retrieveClientClaims()))return[2];t.label=1;case 1:return t.trys.push([1,3,,4]),[4,fetch(this.apiUrl("/api/v1/logout"),{credentials:"include",method:"POST",headers:{"content-type":"application/json","X-PUBLIC-TOKEN":e.publicToken}})];case 2:return t.sent(),[3,4];case 3:return t.sent(),[3,4];case 4:return this.clearClientClaims(),[2]}})})},e.prototype.getConnectLocation=function(e){return this.apiUrl("/connect?redirectUri="+encodeURIComponent(e||window.location.href))},e.prototype.getGoogleLoginLocation=function(e){return this.apiUrl("/login/google?redirectUri="+encodeURIComponent(e||window.location.href))},e.prototype.getLoginLocation=function(){return this.apiUrl("/login")},e.prototype.getUserEmail=function(){var e=this.retrieveClientClaims();return e?e.email:null},e.prototype.getUserName=function(){var e=this.retrieveClientClaims();return e?e.name:null},e.prototype.isLoggedIn=function(){var e=this.retrieveClientClaims();return null!==e&&this.areClientClaimsValid(e)},e.prototype.runOperation=function(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var n,r,i;return o(this,function(o){switch(o.label){case 0:n={"content-type":"application/json"},(r=this.retrieveClientClaims())&&(n["X-PUBLIC-TOKEN"]=r.publicToken),o.label=1;case 1:return o.trys.push([1,6,,7]),[4,fetch(this.apiUrl("/api/v1/execute/"+e),{credentials:"include",method:"POST",headers:n,body:JSON.stringify({parameters:t})})];case 2:return(i=o.sent()).status>=200&&i.status<300?[4,i.json()]:[3,4];case 3:return[2,o.sent()];case 4:throw i;case 5:return[3,7];case 6:throw o.sent();case 7:return[2]}})})},e}();n.d(t,"TRANSPOSIT_CONSUME_KEY_PREFIX",function(){return"TRANSPOSIT_CONSUME_KEY"}),n.d(t,"Transposit",function(){return a})}])}); | ||
//# sourceMappingURL=bundle.prod.map |
@@ -23,2 +23,3 @@ import { EndRequestLog } from "."; | ||
private retrieveClientClaims; | ||
private areClientClaimsValid; | ||
private persistClientClaims; | ||
@@ -25,0 +26,0 @@ private clearClientClaims; |
{ | ||
"name": "transposit", | ||
"version": "0.7.6", | ||
"version": "0.7.7", | ||
"author": "Transposit", | ||
@@ -44,5 +44,7 @@ "description": "SDK for web apps using Transposit as a backend", | ||
"@types/jsdom": "^12.2.0", | ||
"@types/mockdate": "^2.0.0", | ||
"jest": "^23.6.0", | ||
"jest-junit": "^5.2.0", | ||
"jest-localstorage-mock": "^2.3.0", | ||
"mockdate": "^2.0.2", | ||
"npm-run-all": "^4.1.2", | ||
@@ -49,0 +51,0 @@ "prettier": "^1.10.2", |
@@ -17,3 +17,5 @@ /* | ||
import * as MockDate from "mockdate"; | ||
import { Transposit, TRANSPOSIT_CONSUME_KEY_PREFIX } from "../Transposit"; | ||
import DoneCallback = jest.DoneCallback; | ||
@@ -30,2 +32,3 @@ | ||
jest.clearAllMocks(); | ||
MockDate.reset(); | ||
}); | ||
@@ -216,2 +219,35 @@ | ||
describe("isLoggedIn", () => { | ||
it("knows when you're logged in", () => { | ||
// 3 days before expiration | ||
MockDate.set((jplaceArbysClaims.exp - 60 * 60 * 24 * 3) * 1000); | ||
const clientJwt: string = createUnsignedJwt(jplaceArbysClaims); | ||
window.location.href = `https://arbys.com/?clientJwt=${clientJwt}&needsKeys=false`; | ||
const transposit: Transposit = makeArbysTransposit(); | ||
transposit.handleLogin(); | ||
expect(transposit.isLoggedIn()).toBe(true); | ||
}); | ||
it("knows when you're logged out", () => { | ||
window.location.href = `https://arbys.com/`; | ||
const transposit: Transposit = makeArbysTransposit(); | ||
expect(transposit.isLoggedIn()).toBe(false); | ||
}); | ||
it("knows when your session expired", () => { | ||
// 3 days after expiration | ||
MockDate.set((jplaceArbysClaims.exp + 60 * 60 * 24 * 3) * 1000); | ||
const clientJwt: string = createUnsignedJwt(jplaceArbysClaims); | ||
window.location.href = `https://arbys.com/?clientJwt=${clientJwt}&needsKeys=false`; | ||
const transposit: Transposit = makeArbysTransposit(); | ||
transposit.handleLogin(); | ||
expect(transposit.isLoggedIn()).toBe(false); | ||
}); | ||
}); | ||
describe("logout", () => { | ||
@@ -218,0 +254,0 @@ let transposit: Transposit; |
@@ -73,2 +73,8 @@ /* | ||
private areClientClaimsValid(clientClaims: ClientClaims): boolean { | ||
const expiration = clientClaims.exp * 1000; | ||
const now = Date.now(); | ||
return expiration > now; | ||
} | ||
private persistClientClaims(clientClaimsJSON: string): void { | ||
@@ -221,3 +227,3 @@ localStorage.setItem(this.getConsumeKey(), clientClaimsJSON); | ||
const clientClaims = this.retrieveClientClaims(); | ||
return !!clientClaims; | ||
return clientClaims !== null && this.areClientClaimsValid(clientClaims); | ||
} | ||
@@ -224,0 +230,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
104155
1221
17