@develit-services/rbac 0.0.2 → 0.1.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.
@@ -1,2 +1,2 @@
1
- export { r as role, a as roleScope, u as userRole, b as userScope } from '../shared/rbac.BmuK3PNh.cjs';
1
+ export { a as role, r as roleScope, u as userRole, b as userScope } from '../shared/rbac.CqpxM3E5.cjs';
2
2
  import 'drizzle-orm/sqlite-core';
@@ -1,2 +1,2 @@
1
- export { r as role, a as roleScope, u as userRole, b as userScope } from '../shared/rbac.BmuK3PNh.mjs';
1
+ export { a as role, r as roleScope, u as userRole, b as userScope } from '../shared/rbac.CqpxM3E5.mjs';
2
2
  import 'drizzle-orm/sqlite-core';
@@ -1,2 +1,2 @@
1
- export { r as role, a as roleScope, u as userRole, b as userScope } from '../shared/rbac.BmuK3PNh.js';
1
+ export { a as role, r as roleScope, u as userRole, b as userScope } from '../shared/rbac.CqpxM3E5.js';
2
2
  import 'drizzle-orm/sqlite-core';
@@ -1,3 +1,3 @@
1
- export { r as role, a as roleScope, u as userRole, b as userScope } from '../shared/rbac.CJLU5iuV.mjs';
1
+ export { a as role, r as roleScope, u as userRole, b as userScope } from '../shared/rbac.D5OV7UPA.mjs';
2
2
  import '@develit-io/backend-sdk';
3
3
  import 'drizzle-orm/sqlite-core';
@@ -2,12 +2,12 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- const cloudflare_workers = require('cloudflare:workers');
6
5
  const backendSdk = require('@develit-io/backend-sdk');
7
- const verifyScope = require('../shared/rbac.CtYOhFTw.cjs');
8
6
  const database_schema = require('../shared/rbac.Cra1T2nC.cjs');
9
7
  const drizzleOrm = require('drizzle-orm');
8
+ const verifyScope = require('../shared/rbac.CS8i19gH.cjs');
10
9
  const zod = require('zod');
10
+ const cloudflare_workers = require('cloudflare:workers');
11
11
  const d1 = require('drizzle-orm/d1');
12
12
  require('drizzle-orm/sqlite-core');
13
13
 
@@ -369,12 +369,20 @@ var __decorateClass = (decorators, target, key, kind) => {
369
369
  if (kind && result) __defProp(target, key, result);
370
370
  return result;
371
371
  };
372
- let RbacServiceBase = class extends backendSdk.develitWorker(
373
- cloudflare_workers.WorkerEntrypoint
374
- ) {
375
- constructor(ctx, env) {
372
+ let RbacServiceBase = class extends backendSdk.develitWorker(cloudflare_workers.WorkerEntrypoint) {
373
+ constructor(ctx, env, scopes) {
376
374
  super(ctx, env);
377
375
  this.db = d1.drizzle(this.env.RBAC_D1, { schema: tables });
376
+ this.SCOPES = scopes;
377
+ }
378
+ validateScope(scope) {
379
+ if (!this.SCOPES.some((s) => s.value === scope)) {
380
+ throw backendSdk.createInternalError(null, {
381
+ message: `Invalid scope: ${scope}. Available scopes: ${this.SCOPES.map((s) => s.value).join(", ")}`,
382
+ status: 400,
383
+ code: "INVALID_SCOPE"
384
+ });
385
+ }
378
386
  }
379
387
  async createRole(input) {
380
388
  return this.handleAction(
@@ -471,6 +479,7 @@ let RbacServiceBase = class extends backendSdk.develitWorker(
471
479
  { data: input, schema: verifyScope.grantScopeToUserInputSchema },
472
480
  { successMessage: "Scope successfully granted to user." },
473
481
  async ({ userId, scope, resourceId }) => {
482
+ this.validateScope(scope);
474
483
  const userScope = await getScopesByUserQuery({ db: this.db, userId });
475
484
  if (userScope.some((s) => s.scope === scope)) {
476
485
  throw backendSdk.createInternalError(null, {
@@ -495,6 +504,7 @@ let RbacServiceBase = class extends backendSdk.develitWorker(
495
504
  { successMessage: "Scopes successfully granted to user." },
496
505
  async ({ userId, scopes }) => {
497
506
  for (const scope of scopes) {
507
+ this.validateScope(scope.scope);
498
508
  const userScopes = await getScopesByUserQuery({ db: this.db, userId });
499
509
  if (userScopes.some((s) => s.scope === scope.scope)) {
500
510
  throw backendSdk.createInternalError(null, {
@@ -518,6 +528,7 @@ let RbacServiceBase = class extends backendSdk.develitWorker(
518
528
  { data: input, schema: verifyScope.revokeScopeFromUserInputSchema },
519
529
  { successMessage: "Scope successfully revoked from user." },
520
530
  async ({ userId, scope, resourceId }) => {
531
+ this.validateScope(scope);
521
532
  const { command } = await revokeScopeFromUserCommand({
522
533
  db: this.db,
523
534
  userId,
@@ -534,6 +545,7 @@ let RbacServiceBase = class extends backendSdk.develitWorker(
534
545
  { data: input, schema: verifyScope.grantScopeToRoleInputSchema },
535
546
  { successMessage: "Scope successfully granted to role." },
536
547
  async ({ roleId, scope, resourceId }) => {
548
+ this.validateScope(scope);
537
549
  const { command } = await grantScopeToRoleCommand({
538
550
  db: this.db,
539
551
  scope,
@@ -550,6 +562,7 @@ let RbacServiceBase = class extends backendSdk.develitWorker(
550
562
  { data: input, schema: verifyScope.revokeScopeFromRoleInputSchema },
551
563
  { successMessage: "Scope successfully revoked from role." },
552
564
  async ({ roleId, scope, resourceId }) => {
565
+ this.validateScope(scope);
553
566
  const { command } = await revokeScopeFromRoleCommand({
554
567
  db: this.db,
555
568
  roleId,
@@ -587,8 +600,9 @@ let RbacServiceBase = class extends backendSdk.develitWorker(
587
600
  return {
588
601
  roles,
589
602
  rolesCount: roles.length,
590
- scopes: [...verifyScope.SCOPES],
591
- scopesCount: [...verifyScope.SCOPES].length,
603
+ scopes: [...this.SCOPES.map((s) => s.value)],
604
+ labeledScopes: this.SCOPES,
605
+ scopesCount: [...this.SCOPES].length,
592
606
  roleScopes,
593
607
  roleScopesCount: roleScopes.length
594
608
  };
@@ -637,6 +651,9 @@ let RbacServiceBase = class extends backendSdk.develitWorker(
637
651
  { data: input, schema: verifyScope.verifyAccessInputSchema },
638
652
  { successMessage: "Access verification completed." },
639
653
  async ({ userId, accessRequests, jwt }) => {
654
+ for (const request of accessRequests) {
655
+ this.validateScope(request.scope);
656
+ }
640
657
  const userPermissionsResponse = await this.getUserPermissions({
641
658
  userId
642
659
  });
@@ -762,10 +779,10 @@ __decorateClass([
762
779
  RbacServiceBase = __decorateClass([
763
780
  backendSdk.service("rbac")
764
781
  ], RbacServiceBase);
765
- function defineRbacService() {
782
+ function defineRbacService(config = { scopes: [] }) {
766
783
  return class RbacService extends RbacServiceBase {
767
784
  constructor(ctx, env) {
768
- super(ctx, env);
785
+ super(ctx, env, config.scopes);
769
786
  }
770
787
  };
771
788
  }
@@ -1,17 +1,19 @@
1
1
  import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
2
2
  import { IRPCResponse } from '@develit-io/backend-sdk';
3
+ import { L as LabeledScope, t as tables, C as CreateRoleInput, a as CreateRoleOutput, A as AssignRoleToUserInput, b as AssignRoleToUserOutput, c as AssignRolesToUserInput, d as AssignRolesToUserOutput, R as RevokeRoleFromUserInput, e as RevokeRoleFromUserOutput, G as GrantScopeToUserInput, f as GrantScopeToUserOutput, g as GrantScopesToUserInput, h as GrantScopesToUserOutput, i as RevokeScopeFromUserInput, j as RevokeScopeFromUserOutput, k as GrantScopeToRoleInput, l as GrantScopeToRoleOutput, m as RevokeScopeFromRoleInput, n as RevokeScopeFromRoleOutput, o as GetPermissionsOutput, p as GetUserPermissionsInput, q as GetUserPermissionsOutput, V as VerifyAccessInput, r as VerifyAccessOutput, D as DeleteRoleInput, s as DeleteRoleOutput, U as UpdateRoleInput, u as UpdateRoleOutput } from '../shared/rbac.CZnF-YgH.cjs';
3
4
  import { WorkerEntrypoint } from 'cloudflare:workers';
4
- import { t as tables, C as CreateRoleInput, a as CreateRoleOutput, A as AssignRoleToUserInput, b as AssignRoleToUserOutput, c as AssignRolesToUserInput, d as AssignRolesToUserOutput, R as RevokeRoleFromUserInput, e as RevokeRoleFromUserOutput, G as GrantScopeToUserInput, f as GrantScopeToUserOutput, g as GrantScopesToUserInput, h as GrantScopesToUserOutput, i as RevokeScopeFromUserInput, j as RevokeScopeFromUserOutput, k as GrantScopeToRoleInput, l as GrantScopeToRoleOutput, m as RevokeScopeFromRoleInput, n as RevokeScopeFromRoleOutput, o as GetPermissionsOutput, p as GetUserPermissionsInput, q as GetUserPermissionsOutput, V as VerifyAccessInput, r as VerifyAccessOutput, D as DeleteRoleInput, s as DeleteRoleOutput, U as UpdateRoleInput, u as UpdateRoleOutput } from '../shared/rbac.CaoDccv4.cjs';
5
5
  import { DrizzleD1Database } from 'drizzle-orm/d1';
6
6
  import 'zod';
7
7
  import 'drizzle-orm';
8
- import '../shared/rbac.BmuK3PNh.cjs';
8
+ import '../shared/rbac.CqpxM3E5.cjs';
9
9
  import 'drizzle-orm/sqlite-core';
10
10
 
11
11
  declare const RbacServiceBase_base: (abstract new (ctx: ExecutionContext, env: RbacEnv) => WorkerEntrypoint<RbacEnv, {}>) & (abstract new (...args: any[]) => _develit_io_backend_sdk.DevelitWorkerMethods);
12
- declare class RbacServiceBase extends RbacServiceBase_base {
12
+ declare class RbacServiceBase<TScopes extends readonly LabeledScope[] = LabeledScope[]> extends RbacServiceBase_base {
13
13
  readonly db: DrizzleD1Database<typeof tables>;
14
- constructor(ctx: ExecutionContext, env: RbacEnv);
14
+ readonly SCOPES: TScopes;
15
+ constructor(ctx: ExecutionContext, env: RbacEnv, scopes: TScopes);
16
+ private validateScope;
15
17
  createRole(input: CreateRoleInput): Promise<IRPCResponse<CreateRoleOutput>>;
16
18
  assignRoleToUser(input: AssignRoleToUserInput): Promise<IRPCResponse<AssignRoleToUserOutput>>;
17
19
  assignRolesToUser(input: AssignRolesToUserInput): Promise<IRPCResponse<AssignRolesToUserOutput>>;
@@ -23,11 +25,19 @@ declare class RbacServiceBase extends RbacServiceBase_base {
23
25
  revokeScopeFromRole(input: RevokeScopeFromRoleInput): Promise<IRPCResponse<RevokeScopeFromRoleOutput>>;
24
26
  getPermissions(): Promise<IRPCResponse<GetPermissionsOutput>>;
25
27
  getUserPermissions(input: GetUserPermissionsInput): Promise<IRPCResponse<GetUserPermissionsOutput>>;
26
- verifyAccess(input: VerifyAccessInput): Promise<IRPCResponse<VerifyAccessOutput>>;
28
+ verifyAccess(input: Omit<VerifyAccessInput, 'accessRequests'> & {
29
+ accessRequests: Array<{
30
+ scope: TScopes[number]['value'];
31
+ resourceId?: string;
32
+ resourcePath?: string;
33
+ }>;
34
+ }): Promise<IRPCResponse<VerifyAccessOutput>>;
27
35
  deleteRole(input: DeleteRoleInput): Promise<IRPCResponse<DeleteRoleOutput>>;
28
36
  updateRole(input: UpdateRoleInput): Promise<IRPCResponse<UpdateRoleOutput>>;
29
37
  }
30
- declare function defineRbacService(): new (ctx: ExecutionContext, env: RbacEnv) => RbacServiceBase;
38
+ declare function defineRbacService<const TScopes extends readonly LabeledScope[]>(config?: {
39
+ scopes: TScopes;
40
+ }): new (ctx: ExecutionContext, env: RbacEnv) => RbacServiceBase<TScopes>;
31
41
 
32
42
  declare const _default: new (ctx: ExecutionContext, env: RbacEnv) => WorkerEntrypoint<RbacEnv>;
33
43
 
@@ -1,17 +1,19 @@
1
1
  import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
2
2
  import { IRPCResponse } from '@develit-io/backend-sdk';
3
+ import { L as LabeledScope, t as tables, C as CreateRoleInput, a as CreateRoleOutput, A as AssignRoleToUserInput, b as AssignRoleToUserOutput, c as AssignRolesToUserInput, d as AssignRolesToUserOutput, R as RevokeRoleFromUserInput, e as RevokeRoleFromUserOutput, G as GrantScopeToUserInput, f as GrantScopeToUserOutput, g as GrantScopesToUserInput, h as GrantScopesToUserOutput, i as RevokeScopeFromUserInput, j as RevokeScopeFromUserOutput, k as GrantScopeToRoleInput, l as GrantScopeToRoleOutput, m as RevokeScopeFromRoleInput, n as RevokeScopeFromRoleOutput, o as GetPermissionsOutput, p as GetUserPermissionsInput, q as GetUserPermissionsOutput, V as VerifyAccessInput, r as VerifyAccessOutput, D as DeleteRoleInput, s as DeleteRoleOutput, U as UpdateRoleInput, u as UpdateRoleOutput } from '../shared/rbac.DhS7RHC3.mjs';
3
4
  import { WorkerEntrypoint } from 'cloudflare:workers';
4
- import { t as tables, C as CreateRoleInput, a as CreateRoleOutput, A as AssignRoleToUserInput, b as AssignRoleToUserOutput, c as AssignRolesToUserInput, d as AssignRolesToUserOutput, R as RevokeRoleFromUserInput, e as RevokeRoleFromUserOutput, G as GrantScopeToUserInput, f as GrantScopeToUserOutput, g as GrantScopesToUserInput, h as GrantScopesToUserOutput, i as RevokeScopeFromUserInput, j as RevokeScopeFromUserOutput, k as GrantScopeToRoleInput, l as GrantScopeToRoleOutput, m as RevokeScopeFromRoleInput, n as RevokeScopeFromRoleOutput, o as GetPermissionsOutput, p as GetUserPermissionsInput, q as GetUserPermissionsOutput, V as VerifyAccessInput, r as VerifyAccessOutput, D as DeleteRoleInput, s as DeleteRoleOutput, U as UpdateRoleInput, u as UpdateRoleOutput } from '../shared/rbac.DH5084jg.mjs';
5
5
  import { DrizzleD1Database } from 'drizzle-orm/d1';
6
6
  import 'zod';
7
7
  import 'drizzle-orm';
8
- import '../shared/rbac.BmuK3PNh.mjs';
8
+ import '../shared/rbac.CqpxM3E5.mjs';
9
9
  import 'drizzle-orm/sqlite-core';
10
10
 
11
11
  declare const RbacServiceBase_base: (abstract new (ctx: ExecutionContext, env: RbacEnv) => WorkerEntrypoint<RbacEnv, {}>) & (abstract new (...args: any[]) => _develit_io_backend_sdk.DevelitWorkerMethods);
12
- declare class RbacServiceBase extends RbacServiceBase_base {
12
+ declare class RbacServiceBase<TScopes extends readonly LabeledScope[] = LabeledScope[]> extends RbacServiceBase_base {
13
13
  readonly db: DrizzleD1Database<typeof tables>;
14
- constructor(ctx: ExecutionContext, env: RbacEnv);
14
+ readonly SCOPES: TScopes;
15
+ constructor(ctx: ExecutionContext, env: RbacEnv, scopes: TScopes);
16
+ private validateScope;
15
17
  createRole(input: CreateRoleInput): Promise<IRPCResponse<CreateRoleOutput>>;
16
18
  assignRoleToUser(input: AssignRoleToUserInput): Promise<IRPCResponse<AssignRoleToUserOutput>>;
17
19
  assignRolesToUser(input: AssignRolesToUserInput): Promise<IRPCResponse<AssignRolesToUserOutput>>;
@@ -23,11 +25,19 @@ declare class RbacServiceBase extends RbacServiceBase_base {
23
25
  revokeScopeFromRole(input: RevokeScopeFromRoleInput): Promise<IRPCResponse<RevokeScopeFromRoleOutput>>;
24
26
  getPermissions(): Promise<IRPCResponse<GetPermissionsOutput>>;
25
27
  getUserPermissions(input: GetUserPermissionsInput): Promise<IRPCResponse<GetUserPermissionsOutput>>;
26
- verifyAccess(input: VerifyAccessInput): Promise<IRPCResponse<VerifyAccessOutput>>;
28
+ verifyAccess(input: Omit<VerifyAccessInput, 'accessRequests'> & {
29
+ accessRequests: Array<{
30
+ scope: TScopes[number]['value'];
31
+ resourceId?: string;
32
+ resourcePath?: string;
33
+ }>;
34
+ }): Promise<IRPCResponse<VerifyAccessOutput>>;
27
35
  deleteRole(input: DeleteRoleInput): Promise<IRPCResponse<DeleteRoleOutput>>;
28
36
  updateRole(input: UpdateRoleInput): Promise<IRPCResponse<UpdateRoleOutput>>;
29
37
  }
30
- declare function defineRbacService(): new (ctx: ExecutionContext, env: RbacEnv) => RbacServiceBase;
38
+ declare function defineRbacService<const TScopes extends readonly LabeledScope[]>(config?: {
39
+ scopes: TScopes;
40
+ }): new (ctx: ExecutionContext, env: RbacEnv) => RbacServiceBase<TScopes>;
31
41
 
32
42
  declare const _default: new (ctx: ExecutionContext, env: RbacEnv) => WorkerEntrypoint<RbacEnv>;
33
43
 
@@ -1,17 +1,19 @@
1
1
  import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
2
2
  import { IRPCResponse } from '@develit-io/backend-sdk';
3
+ import { L as LabeledScope, t as tables, C as CreateRoleInput, a as CreateRoleOutput, A as AssignRoleToUserInput, b as AssignRoleToUserOutput, c as AssignRolesToUserInput, d as AssignRolesToUserOutput, R as RevokeRoleFromUserInput, e as RevokeRoleFromUserOutput, G as GrantScopeToUserInput, f as GrantScopeToUserOutput, g as GrantScopesToUserInput, h as GrantScopesToUserOutput, i as RevokeScopeFromUserInput, j as RevokeScopeFromUserOutput, k as GrantScopeToRoleInput, l as GrantScopeToRoleOutput, m as RevokeScopeFromRoleInput, n as RevokeScopeFromRoleOutput, o as GetPermissionsOutput, p as GetUserPermissionsInput, q as GetUserPermissionsOutput, V as VerifyAccessInput, r as VerifyAccessOutput, D as DeleteRoleInput, s as DeleteRoleOutput, U as UpdateRoleInput, u as UpdateRoleOutput } from '../shared/rbac.DqMMROM3.js';
3
4
  import { WorkerEntrypoint } from 'cloudflare:workers';
4
- import { t as tables, C as CreateRoleInput, a as CreateRoleOutput, A as AssignRoleToUserInput, b as AssignRoleToUserOutput, c as AssignRolesToUserInput, d as AssignRolesToUserOutput, R as RevokeRoleFromUserInput, e as RevokeRoleFromUserOutput, G as GrantScopeToUserInput, f as GrantScopeToUserOutput, g as GrantScopesToUserInput, h as GrantScopesToUserOutput, i as RevokeScopeFromUserInput, j as RevokeScopeFromUserOutput, k as GrantScopeToRoleInput, l as GrantScopeToRoleOutput, m as RevokeScopeFromRoleInput, n as RevokeScopeFromRoleOutput, o as GetPermissionsOutput, p as GetUserPermissionsInput, q as GetUserPermissionsOutput, V as VerifyAccessInput, r as VerifyAccessOutput, D as DeleteRoleInput, s as DeleteRoleOutput, U as UpdateRoleInput, u as UpdateRoleOutput } from '../shared/rbac.CHxy3VJb.js';
5
5
  import { DrizzleD1Database } from 'drizzle-orm/d1';
6
6
  import 'zod';
7
7
  import 'drizzle-orm';
8
- import '../shared/rbac.BmuK3PNh.js';
8
+ import '../shared/rbac.CqpxM3E5.js';
9
9
  import 'drizzle-orm/sqlite-core';
10
10
 
11
11
  declare const RbacServiceBase_base: (abstract new (ctx: ExecutionContext, env: RbacEnv) => WorkerEntrypoint<RbacEnv, {}>) & (abstract new (...args: any[]) => _develit_io_backend_sdk.DevelitWorkerMethods);
12
- declare class RbacServiceBase extends RbacServiceBase_base {
12
+ declare class RbacServiceBase<TScopes extends readonly LabeledScope[] = LabeledScope[]> extends RbacServiceBase_base {
13
13
  readonly db: DrizzleD1Database<typeof tables>;
14
- constructor(ctx: ExecutionContext, env: RbacEnv);
14
+ readonly SCOPES: TScopes;
15
+ constructor(ctx: ExecutionContext, env: RbacEnv, scopes: TScopes);
16
+ private validateScope;
15
17
  createRole(input: CreateRoleInput): Promise<IRPCResponse<CreateRoleOutput>>;
16
18
  assignRoleToUser(input: AssignRoleToUserInput): Promise<IRPCResponse<AssignRoleToUserOutput>>;
17
19
  assignRolesToUser(input: AssignRolesToUserInput): Promise<IRPCResponse<AssignRolesToUserOutput>>;
@@ -23,11 +25,19 @@ declare class RbacServiceBase extends RbacServiceBase_base {
23
25
  revokeScopeFromRole(input: RevokeScopeFromRoleInput): Promise<IRPCResponse<RevokeScopeFromRoleOutput>>;
24
26
  getPermissions(): Promise<IRPCResponse<GetPermissionsOutput>>;
25
27
  getUserPermissions(input: GetUserPermissionsInput): Promise<IRPCResponse<GetUserPermissionsOutput>>;
26
- verifyAccess(input: VerifyAccessInput): Promise<IRPCResponse<VerifyAccessOutput>>;
28
+ verifyAccess(input: Omit<VerifyAccessInput, 'accessRequests'> & {
29
+ accessRequests: Array<{
30
+ scope: TScopes[number]['value'];
31
+ resourceId?: string;
32
+ resourcePath?: string;
33
+ }>;
34
+ }): Promise<IRPCResponse<VerifyAccessOutput>>;
27
35
  deleteRole(input: DeleteRoleInput): Promise<IRPCResponse<DeleteRoleOutput>>;
28
36
  updateRole(input: UpdateRoleInput): Promise<IRPCResponse<UpdateRoleOutput>>;
29
37
  }
30
- declare function defineRbacService(): new (ctx: ExecutionContext, env: RbacEnv) => RbacServiceBase;
38
+ declare function defineRbacService<const TScopes extends readonly LabeledScope[]>(config?: {
39
+ scopes: TScopes;
40
+ }): new (ctx: ExecutionContext, env: RbacEnv) => RbacServiceBase<TScopes>;
31
41
 
32
42
  declare const _default: new (ctx: ExecutionContext, env: RbacEnv) => WorkerEntrypoint<RbacEnv>;
33
43
 
@@ -1,9 +1,9 @@
1
- import { WorkerEntrypoint } from 'cloudflare:workers';
2
1
  import { uuidv4, first, createInternalError, develitWorker, action, service } from '@develit-io/backend-sdk';
3
- import { c as createRoleInputSchema, a as assignRoleToUserInputSchema, b as assignRolesToUserInputSchema, r as revokeRoleFromUserInputSchema, f as grantScopeToUserInputSchema, h as grantScopesToUserInputSchema, j as revokeScopeFromUserInputSchema, e as grantScopeToRoleInputSchema, i as revokeScopeFromRoleInputSchema, S as SCOPES, g as getUserPermissionsInputSchema, v as verifyAccessInputSchema, d as deleteRoleInputSchema, u as updateRoleInputSchema } from '../shared/rbac.CY8-sBAP.mjs';
4
- import { s as schema } from '../shared/rbac.CJLU5iuV.mjs';
2
+ import { s as schema } from '../shared/rbac.D5OV7UPA.mjs';
5
3
  import { eq, and, count, inArray } from 'drizzle-orm';
4
+ import { c as createRoleInputSchema, a as assignRoleToUserInputSchema, b as assignRolesToUserInputSchema, r as revokeRoleFromUserInputSchema, f as grantScopeToUserInputSchema, h as grantScopesToUserInputSchema, j as revokeScopeFromUserInputSchema, e as grantScopeToRoleInputSchema, i as revokeScopeFromRoleInputSchema, g as getUserPermissionsInputSchema, v as verifyAccessInputSchema, d as deleteRoleInputSchema, u as updateRoleInputSchema } from '../shared/rbac.wzMQF48s.mjs';
6
5
  import { z } from 'zod';
6
+ import { WorkerEntrypoint } from 'cloudflare:workers';
7
7
  import { drizzle } from 'drizzle-orm/d1';
8
8
  import 'drizzle-orm/sqlite-core';
9
9
 
@@ -365,12 +365,20 @@ var __decorateClass = (decorators, target, key, kind) => {
365
365
  if (kind && result) __defProp(target, key, result);
366
366
  return result;
367
367
  };
368
- let RbacServiceBase = class extends develitWorker(
369
- WorkerEntrypoint
370
- ) {
371
- constructor(ctx, env) {
368
+ let RbacServiceBase = class extends develitWorker(WorkerEntrypoint) {
369
+ constructor(ctx, env, scopes) {
372
370
  super(ctx, env);
373
371
  this.db = drizzle(this.env.RBAC_D1, { schema: tables });
372
+ this.SCOPES = scopes;
373
+ }
374
+ validateScope(scope) {
375
+ if (!this.SCOPES.some((s) => s.value === scope)) {
376
+ throw createInternalError(null, {
377
+ message: `Invalid scope: ${scope}. Available scopes: ${this.SCOPES.map((s) => s.value).join(", ")}`,
378
+ status: 400,
379
+ code: "INVALID_SCOPE"
380
+ });
381
+ }
374
382
  }
375
383
  async createRole(input) {
376
384
  return this.handleAction(
@@ -467,6 +475,7 @@ let RbacServiceBase = class extends develitWorker(
467
475
  { data: input, schema: grantScopeToUserInputSchema },
468
476
  { successMessage: "Scope successfully granted to user." },
469
477
  async ({ userId, scope, resourceId }) => {
478
+ this.validateScope(scope);
470
479
  const userScope = await getScopesByUserQuery({ db: this.db, userId });
471
480
  if (userScope.some((s) => s.scope === scope)) {
472
481
  throw createInternalError(null, {
@@ -491,6 +500,7 @@ let RbacServiceBase = class extends develitWorker(
491
500
  { successMessage: "Scopes successfully granted to user." },
492
501
  async ({ userId, scopes }) => {
493
502
  for (const scope of scopes) {
503
+ this.validateScope(scope.scope);
494
504
  const userScopes = await getScopesByUserQuery({ db: this.db, userId });
495
505
  if (userScopes.some((s) => s.scope === scope.scope)) {
496
506
  throw createInternalError(null, {
@@ -514,6 +524,7 @@ let RbacServiceBase = class extends develitWorker(
514
524
  { data: input, schema: revokeScopeFromUserInputSchema },
515
525
  { successMessage: "Scope successfully revoked from user." },
516
526
  async ({ userId, scope, resourceId }) => {
527
+ this.validateScope(scope);
517
528
  const { command } = await revokeScopeFromUserCommand({
518
529
  db: this.db,
519
530
  userId,
@@ -530,6 +541,7 @@ let RbacServiceBase = class extends develitWorker(
530
541
  { data: input, schema: grantScopeToRoleInputSchema },
531
542
  { successMessage: "Scope successfully granted to role." },
532
543
  async ({ roleId, scope, resourceId }) => {
544
+ this.validateScope(scope);
533
545
  const { command } = await grantScopeToRoleCommand({
534
546
  db: this.db,
535
547
  scope,
@@ -546,6 +558,7 @@ let RbacServiceBase = class extends develitWorker(
546
558
  { data: input, schema: revokeScopeFromRoleInputSchema },
547
559
  { successMessage: "Scope successfully revoked from role." },
548
560
  async ({ roleId, scope, resourceId }) => {
561
+ this.validateScope(scope);
549
562
  const { command } = await revokeScopeFromRoleCommand({
550
563
  db: this.db,
551
564
  roleId,
@@ -583,8 +596,9 @@ let RbacServiceBase = class extends develitWorker(
583
596
  return {
584
597
  roles,
585
598
  rolesCount: roles.length,
586
- scopes: [...SCOPES],
587
- scopesCount: [...SCOPES].length,
599
+ scopes: [...this.SCOPES.map((s) => s.value)],
600
+ labeledScopes: this.SCOPES,
601
+ scopesCount: [...this.SCOPES].length,
588
602
  roleScopes,
589
603
  roleScopesCount: roleScopes.length
590
604
  };
@@ -633,6 +647,9 @@ let RbacServiceBase = class extends develitWorker(
633
647
  { data: input, schema: verifyAccessInputSchema },
634
648
  { successMessage: "Access verification completed." },
635
649
  async ({ userId, accessRequests, jwt }) => {
650
+ for (const request of accessRequests) {
651
+ this.validateScope(request.scope);
652
+ }
636
653
  const userPermissionsResponse = await this.getUserPermissions({
637
654
  userId
638
655
  });
@@ -758,10 +775,10 @@ __decorateClass([
758
775
  RbacServiceBase = __decorateClass([
759
776
  service("rbac")
760
777
  ], RbacServiceBase);
761
- function defineRbacService() {
778
+ function defineRbacService(config = { scopes: [] }) {
762
779
  return class RbacService extends RbacServiceBase {
763
780
  constructor(ctx, env) {
764
- super(ctx, env);
781
+ super(ctx, env, config.scopes);
765
782
  }
766
783
  };
767
784
  }
@@ -0,0 +1,161 @@
1
+ 'use strict';
2
+
3
+ const zod = require('zod');
4
+
5
+ const assignRoleToUserInputSchema = zod.z.object({
6
+ userId: zod.z.uuid(),
7
+ roleId: zod.z.uuid()
8
+ });
9
+
10
+ const assignRolesToUserInputSchema = zod.z.object({
11
+ userId: zod.z.uuid(),
12
+ roles: zod.z.array(zod.z.uuid())
13
+ });
14
+
15
+ const createRoleInputSchema = zod.z.object({
16
+ name: zod.z.string()
17
+ });
18
+
19
+ const deleteRoleInputSchema = zod.z.object({
20
+ id: zod.z.uuid()
21
+ });
22
+
23
+ const getUserPermissionsInputSchema = zod.z.object({
24
+ userId: zod.z.uuid()
25
+ });
26
+
27
+ const grantScopeToRoleInputSchema = zod.z.object({
28
+ roleId: zod.z.uuid(),
29
+ scope: zod.z.string(),
30
+ resourceId: zod.z.string().optional()
31
+ });
32
+
33
+ const grantScopeToUserInputSchema = zod.z.object({
34
+ userId: zod.z.uuid(),
35
+ scope: zod.z.string(),
36
+ resourceId: zod.z.string().optional()
37
+ });
38
+
39
+ const grantScopesToUserInputSchema = zod.z.object({
40
+ userId: zod.z.uuid(),
41
+ scopes: zod.z.array(
42
+ zod.z.object({
43
+ scope: zod.z.string(),
44
+ resourceId: zod.z.string().optional()
45
+ })
46
+ )
47
+ });
48
+
49
+ const revokeRoleFromUserInputSchema = zod.z.object({
50
+ userId: zod.z.uuid(),
51
+ roleId: zod.z.uuid()
52
+ });
53
+
54
+ const revokeScopeFromRoleInputSchema = zod.z.object({
55
+ roleId: zod.z.uuid(),
56
+ scope: zod.z.string(),
57
+ resourceId: zod.z.string().optional()
58
+ });
59
+
60
+ const revokeScopeFromUserInputSchema = zod.z.object({
61
+ userId: zod.z.uuid(),
62
+ scope: zod.z.string(),
63
+ resourceId: zod.z.string().optional()
64
+ });
65
+
66
+ const updateRoleInputSchema = zod.z.object({
67
+ id: zod.z.uuid(),
68
+ name: zod.z.string()
69
+ });
70
+
71
+ const userDataSchema = zod.z.object({
72
+ referenceId: zod.z.string(),
73
+ email: zod.z.string().optional(),
74
+ role: zod.z.string().optional()
75
+ });
76
+ const jwtPayloadSchema = zod.z.object({
77
+ sub: zod.z.string(),
78
+ user: zod.z.object({
79
+ id: zod.z.uuid(),
80
+ createdAt: zod.z.date().nullable(),
81
+ updatedAt: zod.z.date().nullable(),
82
+ deletedAt: zod.z.date().nullable(),
83
+ role: zod.z.string(),
84
+ email: zod.z.email(),
85
+ rawAppMetaData: zod.z.any(),
86
+ rawUserMetaData: zod.z.any(),
87
+ isSuperAdmin: zod.z.boolean().default(false),
88
+ isSsoUser: zod.z.boolean().default(false),
89
+ lastSignInAt: zod.z.date().nullable().optional(),
90
+ emailConfirmedAt: zod.z.date().nullable().optional(),
91
+ confirmationSentAt: zod.z.date().nullable().optional(),
92
+ recoverySentAt: zod.z.date().nullable().optional(),
93
+ emailChangeToken: zod.z.string().nullable().optional(),
94
+ emailChangeSentAt: zod.z.date().nullable().optional(),
95
+ isBanned: zod.z.boolean().default(false).nullable().optional()
96
+ }),
97
+ iat: zod.z.number(),
98
+ exp: zod.z.number(),
99
+ userData: userDataSchema.optional()
100
+ });
101
+
102
+ const verifyAccessInputSchema = zod.z.object({
103
+ userId: zod.z.uuid(),
104
+ accessRequests: zod.z.array(
105
+ zod.z.object({
106
+ scope: zod.z.string(),
107
+ resourceId: zod.z.string().optional(),
108
+ resourcePath: zod.z.string().optional()
109
+ })
110
+ ),
111
+ jwt: jwtPayloadSchema.extend({
112
+ createdAt: zod.z.coerce.date().nullable().optional(),
113
+ updatedAt: zod.z.coerce.date().nullable().optional(),
114
+ deletedAt: zod.z.coerce.date().nullable().optional(),
115
+ lastSignInAt: zod.z.coerce.date().nullable().optional(),
116
+ emailConfirmedAt: zod.z.coerce.date().nullable().optional(),
117
+ confirmationSentAt: zod.z.coerce.date().nullable().optional(),
118
+ recoverySentAt: zod.z.coerce.date().nullable().optional(),
119
+ emailChangeSentAt: zod.z.coerce.date().nullable().optional(),
120
+ user: jwtPayloadSchema.shape.user.extend({
121
+ createdAt: zod.z.coerce.date().nullable().optional(),
122
+ updatedAt: zod.z.coerce.date().nullable().optional(),
123
+ deletedAt: zod.z.coerce.date().nullable().optional(),
124
+ lastSignInAt: zod.z.coerce.date().nullable().optional(),
125
+ emailConfirmedAt: zod.z.coerce.date().nullable().optional(),
126
+ confirmationSentAt: zod.z.coerce.date().nullable().optional(),
127
+ recoverySentAt: zod.z.coerce.date().nullable().optional(),
128
+ emailChangeSentAt: zod.z.coerce.date().nullable().optional()
129
+ })
130
+ }).optional()
131
+ });
132
+
133
+ const verifyScopeInputSchema = zod.z.object({
134
+ scopes: zod.z.array(zod.z.string()),
135
+ resourceId: zod.z.string().optional(),
136
+ jwt: zod.z.object({
137
+ sub: zod.z.uuid(),
138
+ rbac: zod.z.object({
139
+ roles: zod.z.array(zod.z.string()).optional()
140
+ })
141
+ })
142
+ });
143
+ const verifyScopeOutputSchema = zod.z.object({
144
+ isVerified: zod.z.boolean().default(false)
145
+ });
146
+
147
+ exports.assignRoleToUserInputSchema = assignRoleToUserInputSchema;
148
+ exports.assignRolesToUserInputSchema = assignRolesToUserInputSchema;
149
+ exports.createRoleInputSchema = createRoleInputSchema;
150
+ exports.deleteRoleInputSchema = deleteRoleInputSchema;
151
+ exports.getUserPermissionsInputSchema = getUserPermissionsInputSchema;
152
+ exports.grantScopeToRoleInputSchema = grantScopeToRoleInputSchema;
153
+ exports.grantScopeToUserInputSchema = grantScopeToUserInputSchema;
154
+ exports.grantScopesToUserInputSchema = grantScopesToUserInputSchema;
155
+ exports.revokeRoleFromUserInputSchema = revokeRoleFromUserInputSchema;
156
+ exports.revokeScopeFromRoleInputSchema = revokeScopeFromRoleInputSchema;
157
+ exports.revokeScopeFromUserInputSchema = revokeScopeFromUserInputSchema;
158
+ exports.updateRoleInputSchema = updateRoleInputSchema;
159
+ exports.verifyAccessInputSchema = verifyAccessInputSchema;
160
+ exports.verifyScopeInputSchema = verifyScopeInputSchema;
161
+ exports.verifyScopeOutputSchema = verifyScopeOutputSchema;