Skip to content

Commit d742439

Browse files
authored
Add ttl and context to SessionParams and create a separate SelfServiceSessionParams for creating self service sessions (#33)
* Add ttl and context to SessionParams and create a separate SelfServiceSessionParams for creating self service sessions * Add role parameter to methods assigning/removing users to/from tenants * Update self service strategies to rbac and fgac * Update tests * Move tenantId from SessionParams to SelfServiceSessionParams
1 parent 9585165 commit d742439

File tree

6 files changed

+65
-40
lines changed

6 files changed

+65
-40
lines changed

src/modules/Session.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import WarrantClient from "../WarrantClient";
22
import { SELF_SERVICE_DASH_URL_BASE } from "../constants";
3-
import { SessionParams } from "../types/Session";
3+
import { SelfServiceSessionParams, SessionParams } from "../types/Session";
44

55
export default class Session {
66
/**
@@ -10,7 +10,7 @@ export default class Session {
1010
* @param session A session object containing the userId, redirectUrl, and optional tenantId for which the authorization session should be created.
1111
* @returns A session token that can be passed to any of the Warrant client-side SDKs to allow the SDK to make client-side authorization checks for the specified user.
1212
*/
13-
public static async createAuthorizationSession(session: SessionParams): Promise<string> {
13+
public static async createAuthorizationSession(session: SessionParams): Promise<string> {
1414
try {
1515
const sess = await WarrantClient.httpClient.post({
1616
url: "/v1/sessions",
@@ -33,7 +33,7 @@ export default class Session {
3333
* @param session A session object containing the userId, redirectUrl, and optional tenantId for which the self service session should be created.
3434
* @returns A url pointing to the self-service dashboard that will allow the specified user to make changes to the roles and permissions of users in their tenant.
3535
*/
36-
public static async createSelfServiceSession(session: SessionParams, redirectUrl: string): Promise<string> {
36+
public static async createSelfServiceSession(session: SelfServiceSessionParams, redirectUrl: string): Promise<string> {
3737
try {
3838
const sess = await WarrantClient.httpClient.post({
3939
url: "/v1/sessions",

src/modules/Tenant.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ export default class Tenant implements WarrantObject {
118118
return User.listUsersForTenant(this.tenantId, listOptions);
119119
}
120120

121-
public async assignUser(userId: string): Promise<Warrant> {
122-
return User.assignUserToTenant(this.tenantId, userId);
121+
public async assignUser(userId: string, role: string): Promise<Warrant> {
122+
return User.assignUserToTenant(this.tenantId, userId, role);
123123
}
124124

125-
public async removeUser(userId: string): Promise<void> {
126-
return User.removeUserFromTenant(this.tenantId, userId);
125+
public async removeUser(userId: string, role: string): Promise<void> {
126+
return User.removeUserFromTenant(this.tenantId, userId, role);
127127
}
128128

129129
public async listPricingTiers(listOptions: ListPricingTierOptions = {}): Promise<PricingTier[]> {

src/modules/User.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ListRoleOptions } from "../types/Role";
1414
import { CreateUserParams, ListUserOptions, UpdateUserParams } from "../types/User";
1515
import { ListTenantOptions } from "../types/Tenant";
1616
import { Context, WarrantObject } from "../types/Warrant";
17+
import WarrantModule from "./WarrantModule";
1718

1819
export default class User implements WarrantObject {
1920
userId: string;
@@ -114,24 +115,32 @@ export default class User implements WarrantObject {
114115
}
115116
}
116117

117-
public static async assignUserToTenant(tenantId: string, userId: string): Promise<Warrant> {
118-
try {
119-
return await WarrantClient.httpClient.post({
120-
url: `/v1/tenants/${tenantId}/users/${userId}`,
121-
});
122-
} catch (e) {
123-
throw e;
124-
}
125-
}
126-
127-
public static async removeUserFromTenant(tenantId: string, userId: string): Promise<void> {
128-
try {
129-
return await WarrantClient.httpClient.delete({
130-
url: `/v1/tenants/${tenantId}/users/${userId}`,
131-
});
132-
} catch (e) {
133-
throw e;
134-
}
118+
public static async assignUserToTenant(tenantId: string, userId: string, role: string): Promise<Warrant> {
119+
return WarrantModule.create({
120+
object: {
121+
objectType: "tenant",
122+
objectId: tenantId,
123+
},
124+
relation: role,
125+
subject: {
126+
objectType: "user",
127+
objectId: userId,
128+
}
129+
});
130+
}
131+
132+
public static async removeUserFromTenant(tenantId: string, userId: string, role: string): Promise<void> {
133+
return WarrantModule.delete({
134+
object: {
135+
objectType: "tenant",
136+
objectId: tenantId,
137+
},
138+
relation: role,
139+
subject: {
140+
objectType: "user",
141+
objectId: userId,
142+
}
143+
});
135144
}
136145

137146
//

src/types/Session.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1+
import { Context } from "./Warrant";
2+
13
export interface SessionParams {
24
userId: string;
3-
tenantId?: string;
5+
ttl?: number;
6+
context?: Context;
7+
}
8+
9+
export interface SelfServiceSessionParams extends SessionParams {
10+
tenantId: string;
11+
selfServiceStrategy: SelfServiceStrategy;
12+
}
13+
14+
export enum SelfServiceStrategy {
15+
RBAC = "rbac",
16+
FGAC = "fgac",
417
}

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export { CreatePermissionParams, ListPermissionOptions, UpdatePermissionParams }
55
export { CreatePricingTierParams, ListPricingTierOptions } from "./PricingTier";
66
export { default as Query } from "./Query";
77
export { CreateRoleParams, ListRoleOptions, UpdateRoleParams } from "./Role";
8-
export { SessionParams } from "./Session";
8+
export { SessionParams, SelfServiceSessionParams, SelfServiceStrategy } from "./Session";
99
export { CreateTenantParams, ListTenantOptions, UpdateTenantParams } from "./Tenant";
1010
export { CreateUserParams, ListUserOptions, UpdateUserParams } from "./User";
1111
export { default as Warrant, ListWarrantOptions, Context, Subject, WarrantObject } from "./Warrant";

test/LiveTest.spec.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Query, WarrantClient } = require("../dist/index");
1+
const { SelfServiceStrategy, Query, WarrantClient } = require("../dist/index");
22
var assert = require('assert');
33

44
// Uncomment .skip and add your API_KEY to run tests
@@ -56,8 +56,8 @@ describe.skip('Live Test', function () {
5656
});
5757

5858
it('CRUD roles', async function () {
59-
const adminRole = await this.warrant.Role.create({ roleId: "administrator", name: "Admin", description: "The admin role" });
60-
assert.strictEqual(adminRole.roleId, "administrator");
59+
const adminRole = await this.warrant.Role.create({ roleId: "admin", name: "Admin", description: "The admin role" });
60+
assert.strictEqual(adminRole.roleId, "admin");
6161
assert.strictEqual(adminRole.name, "Admin");
6262
assert.strictEqual(adminRole.description, "The admin role");
6363

@@ -74,12 +74,12 @@ describe.skip('Live Test', function () {
7474
assert.strictEqual(refetchedRole.description, "Updated desc");
7575

7676
let roles = await this.warrant.Role.listRoles({ limit: 10, page: 1 });
77-
assert.strictEqual(roles.length, 3); // includes default 'admin' role
77+
assert.strictEqual(roles.length, 2);
7878

7979
await this.warrant.Role.delete(adminRole.roleId);
8080
await this.warrant.Role.delete(viewerRole.roleId);
8181
roles = await this.warrant.Role.listRoles({ limit: 10, page: 1 });
82-
assert.strictEqual(roles.length, 1);
82+
assert.strictEqual(roles.length, 0);
8383
});
8484

8585
it('CRUD permissions', async function () {
@@ -101,12 +101,12 @@ describe.skip('Live Test', function () {
101101
assert.strictEqual(refetchedPermission.description, "Updated desc");
102102

103103
let permissions = await this.warrant.Permission.listPermissions({ limit: 10, page: 1 });
104-
assert.strictEqual(permissions.length, 3); // includes default 'view-self-service-dashboard' permission
104+
assert.strictEqual(permissions.length, 2);
105105

106106
await this.warrant.Permission.delete(permission1.permissionId);
107107
await this.warrant.Permission.delete(permission2.permissionId);
108108
permissions = await this.warrant.Permission.listPermissions({ limit: 10, page: 1 });
109-
assert.strictEqual(permissions.length, 1);
109+
assert.strictEqual(permissions.length, 0);
110110
});
111111

112112
it('CRUD features', async function () {
@@ -183,7 +183,7 @@ describe.skip('Live Test', function () {
183183
assert.strictEqual(tenant1Users.length, 0);
184184

185185
// Assign user1 -> tenant1
186-
await this.warrant.User.assignUserToTenant(tenant1.tenantId, user1.userId);
186+
await this.warrant.User.assignUserToTenant(tenant1.tenantId, user1.userId, "member");
187187

188188
user1Tenants = await this.warrant.Tenant.listTenantsForUser(user1.userId, { limit: 100, page: 1 });
189189
assert.strictEqual(user1Tenants.length, 1);
@@ -194,7 +194,7 @@ describe.skip('Live Test', function () {
194194
assert.strictEqual(tenant1Users[0].userId, user1.userId);
195195

196196
// Remove user1 -> tenant1
197-
await this.warrant.User.removeUserFromTenant(tenant1.tenantId, user1.userId);
197+
await this.warrant.User.removeUserFromTenant(tenant1.tenantId, user1.userId, "member");
198198

199199
user1Tenants = await this.warrant.Tenant.listTenantsForUser(user1.userId, { limit: 100, page: 1 });
200200
assert.strictEqual(user1Tenants.length, 0);
@@ -215,7 +215,7 @@ describe.skip('Live Test', function () {
215215
const viewerUser = await this.warrant.User.create();
216216

217217
// Create roles
218-
const adminRole = await this.warrant.Role.create({ roleId: "administrator", name: "Admin", description: "The admin role" });
218+
const adminRole = await this.warrant.Role.create({ roleId: "admin", name: "Admin", description: "The admin role" });
219219
const viewerRole = await this.warrant.Role.create({ roleId: "viewer", name: "Viewer", description: "The viewer role" });
220220

221221
// Create permissions
@@ -462,13 +462,16 @@ describe.skip('Live Test', function () {
462462
const user = await this.warrant.User.create();
463463
const tenant = await this.warrant.Tenant.create();
464464

465-
await this.warrant.User.assignUserToTenant(tenant.tenantId, user.userId);
466-
await this.warrant.Permission.assignPermissionToUser(user.userId, "view-self-service-dashboard");
465+
await this.warrant.User.assignUserToTenant(tenant.tenantId, user.userId, "admin");
467466

468467
const userAuthzSession = await this.warrant.Session.createAuthorizationSession({ userId: user.userId });
469468
assert(userAuthzSession);
470469

471-
const userSelfServicDashboardUrl = await this.warrant.Session.createSelfServiceSession({ userId: user.userId, tenantId: tenant.tenantId }, "http://localhost:8080");
470+
const userSelfServicDashboardUrl = await this.warrant.Session.createSelfServiceSession({
471+
userId: user.userId,
472+
tenantId: tenant.tenantId,
473+
selfServiceStrategy: SelfServiceStrategy.FGAC,
474+
}, "http://localhost:8080");
472475
assert(userSelfServicDashboardUrl);
473476

474477
await this.warrant.User.delete(user.userId);

0 commit comments

Comments
 (0)