Skip to content

Commit

Permalink
Merge pull request #23 from VirgilSecurity/v0.3.1
Browse files Browse the repository at this point in the history
v0.3.1
  • Loading branch information
vadimavdeev authored Oct 9, 2019
2 parents c614e33 + b478ece commit 24e6366
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<script src="https://unpkg.com/@virgilsecurity/keyknox/dist/keyknox.umd.js"></script>
<script src="https://unpkg.com/@virgilsecurity/keyknox@0.3.1/dist/keyknox.umd.js"></script>
```

You will also need to add `virgil-crypto` and `virgil-sdk` scripts.
```html
<script src="https://unpkg.com/virgil-crypto@next/dist/browser.umd.js"></script>
<script src="https://unpkg.com/virgil-sdk@next/dist/virgil-sdk.browser.umd.js"></script>
<script src="https://unpkg.com/virgil-crypto@4.0.0-alpha.13/dist/browser.umd.js"></script>
<script src="https://unpkg.com/virgil-sdk@6.0.0-alpha.4/dist/virgil-sdk.browser.umd.js"></script>
```

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.
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -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"
}
}
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -32,6 +33,7 @@ const createEntry = format => ({
},
},
plugins: [
json({ compact: true }),
format === FORMAT.UMD && nodeResolve({ browser: true, preferBuiltins: false }),
format === FORMAT.UMD && commonjs(),
typescript({
Expand Down
51 changes: 32 additions & 19 deletions src/clients/KeyknoxClient.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}

Expand All @@ -34,32 +37,33 @@ 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<KeyknoxData>('/keyknox/v1', payload, config);
return KeyknoxClient.getKeyknoxValue(response);
}

async pullValue(token: string): Promise<EncryptedKeyknoxValue> {
const config = {
headers: {
Authorization: KeyknoxClient.getAuthorizationHeader(token),
},
headers: KeyknoxClient.getHeaders({
virgilAgent: this.virgilAgent,
accessToken: token,
}),
};
const response = await this.axios.get<KeyknoxData>('/keyknox/v1', config);
return KeyknoxClient.getKeyknoxValue(response);
}

async resetValue(token: string): Promise<DecryptedKeyknoxValue> {
const config: AxiosRequestConfig = {
headers: {
Authorization: KeyknoxClient.getAuthorizationHeader(token),
},
headers: KeyknoxClient.getHeaders({
virgilAgent: this.virgilAgent,
accessToken: token,
}),
};
const response = await this.axios.post<KeyknoxData>('/keyknox/v1/reset', null, config);
return KeyknoxClient.getKeyknoxValue(response);
Expand All @@ -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) {
Expand Down

0 comments on commit 24e6366

Please sign in to comment.