@serialsubscriptions/platform-integration 0.0.8-5.1

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.
Files changed (64) hide show
  1. package/README.md +1 -0
  2. package/lib/SSIProject.d.ts +343 -0
  3. package/lib/SSIProject.js +429 -0
  4. package/lib/SSIProjectApi.d.ts +384 -0
  5. package/lib/SSIProjectApi.js +534 -0
  6. package/lib/SSISubscribedFeatureApi.d.ts +387 -0
  7. package/lib/SSISubscribedFeatureApi.js +511 -0
  8. package/lib/SSISubscribedLimitApi.d.ts +384 -0
  9. package/lib/SSISubscribedLimitApi.js +534 -0
  10. package/lib/SSISubscribedPlanApi.d.ts +395 -0
  11. package/lib/SSISubscribedPlanApi.js +567 -0
  12. package/lib/SubscribedPlanManager.d.ts +400 -0
  13. package/lib/SubscribedPlanManager.js +319 -0
  14. package/lib/UsageApi.d.ts +128 -0
  15. package/lib/UsageApi.js +224 -0
  16. package/lib/auth.server.d.ts +212 -0
  17. package/lib/auth.server.js +624 -0
  18. package/lib/cache/SSICache.d.ts +40 -0
  19. package/lib/cache/SSICache.js +134 -0
  20. package/lib/cache/backends/MemoryCacheBackend.d.ts +15 -0
  21. package/lib/cache/backends/MemoryCacheBackend.js +46 -0
  22. package/lib/cache/backends/RedisCacheBackend.d.ts +27 -0
  23. package/lib/cache/backends/RedisCacheBackend.js +95 -0
  24. package/lib/cache/constants.d.ts +7 -0
  25. package/lib/cache/constants.js +10 -0
  26. package/lib/cache/types.d.ts +27 -0
  27. package/lib/cache/types.js +2 -0
  28. package/lib/clientConfig.d.ts +42 -0
  29. package/lib/clientConfig.js +80 -0
  30. package/lib/frontend/index.d.ts +3 -0
  31. package/lib/frontend/index.js +12 -0
  32. package/lib/frontend/session/SessionClient.d.ts +36 -0
  33. package/lib/frontend/session/SessionClient.js +151 -0
  34. package/lib/index.d.ts +17 -0
  35. package/lib/index.js +40 -0
  36. package/lib/lib/session/SessionClient.d.ts +11 -0
  37. package/lib/lib/session/SessionClient.js +47 -0
  38. package/lib/lib/session/index.d.ts +3 -0
  39. package/lib/lib/session/index.js +3 -0
  40. package/lib/lib/session/stores/MemoryStore.d.ts +7 -0
  41. package/lib/lib/session/stores/MemoryStore.js +23 -0
  42. package/lib/lib/session/stores/index.d.ts +1 -0
  43. package/lib/lib/session/stores/index.js +1 -0
  44. package/lib/lib/session/types.d.ts +37 -0
  45. package/lib/lib/session/types.js +1 -0
  46. package/lib/requestConfig.d.ts +60 -0
  47. package/lib/requestConfig.js +151 -0
  48. package/lib/session/SessionClient.d.ts +19 -0
  49. package/lib/session/SessionClient.js +132 -0
  50. package/lib/session/SessionManager.d.ts +142 -0
  51. package/lib/session/SessionManager.js +437 -0
  52. package/lib/stateStore.d.ts +5 -0
  53. package/lib/stateStore.js +9 -0
  54. package/lib/storage/SSIStorage.d.ts +24 -0
  55. package/lib/storage/SSIStorage.js +117 -0
  56. package/lib/storage/backends/MemoryBackend.d.ts +10 -0
  57. package/lib/storage/backends/MemoryBackend.js +44 -0
  58. package/lib/storage/backends/PostgresBackend.d.ts +24 -0
  59. package/lib/storage/backends/PostgresBackend.js +106 -0
  60. package/lib/storage/backends/RedisBackend.d.ts +19 -0
  61. package/lib/storage/backends/RedisBackend.js +78 -0
  62. package/lib/storage/types.d.ts +27 -0
  63. package/lib/storage/types.js +2 -0
  64. package/package.json +74 -0
@@ -0,0 +1,212 @@
1
+ import { SSIStorage } from "./storage/SSIStorage";
2
+ import type { AuthConfigInput } from "./requestConfig";
3
+ export declare const scopes: {
4
+ readonly defaultScopes: "openid profile email view_project create_project delete_project update_project view_subscribed_plan view_subscribed_feature view_subscribed_limit access_subscription_usage";
5
+ };
6
+ /**
7
+ * Check if a JWT is expired (or within bufferSeconds of expiry) by decoding its payload.
8
+ * Does not verify the signature — use only for expiry checks (e.g. "should I refresh?").
9
+ * For authorization, use AuthServer.verifyJwt() or verifyAndDecodeJwt().
10
+ *
11
+ * @param jwt - The JWT string (e.g. access_token or id_token from session).
12
+ * @param bufferSeconds - If provided, treat the token as "expired" when it would expire within this many seconds (default: 0).
13
+ * @returns true if the token is expired (or expiring within bufferSeconds), or if the JWT is missing/invalid; false otherwise.
14
+ */
15
+ export declare function isJwtExpired(jwt: string, bufferSeconds?: number): boolean;
16
+ /**
17
+ * Get the number of seconds until a JWT expires, by decoding its payload.
18
+ * Does not verify the signature. For authorization use AuthServer.verifyJwt().
19
+ *
20
+ * @param jwt - The JWT string.
21
+ * @returns Seconds until expiry (0 if already expired), or null if the JWT has no exp claim or is invalid.
22
+ */
23
+ export declare function getJwtExpirySeconds(jwt: string): number | null;
24
+ export type AuthConfig = {
25
+ issuerBaseUrl?: string;
26
+ authorizationEndpoint?: string;
27
+ tokenEndpoint?: string;
28
+ logoutEndpoint?: string;
29
+ jwksUri?: string;
30
+ jwksPath?: string;
31
+ jwks?: {
32
+ keys: Array<{
33
+ kty: string;
34
+ n: string;
35
+ e: string;
36
+ kid?: string;
37
+ }>;
38
+ };
39
+ clientId?: string;
40
+ clientSecret?: string;
41
+ redirectUri?: string;
42
+ scopes?: string | string[];
43
+ audience?: string;
44
+ storage?: SSIStorage;
45
+ };
46
+ export type LoginUrlOptions = {
47
+ stateKey?: string;
48
+ extraParams?: Record<string, string>;
49
+ stateTtlSeconds?: number;
50
+ };
51
+ export type CallbackParams = {
52
+ code?: string | null;
53
+ state?: string | null;
54
+ };
55
+ export type TokenResponse = {
56
+ access_token?: string;
57
+ id_token?: string;
58
+ token_type?: string;
59
+ refresh_token?: string;
60
+ expires_in?: number;
61
+ scope?: string;
62
+ raw: any;
63
+ id_claims?: Record<string, unknown>;
64
+ access_claims?: Record<string, unknown>;
65
+ };
66
+ export declare class AuthServer {
67
+ private cfg;
68
+ private stateStorage;
69
+ private jwksCache;
70
+ private lastTokens?;
71
+ /** Accepts AuthConfig or SSIRequestConfig (e.g. from getRequestConfig(req)). */
72
+ constructor(config?: AuthConfigInput);
73
+ /**
74
+ * Build the authorization URL and persist a CSRF state for the callback.
75
+ * Returns: { url, stateKey, stateValue }
76
+ */
77
+ getLoginUrl(opts?: LoginUrlOptions): Promise<{
78
+ url: string;
79
+ stateKey: string;
80
+ stateValue: `${string}-${string}-${string}-${string}-${string}`;
81
+ }>;
82
+ /**
83
+ * Build the logout URL for RP-initiated logout.
84
+ * Common params: id_token_hint, post_logout_redirect_uri
85
+ */
86
+ getLogoutUrl(opts?: {
87
+ idTokenHint?: string;
88
+ postLogoutRedirectUri?: string;
89
+ extraParams?: Record<string, string>;
90
+ }): {
91
+ url: string;
92
+ };
93
+ /**
94
+ * Handle the OAuth callback: validates state and exchanges the code for tokens.
95
+ * Returns TokenResponse (and includes raw for debugging).
96
+ */
97
+ handleCallback(params: CallbackParams): Promise<TokenResponse>;
98
+ /**
99
+ * Exchange a refresh_token for new tokens.
100
+ * Verifies any returned JWTs and returns a TokenResponse.
101
+ *
102
+ * @param refreshToken - The refresh token to exchange for new tokens
103
+ * @param options - Optional parameters for the refresh request
104
+ * @param options.scope - Optional scope parameter (cannot exceed originally granted scope)
105
+ * @param options.useAuthHeader - Use Authorization header instead of client_secret in body
106
+ * @returns Promise<TokenResponse> - New tokens with verified claims
107
+ */
108
+ refreshTokens(refreshToken: string, options?: {
109
+ scope?: string;
110
+ useAuthHeader?: boolean;
111
+ }): Promise<TokenResponse>;
112
+ /**
113
+ * Fetch and cache JWKS from the remote endpoint.
114
+ * Uses SSICache to cache the JWKS response.
115
+ * @private
116
+ */
117
+ private fetchAndCacheJWKS;
118
+ /**
119
+ * Verify JWT signature and claims using jose library.
120
+ * Handles JWKS fetching, caching, kid selection, and algorithm validation.
121
+ * @private
122
+ */
123
+ private verifyWithIssuer;
124
+ /**
125
+ * Public method to verify a JWT.
126
+ * Use this in middleware or anywhere you need to validate tokens.
127
+ */
128
+ verifyJwt<T = unknown>(jwt: string): Promise<T>;
129
+ /**
130
+ * Verify and decode a JWT before returning its claims.
131
+ * Throws if the JWT is invalid, expired, or fails signature verification.
132
+ *
133
+ * This method is safe to use in middleware or debugging contexts when you
134
+ * simply need the decoded, verified claims from a JWT string.
135
+ */
136
+ verifyAndDecodeJwt<T = unknown>(jwt?: string): Promise<T>;
137
+ /**
138
+ * Automatically refresh tokens if they are expired or about to expire.
139
+ * This is a convenience method that handles the refresh logic automatically.
140
+ *
141
+ * @param refreshToken - The refresh token to use for refreshing
142
+ * @param options - Optional parameters for the refresh request
143
+ * @param options.bufferSeconds - How many seconds before expiry to consider tokens as "about to expire" (default: 60)
144
+ * @param options.scope - Optional scope parameter
145
+ * @param options.useAuthHeader - Use Authorization header instead of client_secret in body
146
+ * @returns Promise<TokenResponse | null> - New tokens if refreshed, null if not needed
147
+ */
148
+ autoRefreshTokens(refreshToken: string, options?: {
149
+ bufferSeconds?: number;
150
+ scope?: string;
151
+ useAuthHeader?: boolean;
152
+ }): Promise<TokenResponse | null>;
153
+ /**
154
+ * Check if the current tokens are expired or about to expire.
155
+ *
156
+ * @param bufferSeconds - How many seconds before expiry to consider tokens as "about to expire" (default: 60)
157
+ * @returns boolean - true if tokens need refreshing, false otherwise
158
+ */
159
+ needsTokenRefresh(bufferSeconds?: number): boolean;
160
+ /**
161
+ * Get the time until the current access token expires.
162
+ *
163
+ * @returns number - Seconds until expiry, or 0 if no token or expired
164
+ */
165
+ getTokenExpirySeconds(): number;
166
+ /** Returns the last TokenResponse produced by handleCallback/refreshTokens in this instance */
167
+ getLastTokenResponse(): TokenResponse | undefined;
168
+ }
169
+ /**
170
+ * USAGE EXAMPLES
171
+ *
172
+ * // Basic token refresh
173
+ * const auth = new AuthServer(config);
174
+ * const newTokens = await auth.refreshTokens(refreshToken);
175
+ *
176
+ * // Refresh with Authorization header instead of client_secret in body
177
+ * const newTokens = await auth.refreshTokens(refreshToken, {
178
+ * useAuthHeader: true
179
+ * });
180
+ *
181
+ * // Refresh with specific scope (cannot exceed originally granted scope)
182
+ * const newTokens = await auth.refreshTokens(refreshToken, {
183
+ * scope: "openid profile email offline_access"
184
+ * });
185
+ *
186
+ * // Automatic refresh - only refreshes if tokens are expired or about to expire
187
+ * const newTokens = await auth.autoRefreshTokens(refreshToken, {
188
+ * bufferSeconds: 120, // Refresh if expires within 2 minutes
189
+ * useAuthHeader: true
190
+ * });
191
+ *
192
+ * // Check if tokens need refreshing
193
+ * if (auth.needsTokenRefresh(60)) {
194
+ * const newTokens = await auth.refreshTokens(refreshToken);
195
+ * }
196
+ *
197
+ * // Get time until token expires
198
+ * const secondsUntilExpiry = auth.getTokenExpirySeconds();
199
+ * console.log(`Token expires in ${secondsUntilExpiry} seconds`);
200
+ *
201
+ * // Error handling
202
+ * try {
203
+ * const newTokens = await auth.refreshTokens(refreshToken);
204
+ * // Use new tokens...
205
+ * } catch (error) {
206
+ * if (error.message.includes('invalid_grant')) {
207
+ * // Refresh token is invalid/expired - redirect to login
208
+ * return Response.redirect('/login');
209
+ * }
210
+ * // Handle other errors...
211
+ * }
212
+ */