@byearlybird/crypto 0.3.0 → 0.4.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/dist/index.d.mts CHANGED
@@ -10,6 +10,16 @@ type AuthPayload = {
10
10
  timestamp: number;
11
11
  bodyHash?: string;
12
12
  };
13
+ type ParseSuccess = {
14
+ ok: true;
15
+ data: AuthPayload;
16
+ signature: string;
17
+ };
18
+ type ParseFailure = {
19
+ ok: false;
20
+ message: string;
21
+ };
22
+ type ParseResult = ParseSuccess | ParseFailure;
13
23
  type ValidateSuccess = {
14
24
  ok: true;
15
25
  data: AuthPayload;
@@ -27,8 +37,8 @@ declare function createAuthHeader(args: {
27
37
  serializedBody?: string;
28
38
  privateKey: CryptoKey;
29
39
  }): Promise<string>;
30
- declare function validateAuthHeader(header: string, options: {
31
- publicKey: CryptoKey;
40
+ declare function parseAuthHeader(header: string): ParseResult;
41
+ declare function validateAuthPayload(payload: AuthPayload, options: {
32
42
  vaultId: string;
33
43
  method: string;
34
44
  path: string;
@@ -57,4 +67,4 @@ declare function bytesToBase64(bytes: Uint8Array): string;
57
67
  declare function base64ToBytes(base64: string): Uint8Array;
58
68
  declare function hashString(value: string): Promise<string>;
59
69
  //#endregion
60
- export { type AuthPayload, type ValidateFailure, type ValidateResult, type ValidateSuccess, base64ToBytes, bytesToBase64, createAuthHeader, decrypt, deriveVaultId, encrypt, exportEncryptionKey, exportPrivateKey, exportPublicKey, generateEncryptionKey, generateSigningKeyPair, hashString, importEncryptionKey, importPrivateKey, importPublicKey, sign, validateAuthHeader, verify };
70
+ export { AuthPayload, ParseFailure, ParseResult, ParseSuccess, ValidateFailure, ValidateResult, ValidateSuccess, base64ToBytes, bytesToBase64, createAuthHeader, decrypt, deriveVaultId, encrypt, exportEncryptionKey, exportPrivateKey, exportPublicKey, generateEncryptionKey, generateSigningKeyPair, hashString, importEncryptionKey, importPrivateKey, importPublicKey, parseAuthHeader, sign, validateAuthPayload, verify };
package/dist/index.mjs CHANGED
@@ -54,54 +54,50 @@ async function createAuthHeader(args) {
54
54
  const payload = await generateAuthPayload(payloadArgs);
55
55
  return makeAuthHeader(payload, await sign(makeCanonicalString(payload), privateKey));
56
56
  }
57
- async function validateAuthHeader(header, options) {
58
- let parsed;
57
+ function parseAuthHeader(header) {
59
58
  try {
60
- parsed = parseAuthHeader(header);
59
+ return parseAuthHeaderInternal(header);
61
60
  } catch {
62
61
  return {
63
62
  ok: false,
64
63
  message: "Malformed auth header"
65
64
  };
66
65
  }
67
- const { signature,...payload } = parsed;
68
- if (parsed.vaultId !== options.vaultId) return {
66
+ }
67
+ async function validateAuthPayload(payload, options) {
68
+ if (payload.vaultId !== options.vaultId) return {
69
69
  ok: false,
70
70
  message: "Vault id mismatch"
71
71
  };
72
- if (parsed.method !== options.method) return {
72
+ if (payload.method !== options.method) return {
73
73
  ok: false,
74
74
  message: "Method mismatch"
75
75
  };
76
- if (parsed.pathWithQuery !== options.path) return {
76
+ if (payload.pathWithQuery !== options.path) return {
77
77
  ok: false,
78
78
  message: "Path mismatch"
79
79
  };
80
- if (Math.abs(Date.now() - parsed.timestamp) > options.ttl) return {
80
+ if (Math.abs(Date.now() - payload.timestamp) > options.ttl) return {
81
81
  ok: false,
82
82
  message: "Expired"
83
83
  };
84
- if (parsed.bodyHash) {
84
+ if (payload.bodyHash) {
85
85
  if (!options.body) return {
86
86
  ok: false,
87
87
  message: "Body hash mismatch"
88
88
  };
89
89
  const serverHash = await hashString(options.body);
90
- if (parsed.bodyHash !== serverHash) return {
90
+ if (payload.bodyHash !== serverHash) return {
91
91
  ok: false,
92
92
  message: "Body hash mismatch"
93
93
  };
94
94
  }
95
- if (!await verify(makeCanonicalString(payload), signature, options.publicKey)) return {
96
- ok: false,
97
- message: "Invalid signature"
98
- };
99
95
  return {
100
96
  ok: true,
101
97
  data: payload
102
98
  };
103
99
  }
104
- function parseAuthHeader(header) {
100
+ function parseAuthHeaderInternal(header) {
105
101
  const spaceIdx = header.indexOf(" ");
106
102
  if (spaceIdx === -1) throw new Error("Malformed auth header: missing scheme separator");
107
103
  const scheme = header.slice(0, spaceIdx);
@@ -122,18 +118,21 @@ function parseAuthHeader(header) {
122
118
  if (!vid || !n || !m || !p || !t || !sig) throw new Error("Malformed auth header: missing required params");
123
119
  const timestamp = Number(t);
124
120
  if (Number.isNaN(timestamp)) throw new Error("Malformed auth header: timestamp is not a number");
125
- const result = {
121
+ const data = {
126
122
  scheme,
127
123
  vaultId: vid,
128
124
  nonce: n,
129
125
  method: m,
130
126
  pathWithQuery: p,
131
- timestamp,
132
- signature: sig
127
+ timestamp
133
128
  };
134
129
  const bh = params.get("bh");
135
- if (bh) result.bodyHash = bh;
136
- return result;
130
+ if (bh) data.bodyHash = bh;
131
+ return {
132
+ ok: true,
133
+ data,
134
+ signature: sig
135
+ };
137
136
  }
138
137
  async function generateAuthPayload(args) {
139
138
  const { serializedBody,...rest } = args;
@@ -211,4 +210,4 @@ async function decrypt(encoded, key) {
211
210
  }
212
211
 
213
212
  //#endregion
214
- export { base64ToBytes, bytesToBase64, createAuthHeader, decrypt, deriveVaultId, encrypt, exportEncryptionKey, exportPrivateKey, exportPublicKey, generateEncryptionKey, generateSigningKeyPair, hashString, importEncryptionKey, importPrivateKey, importPublicKey, sign, validateAuthHeader, verify };
213
+ export { base64ToBytes, bytesToBase64, createAuthHeader, decrypt, deriveVaultId, encrypt, exportEncryptionKey, exportPrivateKey, exportPublicKey, generateEncryptionKey, generateSigningKeyPair, hashString, importEncryptionKey, importPrivateKey, importPublicKey, parseAuthHeader, sign, validateAuthPayload, verify };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byearlybird/crypto",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Lightweight E2EE toolkit for web apps - zero dependencies + vault key security",
5
5
  "type": "module",
6
6
  "license": "MIT",