vanishly 0.0.0-stage → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -2
- package/client.js +173 -0
- package/crypto.js +185 -0
- package/index.d.ts +46 -0
- package/index.js +4 -0
- package/package.json +14 -4
package/README.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# vanishly (JavaScript / TypeScript)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Client for [Vanishly](https://github.com/DennisPohlmann/Vanishly), one-time
|
|
4
|
+
secrets, for browsers, Node.js 20+, Deno and Bun (Web Crypto, `fetch`, no
|
|
5
|
+
dependencies; types included).
|
|
6
|
+
|
|
7
|
+
Full documentation: [docs/CLIENTS.md](https://github.com/DennisPohlmann/Vanishly/blob/main/docs/CLIENTS.md). End-to-end encrypted (crypto format v1, the same as the web pages): the secret, the key and the passphrase never reach the server; the key is only in the link's `#` part.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install vanishly
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { Client } from 'vanishly';
|
|
15
|
+
|
|
16
|
+
const c = new Client('https://ots.example.com', { token: undefined }); // vnly_... with sign-in
|
|
17
|
+
const res = await c.create('s3cret', '', { ttl: 3600, views: 1 });
|
|
18
|
+
console.log(res.shareUrl, res.receiptUrl);
|
|
19
|
+
const { secret, viewsLeft } = await c.open(res.shareUrl);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Every endpoint has a method (`receipt`, `burn`, `me`, `tokens`, `audit`,
|
|
23
|
+
...); `request(method, path, body)` calls any other. Errors are `ApiError`
|
|
24
|
+
(`status`, `error`, `message`). `vanishly/crypto` is the crypto alone.
|
package/client.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// HTTP client for the Vanishly API (/api/v1), with fetch.
|
|
2
|
+
import { decryptSecret, encryptSecret, unlockKey, verifierFor } from './crypto.js';
|
|
3
|
+
|
|
4
|
+
export const VERSION = '4.2.0';
|
|
5
|
+
const LINK = /^(https?:\/\/[^#]+?)\/s\/([A-Za-z0-9_-]{22})#([A-Za-z0-9_-]{43})$/;
|
|
6
|
+
const OPTIONS = ['ttl', 'views', 'confirmed', 'recipients', 'recipientGroups', 'responsible', 'responsibleGroups', 'comment'];
|
|
7
|
+
|
|
8
|
+
/** An answer other than 2xx. */
|
|
9
|
+
export class ApiError extends Error {
|
|
10
|
+
/** @param {number} status @param {string} error @param {string} message @param {any} body */
|
|
11
|
+
constructor(status, error, message, body) {
|
|
12
|
+
super(`${status} ${error}: ${message}`);
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.error = error;
|
|
15
|
+
this.body = body ?? {};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Splits https://host/s/<id>#<key> into { base, id, key }. @param {string} link */
|
|
20
|
+
export function parseLink(link) {
|
|
21
|
+
const m = LINK.exec(String(link).trim());
|
|
22
|
+
if (!m) throw new Error('not a secret link: https://.../s/<id>#<key>');
|
|
23
|
+
return { base: m[1], id: m[2], key: m[3] };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @param {Record<string, any>} o */
|
|
27
|
+
function options(o = {}) {
|
|
28
|
+
const bad = Object.keys(o).filter((k) => !OPTIONS.includes(k));
|
|
29
|
+
if (bad.length) throw new TypeError(`unknown option(s): ${bad.join(', ')}`);
|
|
30
|
+
return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined && v !== null));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const seg = (/** @type {string} */ s) => encodeURIComponent(s);
|
|
34
|
+
|
|
35
|
+
export class Client {
|
|
36
|
+
/**
|
|
37
|
+
* @param {string} baseUrl e.g. https://ots.example.com
|
|
38
|
+
* @param {{ token?: string, runId?: string, comment?: string, onBehalfOf?: string, clientName?: string, fetch?: typeof fetch }} [opts]
|
|
39
|
+
*/
|
|
40
|
+
constructor(baseUrl, opts = {}) {
|
|
41
|
+
if (!/^https?:\/\//.test(baseUrl || '')) throw new Error('baseUrl must start with https:// or http://');
|
|
42
|
+
this.baseUrl = baseUrl.replace(/\/+$/, '');
|
|
43
|
+
this.opts = opts;
|
|
44
|
+
/** Set when the server recommends a newer client. @type {string | null} */
|
|
45
|
+
this.recommendedVersion = null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Any API call; path is below /api ("/v1/config").
|
|
50
|
+
* @param {string} method @param {string} path @param {any} [body] @param {Record<string, any>} [query] @param {{ raw?: boolean }} [o]
|
|
51
|
+
*/
|
|
52
|
+
async request(method, path, body, query, o = {}) {
|
|
53
|
+
const q = new URLSearchParams(Object.entries(query || {}).filter(([, v]) => v !== undefined && v !== null && v !== '').map(([k, v]) => [k, String(v)]));
|
|
54
|
+
const url = `${this.baseUrl}/api${path}${q.size ? `?${q}` : ''}`;
|
|
55
|
+
const name = this.opts.clientName || 'js';
|
|
56
|
+
/** @type {Record<string, string>} */
|
|
57
|
+
const headers = { Accept: 'application/json', 'X-Vanishly-Client': `${name}/${VERSION}` };
|
|
58
|
+
if (body !== undefined) headers['Content-Type'] = 'application/json';
|
|
59
|
+
if (this.opts.token) headers.Authorization = `Bearer ${this.opts.token}`;
|
|
60
|
+
if (this.opts.runId) headers['X-Run-Id'] = this.opts.runId;
|
|
61
|
+
if (this.opts.comment) headers['X-Audit-Comment'] = encodeURIComponent(this.opts.comment);
|
|
62
|
+
if (this.opts.onBehalfOf) headers['X-On-Behalf-Of'] = this.opts.onBehalfOf;
|
|
63
|
+
const res = await (this.opts.fetch || fetch)(url, { method: method.toUpperCase(), headers, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
64
|
+
const rec = res.headers.get('x-vanishly-client-recommended');
|
|
65
|
+
if (rec) this.recommendedVersion = rec;
|
|
66
|
+
const text = await res.text();
|
|
67
|
+
if (!res.ok) {
|
|
68
|
+
let b = {};
|
|
69
|
+
try {
|
|
70
|
+
b = JSON.parse(text);
|
|
71
|
+
} catch {}
|
|
72
|
+
throw new ApiError(res.status, b.error || 'http_error', b.message || res.statusText, b);
|
|
73
|
+
}
|
|
74
|
+
if (o.raw) return text;
|
|
75
|
+
return text ? JSON.parse(text) : null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Encrypts here and stores it; adds shareUrl and receiptUrl. @param {string} secret @param {string} [passphrase] @param {Record<string, any>} [opts] */
|
|
79
|
+
async create(secret, passphrase = '', opts = {}) {
|
|
80
|
+
const { key, payload } = await encryptSecret(secret, passphrase);
|
|
81
|
+
const res = await this.createSecret({ ...JSON.parse(JSON.stringify(payload)), ...options(opts) });
|
|
82
|
+
res.shareUrl = `${this.baseUrl}${res.secretPath}#${key}`;
|
|
83
|
+
res.receiptUrl = res.receiptPath ? `${this.baseUrl}${res.receiptPath}#${key}` : null;
|
|
84
|
+
return res;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Takes one view and decrypts here. @param {string} link @param {string} [passphrase] */
|
|
88
|
+
async open(link, passphrase = '') {
|
|
89
|
+
const { base, id, key } = parseLink(link);
|
|
90
|
+
if (base !== this.baseUrl) throw new Error(`the link belongs to ${base}, not to ${this.baseUrl}`);
|
|
91
|
+
let pk;
|
|
92
|
+
let verifier;
|
|
93
|
+
if (passphrase) {
|
|
94
|
+
const meta = await this.secretMeta(id);
|
|
95
|
+
if (meta.passphrase) {
|
|
96
|
+
pk = await unlockKey(passphrase, meta);
|
|
97
|
+
verifier = await verifierFor(pk, meta);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const payload = await this.reveal(id, verifier);
|
|
101
|
+
return { secret: await decryptSecret(payload, key, pk), viewsLeft: payload.viewsLeft };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
index() { return this.request('GET', '/v1'); }
|
|
105
|
+
config() { return this.request('GET', '/v1/config'); }
|
|
106
|
+
downloads() { return this.request('GET', '/v1/downloads'); }
|
|
107
|
+
/** @param {Record<string, any>} payload */
|
|
108
|
+
createSecret(payload) { return this.request('POST', '/v1/secrets', payload); }
|
|
109
|
+
/** Server-side encryption: the server sees the secret. @param {string} secret @param {string} [passphrase] @param {Record<string, any>} [opts] */
|
|
110
|
+
share(secret, passphrase = '', opts = {}) { return this.request('POST', '/v1/share', { secret, ...(passphrase ? { passphrase } : {}), ...options(opts) }); }
|
|
111
|
+
/** Server-side reveal: the server sees key and secret. @param {string} link @param {string} [passphrase] */
|
|
112
|
+
openOnServer(link, passphrase = '') { return this.request('POST', '/v1/open', { link, ...(passphrase ? { passphrase } : {}) }); }
|
|
113
|
+
/** @param {string} id */
|
|
114
|
+
secretMeta(id) { return this.request('GET', `/v1/secrets/${seg(id)}`); }
|
|
115
|
+
/** @param {string} id @param {string} [verifier] */
|
|
116
|
+
reveal(id, verifier) { return this.request('POST', `/v1/secrets/${seg(id)}/reveal`, verifier ? { verifier } : {}); }
|
|
117
|
+
/** @param {string} id */
|
|
118
|
+
receipt(id) { return this.request('GET', `/v1/receipts/${seg(id)}`); }
|
|
119
|
+
/** @param {string} id */
|
|
120
|
+
burn(id) { return this.request('POST', `/v1/receipts/${seg(id)}/burn`, {}); }
|
|
121
|
+
me() { return this.request('GET', '/v1/me'); }
|
|
122
|
+
/** @param {Record<string, any>} [q] */
|
|
123
|
+
mySecrets(q) { return this.request('GET', '/v1/me/secrets', undefined, q); }
|
|
124
|
+
/** @param {string} ref */
|
|
125
|
+
mySecret(ref) { return this.request('GET', `/v1/me/secrets/${seg(ref)}`); }
|
|
126
|
+
/** @param {string} ref */
|
|
127
|
+
burnMySecret(ref) { return this.request('POST', `/v1/me/secrets/${seg(ref)}/burn`, {}); }
|
|
128
|
+
groups(q = '') { return this.request('GET', '/v1/groups', undefined, { q }); }
|
|
129
|
+
people(q = '') { return this.request('GET', '/v1/people', undefined, { q }); }
|
|
130
|
+
tokens() { return this.request('GET', '/v1/tokens'); }
|
|
131
|
+
/** @param {Record<string, any>} body */
|
|
132
|
+
createToken(body) { return this.request('POST', '/v1/tokens', body); }
|
|
133
|
+
/** @param {string} id */
|
|
134
|
+
revokeToken(id) { return this.request('POST', `/v1/tokens/${seg(id)}/revoke`, {}); }
|
|
135
|
+
adminStatus() { return this.request('GET', '/v1/admin/status'); }
|
|
136
|
+
adminTokens() { return this.request('GET', '/v1/admin/tokens'); }
|
|
137
|
+
/** @param {string} id */
|
|
138
|
+
adminRevokeToken(id) { return this.request('POST', `/v1/admin/tokens/${seg(id)}/revoke`, {}); }
|
|
139
|
+
/** format csv or jsonl comes back as text. @param {Record<string, any>} [q] */
|
|
140
|
+
audit(q = {}) { return this.request('GET', '/v1/admin/audit', undefined, q, { raw: q.format === 'csv' || q.format === 'jsonl' }); }
|
|
141
|
+
auditVerify() { return this.request('GET', '/v1/admin/audit/verify'); }
|
|
142
|
+
tokenPolicies() { return this.request('GET', '/v1/admin/token-policies'); }
|
|
143
|
+
/** @param {Record<string, any>} body */
|
|
144
|
+
setTokenPolicy(body) { return this.request('POST', '/v1/admin/token-policies', body); }
|
|
145
|
+
/** @param {Record<string, any>} [q] */
|
|
146
|
+
adminSecrets(q) { return this.request('GET', '/v1/admin/secrets', undefined, q); }
|
|
147
|
+
/** @param {string} ref */
|
|
148
|
+
adminSecret(ref) { return this.request('GET', `/v1/admin/secrets/${seg(ref)}`); }
|
|
149
|
+
/** @param {string} ref */
|
|
150
|
+
adminBurn(ref) { return this.request('POST', `/v1/admin/secrets/${seg(ref)}/burn`, {}); }
|
|
151
|
+
serviceAccounts() { return this.request('GET', '/v1/admin/service-accounts'); }
|
|
152
|
+
/** @param {Record<string, any>} body */
|
|
153
|
+
createServiceAccount(body) { return this.request('POST', '/v1/admin/service-accounts', body); }
|
|
154
|
+
/** @param {string} name @param {Record<string, any>} body */
|
|
155
|
+
changeServiceAccount(name, body) { return this.request('POST', `/v1/admin/service-accounts/${seg(name)}`, body); }
|
|
156
|
+
/** @param {string} name */
|
|
157
|
+
deleteServiceAccount(name) { return this.request('POST', `/v1/admin/service-accounts/${seg(name)}/delete`, {}); }
|
|
158
|
+
/** @param {string} name */
|
|
159
|
+
serviceAccountTokens(name) { return this.request('GET', `/v1/admin/service-accounts/${seg(name)}/tokens`); }
|
|
160
|
+
/** @param {string} name @param {Record<string, any>} [body] */
|
|
161
|
+
issueServiceAccountToken(name, body = {}) { return this.request('POST', `/v1/admin/service-accounts/${seg(name)}/tokens`, body); }
|
|
162
|
+
/** @param {string} name @param {string} id @param {Record<string, any>} [body] */
|
|
163
|
+
rotateServiceAccountToken(name, id, body = {}) { return this.request('POST', `/v1/admin/service-accounts/${seg(name)}/tokens/${seg(id)}/rotate`, body); }
|
|
164
|
+
users() { return this.request('GET', '/v1/admin/users'); }
|
|
165
|
+
/** @param {Record<string, any>} body */
|
|
166
|
+
createUser(body) { return this.request('POST', '/v1/admin/users', body); }
|
|
167
|
+
/** @param {string} user @param {Record<string, any>} body */
|
|
168
|
+
changeUser(user, body) { return this.request('POST', `/v1/admin/users/${seg(user)}`, body); }
|
|
169
|
+
/** @param {string} user @param {Record<string, any>} [body] */
|
|
170
|
+
resetUserPassword(user, body = {}) { return this.request('POST', `/v1/admin/users/${seg(user)}/password`, body); }
|
|
171
|
+
/** @param {string} user */
|
|
172
|
+
deleteUser(user) { return this.request('POST', `/v1/admin/users/${seg(user)}/delete`, {}); }
|
|
173
|
+
}
|
package/crypto.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// Vanishly - end-to-end encryption of a secret, shared by the browser
|
|
2
|
+
// (public/js/app.js) and the server (POST /api/v1/share).
|
|
3
|
+
//
|
|
4
|
+
// Format v1:
|
|
5
|
+
// key 32 random bytes, only ever in the URL fragment (#...), which
|
|
6
|
+
// browsers never send to the server.
|
|
7
|
+
// passphrase optional: pk = PBKDF2-SHA256(passphrase, salt, iterations).
|
|
8
|
+
// encKey HKDF-SHA256(ikm = key || pk, salt, info "vanishly v1 enc")
|
|
9
|
+
// verifier HKDF-SHA256(ikm = pk, salt, info "vanishly v1 auth"); the
|
|
10
|
+
// server stores only SHA-256(verifier) and checks it before it
|
|
11
|
+
// hands out (and deletes) the ciphertext, so a wrong passphrase
|
|
12
|
+
// does not burn the secret.
|
|
13
|
+
// ciphertext AES-256-GCM(encKey, iv 12 bytes, aad "vanishly v1").
|
|
14
|
+
// The server holds the ciphertext and the verifier hash, never the key, so
|
|
15
|
+
// it cannot read a secret even with the passphrase.
|
|
16
|
+
|
|
17
|
+
export const VERSION = 1;
|
|
18
|
+
export const PBKDF2_ITERATIONS = 600000;
|
|
19
|
+
|
|
20
|
+
const enc = new TextEncoder();
|
|
21
|
+
const dec = new TextDecoder();
|
|
22
|
+
const subtle = () => globalThis.crypto.subtle;
|
|
23
|
+
|
|
24
|
+
const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
25
|
+
|
|
26
|
+
/** @param {Uint8Array} bytes */
|
|
27
|
+
export function toB64url(bytes) {
|
|
28
|
+
let out = '';
|
|
29
|
+
let i = 0;
|
|
30
|
+
for (; i + 2 < bytes.length; i += 3) {
|
|
31
|
+
const n = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
|
|
32
|
+
out += B64[(n >> 18) & 63] + B64[(n >> 12) & 63] + B64[(n >> 6) & 63] + B64[n & 63];
|
|
33
|
+
}
|
|
34
|
+
const rest = bytes.length - i;
|
|
35
|
+
if (rest === 1) {
|
|
36
|
+
const n = bytes[i] << 16;
|
|
37
|
+
out += B64[(n >> 18) & 63] + B64[(n >> 12) & 63];
|
|
38
|
+
} else if (rest === 2) {
|
|
39
|
+
const n = (bytes[i] << 16) | (bytes[i + 1] << 8);
|
|
40
|
+
out += B64[(n >> 18) & 63] + B64[(n >> 12) & 63] + B64[(n >> 6) & 63];
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @param {string} str */
|
|
46
|
+
export function fromB64url(str) {
|
|
47
|
+
if (typeof str !== 'string' || !/^[A-Za-z0-9_-]*$/.test(str) || str.length % 4 === 1) {
|
|
48
|
+
throw new Error('invalid base64url');
|
|
49
|
+
}
|
|
50
|
+
const out = new Uint8Array(Math.floor((str.length * 3) / 4));
|
|
51
|
+
let o = 0;
|
|
52
|
+
for (let i = 0; i < str.length; i += 4) {
|
|
53
|
+
const c = [0, 1, 2, 3].map((k) => (i + k < str.length ? B64.indexOf(str[i + k]) : 0));
|
|
54
|
+
const n = (c[0] << 18) | (c[1] << 12) | (c[2] << 6) | c[3];
|
|
55
|
+
if (o < out.length) out[o++] = (n >> 16) & 255;
|
|
56
|
+
if (o < out.length) out[o++] = (n >> 8) & 255;
|
|
57
|
+
if (o < out.length) out[o++] = n & 255;
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** @param {number} n */
|
|
63
|
+
export function randomBytes(n) {
|
|
64
|
+
return globalThis.crypto.getRandomValues(new Uint8Array(n));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @param {Uint8Array[]} parts */
|
|
68
|
+
function concat(...parts) {
|
|
69
|
+
const out = new Uint8Array(parts.reduce((s, p) => s + p.length, 0));
|
|
70
|
+
let o = 0;
|
|
71
|
+
for (const p of parts) {
|
|
72
|
+
out.set(p, o);
|
|
73
|
+
o += p.length;
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param {Uint8Array} ikm
|
|
80
|
+
* @param {Uint8Array} salt
|
|
81
|
+
* @param {string} info
|
|
82
|
+
* @param {boolean} asKey
|
|
83
|
+
*/
|
|
84
|
+
async function hkdf(ikm, salt, info, asKey) {
|
|
85
|
+
const base = await subtle().importKey('raw', /** @type {BufferSource} */ (ikm), 'HKDF', false, ['deriveBits', 'deriveKey']);
|
|
86
|
+
const params = { name: 'HKDF', hash: 'SHA-256', salt: /** @type {BufferSource} */ (salt), info: enc.encode(info) };
|
|
87
|
+
if (asKey) {
|
|
88
|
+
return subtle().deriveKey(params, base, { name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']);
|
|
89
|
+
}
|
|
90
|
+
return new Uint8Array(await subtle().deriveBits(params, base, 256));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @param {string} passphrase
|
|
95
|
+
* @param {Uint8Array} salt
|
|
96
|
+
* @param {number} iterations
|
|
97
|
+
*/
|
|
98
|
+
async function passKey(passphrase, salt, iterations) {
|
|
99
|
+
const base = await subtle().importKey('raw', enc.encode(passphrase), 'PBKDF2', false, ['deriveBits']);
|
|
100
|
+
return new Uint8Array(
|
|
101
|
+
await subtle().deriveBits({ name: 'PBKDF2', hash: 'SHA-256', salt: /** @type {BufferSource} */ (salt), iterations }, base, 256),
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Derives the encryption key and, with a passphrase key, the verifier.
|
|
107
|
+
* @param {Uint8Array} key
|
|
108
|
+
* @param {Uint8Array} salt
|
|
109
|
+
* @param {Uint8Array} pk empty without a passphrase
|
|
110
|
+
*/
|
|
111
|
+
async function derive(key, salt, pk) {
|
|
112
|
+
const encKey = /** @type {CryptoKey} */ (await hkdf(concat(key, pk), salt, 'vanishly v1 enc', true));
|
|
113
|
+
const verifier = pk.length
|
|
114
|
+
? toB64url(/** @type {Uint8Array} */ (await hkdf(pk, salt, 'vanishly v1 auth', false)))
|
|
115
|
+
: '';
|
|
116
|
+
return { encKey, verifier };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Encrypts a secret. Returns what goes to the server (payload) and the key
|
|
121
|
+
* for the URL fragment.
|
|
122
|
+
* @param {string} plaintext
|
|
123
|
+
* @param {string} [passphrase]
|
|
124
|
+
*/
|
|
125
|
+
export async function encryptSecret(plaintext, passphrase = '') {
|
|
126
|
+
const key = randomBytes(32);
|
|
127
|
+
const salt = randomBytes(16);
|
|
128
|
+
const iv = randomBytes(12);
|
|
129
|
+
const iterations = PBKDF2_ITERATIONS;
|
|
130
|
+
const pk = passphrase ? await passKey(passphrase, salt, iterations) : new Uint8Array(0);
|
|
131
|
+
const { encKey, verifier } = await derive(key, salt, pk);
|
|
132
|
+
const ct = new Uint8Array(
|
|
133
|
+
await subtle().encrypt(
|
|
134
|
+
{ name: 'AES-GCM', iv, additionalData: enc.encode('vanishly v1') },
|
|
135
|
+
encKey,
|
|
136
|
+
enc.encode(plaintext),
|
|
137
|
+
),
|
|
138
|
+
);
|
|
139
|
+
return {
|
|
140
|
+
key: toB64url(key),
|
|
141
|
+
payload: {
|
|
142
|
+
v: VERSION,
|
|
143
|
+
ciphertext: toB64url(ct),
|
|
144
|
+
iv: toB64url(iv),
|
|
145
|
+
salt: toB64url(salt),
|
|
146
|
+
iterations,
|
|
147
|
+
verifier: verifier || undefined,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Stretches a passphrase once; the result unlocks (verifierFor) and
|
|
154
|
+
* decrypts (decryptSecret) a passphrase-protected secret.
|
|
155
|
+
* @param {string} passphrase
|
|
156
|
+
* @param {{ salt: string, iterations: number }} meta
|
|
157
|
+
*/
|
|
158
|
+
export async function unlockKey(passphrase, meta) {
|
|
159
|
+
return passKey(passphrase, fromB64url(meta.salt), meta.iterations);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The verifier the reveal API checks for a passphrase-protected secret.
|
|
164
|
+
* @param {Uint8Array} pk from unlockKey
|
|
165
|
+
* @param {{ salt: string }} meta
|
|
166
|
+
*/
|
|
167
|
+
export async function verifierFor(pk, meta) {
|
|
168
|
+
return toB64url(/** @type {Uint8Array} */ (await hkdf(pk, fromB64url(meta.salt), 'vanishly v1 auth', false)));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Decrypts a payload returned by the reveal API.
|
|
173
|
+
* @param {{ ciphertext: string, iv: string, salt: string }} payload
|
|
174
|
+
* @param {string} key base64url key from the URL fragment
|
|
175
|
+
* @param {Uint8Array} [pk] from unlockKey, for a passphrase-protected secret
|
|
176
|
+
*/
|
|
177
|
+
export async function decryptSecret(payload, key, pk = new Uint8Array(0)) {
|
|
178
|
+
const { encKey } = await derive(fromB64url(key), fromB64url(payload.salt), pk);
|
|
179
|
+
const pt = await subtle().decrypt(
|
|
180
|
+
{ name: 'AES-GCM', iv: fromB64url(payload.iv), additionalData: enc.encode('vanishly v1') },
|
|
181
|
+
encKey,
|
|
182
|
+
fromB64url(payload.ciphertext),
|
|
183
|
+
);
|
|
184
|
+
return dec.decode(pt);
|
|
185
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export const VERSION: number;
|
|
2
|
+
export const PBKDF2_ITERATIONS: number;
|
|
3
|
+
export interface Payload { v?: number; ciphertext: string; iv: string; salt: string; iterations: number; verifier?: string }
|
|
4
|
+
export function toB64url(bytes: Uint8Array): string;
|
|
5
|
+
export function fromB64url(str: string): Uint8Array;
|
|
6
|
+
export function randomBytes(n: number): Uint8Array;
|
|
7
|
+
export function encryptSecret(plaintext: string, passphrase?: string): Promise<{ key: string; payload: Payload }>;
|
|
8
|
+
export function unlockKey(passphrase: string, meta: { salt: string; iterations: number }): Promise<Uint8Array>;
|
|
9
|
+
export function verifierFor(pk: Uint8Array, meta: { salt: string }): Promise<string>;
|
|
10
|
+
export function decryptSecret(payload: { ciphertext: string; iv: string; salt: string }, key: string, pk?: Uint8Array): Promise<string>;
|
|
11
|
+
|
|
12
|
+
export interface SecretOptions {
|
|
13
|
+
ttl?: number; views?: number; confirmed?: boolean; recipients?: string[]; recipientGroups?: string[];
|
|
14
|
+
responsible?: string[]; responsibleGroups?: string[]; comment?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface ClientOptions { token?: string; runId?: string; comment?: string; onBehalfOf?: string; clientName?: string; fetch?: typeof fetch }
|
|
17
|
+
type Json = any;
|
|
18
|
+
export class ApiError extends Error { status: number; error: string; body: Record<string, any> }
|
|
19
|
+
export function parseLink(link: string): { base: string; id: string; key: string };
|
|
20
|
+
export class Client {
|
|
21
|
+
constructor(baseUrl: string, opts?: ClientOptions);
|
|
22
|
+
baseUrl: string;
|
|
23
|
+
recommendedVersion: string | null;
|
|
24
|
+
request(method: string, path: string, body?: unknown, query?: Record<string, unknown>, o?: { raw?: boolean }): Promise<Json>;
|
|
25
|
+
create(secret: string, passphrase?: string, opts?: SecretOptions): Promise<Json & { shareUrl: string; receiptUrl: string | null }>;
|
|
26
|
+
open(link: string, passphrase?: string): Promise<{ secret: string; viewsLeft: number }>;
|
|
27
|
+
index(): Promise<Json>; config(): Promise<Json>; downloads(): Promise<Json>;
|
|
28
|
+
createSecret(payload: Payload & SecretOptions): Promise<Json>;
|
|
29
|
+
share(secret: string, passphrase?: string, opts?: SecretOptions): Promise<Json>;
|
|
30
|
+
openOnServer(link: string, passphrase?: string): Promise<{ secret: string; viewsLeft: number }>;
|
|
31
|
+
secretMeta(id: string): Promise<Json>; reveal(id: string, verifier?: string): Promise<Json>;
|
|
32
|
+
receipt(id: string): Promise<Json>; burn(id: string): Promise<Json>;
|
|
33
|
+
me(): Promise<Json>; mySecrets(q?: Record<string, unknown>): Promise<Json>; mySecret(ref: string): Promise<Json>; burnMySecret(ref: string): Promise<Json>;
|
|
34
|
+
groups(q?: string): Promise<Json>; people(q?: string): Promise<Json>;
|
|
35
|
+
tokens(): Promise<Json>; createToken(body: Record<string, unknown>): Promise<Json>; revokeToken(id: string): Promise<Json>;
|
|
36
|
+
adminStatus(): Promise<Json>; adminTokens(): Promise<Json>; adminRevokeToken(id: string): Promise<Json>;
|
|
37
|
+
audit(q?: Record<string, unknown>): Promise<Json>; auditVerify(): Promise<Json>;
|
|
38
|
+
tokenPolicies(): Promise<Json>; setTokenPolicy(body: Record<string, unknown>): Promise<Json>;
|
|
39
|
+
adminSecrets(q?: Record<string, unknown>): Promise<Json>; adminSecret(ref: string): Promise<Json>; adminBurn(ref: string): Promise<Json>;
|
|
40
|
+
serviceAccounts(): Promise<Json>; createServiceAccount(body: Record<string, unknown>): Promise<Json>;
|
|
41
|
+
changeServiceAccount(name: string, body: Record<string, unknown>): Promise<Json>; deleteServiceAccount(name: string): Promise<Json>;
|
|
42
|
+
serviceAccountTokens(name: string): Promise<Json>; issueServiceAccountToken(name: string, body?: Record<string, unknown>): Promise<Json>;
|
|
43
|
+
rotateServiceAccountToken(name: string, id: string, body?: Record<string, unknown>): Promise<Json>;
|
|
44
|
+
users(): Promise<Json>; createUser(body: Record<string, unknown>): Promise<Json>; changeUser(user: string, body: Record<string, unknown>): Promise<Json>;
|
|
45
|
+
resetUserPassword(user: string, body?: Record<string, unknown>): Promise<Json>; deleteUser(user: string): Promise<Json>;
|
|
46
|
+
}
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vanishly",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "4.2.0",
|
|
4
|
+
"description": "Client for Vanishly: end-to-end encrypted one-time secrets and the whole API (browsers, Node.js 20+, Deno, Bun)",
|
|
5
|
+
"license": "GPL-3.0-or-later",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": { "types": "./index.d.ts", "default": "./index.js" },
|
|
9
|
+
"./crypto": { "types": "./index.d.ts", "default": "./crypto.js" }
|
|
10
|
+
},
|
|
11
|
+
"files": ["index.js", "client.js", "crypto.js", "index.d.ts", "README.md"],
|
|
12
|
+
"engines": { "node": ">=20" },
|
|
13
|
+
"keywords": ["secrets", "one-time", "end-to-end encryption", "vanishly"],
|
|
14
|
+
"repository": { "type": "git", "url": "git+https://github.com/DennisPohlmann/Vanishly.git", "directory": "clients/js" },
|
|
15
|
+
"scripts": { "test": "node --test test/*.test.js" }
|
|
16
|
+
}
|