@authyon/server 0.2.0-beta.1 → 0.2.0-beta.2

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/README.md CHANGED
@@ -5,7 +5,7 @@ SDK server-side para o [Authyon](https://authyon.com) — administração de amb
5
5
  ## Instalação da beta
6
6
 
7
7
  ```bash
8
- npm install --save-exact @authyon/server@0.2.0-beta.1
8
+ npm install --save-exact @authyon/server@0.2.0-beta.2
9
9
  ```
10
10
 
11
11
  Durante a beta, `npm install @authyon/server@beta` acompanha o prerelease mais recente. Fixar a versão exata é recomendado para builds reproduzíveis.
@@ -93,6 +93,11 @@ Usa as credenciais de ambiente (`clientId`/`clientSecret`) automaticamente — o
93
93
 
94
94
  ```ts
95
95
  const org = await authyon.environment.tenants.create({ name: "Acme", slug: "acme" });
96
+ const tenants = await authyon.environment.tenants.list({
97
+ search: "acme",
98
+ skip: 0,
99
+ take: 20,
100
+ });
96
101
  await authyon.environment.tenants.members.add(org.id, userId, ["owner"]);
97
102
  await authyon.environment.tenants.members.assignRole(org.id, userId, "billing-admin");
98
103
  await authyon.environment.tenants.members.remove(org.id, userId);
package/dist/index.cjs CHANGED
@@ -39,6 +39,7 @@ __export(index_exports, {
39
39
  createAuthyonRules: () => createAuthyonRules,
40
40
  createClient: () => createClient,
41
41
  createExpressAuthorizationMiddleware: () => createExpressAuthorizationMiddleware,
42
+ hasPermission: () => hasPermission,
42
43
  normalizeClientIp: () => normalizeClientIp
43
44
  });
44
45
  module.exports = __toCommonJS(index_exports);
@@ -663,8 +664,8 @@ var AuthyonServerClient = class {
663
664
  )
664
665
  },
665
666
  tenants: {
666
- /** GET /env/tenants — list tenants in the environment. */
667
- list: () => this.request("/env/tenants", { envBearer: true }),
667
+ /** GET /env/tenants — paginated, searchable list of tenants in the environment. */
668
+ list: (params = {}) => this.request("/env/tenants", { envBearer: true, query: params }),
668
669
  /** POST /env/tenants — create a tenant in the environment. */
669
670
  create: (input) => this.request("/env/tenants", { method: "POST", envBearer: true, body: input }),
670
671
  /** GET /env/tenants/{tenantId} — fetch a tenant by id. */
@@ -1080,6 +1081,17 @@ var AuthyonAbilityBuilder = class {
1080
1081
  function createAuthyonAbility(source = {}, options = {}) {
1081
1082
  return new AuthyonAbility(createAuthyonRules(source, options), options.detectSubjectType);
1082
1083
  }
1084
+ function hasPermission(source, requiredPermission) {
1085
+ const requirement = permissionToRule(requiredPermission);
1086
+ if (!requirement || typeof requirement.action !== "string" || typeof requirement.subject !== "string") {
1087
+ return false;
1088
+ }
1089
+ const permissionSource = isPermissionList(source) ? { permissions: source } : source;
1090
+ return createAuthyonAbility(permissionSource).can(requirement.action, requirement.subject);
1091
+ }
1092
+ function isPermissionList(source) {
1093
+ return Array.isArray(source);
1094
+ }
1083
1095
  function createAuthyonRules(source = {}, options = {}) {
1084
1096
  const permissions = /* @__PURE__ */ new Set([
1085
1097
  ...source.permissions ?? [],
@@ -1109,9 +1121,14 @@ function permissionToRule(permission) {
1109
1121
  }
1110
1122
  function matchesToken(value, expected, wildcard) {
1111
1123
  return (Array.isArray(value) ? value : [value]).some(
1112
- (candidate) => candidate === expected || candidate === wildcard || candidate === "*"
1124
+ (candidate) => candidate === expected || candidate === wildcard || candidate === "*" || matchesSegments(candidate, expected)
1113
1125
  );
1114
1126
  }
1127
+ function matchesSegments(pattern, value) {
1128
+ const patternSegments = pattern.split(":");
1129
+ const valueSegments = value.split(":");
1130
+ return patternSegments.length === valueSegments.length && patternSegments.every((segment, index) => segment === "*" || segment === valueSegments[index]);
1131
+ }
1115
1132
  function matchesField(pattern, field) {
1116
1133
  if (pattern === "*" || pattern === field) return true;
1117
1134
  return pattern.endsWith(".*") && field.startsWith(pattern.slice(0, -1));
@@ -1324,5 +1341,6 @@ function unauthenticated() {
1324
1341
  createAuthyonRules,
1325
1342
  createClient,
1326
1343
  createExpressAuthorizationMiddleware,
1344
+ hasPermission,
1327
1345
  normalizeClientIp
1328
1346
  });
package/dist/index.d.cts CHANGED
@@ -118,6 +118,8 @@ interface User {
118
118
  id: string;
119
119
  email: string;
120
120
  username?: string;
121
+ /** Environment-level roles from the database-validated user profile. */
122
+ roles?: string[];
121
123
  permissions?: string[];
122
124
  }
123
125
  interface RoleGrant {
@@ -278,8 +280,7 @@ type LoginActivity = AuditEvent;
278
280
  /**
279
281
  * Envelope every `skip`/`take`-paginated list endpoint returns, confirmed
280
282
  * live for `/env/users`, `/env/audit` and `/env/audit/login-activity`.
281
- * Endpoints without `skip`/`take` params (e.g. `/env/tenants`) return a
282
- * bare array instead.
283
+ * Endpoints without `skip`/`take` params return a bare array instead.
283
284
  */
284
285
  /** OAuth 2.0 client-credentials pair minted in the Authyon console. */
285
286
  interface ClientCredentials {
@@ -476,8 +477,10 @@ declare class AuthyonServerClient {
476
477
  removeTenant: (userId: string, tenantId: string) => Promise<void>;
477
478
  };
478
479
  tenants: {
479
- /** GET /env/tenants — list tenants in the environment. */
480
- list: () => Promise<Organization[]>;
480
+ /** GET /env/tenants — paginated, searchable list of tenants in the environment. */
481
+ list: (params?: {
482
+ search?: string;
483
+ } & PaginationOptions) => Promise<Paged<Organization>>;
481
484
  /** POST /env/tenants — create a tenant in the environment. */
482
485
  create: (input: CreateOrganizationInput) => Promise<Organization>;
483
486
  /** GET /env/tenants/{tenantId} — fetch a tenant by id. */
@@ -733,6 +736,8 @@ declare class AuthyonAbilityBuilder {
733
736
  }
734
737
  /** Creates an ability from Authyon permissions, OAuth scope and optional role rules. */
735
738
  declare function createAuthyonAbility(source?: AuthyonPermissionSource, options?: AuthyonAbilityOptions): AuthyonAbility;
739
+ /** Checks an Authyon permission string using the same wildcard rules as an ability. */
740
+ declare function hasPermission(source: AuthyonPermissionSource | readonly string[], requiredPermission: string): boolean;
736
741
  /** Converts Authyon's `subject:action` permission strings to authorization rules. */
737
742
  declare function createAuthyonRules(source?: AuthyonPermissionSource, options?: AuthyonAbilityOptions): AbilityRule[];
738
743
 
@@ -798,4 +803,4 @@ declare function createExpressAuthorizationMiddleware(client: AuthyonServerClien
798
803
  /** Converts an authorization error into a Fetch Response suitable for Next.js. */
799
804
  declare function createAuthorizationErrorResponse(error: unknown): Response;
800
805
 
801
- export { type AbilityConditions, type AbilityEvent, type AbilityListener, type AbilityRequirement, type AbilityRule, type AbilitySubject, type AuditEvent, type AuthorizationContext, AuthorizationError, type AuthorizationOptions, AuthyonAbility, AuthyonAbilityBuilder, type AuthyonAbilityOptions, AuthyonError, type AuthyonErrorAction, type AuthyonErrorCategory, type AuthyonErrorInterpretation, type AuthyonJwksDiscoveryOptions, type AuthyonJwtClaims, type AuthyonPermissionSource, AuthyonServerClient, AuthyonServerClientBuilder, type AuthyonServerClientOptions, type ClientCredentials, type ClientRequestContext, type CreateOrganizationInput, type CreatePermissionInput, type CreateUserInput, type EnvironmentUser, ErrorCodes, type ExpressAuthorizationMiddlewareOptions, type ExpressNext, type ExpressRequestLike, type ExpressResponseLike, FetchHttpAdapter, type HttpAdapter, type HttpAdapterRequest, type HttpLogEvent, type HttpLogger, type HttpLoggerOptions, type IntrospectResult, type JsonWebKeySet, JwksTokenVerifier, type JwksTokenVerifierOptions, type KnownErrorCode, LoggingHttpAdapter, type LoginActivity, type OpenIdConfiguration, type Organization, type Paged, type PaginationOptions, type Permission, type PermissionsByRole, type RequestAuthorizationOptions, type ReservedPermissions, type Role, type RoleGrant, type RoleInput, type RoleList, type RolePermissions, type SafeJwtAlgorithm, type TenantMembership, TenantScopedClient, type TokenResult, TokenVerificationError, type TokenVerificationErrorCode, type UpdateOrganizationInput, type User, type ValidateResult, type VerifiedAccessToken, authorizeRequest, authorizeToken, createAuthorizationErrorResponse, createAuthyonAbility, createAuthyonRules, createClient, createExpressAuthorizationMiddleware, normalizeClientIp };
806
+ export { type AbilityConditions, type AbilityEvent, type AbilityListener, type AbilityRequirement, type AbilityRule, type AbilitySubject, type AuditEvent, type AuthorizationContext, AuthorizationError, type AuthorizationOptions, AuthyonAbility, AuthyonAbilityBuilder, type AuthyonAbilityOptions, AuthyonError, type AuthyonErrorAction, type AuthyonErrorCategory, type AuthyonErrorInterpretation, type AuthyonJwksDiscoveryOptions, type AuthyonJwtClaims, type AuthyonPermissionSource, AuthyonServerClient, AuthyonServerClientBuilder, type AuthyonServerClientOptions, type ClientCredentials, type ClientRequestContext, type CreateOrganizationInput, type CreatePermissionInput, type CreateUserInput, type EnvironmentUser, ErrorCodes, type ExpressAuthorizationMiddlewareOptions, type ExpressNext, type ExpressRequestLike, type ExpressResponseLike, FetchHttpAdapter, type HttpAdapter, type HttpAdapterRequest, type HttpLogEvent, type HttpLogger, type HttpLoggerOptions, type IntrospectResult, type JsonWebKeySet, JwksTokenVerifier, type JwksTokenVerifierOptions, type KnownErrorCode, LoggingHttpAdapter, type LoginActivity, type OpenIdConfiguration, type Organization, type Paged, type PaginationOptions, type Permission, type PermissionsByRole, type RequestAuthorizationOptions, type ReservedPermissions, type Role, type RoleGrant, type RoleInput, type RoleList, type RolePermissions, type SafeJwtAlgorithm, type TenantMembership, TenantScopedClient, type TokenResult, TokenVerificationError, type TokenVerificationErrorCode, type UpdateOrganizationInput, type User, type ValidateResult, type VerifiedAccessToken, authorizeRequest, authorizeToken, createAuthorizationErrorResponse, createAuthyonAbility, createAuthyonRules, createClient, createExpressAuthorizationMiddleware, hasPermission, normalizeClientIp };
package/dist/index.d.ts CHANGED
@@ -118,6 +118,8 @@ interface User {
118
118
  id: string;
119
119
  email: string;
120
120
  username?: string;
121
+ /** Environment-level roles from the database-validated user profile. */
122
+ roles?: string[];
121
123
  permissions?: string[];
122
124
  }
123
125
  interface RoleGrant {
@@ -278,8 +280,7 @@ type LoginActivity = AuditEvent;
278
280
  /**
279
281
  * Envelope every `skip`/`take`-paginated list endpoint returns, confirmed
280
282
  * live for `/env/users`, `/env/audit` and `/env/audit/login-activity`.
281
- * Endpoints without `skip`/`take` params (e.g. `/env/tenants`) return a
282
- * bare array instead.
283
+ * Endpoints without `skip`/`take` params return a bare array instead.
283
284
  */
284
285
  /** OAuth 2.0 client-credentials pair minted in the Authyon console. */
285
286
  interface ClientCredentials {
@@ -476,8 +477,10 @@ declare class AuthyonServerClient {
476
477
  removeTenant: (userId: string, tenantId: string) => Promise<void>;
477
478
  };
478
479
  tenants: {
479
- /** GET /env/tenants — list tenants in the environment. */
480
- list: () => Promise<Organization[]>;
480
+ /** GET /env/tenants — paginated, searchable list of tenants in the environment. */
481
+ list: (params?: {
482
+ search?: string;
483
+ } & PaginationOptions) => Promise<Paged<Organization>>;
481
484
  /** POST /env/tenants — create a tenant in the environment. */
482
485
  create: (input: CreateOrganizationInput) => Promise<Organization>;
483
486
  /** GET /env/tenants/{tenantId} — fetch a tenant by id. */
@@ -733,6 +736,8 @@ declare class AuthyonAbilityBuilder {
733
736
  }
734
737
  /** Creates an ability from Authyon permissions, OAuth scope and optional role rules. */
735
738
  declare function createAuthyonAbility(source?: AuthyonPermissionSource, options?: AuthyonAbilityOptions): AuthyonAbility;
739
+ /** Checks an Authyon permission string using the same wildcard rules as an ability. */
740
+ declare function hasPermission(source: AuthyonPermissionSource | readonly string[], requiredPermission: string): boolean;
736
741
  /** Converts Authyon's `subject:action` permission strings to authorization rules. */
737
742
  declare function createAuthyonRules(source?: AuthyonPermissionSource, options?: AuthyonAbilityOptions): AbilityRule[];
738
743
 
@@ -798,4 +803,4 @@ declare function createExpressAuthorizationMiddleware(client: AuthyonServerClien
798
803
  /** Converts an authorization error into a Fetch Response suitable for Next.js. */
799
804
  declare function createAuthorizationErrorResponse(error: unknown): Response;
800
805
 
801
- export { type AbilityConditions, type AbilityEvent, type AbilityListener, type AbilityRequirement, type AbilityRule, type AbilitySubject, type AuditEvent, type AuthorizationContext, AuthorizationError, type AuthorizationOptions, AuthyonAbility, AuthyonAbilityBuilder, type AuthyonAbilityOptions, AuthyonError, type AuthyonErrorAction, type AuthyonErrorCategory, type AuthyonErrorInterpretation, type AuthyonJwksDiscoveryOptions, type AuthyonJwtClaims, type AuthyonPermissionSource, AuthyonServerClient, AuthyonServerClientBuilder, type AuthyonServerClientOptions, type ClientCredentials, type ClientRequestContext, type CreateOrganizationInput, type CreatePermissionInput, type CreateUserInput, type EnvironmentUser, ErrorCodes, type ExpressAuthorizationMiddlewareOptions, type ExpressNext, type ExpressRequestLike, type ExpressResponseLike, FetchHttpAdapter, type HttpAdapter, type HttpAdapterRequest, type HttpLogEvent, type HttpLogger, type HttpLoggerOptions, type IntrospectResult, type JsonWebKeySet, JwksTokenVerifier, type JwksTokenVerifierOptions, type KnownErrorCode, LoggingHttpAdapter, type LoginActivity, type OpenIdConfiguration, type Organization, type Paged, type PaginationOptions, type Permission, type PermissionsByRole, type RequestAuthorizationOptions, type ReservedPermissions, type Role, type RoleGrant, type RoleInput, type RoleList, type RolePermissions, type SafeJwtAlgorithm, type TenantMembership, TenantScopedClient, type TokenResult, TokenVerificationError, type TokenVerificationErrorCode, type UpdateOrganizationInput, type User, type ValidateResult, type VerifiedAccessToken, authorizeRequest, authorizeToken, createAuthorizationErrorResponse, createAuthyonAbility, createAuthyonRules, createClient, createExpressAuthorizationMiddleware, normalizeClientIp };
806
+ export { type AbilityConditions, type AbilityEvent, type AbilityListener, type AbilityRequirement, type AbilityRule, type AbilitySubject, type AuditEvent, type AuthorizationContext, AuthorizationError, type AuthorizationOptions, AuthyonAbility, AuthyonAbilityBuilder, type AuthyonAbilityOptions, AuthyonError, type AuthyonErrorAction, type AuthyonErrorCategory, type AuthyonErrorInterpretation, type AuthyonJwksDiscoveryOptions, type AuthyonJwtClaims, type AuthyonPermissionSource, AuthyonServerClient, AuthyonServerClientBuilder, type AuthyonServerClientOptions, type ClientCredentials, type ClientRequestContext, type CreateOrganizationInput, type CreatePermissionInput, type CreateUserInput, type EnvironmentUser, ErrorCodes, type ExpressAuthorizationMiddlewareOptions, type ExpressNext, type ExpressRequestLike, type ExpressResponseLike, FetchHttpAdapter, type HttpAdapter, type HttpAdapterRequest, type HttpLogEvent, type HttpLogger, type HttpLoggerOptions, type IntrospectResult, type JsonWebKeySet, JwksTokenVerifier, type JwksTokenVerifierOptions, type KnownErrorCode, LoggingHttpAdapter, type LoginActivity, type OpenIdConfiguration, type Organization, type Paged, type PaginationOptions, type Permission, type PermissionsByRole, type RequestAuthorizationOptions, type ReservedPermissions, type Role, type RoleGrant, type RoleInput, type RoleList, type RolePermissions, type SafeJwtAlgorithm, type TenantMembership, TenantScopedClient, type TokenResult, TokenVerificationError, type TokenVerificationErrorCode, type UpdateOrganizationInput, type User, type ValidateResult, type VerifiedAccessToken, authorizeRequest, authorizeToken, createAuthorizationErrorResponse, createAuthyonAbility, createAuthyonRules, createClient, createExpressAuthorizationMiddleware, hasPermission, normalizeClientIp };
package/dist/index.js CHANGED
@@ -622,8 +622,8 @@ var AuthyonServerClient = class {
622
622
  )
623
623
  },
624
624
  tenants: {
625
- /** GET /env/tenants — list tenants in the environment. */
626
- list: () => this.request("/env/tenants", { envBearer: true }),
625
+ /** GET /env/tenants — paginated, searchable list of tenants in the environment. */
626
+ list: (params = {}) => this.request("/env/tenants", { envBearer: true, query: params }),
627
627
  /** POST /env/tenants — create a tenant in the environment. */
628
628
  create: (input) => this.request("/env/tenants", { method: "POST", envBearer: true, body: input }),
629
629
  /** GET /env/tenants/{tenantId} — fetch a tenant by id. */
@@ -1039,6 +1039,17 @@ var AuthyonAbilityBuilder = class {
1039
1039
  function createAuthyonAbility(source = {}, options = {}) {
1040
1040
  return new AuthyonAbility(createAuthyonRules(source, options), options.detectSubjectType);
1041
1041
  }
1042
+ function hasPermission(source, requiredPermission) {
1043
+ const requirement = permissionToRule(requiredPermission);
1044
+ if (!requirement || typeof requirement.action !== "string" || typeof requirement.subject !== "string") {
1045
+ return false;
1046
+ }
1047
+ const permissionSource = isPermissionList(source) ? { permissions: source } : source;
1048
+ return createAuthyonAbility(permissionSource).can(requirement.action, requirement.subject);
1049
+ }
1050
+ function isPermissionList(source) {
1051
+ return Array.isArray(source);
1052
+ }
1042
1053
  function createAuthyonRules(source = {}, options = {}) {
1043
1054
  const permissions = /* @__PURE__ */ new Set([
1044
1055
  ...source.permissions ?? [],
@@ -1068,9 +1079,14 @@ function permissionToRule(permission) {
1068
1079
  }
1069
1080
  function matchesToken(value, expected, wildcard) {
1070
1081
  return (Array.isArray(value) ? value : [value]).some(
1071
- (candidate) => candidate === expected || candidate === wildcard || candidate === "*"
1082
+ (candidate) => candidate === expected || candidate === wildcard || candidate === "*" || matchesSegments(candidate, expected)
1072
1083
  );
1073
1084
  }
1085
+ function matchesSegments(pattern, value) {
1086
+ const patternSegments = pattern.split(":");
1087
+ const valueSegments = value.split(":");
1088
+ return patternSegments.length === valueSegments.length && patternSegments.every((segment, index) => segment === "*" || segment === valueSegments[index]);
1089
+ }
1074
1090
  function matchesField(pattern, field) {
1075
1091
  if (pattern === "*" || pattern === field) return true;
1076
1092
  return pattern.endsWith(".*") && field.startsWith(pattern.slice(0, -1));
@@ -1282,5 +1298,6 @@ export {
1282
1298
  createAuthyonRules,
1283
1299
  createClient,
1284
1300
  createExpressAuthorizationMiddleware,
1301
+ hasPermission,
1285
1302
  normalizeClientIp
1286
1303
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@authyon/server",
3
- "version": "0.2.0-beta.1",
3
+ "version": "0.2.0-beta.2",
4
4
  "description": "Authyon SDK for backends — server-to-server organization/member management and access-token verification.",
5
5
  "license": "MIT",
6
6
  "type": "module",