@videsk/jwt-webauth
Advanced tools
Comparing version 2.6.0 to 2.6.1
@@ -11,2 +11,2 @@ "use strict"; | ||
* | ||
*/var e=class{constructor(e={}){const{keys:t,events:s={},attempts:r=3,delay:i=0}=e;this.keys=this.constructor.getKeys(t),this.events=Object.assign({},s),this.storage=this.constructor.getStorage(this.keys.accessToken),this.attempts=r,this.delay=i,this.version="1.0.0",this.log=this.constructor.debugFromStorage(),this.running=!1,this.debug=this.log?this.constructor.debugWrapper:()=>{}}on(e,t){return this.events[e]=t,this}login(e,t,s=!1){return this.set(e,t,s,!0)}async set(e="",t="",s=!1,r=!1){this.storage=s?"localStorage":"sessionStorage";const{accessToken:i=e,refreshToken:o=t}=r?{accessToken:e,refreshToken:t}:this.getTokens();if(this.debug("log","Initializing WebAuth with tokens",i,o),!i)return this.fire("empty",i);const n=[i];o&&n.push(o),this.validate(n),this.setToken({accessToken:i,refreshToken:o});try{const e=await this.fire("verify",i,o,this.events.expired,this.events.error);return this.debug("info","The verification of accessToken is",e),e||o?(e&&(this.running=!0,this.fire("ready")),e?this.observer():this.renew()):this.fire("expired","accessToken")}catch(e){if(this.debug("error","Error trying to verifying accessToken.",e),this.fire("expired","accessToken"),o)return this.renew()}}observer(e=1){this.debug("log","Observer running");const{accessToken:t,refreshToken:s}=this.getTokens();if(!this.running||!t)return this.debug("warn","accessToken is expired or WebAuth is not running.");const r=this.constructor.delayedDate(this.delay)>this.getExpiration(t);return this.debug("log","accessToken is expired?",r),r?s?this.renew(e):this.fire("expired","accessToken"):setTimeout(this.observer.bind(this),1e3)}async renew(e=1){this.debug("log","Trying to renew accessToken with refreshToken"),1===e&&this.fire("expired","accessToken");const{accessToken:t,refreshToken:s}=this.getTokens(),r=(new Date).getTime()>this.getExpiration(s);if(this.debug("log","refreshToken is expired?",r),r)return this.fire("expired","refreshToken");try{const e=await this.fire("renew",s,t,this.events.expired,this.events.error);if(!e)throw new Error("Renewed accessToken is empty, please check.");return this.debug("info","accessToken has renewed",e),this.setToken("accessToken",e),this.fire("renewed"),this.running||(this.running=!0,this.fire("ready")),this.observer()}catch(t){if(this.debug("error","Error trying to renew accessToken",t),e>=this.attempts)return this.fire("error",t);this.debug("log",`Re-trying to get a new accessToken for ${e}nd time.`),setTimeout(this.observer.bind(this),1e3,e+1)}}clean(){this.debug("log","Cleaning store...");Object.values(this.keys).forEach((e=>{window.localStorage.removeItem(e),window.sessionStorage.removeItem(e)}))}logout(){this.debug("log","Login out..."),this.running=!1,this.clean(),this.fire("logout")}validate(e=""){this.debug("log","Validating JWT",e);const t=e=>JSON.parse(window.atob(e.split(".")[1]));try{return Array.isArray(e)?e.some((e=>!t(e))):t(e)}catch(e){throw this.fire("error",e)}}getTokens(e){const t={accessToken:window[this.storage].getItem(this.keys.accessToken)||void 0,refreshToken:window[this.storage].getItem(this.keys.refreshToken)||void 0};return this.debug("log",`Getting token from ${this.storage}`,t),e?t[e]:t}setToken(e="",t=""){if("object"==typeof e)return Object.keys(e).forEach((t=>this.setToken(t,e[t])));this.debug("log",`Saving ${e} on ${this.storage} as ${this.keys[e]}`,t),window[this.storage].setItem(this.keys[e],t)}getExpiration(e){this.debug("log","Getting expiration of JWT",e);const t=this.validate(e);if("object"!=typeof t)throw this.fire("error",new Error("Invalid JWT"),t);return this.debug("log","JWT decoded",t),"exp"in t?1e3*t.exp:1/0}fire(e,...t){if(this.debug("info",`Firing event ${e}`,...t),e in this.events)return this.events[e](...t);if("verify"===e&&!(e in this.events)&&"error"in this.events)throw this.events.error();if("verify"===e&&!(e in this.events)&&!("error"in this.events))throw new Error("Provide a valid verification method.")}static getKeys(e={}){return{accessToken:e.accessToken||"auth-key",refreshToken:e.refreshToken||"auth-key-refresh"}}static getStorage(e){return window.localStorage.getItem(e)?"localStorage":"sessionStorage"}static delayedDate(e=0){return new Date((new Date).setMinutes((new Date).getMinutes()-e))}static debugWrapper(e="log",...t){const s=`color: ${{log:"gray",info:"blue",warn:"yellow",error:"red"}[e]};`;return console.log("%cWebAuth",s,">",...t)}static debugFromStorage(){const e=window.localStorage.getItem("debug")||window.sessionStorage.getItem("debug")||"";return["webauth","*"].includes(e)}};module.exports=e; | ||
*/var e=class{constructor(e={}){const{keys:t,events:s={},attempts:r=3,delay:i=0}=e;this.keys=this.constructor.getKeys(t),this.events=Object.assign({},s),this.storage=this.constructor.getStorage(this.keys.accessToken),this.attempts=r,this.delay=i,this.version="1.0.0",this.log=this.constructor.debugFromStorage(),this.running=!1,this.debug=this.log?this.constructor.debugWrapper:()=>{}}on(e,t){return this.events[e]=t,this}login(e,t,s=!1){return this.storage=s?"localStorage":"sessionStorage",this.set(e,t,s,!0)}async set(e="",t="",s=!1,r=!1){const{accessToken:i=e,refreshToken:o=t}=r?{accessToken:e,refreshToken:t}:this.getTokens();if(this.debug("log","Initializing WebAuth with tokens",i,o),!i)return this.fire("empty",i);const n=[i];o&&n.push(o),this.validate(n),this.setToken({accessToken:i,refreshToken:o});try{const e=await this.fire("verify",i,o,this.events.expired,this.events.error);return this.debug("info","The verification of accessToken is",e),e||o?(e&&(this.running=!0,this.fire("ready")),e?this.observer():this.renew()):this.fire("expired","accessToken")}catch(e){if(this.debug("error","Error trying to verifying accessToken.",e),this.fire("expired","accessToken"),o)return this.renew()}}observer(e=1){this.debug("log","Observer running");const{accessToken:t,refreshToken:s}=this.getTokens();if(!this.running||!t)return this.debug("warn","accessToken is expired or WebAuth is not running.");const r=this.constructor.delayedDate(this.delay)>this.getExpiration(t);return this.debug("log","accessToken is expired?",r),r?s?this.renew(e):this.fire("expired","accessToken"):setTimeout(this.observer.bind(this),1e3)}async renew(e=1){this.debug("log","Trying to renew accessToken with refreshToken"),1===e&&this.fire("expired","accessToken");const{accessToken:t,refreshToken:s}=this.getTokens(),r=(new Date).getTime()>this.getExpiration(s);if(this.debug("log","refreshToken is expired?",r),r)return this.fire("expired","refreshToken");try{const e=await this.fire("renew",s,t,this.events.expired,this.events.error);if(!e)throw new Error("Renewed accessToken is empty, please check.");return this.debug("info","accessToken has renewed",e),this.setToken("accessToken",e),this.fire("renewed"),this.running||(this.running=!0,this.fire("ready")),this.observer()}catch(t){if(this.debug("error","Error trying to renew accessToken",t),e>=this.attempts)return this.fire("error",t);this.debug("log",`Re-trying to get a new accessToken for ${e}nd time.`),setTimeout(this.observer.bind(this),1e3,e+1)}}clean(){this.debug("log","Cleaning store...");Object.values(this.keys).forEach((e=>{window.localStorage.removeItem(e),window.sessionStorage.removeItem(e)}))}logout(){this.debug("log","Login out..."),this.running=!1,this.clean(),this.fire("logout")}validate(e=""){this.debug("log","Validating JWT",e);const t=e=>JSON.parse(window.atob(e.split(".")[1]));try{return Array.isArray(e)?e.some((e=>!t(e))):t(e)}catch(e){throw this.fire("error",e)}}getTokens(e){const t={accessToken:window[this.storage].getItem(this.keys.accessToken)||void 0,refreshToken:window[this.storage].getItem(this.keys.refreshToken)||void 0};return this.debug("log",`Getting token from ${this.storage}`,t),e?t[e]:t}setToken(e="",t=""){if("object"==typeof e)return Object.keys(e).forEach((t=>this.setToken(t,e[t])));this.debug("log",`Saving ${e} on ${this.storage} as ${this.keys[e]}`,t),window[this.storage].setItem(this.keys[e],t)}getExpiration(e){this.debug("log","Getting expiration of JWT",e);const t=this.validate(e);if("object"!=typeof t)throw this.fire("error",new Error("Invalid JWT"),t);return this.debug("log","JWT decoded",t),"exp"in t?1e3*t.exp:1/0}fire(e,...t){if(this.debug("info",`Firing event ${e}`,...t),e in this.events)return this.events[e](...t);if("verify"===e&&!(e in this.events)&&"error"in this.events)throw this.events.error();if("verify"===e&&!(e in this.events)&&!("error"in this.events))throw new Error("Provide a valid verification method.")}static getKeys(e={}){return{accessToken:e.accessToken||"auth-key",refreshToken:e.refreshToken||"auth-key-refresh"}}static getStorage(e){return window.localStorage.getItem(e)?"localStorage":"sessionStorage"}static delayedDate(e=0){return new Date((new Date).setMinutes((new Date).getMinutes()-e))}static debugWrapper(e="log",...t){const s=`color: ${{log:"gray",info:"blue",warn:"yellow",error:"red"}[e]};`;return console.log("%cWebAuth",s,">",...t)}static debugFromStorage(){const e=window.localStorage.getItem("debug")||window.sessionStorage.getItem("debug")||"";return["webauth","*"].includes(e)}};module.exports=e; |
@@ -11,2 +11,2 @@ /** | ||
*/ | ||
var e=class{constructor(e={}){const{keys:t,events:s={},attempts:r=3,delay:i=0}=e;this.keys=this.constructor.getKeys(t),this.events=Object.assign({},s),this.storage=this.constructor.getStorage(this.keys.accessToken),this.attempts=r,this.delay=i,this.version="1.0.0",this.log=this.constructor.debugFromStorage(),this.running=!1,this.debug=this.log?this.constructor.debugWrapper:()=>{}}on(e,t){return this.events[e]=t,this}login(e,t,s=!1){return this.set(e,t,s,!0)}async set(e="",t="",s=!1,r=!1){this.storage=s?"localStorage":"sessionStorage";const{accessToken:i=e,refreshToken:o=t}=r?{accessToken:e,refreshToken:t}:this.getTokens();if(this.debug("log","Initializing WebAuth with tokens",i,o),!i)return this.fire("empty",i);const n=[i];o&&n.push(o),this.validate(n),this.setToken({accessToken:i,refreshToken:o});try{const e=await this.fire("verify",i,o,this.events.expired,this.events.error);return this.debug("info","The verification of accessToken is",e),e||o?(e&&(this.running=!0,this.fire("ready")),e?this.observer():this.renew()):this.fire("expired","accessToken")}catch(e){if(this.debug("error","Error trying to verifying accessToken.",e),this.fire("expired","accessToken"),o)return this.renew()}}observer(e=1){this.debug("log","Observer running");const{accessToken:t,refreshToken:s}=this.getTokens();if(!this.running||!t)return this.debug("warn","accessToken is expired or WebAuth is not running.");const r=this.constructor.delayedDate(this.delay)>this.getExpiration(t);return this.debug("log","accessToken is expired?",r),r?s?this.renew(e):this.fire("expired","accessToken"):setTimeout(this.observer.bind(this),1e3)}async renew(e=1){this.debug("log","Trying to renew accessToken with refreshToken"),1===e&&this.fire("expired","accessToken");const{accessToken:t,refreshToken:s}=this.getTokens(),r=(new Date).getTime()>this.getExpiration(s);if(this.debug("log","refreshToken is expired?",r),r)return this.fire("expired","refreshToken");try{const e=await this.fire("renew",s,t,this.events.expired,this.events.error);if(!e)throw new Error("Renewed accessToken is empty, please check.");return this.debug("info","accessToken has renewed",e),this.setToken("accessToken",e),this.fire("renewed"),this.running||(this.running=!0,this.fire("ready")),this.observer()}catch(t){if(this.debug("error","Error trying to renew accessToken",t),e>=this.attempts)return this.fire("error",t);this.debug("log",`Re-trying to get a new accessToken for ${e}nd time.`),setTimeout(this.observer.bind(this),1e3,e+1)}}clean(){this.debug("log","Cleaning store...");Object.values(this.keys).forEach((e=>{window.localStorage.removeItem(e),window.sessionStorage.removeItem(e)}))}logout(){this.debug("log","Login out..."),this.running=!1,this.clean(),this.fire("logout")}validate(e=""){this.debug("log","Validating JWT",e);const t=e=>JSON.parse(window.atob(e.split(".")[1]));try{return Array.isArray(e)?e.some((e=>!t(e))):t(e)}catch(e){throw this.fire("error",e)}}getTokens(e){const t={accessToken:window[this.storage].getItem(this.keys.accessToken)||void 0,refreshToken:window[this.storage].getItem(this.keys.refreshToken)||void 0};return this.debug("log",`Getting token from ${this.storage}`,t),e?t[e]:t}setToken(e="",t=""){if("object"==typeof e)return Object.keys(e).forEach((t=>this.setToken(t,e[t])));this.debug("log",`Saving ${e} on ${this.storage} as ${this.keys[e]}`,t),window[this.storage].setItem(this.keys[e],t)}getExpiration(e){this.debug("log","Getting expiration of JWT",e);const t=this.validate(e);if("object"!=typeof t)throw this.fire("error",new Error("Invalid JWT"),t);return this.debug("log","JWT decoded",t),"exp"in t?1e3*t.exp:1/0}fire(e,...t){if(this.debug("info",`Firing event ${e}`,...t),e in this.events)return this.events[e](...t);if("verify"===e&&!(e in this.events)&&"error"in this.events)throw this.events.error();if("verify"===e&&!(e in this.events)&&!("error"in this.events))throw new Error("Provide a valid verification method.")}static getKeys(e={}){return{accessToken:e.accessToken||"auth-key",refreshToken:e.refreshToken||"auth-key-refresh"}}static getStorage(e){return window.localStorage.getItem(e)?"localStorage":"sessionStorage"}static delayedDate(e=0){return new Date((new Date).setMinutes((new Date).getMinutes()-e))}static debugWrapper(e="log",...t){const s=`color: ${{log:"gray",info:"blue",warn:"yellow",error:"red"}[e]};`;return console.log("%cWebAuth",s,">",...t)}static debugFromStorage(){const e=window.localStorage.getItem("debug")||window.sessionStorage.getItem("debug")||"";return["webauth","*"].includes(e)}};export{e as default}; | ||
var e=class{constructor(e={}){const{keys:t,events:s={},attempts:r=3,delay:i=0}=e;this.keys=this.constructor.getKeys(t),this.events=Object.assign({},s),this.storage=this.constructor.getStorage(this.keys.accessToken),this.attempts=r,this.delay=i,this.version="1.0.0",this.log=this.constructor.debugFromStorage(),this.running=!1,this.debug=this.log?this.constructor.debugWrapper:()=>{}}on(e,t){return this.events[e]=t,this}login(e,t,s=!1){return this.storage=s?"localStorage":"sessionStorage",this.set(e,t,s,!0)}async set(e="",t="",s=!1,r=!1){const{accessToken:i=e,refreshToken:o=t}=r?{accessToken:e,refreshToken:t}:this.getTokens();if(this.debug("log","Initializing WebAuth with tokens",i,o),!i)return this.fire("empty",i);const n=[i];o&&n.push(o),this.validate(n),this.setToken({accessToken:i,refreshToken:o});try{const e=await this.fire("verify",i,o,this.events.expired,this.events.error);return this.debug("info","The verification of accessToken is",e),e||o?(e&&(this.running=!0,this.fire("ready")),e?this.observer():this.renew()):this.fire("expired","accessToken")}catch(e){if(this.debug("error","Error trying to verifying accessToken.",e),this.fire("expired","accessToken"),o)return this.renew()}}observer(e=1){this.debug("log","Observer running");const{accessToken:t,refreshToken:s}=this.getTokens();if(!this.running||!t)return this.debug("warn","accessToken is expired or WebAuth is not running.");const r=this.constructor.delayedDate(this.delay)>this.getExpiration(t);return this.debug("log","accessToken is expired?",r),r?s?this.renew(e):this.fire("expired","accessToken"):setTimeout(this.observer.bind(this),1e3)}async renew(e=1){this.debug("log","Trying to renew accessToken with refreshToken"),1===e&&this.fire("expired","accessToken");const{accessToken:t,refreshToken:s}=this.getTokens(),r=(new Date).getTime()>this.getExpiration(s);if(this.debug("log","refreshToken is expired?",r),r)return this.fire("expired","refreshToken");try{const e=await this.fire("renew",s,t,this.events.expired,this.events.error);if(!e)throw new Error("Renewed accessToken is empty, please check.");return this.debug("info","accessToken has renewed",e),this.setToken("accessToken",e),this.fire("renewed"),this.running||(this.running=!0,this.fire("ready")),this.observer()}catch(t){if(this.debug("error","Error trying to renew accessToken",t),e>=this.attempts)return this.fire("error",t);this.debug("log",`Re-trying to get a new accessToken for ${e}nd time.`),setTimeout(this.observer.bind(this),1e3,e+1)}}clean(){this.debug("log","Cleaning store...");Object.values(this.keys).forEach((e=>{window.localStorage.removeItem(e),window.sessionStorage.removeItem(e)}))}logout(){this.debug("log","Login out..."),this.running=!1,this.clean(),this.fire("logout")}validate(e=""){this.debug("log","Validating JWT",e);const t=e=>JSON.parse(window.atob(e.split(".")[1]));try{return Array.isArray(e)?e.some((e=>!t(e))):t(e)}catch(e){throw this.fire("error",e)}}getTokens(e){const t={accessToken:window[this.storage].getItem(this.keys.accessToken)||void 0,refreshToken:window[this.storage].getItem(this.keys.refreshToken)||void 0};return this.debug("log",`Getting token from ${this.storage}`,t),e?t[e]:t}setToken(e="",t=""){if("object"==typeof e)return Object.keys(e).forEach((t=>this.setToken(t,e[t])));this.debug("log",`Saving ${e} on ${this.storage} as ${this.keys[e]}`,t),window[this.storage].setItem(this.keys[e],t)}getExpiration(e){this.debug("log","Getting expiration of JWT",e);const t=this.validate(e);if("object"!=typeof t)throw this.fire("error",new Error("Invalid JWT"),t);return this.debug("log","JWT decoded",t),"exp"in t?1e3*t.exp:1/0}fire(e,...t){if(this.debug("info",`Firing event ${e}`,...t),e in this.events)return this.events[e](...t);if("verify"===e&&!(e in this.events)&&"error"in this.events)throw this.events.error();if("verify"===e&&!(e in this.events)&&!("error"in this.events))throw new Error("Provide a valid verification method.")}static getKeys(e={}){return{accessToken:e.accessToken||"auth-key",refreshToken:e.refreshToken||"auth-key-refresh"}}static getStorage(e){return window.localStorage.getItem(e)?"localStorage":"sessionStorage"}static delayedDate(e=0){return new Date((new Date).setMinutes((new Date).getMinutes()-e))}static debugWrapper(e="log",...t){const s=`color: ${{log:"gray",info:"blue",warn:"yellow",error:"red"}[e]};`;return console.log("%cWebAuth",s,">",...t)}static debugFromStorage(){const e=window.localStorage.getItem("debug")||window.sessionStorage.getItem("debug")||"";return["webauth","*"].includes(e)}};export{e as default}; |
@@ -12,2 +12,2 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).WebAuth=t()}(this,(function(){ | ||
*/ | ||
return class{constructor(e={}){const{keys:t,events:s={},attempts:r=3,delay:i=0}=e;this.keys=this.constructor.getKeys(t),this.events=Object.assign({},s),this.storage=this.constructor.getStorage(this.keys.accessToken),this.attempts=r,this.delay=i,this.version="1.0.0",this.log=this.constructor.debugFromStorage(),this.running=!1,this.debug=this.log?this.constructor.debugWrapper:()=>{}}on(e,t){return this.events[e]=t,this}login(e,t,s=!1){return this.set(e,t,s,!0)}async set(e="",t="",s=!1,r=!1){this.storage=s?"localStorage":"sessionStorage";const{accessToken:i=e,refreshToken:o=t}=r?{accessToken:e,refreshToken:t}:this.getTokens();if(this.debug("log","Initializing WebAuth with tokens",i,o),!i)return this.fire("empty",i);const n=[i];o&&n.push(o),this.validate(n),this.setToken({accessToken:i,refreshToken:o});try{const e=await this.fire("verify",i,o,this.events.expired,this.events.error);return this.debug("info","The verification of accessToken is",e),e||o?(e&&(this.running=!0,this.fire("ready")),e?this.observer():this.renew()):this.fire("expired","accessToken")}catch(e){if(this.debug("error","Error trying to verifying accessToken.",e),this.fire("expired","accessToken"),o)return this.renew()}}observer(e=1){this.debug("log","Observer running");const{accessToken:t,refreshToken:s}=this.getTokens();if(!this.running||!t)return this.debug("warn","accessToken is expired or WebAuth is not running.");const r=this.constructor.delayedDate(this.delay)>this.getExpiration(t);return this.debug("log","accessToken is expired?",r),r?s?this.renew(e):this.fire("expired","accessToken"):setTimeout(this.observer.bind(this),1e3)}async renew(e=1){this.debug("log","Trying to renew accessToken with refreshToken"),1===e&&this.fire("expired","accessToken");const{accessToken:t,refreshToken:s}=this.getTokens(),r=(new Date).getTime()>this.getExpiration(s);if(this.debug("log","refreshToken is expired?",r),r)return this.fire("expired","refreshToken");try{const e=await this.fire("renew",s,t,this.events.expired,this.events.error);if(!e)throw new Error("Renewed accessToken is empty, please check.");return this.debug("info","accessToken has renewed",e),this.setToken("accessToken",e),this.fire("renewed"),this.running||(this.running=!0,this.fire("ready")),this.observer()}catch(t){if(this.debug("error","Error trying to renew accessToken",t),e>=this.attempts)return this.fire("error",t);this.debug("log",`Re-trying to get a new accessToken for ${e}nd time.`),setTimeout(this.observer.bind(this),1e3,e+1)}}clean(){this.debug("log","Cleaning store...");Object.values(this.keys).forEach((e=>{window.localStorage.removeItem(e),window.sessionStorage.removeItem(e)}))}logout(){this.debug("log","Login out..."),this.running=!1,this.clean(),this.fire("logout")}validate(e=""){this.debug("log","Validating JWT",e);const t=e=>JSON.parse(window.atob(e.split(".")[1]));try{return Array.isArray(e)?e.some((e=>!t(e))):t(e)}catch(e){throw this.fire("error",e)}}getTokens(e){const t={accessToken:window[this.storage].getItem(this.keys.accessToken)||void 0,refreshToken:window[this.storage].getItem(this.keys.refreshToken)||void 0};return this.debug("log",`Getting token from ${this.storage}`,t),e?t[e]:t}setToken(e="",t=""){if("object"==typeof e)return Object.keys(e).forEach((t=>this.setToken(t,e[t])));this.debug("log",`Saving ${e} on ${this.storage} as ${this.keys[e]}`,t),window[this.storage].setItem(this.keys[e],t)}getExpiration(e){this.debug("log","Getting expiration of JWT",e);const t=this.validate(e);if("object"!=typeof t)throw this.fire("error",new Error("Invalid JWT"),t);return this.debug("log","JWT decoded",t),"exp"in t?1e3*t.exp:1/0}fire(e,...t){if(this.debug("info",`Firing event ${e}`,...t),e in this.events)return this.events[e](...t);if("verify"===e&&!(e in this.events)&&"error"in this.events)throw this.events.error();if("verify"===e&&!(e in this.events)&&!("error"in this.events))throw new Error("Provide a valid verification method.")}static getKeys(e={}){return{accessToken:e.accessToken||"auth-key",refreshToken:e.refreshToken||"auth-key-refresh"}}static getStorage(e){return window.localStorage.getItem(e)?"localStorage":"sessionStorage"}static delayedDate(e=0){return new Date((new Date).setMinutes((new Date).getMinutes()-e))}static debugWrapper(e="log",...t){const s=`color: ${{log:"gray",info:"blue",warn:"yellow",error:"red"}[e]};`;return console.log("%cWebAuth",s,">",...t)}static debugFromStorage(){const e=window.localStorage.getItem("debug")||window.sessionStorage.getItem("debug")||"";return["webauth","*"].includes(e)}}})); | ||
return class{constructor(e={}){const{keys:t,events:s={},attempts:r=3,delay:i=0}=e;this.keys=this.constructor.getKeys(t),this.events=Object.assign({},s),this.storage=this.constructor.getStorage(this.keys.accessToken),this.attempts=r,this.delay=i,this.version="1.0.0",this.log=this.constructor.debugFromStorage(),this.running=!1,this.debug=this.log?this.constructor.debugWrapper:()=>{}}on(e,t){return this.events[e]=t,this}login(e,t,s=!1){return this.storage=s?"localStorage":"sessionStorage",this.set(e,t,s,!0)}async set(e="",t="",s=!1,r=!1){const{accessToken:i=e,refreshToken:o=t}=r?{accessToken:e,refreshToken:t}:this.getTokens();if(this.debug("log","Initializing WebAuth with tokens",i,o),!i)return this.fire("empty",i);const n=[i];o&&n.push(o),this.validate(n),this.setToken({accessToken:i,refreshToken:o});try{const e=await this.fire("verify",i,o,this.events.expired,this.events.error);return this.debug("info","The verification of accessToken is",e),e||o?(e&&(this.running=!0,this.fire("ready")),e?this.observer():this.renew()):this.fire("expired","accessToken")}catch(e){if(this.debug("error","Error trying to verifying accessToken.",e),this.fire("expired","accessToken"),o)return this.renew()}}observer(e=1){this.debug("log","Observer running");const{accessToken:t,refreshToken:s}=this.getTokens();if(!this.running||!t)return this.debug("warn","accessToken is expired or WebAuth is not running.");const r=this.constructor.delayedDate(this.delay)>this.getExpiration(t);return this.debug("log","accessToken is expired?",r),r?s?this.renew(e):this.fire("expired","accessToken"):setTimeout(this.observer.bind(this),1e3)}async renew(e=1){this.debug("log","Trying to renew accessToken with refreshToken"),1===e&&this.fire("expired","accessToken");const{accessToken:t,refreshToken:s}=this.getTokens(),r=(new Date).getTime()>this.getExpiration(s);if(this.debug("log","refreshToken is expired?",r),r)return this.fire("expired","refreshToken");try{const e=await this.fire("renew",s,t,this.events.expired,this.events.error);if(!e)throw new Error("Renewed accessToken is empty, please check.");return this.debug("info","accessToken has renewed",e),this.setToken("accessToken",e),this.fire("renewed"),this.running||(this.running=!0,this.fire("ready")),this.observer()}catch(t){if(this.debug("error","Error trying to renew accessToken",t),e>=this.attempts)return this.fire("error",t);this.debug("log",`Re-trying to get a new accessToken for ${e}nd time.`),setTimeout(this.observer.bind(this),1e3,e+1)}}clean(){this.debug("log","Cleaning store...");Object.values(this.keys).forEach((e=>{window.localStorage.removeItem(e),window.sessionStorage.removeItem(e)}))}logout(){this.debug("log","Login out..."),this.running=!1,this.clean(),this.fire("logout")}validate(e=""){this.debug("log","Validating JWT",e);const t=e=>JSON.parse(window.atob(e.split(".")[1]));try{return Array.isArray(e)?e.some((e=>!t(e))):t(e)}catch(e){throw this.fire("error",e)}}getTokens(e){const t={accessToken:window[this.storage].getItem(this.keys.accessToken)||void 0,refreshToken:window[this.storage].getItem(this.keys.refreshToken)||void 0};return this.debug("log",`Getting token from ${this.storage}`,t),e?t[e]:t}setToken(e="",t=""){if("object"==typeof e)return Object.keys(e).forEach((t=>this.setToken(t,e[t])));this.debug("log",`Saving ${e} on ${this.storage} as ${this.keys[e]}`,t),window[this.storage].setItem(this.keys[e],t)}getExpiration(e){this.debug("log","Getting expiration of JWT",e);const t=this.validate(e);if("object"!=typeof t)throw this.fire("error",new Error("Invalid JWT"),t);return this.debug("log","JWT decoded",t),"exp"in t?1e3*t.exp:1/0}fire(e,...t){if(this.debug("info",`Firing event ${e}`,...t),e in this.events)return this.events[e](...t);if("verify"===e&&!(e in this.events)&&"error"in this.events)throw this.events.error();if("verify"===e&&!(e in this.events)&&!("error"in this.events))throw new Error("Provide a valid verification method.")}static getKeys(e={}){return{accessToken:e.accessToken||"auth-key",refreshToken:e.refreshToken||"auth-key-refresh"}}static getStorage(e){return window.localStorage.getItem(e)?"localStorage":"sessionStorage"}static delayedDate(e=0){return new Date((new Date).setMinutes((new Date).getMinutes()-e))}static debugWrapper(e="log",...t){const s=`color: ${{log:"gray",info:"blue",warn:"yellow",error:"red"}[e]};`;return console.log("%cWebAuth",s,">",...t)}static debugFromStorage(){const e=window.localStorage.getItem("debug")||window.sessionStorage.getItem("debug")||"";return["webauth","*"].includes(e)}}})); |
{ | ||
"name": "@videsk/jwt-webauth", | ||
"version": "2.6.0", | ||
"version": "2.6.1", | ||
"description": "Auto manage JWT access and refresh token like a session, with server-side validation and renew.", | ||
@@ -5,0 +5,0 @@ "umdName": "WebAuth", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package