@mastra/auth-workos 1.4.0 → 1.5.0

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/dist/index.js CHANGED
@@ -846,40 +846,56 @@ var MastraFGAWorkos = class {
846
846
  *
847
847
  * Resolves the user's organization membership ID, maps the permission
848
848
  * via `permissionMapping`, and delegates to `workos.authorization.check()`.
849
+ *
850
+ * When `params.permission` is an array, ANY-of semantics apply: returns true
851
+ * if any single permission in the array authorizes the user.
849
852
  */
850
853
  async check(user, params) {
851
- const checkOptions = this.buildCheckOptions(user, params);
852
- if (!checkOptions) return false;
853
- try {
854
- const result = await this.workos.authorization.check(checkOptions);
855
- return result.authorized;
856
- } catch (error) {
857
- if (isWorkOSResourceNotFoundError(error)) {
858
- return false;
854
+ const permissions = Array.isArray(params.permission) ? params.permission : [params.permission];
855
+ if (permissions.length === 0) return false;
856
+ for (const permission of permissions) {
857
+ const checkOptions = this.buildCheckOptions(user, { ...params, permission });
858
+ if (!checkOptions) continue;
859
+ try {
860
+ const result = await this.workos.authorization.check(checkOptions);
861
+ if (result.authorized) return true;
862
+ } catch (error) {
863
+ if (isWorkOSResourceNotFoundError(error)) continue;
864
+ throw error;
859
865
  }
860
- throw error;
861
866
  }
867
+ return false;
862
868
  }
863
869
  /**
864
870
  * Require that a user has permission, throwing FGADeniedError if not.
871
+ *
872
+ * When `params.permission` is an array, ANY-of semantics apply: passes if any
873
+ * single permission authorizes the user; throws if none do.
865
874
  */
866
875
  async require(user, params) {
867
- const checkOptions = this.buildCheckOptions(user, params, { strictMembershipResolution: true });
868
- if (!checkOptions) {
876
+ const permissions = Array.isArray(params.permission) ? params.permission : [params.permission];
877
+ if (permissions.length === 0) {
869
878
  throw new FGADeniedError(user, params.resource, params.permission);
870
879
  }
871
- try {
872
- const result = await this.workos.authorization.check(checkOptions);
873
- if (!result.authorized) {
874
- throw new FGADeniedError(user, params.resource, params.permission);
875
- }
876
- } catch (error) {
877
- if (error instanceof FGADeniedError) throw error;
878
- if (isWorkOSResourceNotFoundError(error)) {
879
- throw new FGADeniedError(user, params.resource, params.permission);
880
+ let lastError;
881
+ for (const permission of permissions) {
882
+ const checkOptions = this.buildCheckOptions(
883
+ user,
884
+ { ...params, permission },
885
+ { strictMembershipResolution: true }
886
+ );
887
+ if (!checkOptions) continue;
888
+ try {
889
+ const result = await this.workos.authorization.check(checkOptions);
890
+ if (result.authorized) return;
891
+ } catch (error) {
892
+ if (error instanceof FGADeniedError) throw error;
893
+ if (isWorkOSResourceNotFoundError(error)) continue;
894
+ lastError = error;
880
895
  }
881
- throw error;
882
896
  }
897
+ if (lastError) throw lastError;
898
+ throw new FGADeniedError(user, params.resource, params.permission);
883
899
  }
884
900
  /**
885
901
  * Filter resources to only those the user has permission to access.