diff --git a/README.md b/README.md index 8546d8c..e177bec 100644 --- a/README.md +++ b/README.md @@ -25,25 +25,24 @@ You can install this module from npm. Another option is to add it via `script` t ### npm You will need to install `@virgilsecurity/keyknox`. ```sh -npm install @virgilsecurity/keyknox +npm install @virgilsecurity/keyknox@0.3.1 ``` You will also need to install `virgil-crypto` and `virgil-sdk` from npm. ```sh -npm install virgil-crypto@next virgil-sdk@next +npm install virgil-crypto@4.0.0-alpha.13 virgil-sdk@6.0.0-alpha.4 ``` -> Note that minimum supported version of `virgil-crypto` is `4.0.0-alpha.0` and minimum supported version of `virgil-sdk` is `6.0.0-alpha.0`. ### In browser via `script` tag You will need to add `@virgilsecurity/keyknox` script. ```html - + ``` You will also need to add `virgil-crypto` and `virgil-sdk` scripts. ```html - - + + ``` Now you can use global variables `Keyknox`, `Virgil` and `VirgilCrypto` as namespace objects, containing all of `@virgilsecurity/keyknox`, `virgil-sdk` and `virgil-crypto` exports as properties. diff --git a/package.json b/package.json index 8fa144a..ff15ca6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@virgilsecurity/keyknox", - "version": "0.3.0", + "version": "0.3.1", "description": "Keyknox SDK allows developers to communicate with Virgil Keyknox Service to upload, download, and synchronize encrypted sensitive data (private keys) between different devices.", "main": "./dist/keyknox.cjs.js", "module": "./dist/keyknox.es.js", @@ -19,12 +19,12 @@ }, "dependencies": { "@types/base-64": "^0.1.3", - "@virgilsecurity/crypto-types": "^0.1.3", + "@virgilsecurity/crypto-types": "^0.2.0", "axios": "^0.19.0", "base-64": "^0.1.0" }, "peerDependencies": { - "virgil-sdk": "next" + "virgil-sdk": "6.0.0-alpha.4" }, "devDependencies": { "@types/chai": "^4.1.7", @@ -42,13 +42,14 @@ "rimraf": "^2.6.3", "rollup": "^1.14.6", "rollup-plugin-commonjs": "^10.0.2", + "rollup-plugin-json": "^4.0.0", "rollup-plugin-node-resolve": "^5.2.0", "rollup-plugin-terser": "^5.1.1", "rollup-plugin-typescript2": "^0.21.1", "ts-node": "^8.3.0", "typescript": "^3.5.1", "uuid": "^3.3.2", - "virgil-crypto": "next", - "virgil-sdk": "next" + "virgil-crypto": "4.0.0-alpha.13", + "virgil-sdk": "6.0.0-alpha.4" } } diff --git a/rollup.config.js b/rollup.config.js index 891186e..17fda1a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,6 +1,7 @@ const path = require('path'); const commonjs = require('rollup-plugin-commonjs'); +const json = require('rollup-plugin-json'); const nodeResolve = require('rollup-plugin-node-resolve'); const { terser } = require('rollup-plugin-terser'); const typescript = require('rollup-plugin-typescript2'); @@ -32,6 +33,7 @@ const createEntry = format => ({ }, }, plugins: [ + json({ compact: true }), format === FORMAT.UMD && nodeResolve({ browser: true, preferBuiltins: false }), format === FORMAT.UMD && commonjs(), typescript({ diff --git a/src/clients/KeyknoxClient.ts b/src/clients/KeyknoxClient.ts index 5535208..bc77c4f 100644 --- a/src/clients/KeyknoxClient.ts +++ b/src/clients/KeyknoxClient.ts @@ -1,5 +1,7 @@ import axios, { AxiosResponse } from 'axios'; +import { VirgilAgent } from 'virgil-sdk'; +import { version } from '../../package.json'; import { EncryptedKeyknoxValue, DecryptedKeyknoxValue, KeyknoxValue } from '../entities'; import { KeyknoxClientError } from '../errors'; import { AxiosInstance, AxiosError, AxiosRequestConfig } from '../types'; @@ -11,15 +13,16 @@ interface KeyknoxData { version: string; } -const DEFAULT_BASE_URL = 'https://api.virgilsecurity.com'; - export default class KeyknoxClient implements IKeyknoxClient { - private static readonly AUTHORIZATION_PREFIX = 'Virgil'; + private static readonly API_URL = 'https://api.virgilsecurity.com'; + private static readonly PRODUCT_NAME = 'keyknox'; private readonly axios: AxiosInstance; + private readonly virgilAgent: VirgilAgent; - constructor(apiUrl?: string, axiosInstance?: AxiosInstance) { - this.axios = axiosInstance || axios.create({ baseURL: apiUrl || DEFAULT_BASE_URL }); + constructor(apiUrl?: string, axiosInstance?: AxiosInstance, virgilAgent?: VirgilAgent) { + this.axios = axiosInstance || axios.create({ baseURL: apiUrl || KeyknoxClient.API_URL }); + this.virgilAgent = virgilAgent || new VirgilAgent(KeyknoxClient.PRODUCT_NAME, version); this.axios.interceptors.response.use(undefined, KeyknoxClient.responseErrorHandler); } @@ -34,22 +37,22 @@ export default class KeyknoxClient implements IKeyknoxClient { value, }; const config: AxiosRequestConfig = { - headers: { - Authorization: KeyknoxClient.getAuthorizationHeader(token), - }, + headers: KeyknoxClient.getHeaders({ + virgilAgent: this.virgilAgent, + accessToken: token, + keyknoxHash: previousHash, + }), }; - if (previousHash) { - config.headers['Virgil-Keyknox-Previous-Hash'] = previousHash; - } const response = await this.axios.put('/keyknox/v1', payload, config); return KeyknoxClient.getKeyknoxValue(response); } async pullValue(token: string): Promise { const config = { - headers: { - Authorization: KeyknoxClient.getAuthorizationHeader(token), - }, + headers: KeyknoxClient.getHeaders({ + virgilAgent: this.virgilAgent, + accessToken: token, + }), }; const response = await this.axios.get('/keyknox/v1', config); return KeyknoxClient.getKeyknoxValue(response); @@ -57,9 +60,10 @@ export default class KeyknoxClient implements IKeyknoxClient { async resetValue(token: string): Promise { const config: AxiosRequestConfig = { - headers: { - Authorization: KeyknoxClient.getAuthorizationHeader(token), - }, + headers: KeyknoxClient.getHeaders({ + virgilAgent: this.virgilAgent, + accessToken: token, + }), }; const response = await this.axios.post('/keyknox/v1/reset', null, config); return KeyknoxClient.getKeyknoxValue(response); @@ -75,8 +79,17 @@ export default class KeyknoxClient implements IKeyknoxClient { }; } - private static getAuthorizationHeader(token: string) { - return `${KeyknoxClient.AUTHORIZATION_PREFIX} ${token}`; + private static getHeaders(options: { + virgilAgent: VirgilAgent; + accessToken?: string; + keyknoxHash?: string; + }) { + const { virgilAgent, accessToken, keyknoxHash } = options; + return Object.assign( + { 'Virgil-Agent': virgilAgent.value }, + accessToken && { Authorization: `Virgil ${accessToken}` }, + keyknoxHash && { 'Virgil-Keyknox-Previous-Hash': keyknoxHash }, + ); } private static responseErrorHandler(error: AxiosError) {