@adonis-agora/authkit-server 0.43.0 → 0.44.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.
@@ -0,0 +1,26 @@
1
+ import type { AuthAccount } from '../accounts/account_store.js';
2
+ /** The slice of the resolved config the role resolution needs (structurally typed). */
3
+ type RoleResolverConfig = {
4
+ resolveTokenRoles?: (account: AuthAccount, context: {
5
+ clientId?: string;
6
+ activeOrg?: {
7
+ orgId: string;
8
+ orgSlug: string;
9
+ orgRole: string;
10
+ } | null;
11
+ }) => string[] | Promise<string[]>;
12
+ };
13
+ /**
14
+ * An account's effective roles for host-side gating (the admin console).
15
+ *
16
+ * When the host configures `resolveTokenRoles`, that hook is the single source of an account's roles
17
+ * — the same one the OIDC `roles` claim is minted from — so console access tracks the host's role
18
+ * authority (e.g. an app that keeps roles in its own table via `@adonis-agora/authz`) instead of the
19
+ * account's stored `globalRoles`. Falls back to `account.globalRoles ?? []` when no hook is set, so
20
+ * default behavior is unchanged.
21
+ *
22
+ * The console is not OIDC-client- or org-scoped for role purposes, so the hook is called with an
23
+ * empty context (`clientId: undefined`, `activeOrg: null`).
24
+ */
25
+ export declare function resolveAccountRoles(cfg: RoleResolverConfig, account: AuthAccount): Promise<string[]>;
26
+ export {};
@@ -0,0 +1,18 @@
1
+ /**
2
+ * An account's effective roles for host-side gating (the admin console).
3
+ *
4
+ * When the host configures `resolveTokenRoles`, that hook is the single source of an account's roles
5
+ * — the same one the OIDC `roles` claim is minted from — so console access tracks the host's role
6
+ * authority (e.g. an app that keeps roles in its own table via `@adonis-agora/authz`) instead of the
7
+ * account's stored `globalRoles`. Falls back to `account.globalRoles ?? []` when no hook is set, so
8
+ * default behavior is unchanged.
9
+ *
10
+ * The console is not OIDC-client- or org-scoped for role purposes, so the hook is called with an
11
+ * empty context (`clientId: undefined`, `activeOrg: null`).
12
+ */
13
+ export async function resolveAccountRoles(cfg, account) {
14
+ if (cfg.resolveTokenRoles) {
15
+ return cfg.resolveTokenRoles(account, { clientId: undefined, activeOrg: null });
16
+ }
17
+ return account.globalRoles ?? [];
18
+ }
@@ -1,6 +1,7 @@
1
1
  import { readFile } from 'node:fs/promises';
2
2
  import { extname } from 'node:path';
3
3
  import { getAdminPrefix } from '../admin_prefix.js';
4
+ import { resolveAccountRoles } from '../account_roles.js';
4
5
  // ─── Shell HTML cache ─────────────────────────────────────────────────────────
5
6
  /**
6
7
  * Points to the Vite-built index.html inside build/host/ui-dist/.
@@ -120,7 +121,7 @@ export default class AdminShellController {
120
121
  currentUser = {
121
122
  id: account.id,
122
123
  email: account.email,
123
- roles: account.globalRoles ?? [],
124
+ roles: await resolveAccountRoles(cfg, account),
124
125
  };
125
126
  }
126
127
  }
@@ -2,6 +2,7 @@ import { resolveRateLimit } from '../define_config.js';
2
2
  import { createAuthThrottles } from './rate_limit.js';
3
3
  import { ACCOUNT_SESSION_KEY } from './middleware/account_auth.js';
4
4
  import { accountHome } from './account_home.js';
5
+ import { resolveAccountRoles } from './account_roles.js';
5
6
  import { adminApiGuard } from './admin_api/admin_api_guard.js';
6
7
  import { setAdminPrefix, normalizeAdminPrefix, setAdminApiPrefix, normalizeAdminApiPrefix, } from './admin_prefix.js';
7
8
  import { resolveRuntimeSettings } from './runtime_settings.js';
@@ -112,7 +113,10 @@ export const adminGuard = async (ctx, next) => {
112
113
  }
113
114
  const allowed = cfg.admin.roles;
114
115
  const account = await cfg.accountStore.findById(accountId);
115
- const roles = account?.globalRoles ?? [];
116
+ // Resolve roles through the host's role authority (`resolveTokenRoles`) when set — the same source
117
+ // the token claim is minted from — so an app-role admin reaches the console. Falls back to the
118
+ // account's stored `globalRoles` when no hook is configured.
119
+ const roles = account ? await resolveAccountRoles(cfg, account) : [];
116
120
  const isAdmin = roles.some((r) => allowed.includes(r));
117
121
  if (!isAdmin) {
118
122
  // Evita vazar a existência do console admin: redireciona para o accountHome
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonis-agora/authkit-server",
3
- "version": "0.43.0",
3
+ "version": "0.44.0",
4
4
  "description": "AdonisJS OIDC/OAuth2 provider (Identity Provider) toolkit: ejectable auth server with sessions, rate-limiting, MFA/TOTP, audit log, federated logout and OpenTelemetry metrics.",
5
5
  "license": "MIT",
6
6
  "author": "dudousxd",