@scalekit-sdk/node 2.1.7 → 2.1.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/buf.gen.yaml +1 -0
- package/lib/auth.d.ts +41 -9
- package/lib/auth.js +44 -12
- package/lib/auth.js.map +1 -1
- package/lib/connection.d.ts +195 -21
- package/lib/connection.js +197 -23
- package/lib/connection.js.map +1 -1
- package/lib/core.d.ts +2 -2
- package/lib/core.js +13 -12
- package/lib/core.js.map +1 -1
- package/lib/directory.d.ts +293 -40
- package/lib/directory.js +308 -44
- package/lib/directory.js.map +1 -1
- package/lib/domain.d.ts +166 -18
- package/lib/domain.js +178 -29
- package/lib/domain.js.map +1 -1
- package/lib/organization.d.ts +404 -44
- package/lib/organization.js +419 -54
- package/lib/organization.js.map +1 -1
- package/lib/permission.d.ts +179 -35
- package/lib/permission.js +190 -38
- package/lib/permission.js.map +1 -1
- package/lib/pkg/grpc/scalekit/v1/auth/auth_connect.d.ts +3 -3
- package/lib/pkg/grpc/scalekit/v1/auth/auth_connect.js +2 -2
- package/lib/pkg/grpc/scalekit/v1/auth/auth_connect.js.map +1 -1
- package/lib/pkg/grpc/scalekit/v1/auth/auth_pb.d.ts +16 -16
- package/lib/pkg/grpc/scalekit/v1/auth/auth_pb.js +21 -21
- package/lib/pkg/grpc/scalekit/v1/auth/auth_pb.js.map +1 -1
- package/lib/pkg/grpc/scalekit/v1/auth/webauthn_connect.d.ts +82 -0
- package/lib/pkg/grpc/scalekit/v1/auth/webauthn_connect.js +90 -0
- package/lib/pkg/grpc/scalekit/v1/auth/webauthn_connect.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/auth/webauthn_pb.d.ts +647 -0
- package/lib/pkg/grpc/scalekit/v1/auth/webauthn_pb.js +993 -0
- package/lib/pkg/grpc/scalekit/v1/auth/webauthn_pb.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.d.ts +142 -0
- package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.js +165 -1
- package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.js.map +1 -1
- package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.d.ts +1 -10
- package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.js +0 -9
- package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.js.map +1 -1
- package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.d.ts +28 -63
- package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.js +9 -90
- package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.js.map +1 -1
- package/lib/pkg/grpc/scalekit/v1/users/users_pb.d.ts +2 -2
- package/lib/pkg/grpc/scalekit/v1/users/users_pb.js +1 -1
- package/lib/pkg/grpc/scalekit/v1/users/users_pb.js.map +1 -1
- package/lib/role.d.ts +252 -56
- package/lib/role.js +262 -62
- package/lib/role.js.map +1 -1
- package/lib/scalekit.d.ts +323 -54
- package/lib/scalekit.js +354 -76
- package/lib/scalekit.js.map +1 -1
- package/lib/session.d.ts +235 -22
- package/lib/session.js +237 -24
- package/lib/session.js.map +1 -1
- package/lib/user.d.ts +571 -53
- package/lib/user.js +598 -89
- package/lib/user.js.map +1 -1
- package/lib/webauthn.d.ts +33 -0
- package/lib/webauthn.js +80 -0
- package/lib/webauthn.js.map +1 -0
- package/package.json +2 -2
- package/src/auth.ts +53 -19
- package/src/connection.ts +237 -62
- package/src/core.ts +39 -33
- package/src/directory.ts +356 -98
- package/src/domain.ts +215 -68
- package/src/organization.ts +490 -112
- package/src/permission.ts +234 -88
- package/src/pkg/grpc/scalekit/v1/auth/auth_connect.ts +3 -3
- package/src/pkg/grpc/scalekit/v1/auth/auth_pb.ts +24 -24
- package/src/pkg/grpc/scalekit/v1/auth/webauthn_connect.ts +89 -0
- package/src/pkg/grpc/scalekit/v1/auth/webauthn_pb.ts +1263 -0
- package/src/pkg/grpc/scalekit/v1/commons/commons_pb.ts +217 -0
- package/src/pkg/grpc/scalekit/v1/connections/connections_connect.ts +1 -10
- package/src/pkg/grpc/scalekit/v1/connections/connections_pb.ts +42 -129
- package/src/pkg/grpc/scalekit/v1/users/users_pb.ts +3 -3
- package/src/role.ts +336 -136
- package/src/scalekit.ts +478 -149
- package/src/session.ts +266 -63
- package/src/user.ts +675 -168
- package/src/webauthn.ts +98 -0
package/lib/scalekit.d.ts
CHANGED
|
@@ -8,17 +8,39 @@ import UserClient from './user';
|
|
|
8
8
|
import SessionClient from './session';
|
|
9
9
|
import RoleClient from './role';
|
|
10
10
|
import PermissionClient from './permission';
|
|
11
|
+
import WebAuthnClient from './webauthn';
|
|
11
12
|
import { IdpInitiatedLoginClaims } from './types/auth';
|
|
12
13
|
import { AuthenticationOptions, AuthenticationResponse, AuthorizationUrlOptions, LogoutUrlOptions, RefreshTokenResponse, TokenValidationOptions } from './types/scalekit';
|
|
13
14
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
15
|
+
* Main Scalekit SDK client for interacting with all Scalekit API endpoints.
|
|
16
|
+
*
|
|
17
|
+
* TIP: You can use it as a singleton object - that is you can initialize it just once and use the same client variable wherever required.
|
|
18
|
+
*
|
|
19
|
+
* This is the primary entry point for interacting with Scalekit's authentication services,
|
|
20
|
+
* including SSO, SCIM, user management, roles, permissions, and passwordless authentication.
|
|
21
|
+
*
|
|
22
|
+
* You can find the Environment URL, Client ID and Client Secret in Scalekit Dashboard -> Developers (Settings) -> API Credentials
|
|
23
|
+
*
|
|
24
|
+
* @param {string} envUrl - The Scalekit environment URL (e.g., "https://yourorg.scalekit.com" or your configured custom domain like "https://auth.yourapp.ai")
|
|
25
|
+
* @param {string} clientId - Your Scalekit client ID from the Scalekit Dashboard
|
|
26
|
+
* @param {string} clientSecret - Your Scalekit client secret from the Scalekit Dashboard
|
|
27
|
+
*
|
|
19
28
|
* @example
|
|
20
|
-
*
|
|
21
|
-
|
|
29
|
+
* // Initialize the Scalekit client
|
|
30
|
+
* import { ScalekitClient } from '@scalekit-sdk/node';
|
|
31
|
+
*
|
|
32
|
+
* const scalekitClient = new ScalekitClient(
|
|
33
|
+
* process.env.SCALEKIT_ENV_URL,
|
|
34
|
+
* process.env.SCALEKIT_CLIENT_ID,
|
|
35
|
+
* process.env.SCALEKIT_CLIENT_SECRET
|
|
36
|
+
* );
|
|
37
|
+
*
|
|
38
|
+
* // Access various client modules
|
|
39
|
+
* const organizations = await scalekitClient.organization.listOrganization();
|
|
40
|
+
* const users = await scalekitClient.user.listUsers();
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://docs.scalekit.com/apis/ | Scalekit API Documentation}
|
|
43
|
+
*/
|
|
22
44
|
export default class ScalekitClient {
|
|
23
45
|
private readonly coreClient;
|
|
24
46
|
private readonly grpcConnect;
|
|
@@ -32,48 +54,194 @@ export default class ScalekitClient {
|
|
|
32
54
|
readonly role: RoleClient;
|
|
33
55
|
readonly permission: PermissionClient;
|
|
34
56
|
readonly auth: AuthClient;
|
|
57
|
+
readonly webauthn: WebAuthnClient;
|
|
35
58
|
constructor(envUrl: string, clientId: string, clientSecret: string);
|
|
36
59
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* @param {string}
|
|
43
|
-
*
|
|
44
|
-
* @param {
|
|
45
|
-
* @param {string} options.
|
|
46
|
-
* @param {string} options.
|
|
47
|
-
* @param {string} options.
|
|
48
|
-
* @param {string} options.
|
|
49
|
-
* @param {string} options.
|
|
50
|
-
* @param {string} options.
|
|
60
|
+
* Utility method to generate the OAuth 2.0 authorization URL to initiate the SSO authentication flow.
|
|
61
|
+
*
|
|
62
|
+
* This method doesn't make any network calls but instead generates a fully formed Authorization URL
|
|
63
|
+
* as a string that you can redirect your users to initiate authentication.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} redirectUri - The URL where users will be redirected after authentication.
|
|
66
|
+
* Must match one of the redirect URIs configured in your Scalekit dashboard.
|
|
67
|
+
* @param {AuthorizationUrlOptions} [options] - Optional configuration for the authorization request
|
|
68
|
+
* @param {string[]} [options.scopes=['openid', 'profile', 'email']] - OAuth scopes to request. Default includes openid, profile, and email.
|
|
69
|
+
* @param {string} [options.state] - Opaque value to maintain state between request and callback. Used to prevent CSRF attacks.
|
|
70
|
+
* @param {string} [options.nonce] - String value used to associate a client session with an ID Token.
|
|
71
|
+
* @param {string} [options.loginHint] - Hint to the authorization server about the login identifier the user might use (e.g., email address).
|
|
72
|
+
* @param {string} [options.domainHint] - Domain hint to identify which organization's IdP to use for authentication.
|
|
73
|
+
* @param {string} [options.connectionId] - Specific SSO connection ID to use for authentication.
|
|
74
|
+
* @param {string} [options.organizationId] - Organization ID to authenticate against.
|
|
75
|
+
* @param {string} [options.provider] - Social login provider (e.g., 'google', 'github', 'microsoft').
|
|
76
|
+
* @param {string} [options.codeChallenge] - PKCE code challenge for enhanced security in public clients.
|
|
77
|
+
* @param {string} [options.codeChallengeMethod] - Method used to generate the code challenge (we support only 'S256').
|
|
78
|
+
* @param {string} [options.prompt] - Controls the authorization server's authentication behavior (e.g., 'login', 'consent', 'create').
|
|
79
|
+
*
|
|
80
|
+
* @returns {string} The complete authorization URL to redirect the user to
|
|
51
81
|
*
|
|
52
82
|
* @example
|
|
53
|
-
*
|
|
54
|
-
* const
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
83
|
+
* // Initiate Enterprise SSO authentication for a given org_id
|
|
84
|
+
* const authUrl = scalekitClient.getAuthorizationUrl(
|
|
85
|
+
* 'https://yourapp.com/auth/callback',
|
|
86
|
+
* {
|
|
87
|
+
* state: 'random-state-value',
|
|
88
|
+
* organizationId: 'org_123456'
|
|
89
|
+
* }
|
|
90
|
+
* );
|
|
91
|
+
* // Redirect user to authUrl
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* // Initiate Enterprise SSO authentication for a specific connection id
|
|
95
|
+
* // optionally, pass the loginhint to the 3rd party identity provider.
|
|
96
|
+
* const authUrl = scalekitClient.getAuthorizationUrl(
|
|
97
|
+
* 'https://yourapp.com/auth/callback',
|
|
98
|
+
* {
|
|
99
|
+
* connectionId: 'conn_abc123',
|
|
100
|
+
* loginHint: 'user@company.com'
|
|
101
|
+
* }
|
|
102
|
+
* );
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* // Social login
|
|
106
|
+
* const authUrl = scalekitClient.getAuthorizationUrl(
|
|
107
|
+
* 'https://yourapp.com/auth/callback',
|
|
108
|
+
* {
|
|
109
|
+
* provider: 'google',
|
|
110
|
+
* state: 'random-state'
|
|
111
|
+
* }
|
|
112
|
+
* );
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* // PKCE flow for public clients
|
|
116
|
+
* const authUrl = scalekitClient.getAuthorizationUrl(
|
|
117
|
+
* 'https://yourapp.com/auth/callback',
|
|
118
|
+
* {
|
|
119
|
+
* codeChallenge: 'your-code-challenge',
|
|
120
|
+
* codeChallengeMethod: 'S256',
|
|
121
|
+
* organizationId: 'org_123456'
|
|
122
|
+
* }
|
|
123
|
+
* );
|
|
124
|
+
*
|
|
125
|
+
* @see {@link https://docs.scalekit.com/apis/#tag/api%20auth | Authentication API Documentation}
|
|
126
|
+
* @see {@link authenticateWithCode} - Use this method to exchange the authorization code for tokens
|
|
59
127
|
*/
|
|
60
128
|
getAuthorizationUrl(redirectUri: string, options?: AuthorizationUrlOptions): string;
|
|
61
129
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
130
|
+
* Exchanges an authorization code for access tokens and user information.
|
|
131
|
+
*
|
|
132
|
+
* This method completes the OAuth 2.0 authorization code flow by exchanging the code
|
|
133
|
+
* received in the callback for access tokens, ID tokens, and user profile information.
|
|
134
|
+
* Call this method in your redirect URI handler after receiving the authorization code.
|
|
135
|
+
*
|
|
136
|
+
* @param {string} code - The authorization code received in the callback URL after user authentication
|
|
137
|
+
* @param {string} redirectUri - The same redirect URI used in getAuthorizationUrl(). Must match exactly.
|
|
138
|
+
* @param {AuthenticationOptions} [options] - Optional authentication configuration
|
|
139
|
+
* @param {string} [options.codeVerifier] - PKCE code verifier to validate the code challenge (required if PKCE was used)
|
|
140
|
+
*
|
|
141
|
+
* @returns {Promise<AuthenticationResponse>} Authentication response containing:
|
|
142
|
+
* - user: User profile information (email, name, organization, etc.)
|
|
143
|
+
* - idToken: JWT ID token containing user claims
|
|
144
|
+
* - accessToken: Access token for API authorization
|
|
145
|
+
* - expiresIn: Token expiration time in seconds
|
|
146
|
+
* - refreshToken: Refresh token for obtaining new access tokens
|
|
147
|
+
*
|
|
148
|
+
* @throws {Error} When the authorization code is invalid, expired, or already used
|
|
149
|
+
* @throws {Error} When the redirect URI doesn't match the one used in authorization
|
|
150
|
+
* @throws {Error} When PKCE code verifier is invalid or missing
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* // Basic code exchange (server-side flow)
|
|
154
|
+
* app.get('/auth/callback', async (req, res) => {
|
|
155
|
+
* const { code } = req.query;
|
|
156
|
+
*
|
|
157
|
+
* try {
|
|
158
|
+
* const result = await scalekitClient.authenticateWithCode(
|
|
159
|
+
* code,
|
|
160
|
+
* 'https://yourapp.com/auth/callback'
|
|
161
|
+
* );
|
|
162
|
+
*
|
|
163
|
+
* // Store tokens securely
|
|
164
|
+
* req.session.accessToken = result.accessToken;
|
|
165
|
+
* req.session.user = result.user;
|
|
166
|
+
*
|
|
167
|
+
* res.redirect('/dashboard');
|
|
168
|
+
* } catch (error) {
|
|
169
|
+
* console.error('Authentication failed:', error);
|
|
170
|
+
* res.redirect('/login?error=auth_failed');
|
|
171
|
+
* }
|
|
172
|
+
* });
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* // PKCE flow (for public clients)
|
|
176
|
+
* app.get('/auth/callback', async (req, res) => {
|
|
177
|
+
* const { code } = req.query;
|
|
178
|
+
* const codeVerifier = req.session.codeVerifier; // Stored during authorization
|
|
179
|
+
*
|
|
180
|
+
* const result = await scalekitClient.authenticateWithCode(
|
|
181
|
+
* code,
|
|
182
|
+
* 'https://yourapp.com/auth/callback',
|
|
183
|
+
* { codeVerifier }
|
|
184
|
+
* );
|
|
185
|
+
*
|
|
186
|
+
* // Use result.user, result.accessToken, etc.
|
|
187
|
+
* });
|
|
188
|
+
*
|
|
189
|
+
* @see {@link getAuthorizationUrl} - Generate the authorization URL first
|
|
190
|
+
* @see {@link validateAccessToken} - Validate tokens in subsequent requests
|
|
68
191
|
*/
|
|
69
192
|
authenticateWithCode(code: string, redirectUri: string, options?: AuthenticationOptions): Promise<AuthenticationResponse>;
|
|
70
193
|
/**
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
194
|
+
* Extracts and validates claims from an IdP-initiated login token.
|
|
195
|
+
*
|
|
196
|
+
* Use this method when handling IdP-initiated SSO flows, where the authentication is
|
|
197
|
+
* initiated from the identity provider's portal rather than your application. This validates
|
|
198
|
+
* the token and returns the necessary information to initiate a new SP Initiated SSO workflow.
|
|
199
|
+
*
|
|
200
|
+
* @param {string} idpInitiatedLoginToken - The token received in the 'idp_initiated_login' query parameter
|
|
201
|
+
* @param {TokenValidationOptions} [options] - Optional token validation configuration
|
|
202
|
+
* @param {string} [options.issuer] - Expected token issuer for validation
|
|
203
|
+
* @param {string} [options.audience] - Expected token audience for validation
|
|
204
|
+
*
|
|
205
|
+
* @returns {Promise<IdpInitiatedLoginClaims>} Claims containing:
|
|
206
|
+
* - connection_id: The SSO connection identifier
|
|
207
|
+
* - organization_id: The organization identifier
|
|
208
|
+
* - login_hint: User's email or login identifier
|
|
209
|
+
* - relay_state: Optional state parameter from the IdP
|
|
210
|
+
*
|
|
211
|
+
* @throws {ScalekitValidateTokenFailureException} When token validation fails
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* // Handle IdP-initiated login
|
|
215
|
+
* app.get('/auth/callback', async (req, res) => {
|
|
216
|
+
* const { idp_initiated_login } = req.query;
|
|
217
|
+
*
|
|
218
|
+
* if (idp_initiated_login) {
|
|
219
|
+
* try {
|
|
220
|
+
* const claims = await scalekitClient.getIdpInitiatedLoginClaims(idp_initiated_login);
|
|
221
|
+
*
|
|
222
|
+
* // Redirect to authorization URL with the claims
|
|
223
|
+
* const authUrl = scalekitClient.getAuthorizationUrl(
|
|
224
|
+
* 'https://yourapp.com/auth/callback',
|
|
225
|
+
* {
|
|
226
|
+
* connectionId: claims.connection_id,
|
|
227
|
+
* organizationId: claims.organization_id,
|
|
228
|
+
* loginHint: claims.login_hint,
|
|
229
|
+
* ...(claims.relay_state && { state: claims.relay_state })
|
|
230
|
+
* }
|
|
231
|
+
* );
|
|
232
|
+
*
|
|
233
|
+
* return res.redirect(authUrl);
|
|
234
|
+
* } catch (error) {
|
|
235
|
+
* console.error('IdP-initiated login failed:', error);
|
|
236
|
+
* return res.redirect('/login?error=idp_login_failed');
|
|
237
|
+
* }
|
|
238
|
+
* }
|
|
239
|
+
* // Handle normal callback flow...
|
|
240
|
+
* });
|
|
241
|
+
*
|
|
242
|
+
* @see {@link https://docs.scalekit.com/sso/guides/idp-init-sso/ | IdP-Initiated SSO Documentation}
|
|
243
|
+
* @see {@link getAuthorizationUrl} - Use the claims to construct the authorization URL
|
|
244
|
+
*/
|
|
77
245
|
getIdpInitiatedLoginClaims(idpInitiatedLoginToken: string, options?: TokenValidationOptions): Promise<IdpInitiatedLoginClaims>;
|
|
78
246
|
/**
|
|
79
247
|
* Validates the access token and returns a boolean result.
|
|
@@ -100,21 +268,69 @@ export default class ScalekitClient {
|
|
|
100
268
|
*/
|
|
101
269
|
getLogoutUrl(options?: LogoutUrlOptions): string;
|
|
102
270
|
/**
|
|
103
|
-
*
|
|
271
|
+
* Verifies the authenticity and integrity of webhook payloads from Scalekit.
|
|
104
272
|
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
273
|
+
* Use this method to validate webhook requests from Scalekit by verifying the HMAC signature.
|
|
274
|
+
* This ensures the webhook was sent by Scalekit and hasn't been tampered with. The method
|
|
275
|
+
* checks the signature and timestamp to prevent replay attacks (5-minute tolerance window).
|
|
276
|
+
*
|
|
277
|
+
* @param {string} secret - Your webhook signing secret from the Scalekit dashboard (format: 'whsec_...')
|
|
278
|
+
* @param {Record<string, string>} headers - The HTTP headers from the webhook request
|
|
279
|
+
* @param {string} payload - The raw webhook request body as a string
|
|
280
|
+
*
|
|
281
|
+
* @returns {boolean} Returns true if the webhook signature is valid
|
|
282
|
+
*
|
|
283
|
+
* @throws {WebhookVerificationError} When required headers are missing
|
|
284
|
+
* @throws {WebhookVerificationError} When the secret format is invalid
|
|
285
|
+
* @throws {WebhookVerificationError} When the signature doesn't match
|
|
286
|
+
* @throws {WebhookVerificationError} When the timestamp is too old (>5 minutes) or in the future
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* // Express.js webhook handler
|
|
290
|
+
* app.post('/webhooks/scalekit', express.raw({ type: 'application/json' }), (req, res) => {
|
|
291
|
+
* const secret = process.env.SCALEKIT_WEBHOOK_SECRET;
|
|
292
|
+
* const headers = req.headers;
|
|
293
|
+
* const payload = req.body.toString();
|
|
294
|
+
*
|
|
295
|
+
* try {
|
|
296
|
+
* const isValid = scalekitClient.verifyWebhookPayload(secret, headers, payload);
|
|
297
|
+
*
|
|
298
|
+
* if (isValid) {
|
|
299
|
+
* const event = JSON.parse(payload);
|
|
300
|
+
*
|
|
301
|
+
* // Process the webhook event
|
|
302
|
+
* switch (event.type) {
|
|
303
|
+
* case 'user.created':
|
|
304
|
+
* console.log('New user created:', event.data);
|
|
305
|
+
* break;
|
|
306
|
+
* case 'connection.enabled':
|
|
307
|
+
* console.log('Connection enabled:', event.data);
|
|
308
|
+
* break;
|
|
309
|
+
* }
|
|
310
|
+
*
|
|
311
|
+
* res.status(200).send('Webhook received');
|
|
312
|
+
* }
|
|
313
|
+
* } catch (error) {
|
|
314
|
+
* console.error('Webhook verification failed:', error);
|
|
315
|
+
* res.status(400).send('Invalid webhook signature');
|
|
316
|
+
* }
|
|
317
|
+
* });
|
|
318
|
+
*
|
|
319
|
+
* @see {@link https://docs.scalekit.com/reference/webhooks/overview/ | Webhook Documentation}
|
|
320
|
+
* @see {@link verifyInterceptorPayload} - Similar method for interceptor payloads
|
|
109
321
|
*/
|
|
110
322
|
verifyWebhookPayload(secret: string, headers: Record<string, string>, payload: string): boolean;
|
|
111
323
|
/**
|
|
112
|
-
*
|
|
324
|
+
* Verifies the authenticity and integrity of interceptor payloads from Scalekit.
|
|
113
325
|
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
326
|
+
* Use this method to validate HTTP interceptor requests from Scalekit by verifying the HMAC signature.
|
|
327
|
+
* This ensures the interceptor payload was sent by Scalekit and hasn't been tampered with. The method
|
|
328
|
+
* checks the signature and timestamp to prevent replay attacks (5-minute tolerance window)
|
|
329
|
+
*
|
|
330
|
+
* @param {string} secret Your interceptor signing secret that you can copy from Scalekit Dashboard
|
|
331
|
+
* @param {Record<string, string>} headers The HTTP headers from the interceptor request
|
|
332
|
+
* @param {string} payload The raw interceptor request body as a string
|
|
333
|
+
* @return {boolean} Returns true if the interceptor payload is valid.
|
|
118
334
|
*/
|
|
119
335
|
verifyInterceptorPayload(secret: string, headers: Record<string, string>, payload: string): boolean;
|
|
120
336
|
/**
|
|
@@ -129,7 +345,7 @@ export default class ScalekitClient {
|
|
|
129
345
|
*/
|
|
130
346
|
private verifyPayloadSignature;
|
|
131
347
|
/**
|
|
132
|
-
* Validates a token and returns
|
|
348
|
+
* Validates a token and returns the claims as json payload if valid.
|
|
133
349
|
* Supports issuer, audience, and scope validation.
|
|
134
350
|
*
|
|
135
351
|
* @param {string} token The token to be validated
|
|
@@ -170,10 +386,63 @@ export default class ScalekitClient {
|
|
|
170
386
|
*/
|
|
171
387
|
private computeSignature;
|
|
172
388
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
389
|
+
* Obtains a new access token using a refresh token.
|
|
390
|
+
*
|
|
391
|
+
* Use this method to get a new access token when the current one expires, without requiring
|
|
392
|
+
* the user to re-authenticate. This implements the OAuth 2.0 refresh token grant type.
|
|
393
|
+
* The method returns both a new access token and a new refresh token (token rotation).
|
|
394
|
+
*
|
|
395
|
+
* @param {string} refreshToken - The refresh token obtained from a previous authentication
|
|
396
|
+
*
|
|
397
|
+
* @returns {Promise<RefreshTokenResponse>} Response containing:
|
|
398
|
+
* - accessToken: New access token for API authorization
|
|
399
|
+
* - refreshToken: New refresh token (the old one is invalidated)
|
|
400
|
+
*
|
|
401
|
+
* @throws {Error} When the refresh token is missing
|
|
402
|
+
* @throws {Error} When the refresh token is invalid, expired, or revoked
|
|
403
|
+
* @throws {Error} When the authentication server response is invalid
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* // Refresh tokens before they expire
|
|
407
|
+
* async function refreshUserToken(userId) {
|
|
408
|
+
* try {
|
|
409
|
+
* const oldRefreshToken = await getStoredRefreshToken(userId);
|
|
410
|
+
*
|
|
411
|
+
* const result = await scalekitClient.refreshAccessToken(oldRefreshToken);
|
|
412
|
+
*
|
|
413
|
+
* // Store the new tokens (old refresh token is now invalid)
|
|
414
|
+
* await storeTokens(userId, {
|
|
415
|
+
* accessToken: result.accessToken,
|
|
416
|
+
* refreshToken: result.refreshToken
|
|
417
|
+
* });
|
|
418
|
+
*
|
|
419
|
+
* return result.accessToken;
|
|
420
|
+
* } catch (error) {
|
|
421
|
+
* console.error('Token refresh failed:', error);
|
|
422
|
+
* // Redirect user to login
|
|
423
|
+
* throw new Error('Please log in again');
|
|
424
|
+
* }
|
|
425
|
+
* }
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* // Automatic token refresh middleware
|
|
429
|
+
* app.use(async (req, res, next) => {
|
|
430
|
+
* const accessToken = req.session.accessToken;
|
|
431
|
+
* const refreshToken = req.session.refreshToken;
|
|
432
|
+
*
|
|
433
|
+
* // Check if access token is expired (decode JWT and check exp claim)
|
|
434
|
+
* if (isTokenExpired(accessToken) && refreshToken) {
|
|
435
|
+
* try {
|
|
436
|
+
* const result = await scalekitClient.refreshAccessToken(refreshToken);
|
|
437
|
+
* req.session.accessToken = result.accessToken;
|
|
438
|
+
* req.session.refreshToken = result.refreshToken;
|
|
439
|
+
* } catch (error) {
|
|
440
|
+
* return res.redirect('/login');
|
|
441
|
+
* }
|
|
442
|
+
* }
|
|
443
|
+
* next();
|
|
444
|
+
* });
|
|
445
|
+
*
|
|
177
446
|
*/
|
|
178
447
|
refreshAccessToken(refreshToken: string): Promise<RefreshTokenResponse>;
|
|
179
448
|
}
|