@evervault/sdk
Advanced tools
Comparing version 4.0.1 to 4.1.0
@@ -212,2 +212,46 @@ const { errors, Datatypes } = require('../utils'); | ||
const createToken = async (action, payload, expiry) => { | ||
let wellFormedExpiry; | ||
if (expiry) { | ||
if (expiry && expiry instanceof Date) { | ||
wellFormedExpiry = expiry.getUTCMilliseconds(); | ||
} else if (expiry && typeof expiry === 'number') { | ||
wellFormedExpiry = expiry; | ||
} else { | ||
throw new errors.TokenCreationError( | ||
`Expiry must be a Date object, got ${typeof expiry}` | ||
); | ||
} | ||
const now = new Date().getUTCMilliseconds(); | ||
if (wellFormedExpiry < now || wellFormedExpiry - now > 600000) { | ||
throw new errors.TokenCreationError( | ||
'Expiry must be in the next 10 minutes' | ||
); | ||
} | ||
} | ||
const response = await post( | ||
`${config.baseUrl}/client-side-tokens`, | ||
{ | ||
action, | ||
payload, | ||
expiry: wellFormedExpiry, | ||
}, | ||
{ | ||
'Content-Type': 'application/json', | ||
}, | ||
true, | ||
'json' | ||
); | ||
if (response.statusCode >= 200 && response.statusCode < 300) { | ||
return { | ||
...response.body, | ||
createdAt: new Date(response.body.createdAt), | ||
expiry: new Date(response.body.expiry), | ||
}; | ||
} | ||
throw errors.mapApiResponseToError(response); | ||
}; | ||
return { | ||
@@ -221,3 +265,4 @@ getCageKey, | ||
decrypt, | ||
createToken, | ||
}; | ||
}; |
@@ -6,2 +6,3 @@ declare module "@evervault/sdk" { | ||
decrypt: (encryptedData: any) => Promise<any>; | ||
createClientSideDecryptToken: (payload: any, expiry?: Date) => Promise<{ id: string, token: string, createdAt: Date, expiry: Date }>; | ||
run: <T>(functionName: string, data: object, options?: { async?: boolean, version?: string }) => Promise<{ result: T, runId: string, appUuid: string }>; | ||
@@ -8,0 +9,0 @@ createRunToken: (functionName: string, data: object) => Promise<{ token: string }>; |
@@ -17,2 +17,3 @@ const crypto = require('crypto'); | ||
const { Crypto, Http, RelayOutboundConfig } = require('./core'); | ||
const { TokenCreationError } = require('./utils/errors'); | ||
@@ -395,4 +396,13 @@ const originalRequest = https.request; | ||
} | ||
async createClientSideDecryptToken(payload, expiry = null) { | ||
if (!payload) { | ||
throw new TokenCreationError( | ||
'Payload must be specified when creating a decrypt token' | ||
); | ||
} | ||
return await this.http.createToken('api:decrypt', payload, expiry); | ||
} | ||
} | ||
module.exports = EvervaultClient; |
@@ -36,2 +36,4 @@ class EvervaultError extends Error { | ||
class TokenCreationError extends EvervaultError {} | ||
const mapApiResponseToError = ({ statusCode, body, headers }) => { | ||
@@ -82,2 +84,3 @@ if (statusCode === 401) return new ApiKeyError('Invalid Api Key provided.'); | ||
ExceededMaxFileSizeError, | ||
TokenCreationError, | ||
}; |
{ | ||
"name": "@evervault/sdk", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Node.js SDK for Evervault", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -87,2 +87,17 @@ [![Evervault](https://evervault.com/evervault.svg)](https://evervault.com/) | ||
### evervault.createClientSideDecryptToken() | ||
`evervault.createClientSideDecryptToken()` creates a token that can be used to authenticate a `decrypt()` request | ||
from a frontend/client application. | ||
An API Key with the `Create Token` permission must be used to perform this operation. | ||
```javascript | ||
async evervault.createClientSideDecryptToken(payload: string | Array | Object, expiry: Date); | ||
``` | ||
| Parameter | Type | Description | | ||
| -------------- | --------------------------------| --------------------------------------------------------------------- | | ||
| payload | String, Array, or Object | Data that the token can decrypt. | | ||
| expiry | Date | The expiry of the token, must be < 10 mins from now. (Default 5 mins) | | ||
### evervault.run() | ||
@@ -89,0 +104,0 @@ |
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
68625
1891
184