@sso-id/node 0.2.4 → 0.2.6

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.
@@ -4,6 +4,8 @@ export interface ManagementClientOptions {
4
4
  domain: string;
5
5
  clientId: string;
6
6
  clientSecret: string;
7
+ /** Optional internal ApplicationId; resolved automatically from clientId if omitted */
8
+ applicationId?: string;
7
9
  /** Management API base URL (defaults to "https://api.sso.id") */
8
10
  apiBaseUrl?: string;
9
11
  audience?: string;
@@ -15,17 +17,22 @@ export declare class ManagementClient {
15
17
  private readonly apiBaseUrl;
16
18
  private readonly options;
17
19
  private cachedToken;
20
+ private cachedApplicationId;
18
21
  constructor(options: ManagementClientOptions);
19
22
  private getAccessToken;
20
23
  private request;
24
+ getApplicationId(): Promise<string>;
25
+ assignUserToApplication(userId: string, userDetails?: Record<string, any>): Promise<void>;
21
26
  readonly users: {
22
- list: (query?: {
27
+ /** Lists ONLY users allowed/assigned to this Application */
28
+ list: (_query?: {
23
29
  page?: number;
24
30
  pageSize?: number;
25
31
  search?: string;
26
- }) => Promise<unknown>;
32
+ }) => Promise<Record<string, any>[]>;
27
33
  get: (userId: string) => Promise<unknown>;
28
- create: (data: Record<string, unknown>) => Promise<unknown>;
34
+ /** Creates a user and immediately grants them access to this Application */
35
+ create: (data: Record<string, unknown>) => Promise<Record<string, any>>;
29
36
  invite: (data: Record<string, unknown>) => Promise<unknown>;
30
37
  update: (userId: string, data: Record<string, unknown>) => Promise<unknown>;
31
38
  delete: (userId: string) => Promise<void>;
@@ -42,21 +49,24 @@ export declare class ManagementClient {
42
49
  assignToUser: (userId: string, roleId: string) => Promise<void>;
43
50
  };
44
51
  readonly organizations: {
45
- list: () => Promise<unknown>;
52
+ /** Lists ONLY organizations linked to this Application */
53
+ list: () => Promise<any[]>;
46
54
  get: (orgId: string) => Promise<unknown>;
47
- create: (data: Record<string, unknown>) => Promise<unknown>;
55
+ /** Creates an organization and immediately links it to this Application */
56
+ create: (data: Record<string, unknown>) => Promise<Record<string, any>>;
48
57
  update: (orgId: string, data: Record<string, unknown>) => Promise<unknown>;
49
58
  delete: (orgId: string) => Promise<void>;
50
59
  listUsers: (orgId: string) => Promise<unknown>;
51
60
  addUser: (orgId: string, data: Record<string, unknown>) => Promise<unknown>;
52
61
  removeUser: (orgId: string, userId: string) => Promise<void>;
53
- listGroups: (orgId: string) => Promise<unknown>;
62
+ listGroups: (orgId: string) => Promise<Record<string, any>[]>;
54
63
  addGroup: (orgId: string, data: Record<string, unknown>) => Promise<unknown>;
55
64
  removeGroup: (orgId: string, groupId: string) => Promise<void>;
56
65
  };
57
66
  readonly organizationUnits: {
58
- list: () => Promise<unknown>;
59
- listByOrganization: (orgId: string) => Promise<unknown>;
67
+ /** Lists ONLY organization units belonging to this Application's allowed Organizations */
68
+ list: () => Promise<Record<string, any>[]>;
69
+ listByOrganization: (orgId: string) => Promise<Record<string, any>[]>;
60
70
  get: (orgId: string, unitId: string) => Promise<unknown>;
61
71
  create: (data: Record<string, unknown>) => Promise<unknown>;
62
72
  update: (unitId: string, data: Record<string, unknown>) => Promise<unknown>;
@@ -69,15 +79,37 @@ export declare class ManagementClient {
69
79
  removeGroup: (unitId: string, groupId: string) => Promise<void>;
70
80
  };
71
81
  readonly groups: {
72
- list: () => Promise<unknown>;
82
+ /** Lists ONLY groups scoped to this Application (or its linked Organizations) */
83
+ list: () => Promise<Record<string, any>[]>;
73
84
  get: (groupId: string) => Promise<unknown>;
74
- create: (data: Record<string, unknown>) => Promise<unknown>;
85
+ /** Creates a group and links it to this Application (and primary allowed Organization if present) */
86
+ create: (data: Record<string, unknown>) => Promise<Record<string, any>>;
75
87
  update: (groupId: string, data: Record<string, unknown>) => Promise<unknown>;
76
88
  delete: (groupId: string) => Promise<void>;
77
89
  listUsers: (groupId: string) => Promise<unknown>;
78
90
  addUser: (groupId: string, userId: string) => Promise<unknown>;
79
91
  removeUser: (groupId: string, userId: string) => Promise<void>;
80
92
  };
93
+ readonly appPages: {
94
+ listMenus: () => Promise<unknown[]>;
95
+ createMenu: (data: {
96
+ menuTitle: string;
97
+ menuUrl: string;
98
+ menuDescription?: string;
99
+ parentId?: string;
100
+ sortOrder?: number;
101
+ isCategory?: boolean;
102
+ }) => Promise<unknown>;
103
+ listApis: () => Promise<unknown[]>;
104
+ createApi: (data: {
105
+ apiName: string;
106
+ apiUrl: string;
107
+ httpMethod?: string;
108
+ apiDescription?: string;
109
+ menuId?: string;
110
+ sortOrder?: number;
111
+ }) => Promise<unknown>;
112
+ };
81
113
  readonly scim: {
82
114
  forConnection: (connectionId?: string, scimBearerToken?: string) => {
83
115
  serviceProviderConfig: () => Promise<unknown>;
@@ -3,11 +3,13 @@ export class ManagementClient {
3
3
  apiBaseUrl;
4
4
  options;
5
5
  cachedToken = null;
6
+ cachedApplicationId = null;
6
7
  constructor(options) {
7
8
  const clean = options.domain.replace(/\/+$/, '');
8
9
  this.authBaseUrl = clean.startsWith('http') ? clean : `https://${clean}`;
9
10
  this.apiBaseUrl = (options.apiBaseUrl ?? 'https://api.sso.id').replace(/\/+$/, '');
10
11
  this.options = options;
12
+ this.cachedApplicationId = options.applicationId || null;
11
13
  }
12
14
  async getAccessToken() {
13
15
  if (this.cachedToken && Date.now() < this.cachedToken.expiresAt - 30_000) {
@@ -53,45 +55,122 @@ export class ManagementClient {
53
55
  if (res.status === 204) {
54
56
  return undefined;
55
57
  }
56
- return (await res.json());
58
+ const text = await res.text();
59
+ if (!text)
60
+ return undefined;
61
+ return JSON.parse(text);
62
+ }
63
+ async getApplicationId() {
64
+ if (this.cachedApplicationId)
65
+ return this.cachedApplicationId;
66
+ const apps = await this.request('GET', '/api/applications');
67
+ const list = Array.isArray(apps) ? apps : [];
68
+ const matched = list.find((a) => a.clientID === this.options.clientId ||
69
+ a.clientId === this.options.clientId ||
70
+ a.applicationId === this.options.clientId);
71
+ if (!matched?.applicationId) {
72
+ throw new Error(`Could not resolve ApplicationId for client_id "${this.options.clientId}"`);
73
+ }
74
+ this.cachedApplicationId = String(matched.applicationId);
75
+ return this.cachedApplicationId;
76
+ }
77
+ async assignUserToApplication(userId, userDetails) {
78
+ const appId = await this.getApplicationId();
79
+ const qs = new URLSearchParams({
80
+ RoleId: '0',
81
+ RoleName: 'none',
82
+ ApplicationId: appId,
83
+ CreatedBy: 'system',
84
+ LastModifiedBy: 'system',
85
+ }).toString();
86
+ await this.request('POST', `/api/applications/${encodeURIComponent(appId)}/users?${qs}`, {
87
+ applicationId: appId,
88
+ userId,
89
+ firstName: userDetails?.firstName || 'User',
90
+ lastName: userDetails?.lastName || 'User',
91
+ emailAddress: userDetails?.emailAddress || userDetails?.email || 'user@sso.id',
92
+ username: userDetails?.emailAddress || userDetails?.email || userId,
93
+ password: 'N/A',
94
+ address: 'N/A',
95
+ phoneNumber: 'N/A',
96
+ mobileNumber: 'N/A',
97
+ createdBy: 'system',
98
+ lastModifiedBy: 'system',
99
+ });
57
100
  }
58
101
  users = {
59
- list: (query) => {
60
- const params = new URLSearchParams();
61
- if (query?.page)
62
- params.set('page', String(query.page));
63
- if (query?.pageSize)
64
- params.set('pageSize', String(query.pageSize));
65
- if (query?.search)
66
- params.set('search', query.search);
67
- const qs = params.toString();
68
- return this.request('GET', `/api/users${qs ? `?${qs}` : ''}`);
102
+ /** Lists ONLY users allowed/assigned to this Application */
103
+ list: async (_query) => {
104
+ const appId = await this.getApplicationId();
105
+ const appUsers = await this.request('GET', `/api/applications/${encodeURIComponent(appId)}/users`);
106
+ return Array.isArray(appUsers) ? appUsers : [];
69
107
  },
70
108
  get: (userId) => this.request('GET', `/api/users/${encodeURIComponent(userId)}`),
71
- create: (data) => this.request('POST', '/api/users', {
72
- emailAddress: data.emailAddress ?? data.email ?? data.userName,
73
- firstName: data.firstName ?? data.name ?? data.displayName ?? 'User',
74
- lastName: data.lastName ?? 'User',
75
- ...data,
76
- }),
109
+ /** Creates a user and immediately grants them access to this Application */
110
+ create: async (data) => {
111
+ const payload = {
112
+ emailAddress: data.emailAddress ?? data.email ?? data.userName,
113
+ firstName: data.firstName ?? data.name ?? data.displayName ?? 'User',
114
+ lastName: data.lastName ?? 'User',
115
+ ...data,
116
+ };
117
+ const created = await this.request('POST', '/api/users', payload);
118
+ if (created?.userId) {
119
+ await this.assignUserToApplication(String(created.userId), created);
120
+ }
121
+ return created;
122
+ },
77
123
  invite: (data) => this.request('POST', '/api/users/invite', data),
78
124
  update: (userId, data) => this.request('PUT', `/api/users/${encodeURIComponent(userId)}`, data),
79
- delete: (userId) => this.request('DELETE', `/api/users/${encodeURIComponent(userId)}`),
125
+ delete: async (userId) => {
126
+ const appId = await this.getApplicationId();
127
+ return this.request('DELETE', `/api/applications/${encodeURIComponent(appId)}/users/${encodeURIComponent(userId)}`);
128
+ },
80
129
  listRoles: (userId) => this.request('GET', `/api/users/${encodeURIComponent(userId)}/roles`),
81
130
  assignRole: (userId, roleId) => this.request('POST', `/api/users/${encodeURIComponent(userId)}/roles`, { roleId }),
82
131
  removeRole: (userId, roleId) => this.request('DELETE', `/api/users/${encodeURIComponent(userId)}/roles/${encodeURIComponent(roleId)}`),
83
132
  };
84
133
  roles = {
85
- list: () => this.request('GET', '/api/v1/roles'),
134
+ list: async () => {
135
+ const appId = await this.getApplicationId();
136
+ return this.request('GET', `/api/applications/${encodeURIComponent(appId)}/roles`);
137
+ },
86
138
  create: (data) => this.request('POST', '/api/v1/roles', data),
87
139
  assignToUser: (userId, roleId) => this.request('POST', `/api/users/${encodeURIComponent(userId)}/roles`, { roleId }),
88
140
  };
89
141
  organizations = {
90
- list: () => this.request('GET', '/api/organizations'),
142
+ /** Lists ONLY organizations linked to this Application */
143
+ list: async () => {
144
+ const appId = await this.getApplicationId();
145
+ const raw = await this.request('GET', `/api/applications/${encodeURIComponent(appId)}/organizations`);
146
+ if (!Array.isArray(raw))
147
+ return [];
148
+ return raw.map((item) => ({
149
+ ...(item.organization || {}),
150
+ organizationId: item.organizationId || item.organization?.organizationId,
151
+ applicationId: item.applicationId || appId,
152
+ }));
153
+ },
91
154
  get: (orgId) => this.request('GET', `/api/organizations/${encodeURIComponent(orgId)}`),
92
- create: (data) => this.request('POST', '/api/organizations', data),
155
+ /** Creates an organization and immediately links it to this Application */
156
+ create: async (data) => {
157
+ const appId = await this.getApplicationId();
158
+ const created = await this.request('POST', '/api/organizations', data);
159
+ if (created?.organizationId) {
160
+ await this.request('POST', `/api/applications/${encodeURIComponent(appId)}/organizations`, {
161
+ applicationId: appId,
162
+ organizationId: created.organizationId,
163
+ createdBy: 'system',
164
+ lastModifiedBy: 'system',
165
+ });
166
+ }
167
+ return created;
168
+ },
93
169
  update: (orgId, data) => this.request('PUT', `/api/organizations/${encodeURIComponent(orgId)}`, data),
94
- delete: (orgId) => this.request('DELETE', `/api/organizations/${encodeURIComponent(orgId)}`),
170
+ delete: async (orgId) => {
171
+ const appId = await this.getApplicationId();
172
+ return this.request('DELETE', `/api/applications/${encodeURIComponent(appId)}/organizations/${encodeURIComponent(orgId)}`);
173
+ },
95
174
  listUsers: (orgId) => this.request('GET', `/api/organizations/${encodeURIComponent(orgId)}/users`),
96
175
  addUser: (orgId, data) => this.request('POST', `/api/organizations/${encodeURIComponent(orgId)}/users`, data),
97
176
  removeUser: (orgId, userId) => this.request('DELETE', `/api/organizations/${encodeURIComponent(orgId)}/users/${encodeURIComponent(userId)}`),
@@ -100,10 +179,36 @@ export class ManagementClient {
100
179
  removeGroup: (orgId, groupId) => this.request('DELETE', `/api/organizations/${encodeURIComponent(orgId)}/groups/${encodeURIComponent(groupId)}`),
101
180
  };
102
181
  organizationUnits = {
103
- list: () => this.request('GET', '/api/organization-units'),
182
+ /** Lists ONLY organization units belonging to this Application's allowed Organizations */
183
+ list: async () => {
184
+ const appOrgs = await this.organizations.list();
185
+ if (!Array.isArray(appOrgs) || appOrgs.length === 0)
186
+ return [];
187
+ const allOus = [];
188
+ for (const org of appOrgs) {
189
+ if (!org.organizationId)
190
+ continue;
191
+ try {
192
+ const ous = await this.organizationUnits.listByOrganization(String(org.organizationId));
193
+ if (Array.isArray(ous))
194
+ allOus.push(...ous);
195
+ }
196
+ catch {
197
+ // continue
198
+ }
199
+ }
200
+ return allOus;
201
+ },
104
202
  listByOrganization: (orgId) => this.request('GET', `/api/organization-units/organizationunit/organization/${encodeURIComponent(orgId)}`),
105
203
  get: (orgId, unitId) => this.request('GET', `/api/organization-units/organization/${encodeURIComponent(orgId)}/organizationunit/${encodeURIComponent(unitId)}`),
106
- create: (data) => this.request('POST', '/api/organization-units', data),
204
+ create: async (data) => {
205
+ const appOrgs = await this.organizations.list();
206
+ const allowedIds = new Set(appOrgs.map((o) => String(o.organizationId)));
207
+ if (data.organizationId && !allowedIds.has(String(data.organizationId))) {
208
+ throw new Error('Selected Organization is not linked to this Application.');
209
+ }
210
+ return this.request('POST', '/api/organization-units', data);
211
+ },
107
212
  update: (unitId, data) => this.request('PUT', `/api/organization-units/${encodeURIComponent(unitId)}`, data),
108
213
  delete: (unitId) => this.request('DELETE', `/api/organization-units/${encodeURIComponent(unitId)}`),
109
214
  listUsers: (unitId) => this.request('GET', `/api/organization-units/organizationunit/${encodeURIComponent(unitId)}/users`),
@@ -114,21 +219,143 @@ export class ManagementClient {
114
219
  removeGroup: (unitId, groupId) => this.request('DELETE', `/api/organization-units/organizationUnit/${encodeURIComponent(unitId)}/group/${encodeURIComponent(groupId)}`),
115
220
  };
116
221
  groups = {
117
- list: () => this.request('GET', '/api/groups'),
222
+ /** Lists ONLY groups scoped to this Application (or its linked Organizations) */
223
+ list: async () => {
224
+ const appId = await this.getApplicationId();
225
+ try {
226
+ const appGroups = await this.request('GET', `/api/applications/${encodeURIComponent(appId)}/groups`);
227
+ if (Array.isArray(appGroups))
228
+ return appGroups;
229
+ }
230
+ catch {
231
+ // Fallback to organization-linked or application_Group filtered groups
232
+ }
233
+ const allGroups = await this.request('GET', '/api/groups');
234
+ const list = Array.isArray(allGroups) ? allGroups : [];
235
+ const appOrgs = await this.organizations.list();
236
+ const orgGroupIds = new Set();
237
+ for (const org of appOrgs) {
238
+ if (!org.organizationId)
239
+ continue;
240
+ try {
241
+ const og = await this.organizations.listGroups(String(org.organizationId));
242
+ if (Array.isArray(og)) {
243
+ for (const g of og) {
244
+ const id = g.groupId || g.group?.groupId;
245
+ if (id)
246
+ orgGroupIds.add(String(id));
247
+ }
248
+ }
249
+ }
250
+ catch {
251
+ // continue
252
+ }
253
+ }
254
+ return list.filter((g) => {
255
+ if (orgGroupIds.has(String(g.groupId)))
256
+ return true;
257
+ if (Array.isArray(g.application_Group) && g.application_Group.some((ag) => ag.applicationId === appId)) {
258
+ return true;
259
+ }
260
+ return false;
261
+ });
262
+ },
118
263
  get: (groupId) => this.request('GET', `/api/groups/${encodeURIComponent(groupId)}`),
119
- create: (data) => this.request('POST', '/api/groups', {
120
- groupId: 'new',
121
- groupName: data.groupName ?? data.name,
122
- createdBy: 'system',
123
- lastModifiedBy: 'system',
124
- ...data,
125
- }),
264
+ /** Creates a group and links it to this Application (and primary allowed Organization if present) */
265
+ create: async (data) => {
266
+ const appId = await this.getApplicationId();
267
+ const created = await this.request('POST', '/api/groups', {
268
+ groupId: 'new',
269
+ groupName: data.groupName ?? data.name,
270
+ createdBy: 'system',
271
+ lastModifiedBy: 'system',
272
+ ...data,
273
+ });
274
+ if (created?.groupId) {
275
+ try {
276
+ await this.request('POST', '/api/groups/application-users', {
277
+ applicationId: appId,
278
+ groupId: created.groupId,
279
+ createdBy: 'system',
280
+ lastModifiedBy: 'system',
281
+ });
282
+ }
283
+ catch {
284
+ // continue
285
+ }
286
+ const appOrgs = await this.organizations.list();
287
+ if (appOrgs.length > 0 && appOrgs[0].organizationId) {
288
+ try {
289
+ await this.organizations.addGroup(String(appOrgs[0].organizationId), {
290
+ organizationId: appOrgs[0].organizationId,
291
+ groupId: created.groupId,
292
+ createdBy: 'system',
293
+ lastModifiedBy: 'system',
294
+ });
295
+ }
296
+ catch {
297
+ // continue
298
+ }
299
+ }
300
+ }
301
+ return created;
302
+ },
126
303
  update: (groupId, data) => this.request('PUT', `/api/groups/${encodeURIComponent(groupId)}`, data),
127
304
  delete: (groupId) => this.request('DELETE', `/api/groups/${encodeURIComponent(groupId)}`),
128
305
  listUsers: (groupId) => this.request('GET', `/api/groups/${encodeURIComponent(groupId)}/users`),
129
306
  addUser: (groupId, userId) => this.request('POST', `/api/groups/${encodeURIComponent(groupId)}/users/${encodeURIComponent(userId)}`),
130
307
  removeUser: (groupId, userId) => this.request('DELETE', `/api/groups/${encodeURIComponent(groupId)}/users/${encodeURIComponent(userId)}`),
131
308
  };
309
+ appPages = {
310
+ listMenus: async () => {
311
+ const appId = await this.getApplicationId();
312
+ if (!appId)
313
+ return [];
314
+ const res = await this.request(`/api/applications/${encodeURIComponent(appId)}/menus`).catch(() => []);
315
+ return this.unwrapList(res);
316
+ },
317
+ createMenu: async (data) => {
318
+ const appId = await this.getApplicationId();
319
+ if (!appId)
320
+ throw new Error('Application not found for clientId');
321
+ return this.request(`/api/applications/${encodeURIComponent(appId)}/menus`, {
322
+ method: 'POST',
323
+ body: JSON.stringify({
324
+ applicationId: appId,
325
+ menuTitle: data.menuTitle,
326
+ menuUrl: data.menuUrl,
327
+ menuDescription: data.menuDescription || '',
328
+ parentId: data.parentId || '',
329
+ sortOrder: data.sortOrder ?? 1,
330
+ isCategory: data.isCategory ?? false,
331
+ }),
332
+ });
333
+ },
334
+ listApis: async () => {
335
+ const appId = await this.getApplicationId();
336
+ if (!appId)
337
+ return [];
338
+ const res = await this.request(`/api/applications/${encodeURIComponent(appId)}/apis`).catch(() => []);
339
+ return this.unwrapList(res);
340
+ },
341
+ createApi: async (data) => {
342
+ const appId = await this.getApplicationId();
343
+ if (!appId)
344
+ throw new Error('Application not found for clientId');
345
+ return this.request(`/api/applications/${encodeURIComponent(appId)}/apis`, {
346
+ method: 'POST',
347
+ body: JSON.stringify({
348
+ applicationId: appId,
349
+ apiName: data.apiName,
350
+ apiUrl: data.apiUrl,
351
+ httpMethod: data.httpMethod || 'GET,POST',
352
+ apiDescription: data.apiDescription || '',
353
+ menuId: data.menuId || '',
354
+ sortOrder: data.sortOrder ?? 1,
355
+ }),
356
+ });
357
+ },
358
+ };
132
359
  scim = {
133
360
  forConnection: (connectionId, scimBearerToken) => {
134
361
  const prefix = connectionId
@@ -140,7 +367,22 @@ export class ManagementClient {
140
367
  schemas: () => this.request('GET', `${prefix}/Schemas`, undefined, tok),
141
368
  listUsers: (filter) => this.request('GET', `${prefix}/Users${filter ? `?filter=${encodeURIComponent(filter)}` : ''}`, undefined, tok),
142
369
  getUser: (id) => this.request('GET', `${prefix}/Users/${encodeURIComponent(id)}`, undefined, tok),
143
- createUser: (user) => this.request('POST', `${prefix}/Users`, user, tok),
370
+ createUser: async (user) => {
371
+ const created = await this.request('POST', `${prefix}/Users`, user, tok);
372
+ if (created?.id) {
373
+ try {
374
+ await this.assignUserToApplication(created.id, {
375
+ firstName: created.displayName || created.userName,
376
+ lastName: 'SCIM',
377
+ emailAddress: created.userName,
378
+ });
379
+ }
380
+ catch {
381
+ // continue
382
+ }
383
+ }
384
+ return created;
385
+ },
144
386
  replaceUser: (id, user) => this.request('PUT', `${prefix}/Users/${encodeURIComponent(id)}`, user, tok),
145
387
  patchUser: (id, patchPayload) => this.request('PATCH', `${prefix}/Users/${encodeURIComponent(id)}`, patchPayload, tok),
146
388
  deleteUser: (id) => this.request('DELETE', `${prefix}/Users/${encodeURIComponent(id)}`, undefined, tok),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sso-id/node",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "type": "module",
5
5
  "description": "Official SSO.ID Node.js SDK (RS256 JWKS JWT verification, Express/Fastify middleware, Management API client)",
6
6
  "main": "dist/index.js",