@scalekit-sdk/node 2.1.6 → 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.
Files changed (92) hide show
  1. package/buf.gen.yaml +1 -0
  2. package/lib/auth.d.ts +41 -9
  3. package/lib/auth.js +44 -12
  4. package/lib/auth.js.map +1 -1
  5. package/lib/connection.d.ts +195 -21
  6. package/lib/connection.js +197 -23
  7. package/lib/connection.js.map +1 -1
  8. package/lib/core.d.ts +2 -2
  9. package/lib/core.js +13 -12
  10. package/lib/core.js.map +1 -1
  11. package/lib/directory.d.ts +293 -40
  12. package/lib/directory.js +308 -44
  13. package/lib/directory.js.map +1 -1
  14. package/lib/domain.d.ts +166 -18
  15. package/lib/domain.js +178 -29
  16. package/lib/domain.js.map +1 -1
  17. package/lib/organization.d.ts +407 -40
  18. package/lib/organization.js +433 -49
  19. package/lib/organization.js.map +1 -1
  20. package/lib/permission.d.ts +179 -35
  21. package/lib/permission.js +190 -38
  22. package/lib/permission.js.map +1 -1
  23. package/lib/pkg/grpc/scalekit/v1/auth/auth_connect.d.ts +3 -3
  24. package/lib/pkg/grpc/scalekit/v1/auth/auth_connect.js +2 -2
  25. package/lib/pkg/grpc/scalekit/v1/auth/auth_connect.js.map +1 -1
  26. package/lib/pkg/grpc/scalekit/v1/auth/auth_pb.d.ts +16 -16
  27. package/lib/pkg/grpc/scalekit/v1/auth/auth_pb.js +21 -21
  28. package/lib/pkg/grpc/scalekit/v1/auth/auth_pb.js.map +1 -1
  29. package/lib/pkg/grpc/scalekit/v1/auth/webauthn_connect.d.ts +82 -0
  30. package/lib/pkg/grpc/scalekit/v1/auth/webauthn_connect.js +90 -0
  31. package/lib/pkg/grpc/scalekit/v1/auth/webauthn_connect.js.map +1 -0
  32. package/lib/pkg/grpc/scalekit/v1/auth/webauthn_pb.d.ts +647 -0
  33. package/lib/pkg/grpc/scalekit/v1/auth/webauthn_pb.js +993 -0
  34. package/lib/pkg/grpc/scalekit/v1/auth/webauthn_pb.js.map +1 -0
  35. package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.d.ts +142 -0
  36. package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.js +165 -1
  37. package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.js.map +1 -1
  38. package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.d.ts +1 -10
  39. package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.js +0 -9
  40. package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.js.map +1 -1
  41. package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.d.ts +28 -63
  42. package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.js +9 -90
  43. package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.js.map +1 -1
  44. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_connect.d.ts +6 -6
  45. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_connect.js +5 -5
  46. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_pb.d.ts +19 -30
  47. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_pb.js +22 -31
  48. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_pb.js.map +1 -1
  49. package/lib/pkg/grpc/scalekit/v1/users/users_pb.d.ts +2 -2
  50. package/lib/pkg/grpc/scalekit/v1/users/users_pb.js +1 -1
  51. package/lib/pkg/grpc/scalekit/v1/users/users_pb.js.map +1 -1
  52. package/lib/role.d.ts +252 -56
  53. package/lib/role.js +262 -62
  54. package/lib/role.js.map +1 -1
  55. package/lib/scalekit.d.ts +323 -54
  56. package/lib/scalekit.js +354 -76
  57. package/lib/scalekit.js.map +1 -1
  58. package/lib/session.d.ts +235 -22
  59. package/lib/session.js +237 -24
  60. package/lib/session.js.map +1 -1
  61. package/lib/types/organization.d.ts +3 -0
  62. package/lib/user.d.ts +571 -53
  63. package/lib/user.js +598 -89
  64. package/lib/user.js.map +1 -1
  65. package/lib/webauthn.d.ts +33 -0
  66. package/lib/webauthn.js +80 -0
  67. package/lib/webauthn.js.map +1 -0
  68. package/package.json +2 -2
  69. package/src/auth.ts +53 -19
  70. package/src/connection.ts +237 -62
  71. package/src/core.ts +39 -33
  72. package/src/directory.ts +356 -98
  73. package/src/domain.ts +215 -68
  74. package/src/organization.ts +506 -105
  75. package/src/permission.ts +234 -88
  76. package/src/pkg/grpc/scalekit/v1/auth/auth_connect.ts +3 -3
  77. package/src/pkg/grpc/scalekit/v1/auth/auth_pb.ts +24 -24
  78. package/src/pkg/grpc/scalekit/v1/auth/webauthn_connect.ts +89 -0
  79. package/src/pkg/grpc/scalekit/v1/auth/webauthn_pb.ts +1263 -0
  80. package/src/pkg/grpc/scalekit/v1/commons/commons_pb.ts +217 -0
  81. package/src/pkg/grpc/scalekit/v1/connections/connections_connect.ts +1 -10
  82. package/src/pkg/grpc/scalekit/v1/connections/connections_pb.ts +42 -129
  83. package/src/pkg/grpc/scalekit/v1/organizations/organizations_connect.ts +6 -6
  84. package/src/pkg/grpc/scalekit/v1/organizations/organizations_pb.ts +28 -43
  85. package/src/pkg/grpc/scalekit/v1/users/users_pb.ts +3 -3
  86. package/src/role.ts +336 -136
  87. package/src/scalekit.ts +478 -149
  88. package/src/session.ts +266 -63
  89. package/src/types/organization.ts +4 -0
  90. package/src/user.ts +675 -168
  91. package/src/webauthn.ts +98 -0
  92. package/tests/organization.test.ts +16 -0
package/src/session.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { PartialMessage } from '@bufbuild/protobuf';
2
- import { PromiseClient } from '@connectrpc/connect';
3
- import GrpcConnect from './connect';
4
- import CoreClient from './core';
5
- import { SessionService } from './pkg/grpc/scalekit/v1/sessions/sessions_connect';
6
- import {
1
+ import { PartialMessage } from "@bufbuild/protobuf";
2
+ import { PromiseClient } from "@connectrpc/connect";
3
+ import GrpcConnect from "./connect";
4
+ import CoreClient from "./core";
5
+ import { SessionService } from "./pkg/grpc/scalekit/v1/sessions/sessions_connect";
6
+ import {
7
7
  SessionDetailsRequest,
8
8
  SessionDetails,
9
9
  UserSessionDetailsRequest,
@@ -12,9 +12,9 @@ import {
12
12
  RevokeSessionRequest,
13
13
  RevokeSessionResponse,
14
14
  RevokeAllUserSessionsRequest,
15
- RevokeAllUserSessionsResponse
16
- } from './pkg/grpc/scalekit/v1/sessions/sessions_pb';
17
- import { Timestamp } from '@bufbuild/protobuf';
15
+ RevokeAllUserSessionsResponse,
16
+ } from "./pkg/grpc/scalekit/v1/sessions/sessions_pb";
17
+ import { Timestamp } from "@bufbuild/protobuf";
18
18
 
19
19
  export default class SessionClient {
20
20
  private client: PromiseClient<typeof SessionService>;
@@ -27,45 +27,126 @@ export default class SessionClient {
27
27
  }
28
28
 
29
29
  /**
30
- * Get details for a specific session
31
- * @param {string} sessionId The session id
32
- * @returns {Promise<SessionDetails>} The session details
30
+ * Retrieves comprehensive metadata and status for a specific user session.
31
+ *
32
+ * Fetches complete session information including authentication status, device details,
33
+ * IP address, geolocation, and expiration timelines.
34
+ *
35
+ * @param {string} sessionId - The session identifier to retrieve (format: "ses_...")
36
+ *
37
+ * @returns {Promise<SessionDetails>} Session details containing:
38
+ * - sessionId: Unique session identifier
39
+ * - userId: User who owns this session
40
+ * - organizationId: Organization context for the session
41
+ * - status: Current session status ('active', 'expired', 'revoked', 'logout')
42
+ * - deviceInfo: Browser and operating system information
43
+ * - ipAddress: IP address from which session was created
44
+ * - geoLocation: Geographic location data (city, region, country)
45
+ * - createTime: When the session was created
46
+ * - lastActivityTime: Most recent session activity
47
+ * - idleExpirationTime: When session expires due to inactivity
48
+ * - absoluteExpirationTime: Maximum session lifetime
49
+ * - expirationTime: Actual expiration time (if session has ended)
50
+ *
51
+ * @throws {Error} If the session is not found
52
+ *
53
+ * @example
54
+ * // Get session details
55
+ * const session = await scalekitClient.session.getSession('ses_123456');
56
+ *
57
+ * console.log('Status:', session.status);
58
+ * console.log('User:', session.userId);
59
+ * console.log('Device:', session.deviceInfo);
60
+ * console.log('Location:', session.geoLocation);
61
+ * console.log('Last active:', session.lastActivityTime);
62
+ *
63
+ *
64
+ * @see {@link https://docs.scalekit.com/apis/#tag/sessions | Get Session API}
65
+ * @see {@link getUserSessions} - List all sessions for a user
66
+ * @see {@link revokeSession} - Revoke this session
33
67
  */
34
68
  async getSession(sessionId: string): Promise<SessionDetails> {
35
- return this.coreClient.connectExec(
36
- this.client.getSession,
37
- {
38
- sessionId
39
- }
40
- );
69
+ return this.coreClient.connectExec(this.client.getSession, {
70
+ sessionId,
71
+ });
41
72
  }
42
73
 
43
74
  /**
44
- * Get all session details for a user with pagination and filtering
45
- * @param {string} userId The user id
46
- * @param {object} options The pagination and filtering options
47
- * @param {number} options.pageSize The page size
48
- * @param {string} options.pageToken The page token
49
- * @param {object} options.filter The session filter options
50
- * @param {string[]} options.filter.status The session statuses to filter by
51
- * @param {Date} options.filter.startTime The start time for filtering sessions
52
- * @param {Date} options.filter.endTime The end time for filtering sessions
53
- * @returns {Promise<UserSessionDetails>} The user session details
75
+ * Retrieves a paginated list of all sessions for a specific user across all devices and browsers.
76
+ *
77
+ * Lists all user sessions with support for filtering by status and time range. Essential for
78
+ * displaying active sessions in account management, auditing user activity, verifying authentication
79
+ * across devices, or identifying suspicious sessions. Use filtering to find active sessions,
80
+ * sessions from specific time periods, or sessions with particular statuses.
81
+ *
82
+ * @param {string} userId - The user identifier (format: "usr_...")
83
+ * @param {object} [options] - Optional pagination and filtering parameters
84
+ * @param {number} [options.pageSize] - Number of sessions to return per page
85
+ * @param {string} [options.pageToken] - Token for retrieving the next page from previous response
86
+ * @param {object} [options.filter] - Filter criteria for sessions
87
+ * @param {string[]} [options.filter.status] - Filter by status values: 'active', 'expired', 'revoked', 'logout'
88
+ * (multiple values use OR logic)
89
+ * @param {Date} [options.filter.startTime] - Include sessions created on or after this time
90
+ * @param {Date} [options.filter.endTime] - Include sessions created before this time
91
+ * (must be after startTime)
92
+ *
93
+ * @returns {Promise<UserSessionDetails>} Response containing:
94
+ * - sessions: Array of session objects with device info, location, and status
95
+ * - nextPageToken: Token for fetching the next page
96
+ * - totalSize: Total number of sessions matching the filter
97
+ *
98
+ * @example
99
+ * // List all active sessions for a user
100
+ * const response = await scalekitClient.session.getUserSessions('usr_123456', {
101
+ * filter: { status: ['active'] },
102
+ * pageSize: 20
103
+ * });
104
+ *
105
+ * console.log(`User has ${response.totalSize} active sessions`);
106
+ * response.sessions.forEach(session => {
107
+ * console.log(`- ${session.deviceInfo} from ${session.geoLocation?.city}`);
108
+ * console.log(` Last active: ${session.lastActivityTime}`);
109
+ * });
110
+ *
111
+ * @example
112
+ * // Get all sessions (active and inactive)
113
+ * const response = await scalekitClient.session.getUserSessions('usr_123456');
114
+ * console.log(`Total sessions: ${response.totalSize}`);
115
+ *
116
+ * @example
117
+ * // Find sessions from the last 7 days
118
+ * const sevenDaysAgo = new Date();
119
+ * sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
120
+ *
121
+ * const response = await scalekitClient.session.getUserSessions('usr_123456', {
122
+ * filter: {
123
+ * startTime: sevenDaysAgo,
124
+ * status: ['active', 'logout']
125
+ * }
126
+ * });
127
+ *
128
+ * console.log(`Sessions in last 7 days: ${response.sessions.length}`);
129
+ *
130
+ *
131
+ * @see {@link https://docs.scalekit.com/apis/#tag/sessions | List User Sessions API}
132
+ * @see {@link getSession} - Get details of a specific session
133
+ * @see {@link revokeSession} - Revoke a specific session
134
+ * @see {@link revokeAllUserSessions} - Revoke all sessions for user
54
135
  */
55
136
  async getUserSessions(
56
137
  userId: string,
57
138
  options?: {
58
- pageSize?: number,
59
- pageToken?: string,
139
+ pageSize?: number;
140
+ pageToken?: string;
60
141
  filter?: {
61
- status?: string[],
62
- startTime?: Date,
63
- endTime?: Date
64
- }
142
+ status?: string[];
143
+ startTime?: Date;
144
+ endTime?: Date;
145
+ };
65
146
  }
66
147
  ): Promise<UserSessionDetails> {
67
148
  const request: PartialMessage<UserSessionDetailsRequest> = {
68
- userId
149
+ userId,
69
150
  };
70
151
 
71
152
  if (options?.pageSize !== undefined) {
@@ -78,57 +159,179 @@ export default class SessionClient {
78
159
 
79
160
  if (options?.filter) {
80
161
  const filter = new UserSessionFilter();
81
-
162
+
82
163
  if (options.filter.status) {
83
164
  filter.status = options.filter.status;
84
165
  }
85
-
166
+
86
167
  if (options.filter.startTime) {
87
168
  filter.startTime = Timestamp.fromDate(options.filter.startTime);
88
169
  }
89
-
170
+
90
171
  if (options.filter.endTime) {
91
172
  filter.endTime = Timestamp.fromDate(options.filter.endTime);
92
173
  }
93
-
174
+
94
175
  request.filter = filter;
95
176
  }
96
177
 
97
- return this.coreClient.connectExec(
98
- this.client.getUserSessions,
99
- request
100
- );
178
+ return this.coreClient.connectExec(this.client.getUserSessions, request);
101
179
  }
102
180
 
103
181
  /**
104
- * Revoke a session for a user
105
- * @param {string} sessionId The session id to revoke
106
- * @returns {Promise<RevokeSessionResponse>} The response with revoked session details
182
+ * Immediately invalidates a specific user session by setting its status to 'revoked'.
183
+ *
184
+ * Terminates a single session, forcing the user to re-authenticate on that specific device.
185
+ * Use this for session-level logout, terminating suspicious sessions, forcing re-authentication
186
+ * on specific devices, or implementing "logout from this device" functionality. The revocation
187
+ * is instantaneous and irreversible - once revoked, the session cannot be used for any future
188
+ * API requests or application access.
189
+ *
190
+ * @param {string} sessionId - The session identifier to revoke (format: "ses_...")
191
+ *
192
+ * @returns {Promise<RevokeSessionResponse>} Response containing:
193
+ * - sessionId: The ID of the revoked session
194
+ * - userId: User who owned the session
195
+ * - revokedAt: Timestamp when the session was revoked
196
+ * - status: Updated session status ('revoked')
197
+ *
198
+ * @throws {Error} If the session is not found
199
+ *
200
+ * @example
201
+ * // Revoke a specific session
202
+ * const response = await scalekitClient.session.revokeSession('ses_123456');
203
+ * console.log('Session revoked at:', response.revokedAt);
204
+ * console.log('Status:', response.status); // 'revoked'
205
+ *
206
+ * @example
207
+ * // Logout from specific device
208
+ * app.post('/api/sessions/:sessionId/logout', async (req, res) => {
209
+ * const { sessionId } = req.params;
210
+ *
211
+ * try {
212
+ * await scalekitClient.session.revokeSession(sessionId);
213
+ * res.json({ message: 'Session terminated successfully' });
214
+ * } catch (error) {
215
+ * res.status(500).json({ error: 'Failed to revoke session' });
216
+ * }
217
+ * });
218
+ *
219
+ * @example
220
+ * // Revoke suspicious session
221
+ * const session = await scalekitClient.session.getSession(sessionId);
222
+ *
223
+ * if (session.geoLocation?.country !== expectedCountry) {
224
+ * console.log('Suspicious session detected');
225
+ * console.log(`Location: ${session.geoLocation?.city}, ${session.geoLocation?.country}`);
226
+ * console.log(`IP: ${session.ipAddress}`);
227
+ *
228
+ * await scalekitClient.session.revokeSession(sessionId);
229
+ * console.log('Session revoked for security');
230
+ *
231
+ * // Send security alert to user
232
+ * await sendSecurityAlert(session.userId, {
233
+ * message: 'Unusual login location detected and session terminated',
234
+ * location: session.geoLocation
235
+ * });
236
+ * }
237
+ *
238
+ * @example
239
+ * // Revoke all non-current sessions for a user
240
+ * const response = await scalekitClient.session.getUserSessions(userId, {
241
+ * filter: { status: ['active'] }
242
+ * });
243
+ *
244
+ * for (const session of response.sessions) {
245
+ * if (session.sessionId !== currentSessionId) {
246
+ * await scalekitClient.session.revokeSession(session.sessionId);
247
+ * console.log(`Revoked session from: ${session.deviceInfo}`);
248
+ * }
249
+ * }
250
+ *
251
+ * @see {@link https://docs.scalekit.com/apis/#tag/sessions | Revoke Session API}
252
+ * @see {@link revokeAllUserSessions} - Revoke all sessions for a user
253
+ * @see {@link getUserSessions} - List user sessions to find suspicious ones
254
+ * @see {@link getSession} - Get session details before revoking
107
255
  */
108
256
  async revokeSession(sessionId: string): Promise<RevokeSessionResponse> {
109
- return this.coreClient.connectExec(
110
- this.client.revokeSession,
111
- {
112
- sessionId
113
- }
114
- );
257
+ return this.coreClient.connectExec(this.client.revokeSession, {
258
+ sessionId,
259
+ });
115
260
  }
116
261
 
117
262
  /**
118
- * Revoke all sessions for a user
119
- * @param {string} userId The user id whose sessions should be revoked
120
- * @returns {Promise<RevokeAllUserSessionsResponse>} The response with all revoked session details
263
+ * Immediately invalidates all active sessions for a user across all devices and browsers.
264
+ *
265
+ * Terminates all active sessions simultaneously, forcing the user to re-authenticate everywhere.
266
+ * Use this to implement global logout, force re-authentication after security incidents (password
267
+ * compromise, suspicious activity), terminate all sessions following password reset, or revoke
268
+ * access after account changes. Only active sessions are revoked; already expired, logout, or
269
+ * previously revoked sessions remain unchanged. This operation is atomic, instantaneous, and
270
+ * cannot be undone.
271
+ *
272
+ * @param {string} userId - The user identifier whose sessions should be revoked (format: "usr_...")
273
+ *
274
+ * @returns {Promise<RevokeAllUserSessionsResponse>} Response containing:
275
+ * - revokedSessions: Array of all revoked session objects with details
276
+ * - totalCount: Number of sessions that were revoked
277
+ * - revokedAt: Timestamp when revocation occurred
278
+ *
279
+ * @throws {Error} When userId is missing or invalid
280
+ * @throws {Error} If the user is not found
281
+ *
282
+ * @example
283
+ * // Revoke all user sessions (global logout)
284
+ * const response = await scalekitClient.session.revokeAllUserSessions('usr_123456');
285
+ *
286
+ * console.log(`Revoked ${response.totalCount} sessions`);
287
+ * console.log('User must re-authenticate on all devices');
288
+ *
289
+ * @example
290
+ * // Force logout after password reset
291
+ * app.post('/api/users/:userId/reset-password', async (req, res) => {
292
+ * const { userId } = req.params;
293
+ * const { newPassword } = req.body;
294
+ *
295
+ * try {
296
+ * // Update password
297
+ * await updateUserPassword(userId, newPassword);
298
+ *
299
+ * // Revoke all existing sessions
300
+ * const response = await scalekitClient.session.revokeAllUserSessions(userId);
301
+ *
302
+ * res.json({
303
+ * message: 'Password updated successfully',
304
+ * sessionsRevoked: response.totalCount,
305
+ * note: 'Please log in again with your new password'
306
+ * });
307
+ * } catch (error) {
308
+ * res.status(500).json({ error: 'Failed to reset password' });
309
+ * }
310
+ * });
311
+ *
312
+ * @example
313
+ * // Force re-authentication after role change
314
+ * async function updateUserRole(userId, newRole) {
315
+ * await updateRole(userId, newRole);
316
+ *
317
+ * // Force re-authentication to apply new permissions
318
+ * await scalekitClient.session.revokeAllUserSessions(userId);
319
+ * console.log('User will re-authenticate with new role permissions');
320
+ * }
321
+ *
322
+ * @see {@link https://docs.scalekit.com/apis/#tag/sessions | Revoke All Sessions API}
323
+ * @see {@link revokeSession} - Revoke a specific session
324
+ * @see {@link getUserSessions} - View user's active sessions before revoking
121
325
  */
122
- async revokeAllUserSessions(userId: string): Promise<RevokeAllUserSessionsResponse> {
326
+ async revokeAllUserSessions(
327
+ userId: string
328
+ ): Promise<RevokeAllUserSessionsResponse> {
123
329
  if (!userId) {
124
- throw new Error('userId is required');
330
+ throw new Error("userId is required");
125
331
  }
126
332
 
127
- return this.coreClient.connectExec(
128
- this.client.revokeAllUserSessions,
129
- {
130
- userId
131
- }
132
- );
333
+ return this.coreClient.connectExec(this.client.revokeAllUserSessions, {
334
+ userId,
335
+ });
133
336
  }
134
337
  }
@@ -5,4 +5,8 @@ export type OrganizationSettings = {
5
5
  export type Feature = {
6
6
  name: string;
7
7
  enabled: boolean;
8
+ }
9
+
10
+ export type OrganizationUserManagementSettingsInput = {
11
+ maxAllowedUsers?: number | null;
8
12
  }