@evervault/sdk
Advanced tools
Comparing version 0.2.3 to 0.3.0
80
index.js
@@ -95,4 +95,78 @@ /** @format */ | ||
}, | ||
/** | ||
* Allows data to be stored in the API on behalf of the user | ||
* @param {object} toSave - an object containing the data to be stored | ||
*/ | ||
set: async function(toSave) { | ||
const accessToken = await this.getValidAccessToken(); | ||
return fetch(`${this.urls.api}/v1/data/${this.appId}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${accessToken}` | ||
}, | ||
body: JSON.stringify({ | ||
data: toSave | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((result) => { | ||
if (!result) { | ||
throw new Error('Unable to save data'); | ||
} | ||
return result; | ||
}) | ||
.catch((err) => { | ||
throw new Error('Unable to save data', err); | ||
}); | ||
}, | ||
/** | ||
* Fetch data stored on behalf of the user from the evervault API | ||
* @param {string} toGet [(optional) default = ''] - a string containing the parameter to fetch | ||
*/ | ||
get: async function(toGet = '') { | ||
const accessToken = await this.getValidAccessToken(); | ||
return fetch(`${this.urls.api}/v1/data/${this.appId}/${toGet}`, { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${accessToken}` | ||
} | ||
}) | ||
.then((res) => res.json()) | ||
.then((result) => { | ||
if (!result) { | ||
throw new Error('Unable to get data'); | ||
} | ||
return result; | ||
}) | ||
.catch((err) => { | ||
throw new Error('Unable to get data', err); | ||
}); | ||
}, | ||
/** | ||
* Get a valid API access token, by retrieving from LocalStorage | ||
* or by refreshing if required | ||
*/ | ||
getValidAccessToken: async function() { | ||
if (Utils.checkAccessToken(localStorage.getItem('evervault-accessToken'))) { | ||
return localStorage.getItem('evervault-accessToken'); | ||
} else { | ||
const newToken = await this.refreshAccessToken( | ||
localStorage.getItem('evervault-accessToken'), | ||
localStorage.getItem('evervault-refreshToken') | ||
); | ||
return newToken.accessToken; | ||
} | ||
}, | ||
/** | ||
* Encrypt your user's data with their evervault secret key | ||
@@ -235,7 +309,9 @@ * @param {any} data - data to encrypt | ||
.then((res) => res.json()) | ||
.then(({ accessToken }) => { | ||
.then(({ accessToken, refreshToken }) => { | ||
if (!accessToken) { | ||
throw new Error('Unable to retrive access token'); | ||
} | ||
return accessToken | ||
localStorage.setItem('evervault-accessToken', accessToken); | ||
localStorage.setItem('evervault-refreshToken', refreshToken); | ||
return { accessToken, refreshToken }; | ||
}) | ||
@@ -242,0 +318,0 @@ .catch((err) => { |
{ | ||
"name": "@evervault/sdk", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "evervault Browser SDK", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -11,3 +11,3 @@ <img src="https://raw.githubusercontent.com/evervault/evervault-sdk/master/logo.png" height="35" /> | ||
```javascript | ||
checkAuth(appId): void | ||
evervault.checkAuth(appId): void | ||
``` | ||
@@ -23,3 +23,3 @@ | ||
```javascript | ||
encrypt(data[, encryptOptions]): Promise<String> | ||
evervault.encrypt(data[, encryptOptions]): Promise<String> | ||
``` | ||
@@ -45,3 +45,3 @@ | ||
```javascript | ||
decrypt(data[, privateKey]): Promise<String> | ||
evervault.decrypt(data[, privateKey]): Promise<String> | ||
``` | ||
@@ -56,2 +56,24 @@ | ||
### set | ||
```javascript | ||
evervault.set(toSet): Promise<Object> | ||
``` | ||
Save data on behalf of the user in evervault storage. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| toSave | `Object` | the data to be saved. Must be an object in `{ "key": "value" }` form | | ||
### get | ||
```javascript | ||
evervault.set([toGet]): Promise<Object> | ||
``` | ||
Retrieve data on behalf of the user in evervault storage. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| toGet | `String` | Optional. the data to be retrieved. Must resolve to a previously stored piece of data | | ||
### logout | ||
@@ -58,0 +80,0 @@ ```javascript |
@@ -10,4 +10,16 @@ /** @format */ | ||
localStorage.setItem('evervault-refreshToken', 'refresh'); | ||
localStorage.setItem('evervault-haiku', 'big-bertha-21'); | ||
localStorage.setItem('evervault-haiku', 'spontaneous-seagull-123456'); | ||
const appId = '1ffd79c5-0df1-408d-a279-223cee474e2c'; | ||
it('should initialize an evervault app', function(done) { | ||
try { | ||
evervault | ||
.init(appId) | ||
done(); | ||
} catch (err) { | ||
done(err); | ||
} | ||
}); | ||
const testEncStr = 'test string'; | ||
@@ -257,2 +269,27 @@ it('should return ev formatted encrypted string', function(done) { | ||
}); | ||
const testData = { | ||
'foo': 'bar', | ||
'bar': 'baz' | ||
}; | ||
it('should let a user store data', function(done) { | ||
evervault.set(testData).then((result) => { | ||
chai.assert(result.foo === testData.foo && result.bar === testData.bar); | ||
done(); | ||
}); | ||
}); | ||
it('should let a user get all stored data', function(done) { | ||
evervault.get().then((result) => { | ||
chai.assert(result.foo === testData.foo && result.bar === testData.bar); | ||
done(); | ||
}); | ||
}); | ||
it('should let a user get specfic stored data', function(done) { | ||
evervault.get('foo').then((result) => { | ||
chai.assert(result.foo === testData.foo); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -66,2 +66,9 @@ /** @format */ | ||
static checkAccessToken(accessToken) { | ||
return JSON.parse(this.bufToStr(this.b64ToBuffer(accessToken.split('.')[1]))).exp | ||
> Math.floor(Date.now() / 1000) | ||
? true | ||
: false; | ||
} | ||
static parseEvervaultString(str) { | ||
@@ -68,0 +75,0 @@ const valueArr = str.split(':'); |
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
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
46168
733
93
4