@nerdsystem/sdk 0.0.7 → 0.0.8
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/auth/client.d.ts +3 -0
- package/dist/auth/client.js +14 -1
- package/package.json +1 -1
package/dist/auth/client.d.ts
CHANGED
|
@@ -28,6 +28,9 @@ export declare class AuthServiceClient {
|
|
|
28
28
|
/** The visitor's address, forwarded so the platform rate-limits per person rather than per app. */
|
|
29
29
|
clientIp?: string | undefined);
|
|
30
30
|
get jwksUrl(): string;
|
|
31
|
+
private signature?;
|
|
32
|
+
/** HMAC of the visitor address keyed by SHA-256(tenant secret), which is what the platform stores; an unsigned address is ignored there. */
|
|
33
|
+
private signVisitor;
|
|
31
34
|
private call;
|
|
32
35
|
signup(input: {
|
|
33
36
|
email: string;
|
package/dist/auth/client.js
CHANGED
|
@@ -28,13 +28,26 @@ export class AuthServiceClient {
|
|
|
28
28
|
get jwksUrl() {
|
|
29
29
|
return `${this.env.NERD_AUTH_URL.replace(/\/$/, "")}/.well-known/jwks.json`;
|
|
30
30
|
}
|
|
31
|
+
signature;
|
|
32
|
+
/** HMAC of the visitor address keyed by SHA-256(tenant secret), which is what the platform stores; an unsigned address is ignored there. */
|
|
33
|
+
signVisitor(ip) {
|
|
34
|
+
this.signature ??= (async () => {
|
|
35
|
+
const enc = new TextEncoder();
|
|
36
|
+
const digest = await crypto.subtle.digest("SHA-256", enc.encode(this.env.NERD_AUTH_SECRET));
|
|
37
|
+
const keyHex = Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
38
|
+
const key = await crypto.subtle.importKey("raw", enc.encode(keyHex), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
39
|
+
return Array.from(new Uint8Array(await crypto.subtle.sign("HMAC", key, enc.encode(ip))), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
40
|
+
})();
|
|
41
|
+
return this.signature;
|
|
42
|
+
}
|
|
31
43
|
async call(method, path, body, auth) {
|
|
44
|
+
const visitor = this.clientIp && this.env.NERD_AUTH_SECRET ? { "x-nerd-client-ip": this.clientIp, "x-nerd-client-sig": await this.signVisitor(this.clientIp) } : {};
|
|
32
45
|
const res = await this.fetchImpl(`${this.base}${path}`, {
|
|
33
46
|
method,
|
|
34
47
|
headers: {
|
|
35
48
|
...(body !== undefined ? { "content-type": "application/json" } : {}),
|
|
36
49
|
...(auth ? { authorization: `Bearer ${auth}` } : {}),
|
|
37
|
-
...
|
|
50
|
+
...visitor,
|
|
38
51
|
},
|
|
39
52
|
body: body !== undefined ? JSON.stringify(body) : null,
|
|
40
53
|
});
|