@request-shield/core 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +33 -2
- package/dist/axios.d.ts +21 -0
- package/dist/axios.d.ts.map +1 -0
- package/dist/axios.js +53 -0
- package/dist/axios.js.map +1 -0
- package/dist/client.d.ts +76 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +177 -0
- package/dist/client.js.map +1 -0
- package/dist/fetch.d.ts +7 -0
- package/dist/fetch.d.ts.map +1 -0
- package/dist/fetch.js +41 -0
- package/dist/fetch.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/store.d.ts +41 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +68 -0
- package/dist/store.js.map +1 -0
- package/package.json +42 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 karammm
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @request-shield/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Signs your API requests with a **non-exportable WebCrypto key** so they can't be replayed, tampered with,
|
|
4
|
+
or forged from cURL/Postman. Framework-agnostic: includes a signed `fetch` and an axios adapter.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm install @request-shield/core
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { ShieldClient, createShieldFetch, attachShieldToAxios } from '@request-shield/core';
|
|
12
|
+
|
|
13
|
+
const shield = new ShieldClient({
|
|
14
|
+
apiBase: 'https://api.example.com',
|
|
15
|
+
getAttestationToken: () => getTurnstileToken(), // bot check at registration
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// fetch
|
|
19
|
+
const signedFetch = createShieldFetch(shield);
|
|
20
|
+
await signedFetch('https://api.example.com/api/orders');
|
|
21
|
+
|
|
22
|
+
// or axios
|
|
23
|
+
import axios from 'axios';
|
|
24
|
+
const api = axios.create({ baseURL: 'https://api.example.com' });
|
|
25
|
+
attachShieldToAxios(api, shield, axios.getAdapter);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Your server must verify the signatures. A ready-made Go middleware is included:
|
|
29
|
+
`go get github.com/karammm/request-shield/server/go`.
|
|
30
|
+
|
|
31
|
+
Using a framework? See [`@request-shield/angular`](https://www.npmjs.com/package/@request-shield/angular) and
|
|
32
|
+
[`@request-shield/react`](https://www.npmjs.com/package/@request-shield/react).
|
|
33
|
+
|
|
34
|
+
How it works, what it does and doesn't protect against, and server setup: [full documentation](https://github.com/karammm/request-shield#readme).
|
package/dist/axios.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type ShieldClient } from './client.js';
|
|
2
|
+
type AxiosAdapter = (config: any) => Promise<any>;
|
|
3
|
+
interface AxiosInstanceLike {
|
|
4
|
+
defaults: {
|
|
5
|
+
adapter?: unknown;
|
|
6
|
+
};
|
|
7
|
+
getUri(config?: any): string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Sign every axios request made through `instance`.
|
|
11
|
+
*
|
|
12
|
+
* Signing happens inside the adapter — i.e. after axios has run `transformRequest` —
|
|
13
|
+
* so the signed bytes are exactly the bytes sent.
|
|
14
|
+
*
|
|
15
|
+
* import axios from 'axios';
|
|
16
|
+
* const api = axios.create({ baseURL: 'https://api.example.com' });
|
|
17
|
+
* attachShieldToAxios(api, shieldClient, axios.getAdapter);
|
|
18
|
+
*/
|
|
19
|
+
export declare function attachShieldToAxios(instance: AxiosInstanceLike, client: ShieldClient, getAdapter: (adapter: any) => AxiosAdapter): void;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=axios.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"axios.d.ts","sourceRoot":"","sources":["../src/axios.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,YAAY,EAAqB,MAAM,aAAa,CAAC;AAIvG,KAAK,YAAY,GAAG,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAClD,UAAU,iBAAiB;IACzB,QAAQ,EAAE;QAAE,OAAO,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IAChC,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,GAAG,MAAM,CAAC;CAC9B;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,iBAAiB,EAC3B,MAAM,EAAE,YAAY,EACpB,UAAU,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,YAAY,GACzC,IAAI,CA2BN"}
|
package/dist/axios.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { isRetryableShieldError, resolveUrl } from './client.js';
|
|
2
|
+
/**
|
|
3
|
+
* Sign every axios request made through `instance`.
|
|
4
|
+
*
|
|
5
|
+
* Signing happens inside the adapter — i.e. after axios has run `transformRequest` —
|
|
6
|
+
* so the signed bytes are exactly the bytes sent.
|
|
7
|
+
*
|
|
8
|
+
* import axios from 'axios';
|
|
9
|
+
* const api = axios.create({ baseURL: 'https://api.example.com' });
|
|
10
|
+
* attachShieldToAxios(api, shieldClient, axios.getAdapter);
|
|
11
|
+
*/
|
|
12
|
+
export function attachShieldToAxios(instance, client, getAdapter) {
|
|
13
|
+
const base = getAdapter(instance.defaults.adapter);
|
|
14
|
+
const shielded = async (config, isRetry = false) => {
|
|
15
|
+
const url = resolveUrl(instance.getUri(config));
|
|
16
|
+
if (!client.shouldSign(url))
|
|
17
|
+
return base(config);
|
|
18
|
+
const signed = await client.signRequest(config.method ?? 'get', url, config.data);
|
|
19
|
+
for (const [k, v] of Object.entries(signed.headers))
|
|
20
|
+
setHeader(config.headers, k, v);
|
|
21
|
+
if (signed.contentType && !getHeader(config.headers, 'content-type')) {
|
|
22
|
+
setHeader(config.headers, 'Content-Type', signed.contentType);
|
|
23
|
+
}
|
|
24
|
+
config.data = signed.body;
|
|
25
|
+
try {
|
|
26
|
+
return await base(config);
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
const res = err?.response;
|
|
30
|
+
if (!isRetry && res?.status === 401 && isRetryableShieldError(getHeader(res.headers, 'x-shield-error'))) {
|
|
31
|
+
await client.invalidate(signed.sessionId);
|
|
32
|
+
return shielded(config, true);
|
|
33
|
+
}
|
|
34
|
+
throw err;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
instance.defaults.adapter = (config) => shielded(config);
|
|
38
|
+
}
|
|
39
|
+
function setHeader(headers, k, v) {
|
|
40
|
+
if (typeof headers?.set === 'function')
|
|
41
|
+
headers.set(k, v);
|
|
42
|
+
else
|
|
43
|
+
headers[k] = v;
|
|
44
|
+
}
|
|
45
|
+
function getHeader(headers, k) {
|
|
46
|
+
if (!headers)
|
|
47
|
+
return undefined;
|
|
48
|
+
if (typeof headers.get === 'function')
|
|
49
|
+
return headers.get(k) ?? undefined;
|
|
50
|
+
const hit = Object.keys(headers).find((h) => h.toLowerCase() === k.toLowerCase());
|
|
51
|
+
return hit ? headers[hit] : undefined;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=axios.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"axios.js","sourceRoot":"","sources":["../src/axios.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAwC,MAAM,aAAa,CAAC;AAUvG;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CACjC,QAA2B,EAC3B,MAAoB,EACpB,UAA0C;IAE1C,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAEnD,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAW,EAAE,OAAO,GAAG,KAAK,EAAgB,EAAE;QACpE,MAAM,GAAG,GAAG,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;QAChD,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC;YAAE,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC;QAEjD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,IAAI,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,IAAoB,CAAC,CAAC;QAClG,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAA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AAE,CAAS;IACxC,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,IAAI,OAAO,OAAO,CAAC,GAAG,KAAK,UAAU;QAAE,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC;IAC1E,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;IAClF,OAAO,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACxC,CAAC"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { type SessionStore, type ShieldSession } from './store.js';
|
|
2
|
+
export declare const SHIELD_HEADERS: {
|
|
3
|
+
readonly session: "X-Shield-Session";
|
|
4
|
+
readonly timestamp: "X-Shield-Timestamp";
|
|
5
|
+
readonly nonce: "X-Shield-Nonce";
|
|
6
|
+
readonly signature: "X-Shield-Signature";
|
|
7
|
+
};
|
|
8
|
+
/** Response header the server sets on a shield rejection (value = error code). */
|
|
9
|
+
export declare const SHIELD_ERROR_HEADER = "X-Shield-Error";
|
|
10
|
+
export declare function isRetryableShieldError(code: string | null | undefined): boolean;
|
|
11
|
+
export type SignableBody = string | ArrayBuffer | ArrayBufferView | Blob | URLSearchParams | null | undefined;
|
|
12
|
+
export interface ShieldConfig {
|
|
13
|
+
/** Base URL of your API, e.g. "https://api.example.com". Only requests to this origin get signed. */
|
|
14
|
+
apiBase: string;
|
|
15
|
+
/** Registration endpoint, relative to apiBase. Default "/shield/register". */
|
|
16
|
+
registerPath?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Returns a bot-check / attestation token sent at registration
|
|
19
|
+
* (Cloudflare Turnstile, reCAPTCHA Enterprise, App Attest, Play Integrity…).
|
|
20
|
+
* This is what stops a script from registering its own key — strongly recommended.
|
|
21
|
+
*/
|
|
22
|
+
getAttestationToken?: () => Promise<string> | string;
|
|
23
|
+
/** Sent at registration; the server decides what each platform must prove. Default "web". */
|
|
24
|
+
platform?: string;
|
|
25
|
+
store?: SessionStore;
|
|
26
|
+
/** fetch used for registration. Default globalThis.fetch. */
|
|
27
|
+
fetch?: typeof fetch;
|
|
28
|
+
/** Override which URLs get signed. Default: same origin as apiBase, excluding the register endpoint. */
|
|
29
|
+
shouldSign?: (url: URL) => boolean;
|
|
30
|
+
/** Re-register this long before the session expires. Default 60s. */
|
|
31
|
+
refreshBeforeExpiryMs?: number;
|
|
32
|
+
}
|
|
33
|
+
export interface SignedRequest {
|
|
34
|
+
sessionId: string;
|
|
35
|
+
/** Headers to add to the outgoing request. */
|
|
36
|
+
headers: Record<string, string>;
|
|
37
|
+
/** Exactly the bytes that were signed. Send this as the request body. */
|
|
38
|
+
body: string | ArrayBuffer | undefined;
|
|
39
|
+
/** Content-Type implied by the original body (URLSearchParams / Blob), if any. */
|
|
40
|
+
contentType?: string;
|
|
41
|
+
}
|
|
42
|
+
export declare class ShieldError extends Error {
|
|
43
|
+
readonly code: string;
|
|
44
|
+
readonly status?: number | undefined;
|
|
45
|
+
constructor(message: string, code: string, status?: number | undefined);
|
|
46
|
+
}
|
|
47
|
+
export declare function base64url(bytes: Uint8Array): string;
|
|
48
|
+
export declare function resolveUrl(url: string | URL): URL;
|
|
49
|
+
/** Build the exact string both sides sign/verify. Must match the server implementation. */
|
|
50
|
+
export declare function canonicalString(method: string, pathAndQuery: string, timestamp: string, nonce: string, bodyHash: string): string;
|
|
51
|
+
export declare class ShieldClient {
|
|
52
|
+
private readonly config;
|
|
53
|
+
readonly apiOrigin: string;
|
|
54
|
+
private readonly registerUrl;
|
|
55
|
+
private readonly store;
|
|
56
|
+
private readonly fetchImpl;
|
|
57
|
+
private session;
|
|
58
|
+
private loading;
|
|
59
|
+
private registering;
|
|
60
|
+
constructor(config: ShieldConfig);
|
|
61
|
+
/** Should this URL be signed? Never true for third-party origins, so keys/sessions don't leak. */
|
|
62
|
+
shouldSign(url: string | URL): boolean;
|
|
63
|
+
/** Returns a valid session, registering a fresh key pair if needed. Concurrent callers share one registration. */
|
|
64
|
+
getSession(): Promise<ShieldSession>;
|
|
65
|
+
/** Drop the session if it is still the one that the server rejected. */
|
|
66
|
+
invalidate(sessionId: string): Promise<void>;
|
|
67
|
+
/** Forget the current session entirely (e.g. on logout). */
|
|
68
|
+
reset(): Promise<void>;
|
|
69
|
+
private register;
|
|
70
|
+
/**
|
|
71
|
+
* Sign a request. Send the returned `body` (not your original one) so the bytes on the wire
|
|
72
|
+
* are exactly the bytes that were hashed.
|
|
73
|
+
*/
|
|
74
|
+
signRequest(method: string, url: string | URL, body?: SignableBody): Promise<SignedRequest>;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAExF,eAAO,MAAM,cAAc;;;;;CAKjB,CAAC;AAEX,kFAAkF;AAClF,eAAO,MAAM,mBAAmB,mBAAmB,CAAC;AAKpD,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAE/E;AAED,MAAM,MAAM,YAAY,GACpB,MAAM,GACN,WAAW,GACX,eAAe,GACf,IAAI,GACJ,eAAe,GACf,IAAI,GACJ,SAAS,CAAC;AAEd,MAAM,WAAW,YAAY;IAC3B,qGAAqG;IACrG,OAAO,EAAE,MAAM,CAAC;IAChB,8EAA8E;IAC9E,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,GAAG,MA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,CAAC,KAAK,CAAe;IACrC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAe;IACzC,OAAO,CAAC,OAAO,CAA8B;IAC7C,OAAO,CAAC,OAAO,CAA8B;IAC7C,OAAO,CAAC,WAAW,CAAuC;gBAE7B,MAAM,EAAE,YAAY;IAcjD,kGAAkG;IAClG,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO;IAMtC,kHAAkH;IAC5G,UAAU,IAAI,OAAO,CAAC,aAAa,CAAC;IAmB1C,wEAAwE;IAClE,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMlD,4DAA4D;IACtD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAKd,QAAQ;IAgCtB;;;OAGG;IACG,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;CA2BlG"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { defaultSessionStore } from './store.js';
|
|
2
|
+
export const SHIELD_HEADERS = {
|
|
3
|
+
session: 'X-Shield-Session',
|
|
4
|
+
timestamp: 'X-Shield-Timestamp',
|
|
5
|
+
nonce: 'X-Shield-Nonce',
|
|
6
|
+
signature: 'X-Shield-Signature',
|
|
7
|
+
};
|
|
8
|
+
/** Response header the server sets on a shield rejection (value = error code). */
|
|
9
|
+
export const SHIELD_ERROR_HEADER = 'X-Shield-Error';
|
|
10
|
+
/** Error codes after which the client re-registers and retries once. */
|
|
11
|
+
const RETRYABLE = new Set(['session', 'skew', 'signature']);
|
|
12
|
+
export function isRetryableShieldError(code) {
|
|
13
|
+
return !!code && RETRYABLE.has(code);
|
|
14
|
+
}
|
|
15
|
+
export class ShieldError extends Error {
|
|
16
|
+
code;
|
|
17
|
+
status;
|
|
18
|
+
constructor(message, code, status) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.code = code;
|
|
21
|
+
this.status = status;
|
|
22
|
+
this.name = 'ShieldError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const enc = new TextEncoder();
|
|
26
|
+
export function base64url(bytes) {
|
|
27
|
+
let bin = '';
|
|
28
|
+
for (const b of bytes)
|
|
29
|
+
bin += String.fromCharCode(b);
|
|
30
|
+
return btoa(bin).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
31
|
+
}
|
|
32
|
+
function baseHref() {
|
|
33
|
+
return globalThis.location?.href;
|
|
34
|
+
}
|
|
35
|
+
export function resolveUrl(url) {
|
|
36
|
+
return typeof url === 'string' ? new URL(url, baseHref()) : url;
|
|
37
|
+
}
|
|
38
|
+
/** Build the exact string both sides sign/verify. Must match the server implementation. */
|
|
39
|
+
export function canonicalString(method, pathAndQuery, timestamp, nonce, bodyHash) {
|
|
40
|
+
return ['RS1', method.toUpperCase(), pathAndQuery, timestamp, nonce, bodyHash].join('\n');
|
|
41
|
+
}
|
|
42
|
+
async function normalizeBody(body) {
|
|
43
|
+
if (body == null)
|
|
44
|
+
return { bytes: new Uint8Array(0), body: undefined };
|
|
45
|
+
if (typeof body === 'string')
|
|
46
|
+
return { bytes: enc.encode(body), body };
|
|
47
|
+
if (body instanceof URLSearchParams) {
|
|
48
|
+
const s = body.toString();
|
|
49
|
+
return { bytes: enc.encode(s), body: s, contentType: 'application/x-www-form-urlencoded;charset=UTF-8' };
|
|
50
|
+
}
|
|
51
|
+
if (body instanceof ArrayBuffer)
|
|
52
|
+
return { bytes: new Uint8Array(body), body };
|
|
53
|
+
if (ArrayBuffer.isView(body)) {
|
|
54
|
+
const copy = new Uint8Array(body.byteLength);
|
|
55
|
+
copy.set(new Uint8Array(body.buffer, body.byteOffset, body.byteLength));
|
|
56
|
+
return { bytes: copy, body: copy.buffer };
|
|
57
|
+
}
|
|
58
|
+
if (typeof Blob !== 'undefined' && body instanceof Blob) {
|
|
59
|
+
const buf = await body.arrayBuffer();
|
|
60
|
+
return { bytes: new Uint8Array(buf), body: buf, contentType: body.type || undefined };
|
|
61
|
+
}
|
|
62
|
+
throw new ShieldError('request-shield: unsupported body type (FormData / streams cannot be signed; send JSON, text, bytes or a Blob)', 'unsupported_body');
|
|
63
|
+
}
|
|
64
|
+
export class ShieldClient {
|
|
65
|
+
config;
|
|
66
|
+
apiOrigin;
|
|
67
|
+
registerUrl;
|
|
68
|
+
store;
|
|
69
|
+
fetchImpl;
|
|
70
|
+
session = null;
|
|
71
|
+
loading = null;
|
|
72
|
+
registering = null;
|
|
73
|
+
constructor(config) {
|
|
74
|
+
this.config = config;
|
|
75
|
+
if (!globalThis.crypto?.subtle) {
|
|
76
|
+
throw new ShieldError('request-shield: WebCrypto unavailable (page must be served over https or localhost)', 'no_webcrypto');
|
|
77
|
+
}
|
|
78
|
+
const base = resolveUrl(config.apiBase);
|
|
79
|
+
this.apiOrigin = base.origin;
|
|
80
|
+
this.registerUrl = new URL(config.registerPath ?? '/shield/register', base);
|
|
81
|
+
this.store = config.store ?? defaultSessionStore();
|
|
82
|
+
this.fetchImpl = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
83
|
+
}
|
|
84
|
+
/** Should this URL be signed? Never true for third-party origins, so keys/sessions don't leak. */
|
|
85
|
+
shouldSign(url) {
|
|
86
|
+
const u = resolveUrl(url);
|
|
87
|
+
if (this.config.shouldSign)
|
|
88
|
+
return this.config.shouldSign(u);
|
|
89
|
+
return u.origin === this.apiOrigin && u.pathname !== this.registerUrl.pathname;
|
|
90
|
+
}
|
|
91
|
+
/** Returns a valid session, registering a fresh key pair if needed. Concurrent callers share one registration. */
|
|
92
|
+
async getSession() {
|
|
93
|
+
this.loading ??= this.store
|
|
94
|
+
.load()
|
|
95
|
+
.then((s) => {
|
|
96
|
+
this.session ??= s;
|
|
97
|
+
})
|
|
98
|
+
.catch(() => { });
|
|
99
|
+
await this.loading;
|
|
100
|
+
const s = this.session;
|
|
101
|
+
const margin = this.config.refreshBeforeExpiryMs ?? 60_000;
|
|
102
|
+
if (s && s.expiresAt - margin > Date.now() + s.clockOffset)
|
|
103
|
+
return s;
|
|
104
|
+
this.registering ??= this.register().finally(() => {
|
|
105
|
+
this.registering = null;
|
|
106
|
+
});
|
|
107
|
+
return this.registering;
|
|
108
|
+
}
|
|
109
|
+
/** Drop the session if it is still the one that the server rejected. */
|
|
110
|
+
async invalidate(sessionId) {
|
|
111
|
+
if (this.session?.sessionId !== sessionId)
|
|
112
|
+
return;
|
|
113
|
+
this.session = null;
|
|
114
|
+
await this.store.clear().catch(() => { });
|
|
115
|
+
}
|
|
116
|
+
/** Forget the current session entirely (e.g. on logout). */
|
|
117
|
+
async reset() {
|
|
118
|
+
this.session = null;
|
|
119
|
+
await this.store.clear().catch(() => { });
|
|
120
|
+
}
|
|
121
|
+
async register() {
|
|
122
|
+
const keyPair = await crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, false, [
|
|
123
|
+
'sign',
|
|
124
|
+
'verify',
|
|
125
|
+
]);
|
|
126
|
+
const jwk = await crypto.subtle.exportKey('jwk', keyPair.publicKey);
|
|
127
|
+
const attestation = this.config.getAttestationToken ? await this.config.getAttestationToken() : undefined;
|
|
128
|
+
const res = await this.fetchImpl(this.registerUrl.href, {
|
|
129
|
+
method: 'POST',
|
|
130
|
+
headers: { 'Content-Type': 'application/json' },
|
|
131
|
+
body: JSON.stringify({
|
|
132
|
+
publicKey: { kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y },
|
|
133
|
+
attestation,
|
|
134
|
+
platform: this.config.platform ?? 'web',
|
|
135
|
+
}),
|
|
136
|
+
});
|
|
137
|
+
if (!res.ok) {
|
|
138
|
+
throw new ShieldError(`request-shield: registration failed (${res.status})`, 'register_failed', res.status);
|
|
139
|
+
}
|
|
140
|
+
const data = (await res.json());
|
|
141
|
+
const session = {
|
|
142
|
+
sessionId: data.sessionId,
|
|
143
|
+
expiresAt: data.expiresAt,
|
|
144
|
+
clockOffset: data.serverTime - Date.now(),
|
|
145
|
+
keyPair,
|
|
146
|
+
};
|
|
147
|
+
this.session = session;
|
|
148
|
+
await this.store.save(session).catch(() => { });
|
|
149
|
+
return session;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Sign a request. Send the returned `body` (not your original one) so the bytes on the wire
|
|
153
|
+
* are exactly the bytes that were hashed.
|
|
154
|
+
*/
|
|
155
|
+
async signRequest(method, url, body) {
|
|
156
|
+
const u = resolveUrl(url);
|
|
157
|
+
const normalized = await normalizeBody(body);
|
|
158
|
+
const session = await this.getSession();
|
|
159
|
+
const timestamp = String(Date.now() + session.clockOffset);
|
|
160
|
+
const nonce = base64url(crypto.getRandomValues(new Uint8Array(16)));
|
|
161
|
+
const bodyHash = base64url(new Uint8Array(await crypto.subtle.digest('SHA-256', normalized.bytes)));
|
|
162
|
+
const canonical = canonicalString(method, u.pathname + u.search, timestamp, nonce, bodyHash);
|
|
163
|
+
const signature = await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, session.keyPair.privateKey, enc.encode(canonical));
|
|
164
|
+
return {
|
|
165
|
+
sessionId: session.sessionId,
|
|
166
|
+
headers: {
|
|
167
|
+
[SHIELD_HEADERS.session]: session.sessionId,
|
|
168
|
+
[SHIELD_HEADERS.timestamp]: timestamp,
|
|
169
|
+
[SHIELD_HEADERS.nonce]: nonce,
|
|
170
|
+
[SHIELD_HEADERS.signature]: base64url(new Uint8Array(signature)),
|
|
171
|
+
},
|
|
172
|
+
body: normalized.body,
|
|
173
|
+
contentType: normalized.contentType,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAyC,MAAM,YAAY,CAAC;AAExF,MAAM,CAAC,MAAM,cAAc,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Q,GAAG,SAAS,CAAC,IAAI,UAAU,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACpG,MAAM,SAAS,GAAG,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;QAC7F,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CACxC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,EAClC,OAAO,CAAC,OAAO,CAAC,UAAU,EAC1B,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CACtB,CAAC;QAEF,OAAO;YACL,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,OAAO,EAAE;gBACP,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,SAAS;gBAC3C,CAAC,cAAc,CAAC,SAAS,CAAC,EAAE,SAAS;gBACrC,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,KAAK;gBAC7B,CAAC,cAAc,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC;aACjE;YACD,IAAI,EAAE,UAAU,CAAC,IAAI;YACrB,WAAW,EAAE,UAAU,CAAC,WAAW;SACpC,CAAC;IACJ,CAAC;CACF"}
|
package/dist/fetch.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ShieldClient } from './client.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a drop-in `fetch` that signs requests to your API origin
|
|
4
|
+
* and transparently re-registers + retries once if the session was rejected.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createShieldFetch(client: ShieldClient, baseFetch?: typeof fetch): typeof fetch;
|
|
7
|
+
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,YAAY,EAElB,MAAM,aAAa,CAAC;AAErB;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,YAAY,EAAE,SAAS,CAAC,EAAE,OAAO,KAAK,GAAG,OAAO,KAAK,CAoC9F"}
|
package/dist/fetch.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { isRetryableShieldError, resolveUrl, SHIELD_ERROR_HEADER, } from './client.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a drop-in `fetch` that signs requests to your API origin
|
|
4
|
+
* and transparently re-registers + retries once if the session was rejected.
|
|
5
|
+
*/
|
|
6
|
+
export function createShieldFetch(client, baseFetch) {
|
|
7
|
+
const f = baseFetch ?? globalThis.fetch.bind(globalThis);
|
|
8
|
+
return async (input, init) => {
|
|
9
|
+
const req = typeof Request !== 'undefined' && input instanceof Request ? input : null;
|
|
10
|
+
const url = resolveUrl(req ? req.url : input instanceof URL ? input.href : String(input));
|
|
11
|
+
if (!client.shouldSign(url))
|
|
12
|
+
return f(input, init);
|
|
13
|
+
const method = (init?.method ?? req?.method ?? 'GET').toUpperCase();
|
|
14
|
+
let body;
|
|
15
|
+
if (init && 'body' in init)
|
|
16
|
+
body = init.body;
|
|
17
|
+
else if (req && method !== 'GET' && method !== 'HEAD')
|
|
18
|
+
body = await req.clone().arrayBuffer();
|
|
19
|
+
const baseHeaders = new Headers(req?.headers);
|
|
20
|
+
new Headers(init?.headers).forEach((v, k) => baseHeaders.set(k, v));
|
|
21
|
+
const baseInit = req
|
|
22
|
+
? { credentials: req.credentials, mode: req.mode, cache: req.cache, redirect: req.redirect, signal: req.signal }
|
|
23
|
+
: {};
|
|
24
|
+
const attempt = async (isRetry) => {
|
|
25
|
+
const signed = await client.signRequest(method, url, body);
|
|
26
|
+
const headers = new Headers(baseHeaders);
|
|
27
|
+
for (const [k, v] of Object.entries(signed.headers))
|
|
28
|
+
headers.set(k, v);
|
|
29
|
+
if (signed.contentType && !headers.has('content-type'))
|
|
30
|
+
headers.set('content-type', signed.contentType);
|
|
31
|
+
const res = await f(url.href, { ...baseInit, ...init, method, headers, body: signed.body });
|
|
32
|
+
if (!isRetry && res.status === 401 && isRetryableShieldError(res.headers.get(SHIELD_ERROR_HEADER))) {
|
|
33
|
+
await client.invalidate(signed.sessionId);
|
|
34
|
+
return attempt(true);
|
|
35
|
+
}
|
|
36
|
+
return res;
|
|
37
|
+
};
|
|
38
|
+
return attempt(false);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=fetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,UAAU,EACV,mBAAmB,GAGpB,MAAM,aAAa,CAAC;AAErB;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAoB,EAAE,SAAwB;IAC9E,MAAM,CAAC,GAAG,SAAS,IAAI,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAEzD,OAAO,KAAK,EAAE,KAAwB,EAAE,IAAkB,EAAqB,EAAE;QAC/E,MAAM,GAAG,GAAG,OAAO,OAAO,KAAK,WAAW,IAAI,KAAK,YAAY,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;QACtF,MAAM,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,YAAY,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QAC1F,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC;YAAE,OAAO,CAA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AC;gBAAE,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YACvE,IAAI,MAAM,CAAC,WAAW,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;gBAAE,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;YAExG,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,GAAG,QAAQ,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;YAC5F,IAAI,CAAC,OAAO,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,IAAI,sBAAsB,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC;gBACnG,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;gBAC1C,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;YACvB,CAAC;YACD,OAAO,GAAG,CAAC;QACb,CAAC,CAAC;QAEF,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the signing key + session live on the client.
|
|
3
|
+
*
|
|
4
|
+
* The private key is generated with `extractable: false`, so even code running
|
|
5
|
+
* on the page (devtools, XSS) cannot export the raw key material — it can only
|
|
6
|
+
* ask the browser to sign with it. `CryptoKey` objects survive IndexedDB
|
|
7
|
+
* structured-clone with that property intact.
|
|
8
|
+
*/
|
|
9
|
+
export interface ShieldSession {
|
|
10
|
+
sessionId: string;
|
|
11
|
+
/** Session expiry, in server time (unix ms). */
|
|
12
|
+
expiresAt: number;
|
|
13
|
+
/** serverTime - clientTime, in ms. Added to Date.now() when signing. */
|
|
14
|
+
clockOffset: number;
|
|
15
|
+
keyPair: CryptoKeyPair;
|
|
16
|
+
}
|
|
17
|
+
export interface SessionStore {
|
|
18
|
+
load(): Promise<ShieldSession | null>;
|
|
19
|
+
save(session: ShieldSession): Promise<void>;
|
|
20
|
+
clear(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
export declare class MemorySessionStore implements SessionStore {
|
|
23
|
+
private session;
|
|
24
|
+
load(): Promise<ShieldSession | null>;
|
|
25
|
+
save(session: ShieldSession): Promise<void>;
|
|
26
|
+
clear(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/** Persists the session across page reloads. Falls back to "no session" on any IndexedDB error. */
|
|
29
|
+
export declare class IndexedDBSessionStore implements SessionStore {
|
|
30
|
+
private readonly dbName;
|
|
31
|
+
private readonly storeName;
|
|
32
|
+
private dbPromise;
|
|
33
|
+
constructor(dbName?: string, storeName?: string);
|
|
34
|
+
private db;
|
|
35
|
+
private run;
|
|
36
|
+
load(): Promise<ShieldSession | null>;
|
|
37
|
+
save(session: ShieldSession): Promise<void>;
|
|
38
|
+
clear(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
export declare function defaultSessionStore(): SessionStore;
|
|
41
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,SAAS,EAAE,MAAM,CAAC;IAClB,wEAAwE;IACxE,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,aAAa,CAAC;CACxB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,IAAI,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAC;IACtC,IAAI,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,qBAAa,kBAAmB,YAAW,YAAY;IACrD,OAAO,CAAC,OAAO,CAA8B;IACvC,IAAI;IAGJ,IAAI,CAAC,OAAO,EAAE,aAAa;IAG3B,KAAK;CAGZ;AAID,mGAAmG;AACnG,qBAAa,qBAAsB,YAAW,YAAY;IAItD,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAJ5B,OAAO,CAAC,SAAS,CAAqC;gBAGnC,MAAM,SAAmB,EACzB,SAAS,SAAY;IAGxC,OAAO,CAAC,EAAE;YAUI,GAAG;IASX,IAAI,IAAI,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAQrC,IAAI,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ3C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAO7B;AAED,wBAAgB,mBAAmB,IAAI,YAAY,CAElD"}
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export class MemorySessionStore {
|
|
2
|
+
session = null;
|
|
3
|
+
async load() {
|
|
4
|
+
return this.session;
|
|
5
|
+
}
|
|
6
|
+
async save(session) {
|
|
7
|
+
this.session = session;
|
|
8
|
+
}
|
|
9
|
+
async clear() {
|
|
10
|
+
this.session = null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const RECORD_KEY = 'current';
|
|
14
|
+
/** Persists the session across page reloads. Falls back to "no session" on any IndexedDB error. */
|
|
15
|
+
export class IndexedDBSessionStore {
|
|
16
|
+
dbName;
|
|
17
|
+
storeName;
|
|
18
|
+
dbPromise = null;
|
|
19
|
+
constructor(dbName = 'request-shield', storeName = 'session') {
|
|
20
|
+
this.dbName = dbName;
|
|
21
|
+
this.storeName = storeName;
|
|
22
|
+
}
|
|
23
|
+
db() {
|
|
24
|
+
this.dbPromise ??= new Promise((resolve, reject) => {
|
|
25
|
+
const req = indexedDB.open(this.dbName, 1);
|
|
26
|
+
req.onupgradeneeded = () => req.result.createObjectStore(this.storeName);
|
|
27
|
+
req.onsuccess = () => resolve(req.result);
|
|
28
|
+
req.onerror = () => reject(req.error);
|
|
29
|
+
});
|
|
30
|
+
return this.dbPromise;
|
|
31
|
+
}
|
|
32
|
+
async run(mode, fn) {
|
|
33
|
+
const db = await this.db();
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const req = fn(db.transaction(this.storeName, mode).objectStore(this.storeName));
|
|
36
|
+
req.onsuccess = () => resolve(req.result);
|
|
37
|
+
req.onerror = () => reject(req.error);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async load() {
|
|
41
|
+
try {
|
|
42
|
+
return (await this.run('readonly', (s) => s.get(RECORD_KEY))) ?? null;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async save(session) {
|
|
49
|
+
try {
|
|
50
|
+
await this.run('readwrite', (s) => s.put(session, RECORD_KEY));
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
/* private mode etc. — session just won't survive a reload */
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async clear() {
|
|
57
|
+
try {
|
|
58
|
+
await this.run('readwrite', (s) => s.delete(RECORD_KEY));
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
/* ignore */
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export function defaultSessionStore() {
|
|
66
|
+
return typeof indexedDB !== 'undefined' ? new IndexedDBSessionStore() : new MemorySessionStore();
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAuBA,MAAM,OAAO,kBAAkB;IACrB,OAAO,GAAyB,IAAI,CAAC;IAC7C,KAAK,CAAC,IAAI;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,OAAsB;QAC/B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IACD,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;IACtB,CAAC;CACF;AAED,MAAM,UAAU,GAAG,SAAS,CAAC;AAE7B,mGAAmG;AACnG,MAAM,OAAO,qBAAqB;IAIb;IACA;IAJX,SAAS,GAAgC,IAAI,CAAC;IAEtD,YACmB,SAAS,gBAAgB,EACzB,YAAY,SAAS;QADrB,WAAM,GAAN,MAAM,CAAmB;QACzB,cAAS,GAAT,SAAS,CAAY;IACrC,CAAC;IAEI,EAAE;QACR,IAAI,CAAC,SAAS,KAAK,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACjD,MAAM,GAAG,GAAG,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;YAC3C,GAAG,CAAC,eAAe,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,iBAAiB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YACzE,GAAG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CAAC,CAAC,CAAC;QACjE,CAAC;QAAC,MAAM,CAAC;YACP,6DAA6D;QAC/D,CAAC;IACH,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;QAC3D,CAAC;QAAC,MAAM,CAAC;YACP,YAAY;QACd,CAAC;IACH,CAAC;CACF;AAED,MAAM,UAAU,mBAAmB;IACjC,OAAO,OAAO,SAAS,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,qBAAqB,EAAE,CAAC,CAAC,CAAC,IAAI,kBAAkB,EAAE,CAAC;AACnG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@request-shield/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Signs API requests with a non-extractable WebCrypto key so they can't be replayed or forged from curl/Postman.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"webcrypto",
|
|
7
|
+
"request-signing",
|
|
8
|
+
"api-security",
|
|
9
|
+
"anti-replay",
|
|
10
|
+
"bot-protection"
|
|
11
|
+
],
|
|
12
|
+
"author": "karammm",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"homepage": "https://github.com/karammm/request-shield#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/karammm/request-shield.git",
|
|
18
|
+
"directory": "packages/core"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/karammm/request-shield/issues"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc -p tsconfig.json"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
}
|
|
44
|
+
}
|