@outseta/node-sdk 0.1.1 → 0.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/dist/index.js CHANGED
@@ -1,5 +1,55 @@
1
1
  // src/index.ts
2
2
  import { createClient } from "@outseta/api-client";
3
+
4
+ // src/verify-webhook.ts
5
+ import { createHmac, timingSafeEqual } from "node:crypto";
6
+ function verifyWebhookSignature(signature, body, signingKey) {
7
+ const key = Buffer.from(signingKey, "hex");
8
+ const computed = createHmac("sha256", key).update(Buffer.from(body, "utf-8")).digest("hex");
9
+ const expected = `sha256=${computed}`;
10
+ if (signature.length !== expected.length)
11
+ return false;
12
+ return timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
13
+ }
14
+ // src/verify-jwt.ts
15
+ import { jwtVerify, createRemoteJWKSet, decodeJwt } from "jose";
16
+ async function verifyJwt(token, options) {
17
+ const jwks = createRemoteJWKSet(new URL(`https://${options.subdomain}.outseta.com/.well-known/jwks`));
18
+ const { payload } = await jwtVerify(token, jwks);
19
+ return payload;
20
+ }
21
+ async function verifyJwtWithProfile(token, options) {
22
+ const response = await fetch(`https://${options.subdomain}.outseta.com/api/v1/profile?fields=*`, {
23
+ headers: { Authorization: `Bearer ${token}` }
24
+ });
25
+ if (!response.ok) {
26
+ const error = await response.json().catch(() => ({}));
27
+ throw new Error(`Profile verification failed: [${response.status}] ${error.ErrorMessage || error.Message || response.statusText}`);
28
+ }
29
+ const profile = await response.json();
30
+ const payload = decodeJwt(token);
31
+ return { payload, profile };
32
+ }
33
+ // src/generate-token.ts
34
+ async function generateAccessToken(credentials, email) {
35
+ const response = await fetch(`https://${credentials.subdomain}.outseta.com/api/v1/tokens`, {
36
+ method: "POST",
37
+ headers: {
38
+ Authorization: `Outseta ${credentials.apiKey}:${credentials.apiSecret}`,
39
+ "Content-Type": "application/json"
40
+ },
41
+ body: JSON.stringify({ username: email })
42
+ });
43
+ const data = await response.json();
44
+ if (!response.ok) {
45
+ throw new Error(`Failed to generate token: [${response.status}] ${data.ErrorMessage || data.Message || response.statusText}`);
46
+ }
47
+ return data;
48
+ }
3
49
  export {
50
+ verifyWebhookSignature,
51
+ verifyJwtWithProfile,
52
+ verifyJwt,
53
+ generateAccessToken,
4
54
  createClient
5
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@outseta/node-sdk",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Node.js SDK for the Outseta API",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -23,7 +23,8 @@
23
23
  "lint": "eslint src"
24
24
  },
25
25
  "dependencies": {
26
- "@outseta/api-client": "*"
26
+ "@outseta/api-client": "*",
27
+ "jose": "^6.2.1"
27
28
  },
28
29
  "devDependencies": {
29
30
  "typescript": "^5.7.0"
@@ -0,0 +1,39 @@
1
+ import type { OutsetaCredentials } from "@outseta/api-client";
2
+
3
+ /**
4
+ * Generates a JWT access token for a user by email.
5
+ *
6
+ * Requires server-side API Key + Secret credentials.
7
+ * Useful for logging in users programmatically, testing auth flows,
8
+ * or acting on behalf of a user from a backend service.
9
+ */
10
+ export async function generateAccessToken(
11
+ credentials: OutsetaCredentials,
12
+ email: string,
13
+ ) {
14
+ const response = await fetch(
15
+ `https://${credentials.subdomain}.outseta.com/api/v1/tokens`,
16
+ {
17
+ method: "POST",
18
+ headers: {
19
+ Authorization: `Outseta ${credentials.apiKey}:${credentials.apiSecret}`,
20
+ "Content-Type": "application/json",
21
+ },
22
+ body: JSON.stringify({ username: email }),
23
+ },
24
+ );
25
+
26
+ const data = (await response.json()) as Record<string, unknown>;
27
+
28
+ if (!response.ok) {
29
+ throw new Error(
30
+ `Failed to generate token: [${response.status}] ${
31
+ (data.ErrorMessage as string) ||
32
+ (data.Message as string) ||
33
+ response.statusText
34
+ }`,
35
+ );
36
+ }
37
+
38
+ return data as { access_token: string } & Record<string, unknown>;
39
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,7 @@
1
1
  export { createClient } from "@outseta/api-client";
2
2
  export type { OutsetaCredentials } from "@outseta/api-client";
3
+
4
+ export { verifyWebhookSignature } from "./verify-webhook.js";
5
+ export { verifyJwt, verifyJwtWithProfile } from "./verify-jwt.js";
6
+ export type { JWTPayload } from "./verify-jwt.js";
7
+ export { generateAccessToken } from "./generate-token.js";
@@ -0,0 +1,61 @@
1
+ import { jwtVerify, createRemoteJWKSet, decodeJwt } from "jose";
2
+ export type { JWTPayload } from "jose";
3
+
4
+ /**
5
+ * Verifies an Outseta JWT using the public JWK Set.
6
+ *
7
+ * This is the recommended method — it cryptographically verifies
8
+ * the token signature without making an API call to Outseta.
9
+ *
10
+ * For high-throughput applications, consider caching the JWK Set
11
+ * by reusing the same function across calls (jose handles caching
12
+ * internally via `createRemoteJWKSet`).
13
+ */
14
+ export async function verifyJwt(
15
+ token: string,
16
+ options: { subdomain: string },
17
+ ) {
18
+ const jwks = createRemoteJWKSet(
19
+ new URL(
20
+ `https://${options.subdomain}.outseta.com/.well-known/jwks`,
21
+ ),
22
+ );
23
+ const { payload } = await jwtVerify(token, jwks);
24
+ return payload;
25
+ }
26
+
27
+ /**
28
+ * Verifies an Outseta JWT by calling the profile endpoint.
29
+ *
30
+ * Returns both the decoded JWT payload and the full user profile.
31
+ * Simpler than JWK Set verification but makes a network request
32
+ * to Outseta on every call.
33
+ */
34
+ export async function verifyJwtWithProfile(
35
+ token: string,
36
+ options: { subdomain: string },
37
+ ) {
38
+ const response = await fetch(
39
+ `https://${options.subdomain}.outseta.com/api/v1/profile?fields=*`,
40
+ {
41
+ headers: { Authorization: `Bearer ${token}` },
42
+ },
43
+ );
44
+
45
+ if (!response.ok) {
46
+ const error = (await response.json().catch(() => ({}))) as Record<
47
+ string,
48
+ string
49
+ >;
50
+ throw new Error(
51
+ `Profile verification failed: [${response.status}] ${
52
+ error.ErrorMessage || error.Message || response.statusText
53
+ }`,
54
+ );
55
+ }
56
+
57
+ const profile = (await response.json()) as Record<string, unknown>;
58
+ const payload = decodeJwt(token);
59
+
60
+ return { payload, profile };
61
+ }
@@ -0,0 +1,23 @@
1
+ import { createHmac, timingSafeEqual } from "node:crypto";
2
+
3
+ /**
4
+ * Verifies an Outseta webhook signature.
5
+ *
6
+ * Compare the `x-hub-signature-256` request header against
7
+ * the HMAC-SHA256 computed from the raw request body and
8
+ * your webhook signing key (found in Outseta webhook settings).
9
+ */
10
+ export function verifyWebhookSignature(
11
+ signature: string,
12
+ body: string,
13
+ signingKey: string,
14
+ ): boolean {
15
+ const key = Buffer.from(signingKey, "hex");
16
+ const computed = createHmac("sha256", key)
17
+ .update(Buffer.from(body, "utf-8"))
18
+ .digest("hex");
19
+ const expected = `sha256=${computed}`;
20
+
21
+ if (signature.length !== expected.length) return false;
22
+ return timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
23
+ }