@stacknet/userutils 0.6.0 → 0.6.2

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.
@@ -1,177 +0,0 @@
1
- import { P as PublicSession } from './auth-c1d7Eji2.cjs';
2
-
3
- interface BillingPlan {
4
- id: string;
5
- name: string;
6
- price_cents: number;
7
- token_allocation: number;
8
- features: string;
9
- sort_order?: number;
10
- is_active?: boolean;
11
- }
12
- interface Subscription {
13
- plan?: {
14
- id: string;
15
- name: string;
16
- priceCents: number;
17
- tokenAllocation: number;
18
- };
19
- planId?: string;
20
- planName?: string;
21
- status: string;
22
- tokensUsed?: number;
23
- tokensRemaining?: number;
24
- usagePercent?: number;
25
- periodStart?: number;
26
- periodEnd?: number;
27
- cancelAtPeriodEnd?: boolean;
28
- stripeCustomerId?: string;
29
- }
30
- interface UsageSummary {
31
- ownerId: string;
32
- planAllocation: number;
33
- planId: string | null;
34
- planName: string;
35
- inferenceUsed: number;
36
- ledgerSpent: number;
37
- totalUsed: number;
38
- remaining: number;
39
- percent: number;
40
- exceeded: boolean;
41
- breakdown: {
42
- subscription: number;
43
- prepaidCredit: number;
44
- tokenGrants: number;
45
- inference: number;
46
- skillRegistrations: number;
47
- skillRegistrationCount: number;
48
- };
49
- }
50
- interface PrepaidCheckoutResult {
51
- url: string;
52
- sessionId?: string;
53
- }
54
- interface PrepaidVerifyResult {
55
- alreadyCredited: boolean;
56
- tokensCredited?: number;
57
- amountCents?: number;
58
- paymentRef?: string;
59
- }
60
- interface BillingRecord {
61
- id: string;
62
- recorded_at?: number;
63
- date?: string;
64
- reason?: string;
65
- description?: string;
66
- amount: number;
67
- direction?: string;
68
- source?: string;
69
- status?: string;
70
- payment_ref?: string;
71
- }
72
-
73
- /** Decode JWT payload without verification (server-side helper) */
74
- declare function decodeJWTPayload(jwt: string): Record<string, any> | null;
75
- /** Sign a JWT with HMAC-SHA256 */
76
- declare function signJWT(payload: Record<string, any>, secret: string): string;
77
- /** Verify a JWT signature with HMAC-SHA256 (constant-time comparison) */
78
- declare function verifyJWTSignature(jwt: string, secret: string): boolean;
79
- /** Verify JWT and return payload if valid (checks signature + expiry) */
80
- declare function verifyJWT(jwt: string, secret: string): Record<string, any> | null;
81
- /**
82
- * Check if JWT needs refresh and return a new one if so.
83
- * Returns null if no refresh needed or JWT is invalid.
84
- */
85
- declare function maybeRefreshJWT(jwt: string, secret: string, expirySeconds?: number, refreshWindowSeconds?: number): string | null;
86
- /** Generate a cryptographically secure random token */
87
- declare function generateToken(bytes?: number): string;
88
- /** Options controlling how the real client IP is extracted from a Request.
89
- *
90
- * The previous implementation trusted `X-Forwarded-For[0]` unconditionally,
91
- * which lets any direct caller pin arbitrary values and bypass per-IP rate
92
- * limits. The correct read depends on how many reverse proxies sit between
93
- * the app and the real client: with N trusted hops, the real client IP is
94
- * at position `(length - N)` in the XFF list, because each trusted hop
95
- * appends its peer IP and the attacker-controlled prefix is pushed left. */
96
- interface IPExtractorConfig {
97
- /** Number of trusted reverse-proxy hops between this app and the real client.
98
- * - `0`: do NOT trust X-Forwarded-For (app is directly internet-exposed).
99
- * - `1` (default): one trusted proxy (e.g. single LB, Vercel edge, Nginx).
100
- * - `N`: N trusted hops; the real client IP is `XFF[length - N]`.
101
- * If XFF has fewer entries than expected, the chain is misconfigured or
102
- * spoofed and the extractor falls through to the next source. */
103
- trustedProxyCount?: number;
104
- /** Trust the `X-Real-IP` header. Only enable if your proxy sets it AND
105
- * strips any inbound `X-Real-IP` from clients. Default: `false`. */
106
- trustRealIpHeader?: boolean;
107
- /** Override entirely — return the real client IP from a request however
108
- * you know best (e.g. a platform-specific header like `cf-connecting-ip`
109
- * or `x-vercel-forwarded-for`). If this returns a non-empty string it
110
- * wins; if it returns null/empty the other strategies run. */
111
- customExtractor?: (request: Request) => string | null | undefined;
112
- }
113
- /** Extract the real client IP address from a request.
114
- *
115
- * Default behavior (`trustedProxyCount: 1`) is safe for the common case of
116
- * one trusted reverse proxy. Consumers with no proxy must pass `0`, and
117
- * consumers behind multiple proxies must pass the exact hop count. */
118
- declare function extractIP(request: Request, config?: IPExtractorConfig): string;
119
-
120
- /** Client-side configuration for UserUtilsProvider */
121
- interface UserUtilsConfig {
122
- /** Base URL for API calls (empty string = same origin) */
123
- apiBaseUrl: string;
124
- /** StackNet stack identifier */
125
- stackId?: string;
126
- /** Direct StackNet URL for client-side API calls (e.g. challenges) */
127
- stacknetUrl?: string;
128
- /** Theme preference */
129
- theme?: 'light' | 'dark' | 'system';
130
- }
131
- /** Callbacks for auth and billing events */
132
- interface UserUtilsCallbacks {
133
- onAuthSuccess?: (session: PublicSession) => void;
134
- onAuthError?: (error: Error) => void;
135
- onLogout?: () => void;
136
- onSubscriptionChange?: (subscription: Subscription) => void;
137
- }
138
- /** Server-side configuration for handler factories */
139
- interface ServerConfig {
140
- /** HMAC-SHA256 secret for signing JWTs */
141
- authSecret: string;
142
- /** StackNet backend URL (always https://stacknet.magma-rpc.com) */
143
- stacknetUrl: string;
144
- /** StackNet stack identifier */
145
- stackId: string;
146
- /** JWT secret for re-signing to StackNet (defaults to authSecret) */
147
- stacknetJwtSecret?: string;
148
- /** Cookie domain for subdomain sharing (e.g. '.geoff.ai') */
149
- cookieDomain?: string;
150
- /** Use Secure flag on cookies (default: true) */
151
- secureCookies?: boolean;
152
- /** Session max age in seconds (default: 604800 = 7 days) */
153
- sessionMaxAge?: number;
154
- /** JWT expiry in seconds (default: 900 = 15 minutes) */
155
- jwtExpiry?: number;
156
- /**
157
- * Google OAuth client ID (single). Used by createGoogleOneTapHandler to
158
- * validate the `aud` claim on incoming Google ID tokens. Required for
159
- * Google One Tap — without it, an ID token issued to any other Google
160
- * application could be replayed against this endpoint.
161
- */
162
- googleClientId?: string;
163
- /**
164
- * Google OAuth client IDs (multiple). Use when the stack accepts tokens
165
- * from more than one Google client (e.g. web + native).
166
- */
167
- googleClientIds?: string[];
168
- /**
169
- * How the handlers should derive the real client IP for rate-limit keys.
170
- * Defaults to `{ trustedProxyCount: 1 }`. Set `trustedProxyCount: 0` if
171
- * the app is exposed directly (no proxy) — otherwise any caller can pin
172
- * their X-Forwarded-For and bypass rate limiting.
173
- */
174
- ipConfig?: IPExtractorConfig;
175
- }
176
-
177
- export { type BillingPlan as B, type IPExtractorConfig as I, type PrepaidCheckoutResult as P, type Subscription as S, type UsageSummary as U, type BillingRecord as a, type PrepaidVerifyResult as b, type UserUtilsCallbacks as c, type UserUtilsConfig as d, type ServerConfig as e, decodeJWTPayload as f, extractIP as g, generateToken as h, verifyJWTSignature as i, maybeRefreshJWT as m, signJWT as s, verifyJWT as v };
@@ -1,177 +0,0 @@
1
- import { P as PublicSession } from './auth-c1d7Eji2.js';
2
-
3
- interface BillingPlan {
4
- id: string;
5
- name: string;
6
- price_cents: number;
7
- token_allocation: number;
8
- features: string;
9
- sort_order?: number;
10
- is_active?: boolean;
11
- }
12
- interface Subscription {
13
- plan?: {
14
- id: string;
15
- name: string;
16
- priceCents: number;
17
- tokenAllocation: number;
18
- };
19
- planId?: string;
20
- planName?: string;
21
- status: string;
22
- tokensUsed?: number;
23
- tokensRemaining?: number;
24
- usagePercent?: number;
25
- periodStart?: number;
26
- periodEnd?: number;
27
- cancelAtPeriodEnd?: boolean;
28
- stripeCustomerId?: string;
29
- }
30
- interface UsageSummary {
31
- ownerId: string;
32
- planAllocation: number;
33
- planId: string | null;
34
- planName: string;
35
- inferenceUsed: number;
36
- ledgerSpent: number;
37
- totalUsed: number;
38
- remaining: number;
39
- percent: number;
40
- exceeded: boolean;
41
- breakdown: {
42
- subscription: number;
43
- prepaidCredit: number;
44
- tokenGrants: number;
45
- inference: number;
46
- skillRegistrations: number;
47
- skillRegistrationCount: number;
48
- };
49
- }
50
- interface PrepaidCheckoutResult {
51
- url: string;
52
- sessionId?: string;
53
- }
54
- interface PrepaidVerifyResult {
55
- alreadyCredited: boolean;
56
- tokensCredited?: number;
57
- amountCents?: number;
58
- paymentRef?: string;
59
- }
60
- interface BillingRecord {
61
- id: string;
62
- recorded_at?: number;
63
- date?: string;
64
- reason?: string;
65
- description?: string;
66
- amount: number;
67
- direction?: string;
68
- source?: string;
69
- status?: string;
70
- payment_ref?: string;
71
- }
72
-
73
- /** Decode JWT payload without verification (server-side helper) */
74
- declare function decodeJWTPayload(jwt: string): Record<string, any> | null;
75
- /** Sign a JWT with HMAC-SHA256 */
76
- declare function signJWT(payload: Record<string, any>, secret: string): string;
77
- /** Verify a JWT signature with HMAC-SHA256 (constant-time comparison) */
78
- declare function verifyJWTSignature(jwt: string, secret: string): boolean;
79
- /** Verify JWT and return payload if valid (checks signature + expiry) */
80
- declare function verifyJWT(jwt: string, secret: string): Record<string, any> | null;
81
- /**
82
- * Check if JWT needs refresh and return a new one if so.
83
- * Returns null if no refresh needed or JWT is invalid.
84
- */
85
- declare function maybeRefreshJWT(jwt: string, secret: string, expirySeconds?: number, refreshWindowSeconds?: number): string | null;
86
- /** Generate a cryptographically secure random token */
87
- declare function generateToken(bytes?: number): string;
88
- /** Options controlling how the real client IP is extracted from a Request.
89
- *
90
- * The previous implementation trusted `X-Forwarded-For[0]` unconditionally,
91
- * which lets any direct caller pin arbitrary values and bypass per-IP rate
92
- * limits. The correct read depends on how many reverse proxies sit between
93
- * the app and the real client: with N trusted hops, the real client IP is
94
- * at position `(length - N)` in the XFF list, because each trusted hop
95
- * appends its peer IP and the attacker-controlled prefix is pushed left. */
96
- interface IPExtractorConfig {
97
- /** Number of trusted reverse-proxy hops between this app and the real client.
98
- * - `0`: do NOT trust X-Forwarded-For (app is directly internet-exposed).
99
- * - `1` (default): one trusted proxy (e.g. single LB, Vercel edge, Nginx).
100
- * - `N`: N trusted hops; the real client IP is `XFF[length - N]`.
101
- * If XFF has fewer entries than expected, the chain is misconfigured or
102
- * spoofed and the extractor falls through to the next source. */
103
- trustedProxyCount?: number;
104
- /** Trust the `X-Real-IP` header. Only enable if your proxy sets it AND
105
- * strips any inbound `X-Real-IP` from clients. Default: `false`. */
106
- trustRealIpHeader?: boolean;
107
- /** Override entirely — return the real client IP from a request however
108
- * you know best (e.g. a platform-specific header like `cf-connecting-ip`
109
- * or `x-vercel-forwarded-for`). If this returns a non-empty string it
110
- * wins; if it returns null/empty the other strategies run. */
111
- customExtractor?: (request: Request) => string | null | undefined;
112
- }
113
- /** Extract the real client IP address from a request.
114
- *
115
- * Default behavior (`trustedProxyCount: 1`) is safe for the common case of
116
- * one trusted reverse proxy. Consumers with no proxy must pass `0`, and
117
- * consumers behind multiple proxies must pass the exact hop count. */
118
- declare function extractIP(request: Request, config?: IPExtractorConfig): string;
119
-
120
- /** Client-side configuration for UserUtilsProvider */
121
- interface UserUtilsConfig {
122
- /** Base URL for API calls (empty string = same origin) */
123
- apiBaseUrl: string;
124
- /** StackNet stack identifier */
125
- stackId?: string;
126
- /** Direct StackNet URL for client-side API calls (e.g. challenges) */
127
- stacknetUrl?: string;
128
- /** Theme preference */
129
- theme?: 'light' | 'dark' | 'system';
130
- }
131
- /** Callbacks for auth and billing events */
132
- interface UserUtilsCallbacks {
133
- onAuthSuccess?: (session: PublicSession) => void;
134
- onAuthError?: (error: Error) => void;
135
- onLogout?: () => void;
136
- onSubscriptionChange?: (subscription: Subscription) => void;
137
- }
138
- /** Server-side configuration for handler factories */
139
- interface ServerConfig {
140
- /** HMAC-SHA256 secret for signing JWTs */
141
- authSecret: string;
142
- /** StackNet backend URL (always https://stacknet.magma-rpc.com) */
143
- stacknetUrl: string;
144
- /** StackNet stack identifier */
145
- stackId: string;
146
- /** JWT secret for re-signing to StackNet (defaults to authSecret) */
147
- stacknetJwtSecret?: string;
148
- /** Cookie domain for subdomain sharing (e.g. '.geoff.ai') */
149
- cookieDomain?: string;
150
- /** Use Secure flag on cookies (default: true) */
151
- secureCookies?: boolean;
152
- /** Session max age in seconds (default: 604800 = 7 days) */
153
- sessionMaxAge?: number;
154
- /** JWT expiry in seconds (default: 900 = 15 minutes) */
155
- jwtExpiry?: number;
156
- /**
157
- * Google OAuth client ID (single). Used by createGoogleOneTapHandler to
158
- * validate the `aud` claim on incoming Google ID tokens. Required for
159
- * Google One Tap — without it, an ID token issued to any other Google
160
- * application could be replayed against this endpoint.
161
- */
162
- googleClientId?: string;
163
- /**
164
- * Google OAuth client IDs (multiple). Use when the stack accepts tokens
165
- * from more than one Google client (e.g. web + native).
166
- */
167
- googleClientIds?: string[];
168
- /**
169
- * How the handlers should derive the real client IP for rate-limit keys.
170
- * Defaults to `{ trustedProxyCount: 1 }`. Set `trustedProxyCount: 0` if
171
- * the app is exposed directly (no proxy) — otherwise any caller can pin
172
- * their X-Forwarded-For and bypass rate limiting.
173
- */
174
- ipConfig?: IPExtractorConfig;
175
- }
176
-
177
- export { type BillingPlan as B, type IPExtractorConfig as I, type PrepaidCheckoutResult as P, type Subscription as S, type UsageSummary as U, type BillingRecord as a, type PrepaidVerifyResult as b, type UserUtilsCallbacks as c, type UserUtilsConfig as d, type ServerConfig as e, decodeJWTPayload as f, extractIP as g, generateToken as h, verifyJWTSignature as i, maybeRefreshJWT as m, signJWT as s, verifyJWT as v };