@evervault/sdk
Advanced tools
Comparing version 0.3.3 to 0.3.4
87
index.js
@@ -170,3 +170,2 @@ /** @format */ | ||
); | ||
return newToken.accessToken; | ||
@@ -181,3 +180,3 @@ } | ||
*/ | ||
encrypt: function(data, options = {}) { | ||
encrypt: function(data, options = { preserveObjectShape: true }) { | ||
const _encryptObject = (data, fields) => { | ||
@@ -314,3 +313,8 @@ if (data) { | ||
}) | ||
.then((res) => res.json()) | ||
.then((res) => { | ||
if (res.status === 401) { | ||
return this.logout(); | ||
} | ||
return res.json(); | ||
}) | ||
.then(({ accessToken, refreshToken }) => { | ||
@@ -335,13 +339,21 @@ if (!accessToken) { | ||
if (appId) { | ||
this.init(appId); | ||
this.init(appId); | ||
} | ||
const urlKey = window.location.hash.substring(2); | ||
window.location.hash = '/'; | ||
if (urlKey && Utils.areHashValuesFromAuth(urlKey)) { | ||
//check for support of pushstate | ||
if ('pushState' in history) { | ||
const { pathname, search } = window.location; | ||
const newUrl = pathname + (search ? search : ''); | ||
history.pushState('', document.title, newUrl); | ||
} else { | ||
window.location.hash = '/'; | ||
} | ||
} | ||
const cachedPrivateKey = localStorage.getItem('evervault-privateKey'); | ||
const accessToken = localStorage.getItem('evervault-accessToken'); | ||
const refreshToken = localStorage.getItem('evervault-refreshToken'); | ||
const cachedAccessToken = localStorage.getItem('evervault-accessToken'); | ||
const cachedRefreshToken = localStorage.getItem('evervault-refreshToken'); | ||
const hasStorageCredentials = Boolean( | ||
accessToken && refreshToken && cachedPrivateKey | ||
cachedAccessToken && cachedRefreshToken && cachedPrivateKey | ||
); | ||
@@ -351,9 +363,60 @@ if (!hasStorageCredentials && !urlKey) { | ||
} else if (urlKey) { | ||
//user has key in url but not stored in memory | ||
Utils.setUserKeysInStorage(urlKey); | ||
return true; | ||
//user has key in url but not stored in memory, set keys in storage and return user's haiku | ||
const { accessToken, refreshToken, haiku } = Utils.setUserKeysInStorage( | ||
urlKey | ||
); | ||
this.accessToken = accessToken; | ||
this.refreshToken = refreshToken; | ||
this.haiku = haiku; | ||
return this.keyStore.updateKey().then(() => true); | ||
} else if ( | ||
hasStorageCredentials && | ||
!this.accessToken && | ||
!this.refreshToken && | ||
!this.haiku | ||
) { | ||
this.accessToken = cachedAccessToken; | ||
this.refreshToken = cachedRefreshToken; | ||
this.haiku = localStorage.getItem('evervault-haiku'); | ||
} | ||
return hasStorageCredentials; | ||
}, | ||
auth: (appId) => module.exports.checkAuth(appId), | ||
fetch: function(resource, options) { | ||
const handleRequest = (resource, options, retry) => { | ||
const defaultHeaders = { authorization: `Bearer ${this.accessToken}` }; | ||
const requestHeaders = Object.assign(defaultHeaders, options.headers); | ||
const requestOptions = Object.assign(options, { | ||
headers: requestHeaders, | ||
}); | ||
return fetch(resource, requestOptions).then((res) => { | ||
if (!res.ok && res.status === 401 && retry) { | ||
return this.logout(); | ||
} | ||
return res; | ||
}); | ||
}; | ||
return handleRequest(resource, options).then((res) => { | ||
if (!res.ok && res.status === 401) { | ||
return this.refreshAccessToken( | ||
this.accessToken, | ||
this.refreshToken | ||
).then(({ accessToken, refreshToken }) => { | ||
this.accessToken = accessToken; | ||
this.refreshToken = refreshToken; | ||
return handleRequest(resource, options, true); | ||
}); | ||
} else { | ||
return res; | ||
} | ||
}); | ||
}, | ||
}; |
{ | ||
"name": "@evervault/sdk", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "evervault Browser SDK", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -15,4 +15,3 @@ /** @format */ | ||
try { | ||
evervault | ||
.init(appId) | ||
evervault.init(appId); | ||
@@ -67,3 +66,3 @@ done(); | ||
evervault | ||
.encrypt(obj, { preserveObjectShape: true }) | ||
.encrypt(obj) | ||
.then((encObj) => evervault.decrypt(encObj)) | ||
@@ -98,3 +97,3 @@ .then((decObj) => { | ||
evervault | ||
.encrypt(obj) | ||
.encrypt(obj, { preserveObjectShape: false }) | ||
.then((encObj) => evervault.decrypt(encObj)) | ||
@@ -273,4 +272,4 @@ .then((decObj) => { | ||
const testData = { | ||
'foo': 'bar', | ||
'bar': 'baz' | ||
foo: 'bar', | ||
bar: 'baz', | ||
}; | ||
@@ -277,0 +276,0 @@ it('should let a user store data', function(done) { |
29
utils.js
@@ -67,6 +67,7 @@ /** @format */ | ||
static checkAccessToken(accessToken) { | ||
return JSON.parse(this.bufToStr(this.b64ToBuffer(accessToken.split('.')[1]))).exp | ||
> Math.floor(Date.now() / 1000) | ||
? true | ||
: false; | ||
return JSON.parse( | ||
this.bufToStr(this.b64ToBuffer(accessToken.split('.')[1])) | ||
).exp > Math.floor(Date.now() / 1000) | ||
? true | ||
: false; | ||
} | ||
@@ -116,10 +117,9 @@ | ||
static setUserKeysInStorage(urlKey) { | ||
const keys = urlKey.split(':'); | ||
localStorage.setItem('evervault-privateKey', keys[0]); | ||
localStorage.setItem('evervault-accessToken', keys[1]); | ||
localStorage.setItem('evervault-refreshToken', keys[2]); | ||
localStorage.setItem( | ||
'evervault-haiku', | ||
JSON.parse(window.atob(keys[1].split('.')[1])).haiku | ||
); | ||
const [privateKey, accessToken, refreshToken] = urlKey.split(':'); | ||
localStorage.setItem('evervault-privateKey', privateKey); | ||
localStorage.setItem('evervault-accessToken', accessToken); | ||
localStorage.setItem('evervault-refreshToken', refreshToken); | ||
const haiku = JSON.parse(window.atob(accessToken.split('.')[1])).haiku; | ||
localStorage.setItem('evervault-haiku', haiku); | ||
return { haiku, accessToken, refreshToken }; | ||
} | ||
@@ -155,2 +155,7 @@ | ||
} | ||
static areHashValuesFromAuth(urlValues) { | ||
const authRegex = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=?){1}:((eyJ){1}[A-Za-z0-9-_]+\.(eyJ){1}[A-Za-z0-9-_]+\.[A-Za-z0-9-_.+\/=]*){1}:([a-z]+-[a-z]+-[0-9]{6,8}.[a-f0-9]{8}-[a-f0-9]{3,4}-[a-f0-9]{3,4}-[a-f0-9]{3,4}-[a-f0-9]{12}){1}$/; | ||
return authRegex.test(urlValues); | ||
} | ||
}; |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
49472
801