@adonis-agora/authkit-server 0.82.2 → 0.83.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.
Files changed (31) hide show
  1. package/build/src/accounts/account_store.d.ts +12 -0
  2. package/build/src/accounts/lucid_account_store.js +30 -0
  3. package/build/src/accounts/lucid_store/organizations.d.ts +6 -0
  4. package/build/src/accounts/lucid_store/organizations.js +65 -1
  5. package/build/src/define_config.d.ts +36 -4
  6. package/build/src/host/account_roles.d.ts +14 -3
  7. package/build/src/host/account_roles.js +15 -4
  8. package/build/src/host/admin_api/admin_orgs_service.js +5 -0
  9. package/build/src/host/admin_api/api_users_controller.d.ts +3 -0
  10. package/build/src/host/admin_api/api_users_controller.js +9 -5
  11. package/build/src/host/admin_api/dto.d.ts +24 -0
  12. package/build/src/host/admin_api/dto.js +14 -0
  13. package/build/src/host/admin_api/token_verify_service.js +7 -2
  14. package/build/src/host/admin_console/admin_shell_controller.js +3 -1
  15. package/build/src/host/admin_console/console_users_controller.d.ts +4 -0
  16. package/build/src/host/admin_console/console_users_controller.js +12 -6
  17. package/build/src/host/controllers/interaction_controller.d.ts +8 -0
  18. package/build/src/host/controllers/interaction_controller.js +46 -12
  19. package/build/src/host/controllers/pat_introspection_controller.d.ts +1 -1
  20. package/build/src/host/controllers/pat_introspection_controller.js +2 -1
  21. package/build/src/host/controllers/registration_controller.d.ts +10 -0
  22. package/build/src/host/controllers/registration_controller.js +94 -30
  23. package/build/src/host/controllers/social_controller.js +62 -1
  24. package/build/src/host/register_auth_host.js +4 -0
  25. package/build/src/host/signup_token.d.ts +9 -0
  26. package/build/src/host/signup_token.js +31 -0
  27. package/build/src/host/social_pending.d.ts +17 -0
  28. package/build/src/host/social_pending.js +11 -0
  29. package/build/src/host/ui-dist/assets/{index-C1eSfG8B.js → index-DhQzXw2i.js} +1 -1
  30. package/build/src/host/ui-dist/index.html +1 -1
  31. package/package.json +2 -2
@@ -608,6 +608,18 @@ export interface OrganizationsCapability {
608
608
  listOrgsForAccount(accountId: string): Promise<Array<OrgSummary & {
609
609
  role: string;
610
610
  }>>;
611
+ /**
612
+ * OPTIONAL: every org that has at least one member whose account exists, with
613
+ * its member count, in a bounded number of queries. Must return exactly what
614
+ * the admin fallback builds by walking `listAccounts` (admin order) and calling
615
+ * `listOrgsForAccount` per account: orgs in first-seen order, `role` being the
616
+ * role of the first member seen in that order, `memberCount` being
617
+ * `listOrgMembers(orgId).length`. Absent → the admin service uses that walk.
618
+ */
619
+ listAllOrgsWithMemberCount?(): Promise<Array<OrgSummary & {
620
+ role: string;
621
+ memberCount: number;
622
+ }>>;
611
623
  updateOrg(orgId: string, patch: {
612
624
  name?: string;
613
625
  logoUrl?: string | null;
@@ -121,6 +121,35 @@ function resolveAccountTable(Model) {
121
121
  }
122
122
  return undefined;
123
123
  }
124
+ /** Up to this many ids, filter with `WHERE IN`; above it, read every id instead. */
125
+ const ADMIN_ORDER_IN_LIMIT = 500;
126
+ /**
127
+ * Of `accountIds`, the ones that exist, in the order `listAccounts` pages them
128
+ * (`ORDER BY email ASC`, sorted by the database so collation matches). One query,
129
+ * plain rows (no model hydration), primary key only.
130
+ */
131
+ async function listAccountIdsInAdminOrder(Model, accountIds) {
132
+ if (accountIds.length === 0)
133
+ return [];
134
+ Model.boot?.();
135
+ const pk = Model.$columnsDefinitions?.get?.('id')?.columnName ?? 'id';
136
+ const readAll = async () => (await Model.query().select(pk).orderBy('email', 'asc').pojo());
137
+ let rows;
138
+ if (accountIds.length <= ADMIN_ORDER_IN_LIMIT) {
139
+ try {
140
+ rows = await Model.query().select(pk).whereIn(pk, accountIds).orderBy('email', 'asc').pojo();
141
+ }
142
+ catch {
143
+ // e.g. a member row whose account id doesn't fit the key type: the full read
144
+ // never binds member ids, exactly like the `listAccounts` walk.
145
+ rows = await readAll();
146
+ }
147
+ }
148
+ else {
149
+ rows = await readAll();
150
+ }
151
+ return rows.map((row) => String(row[pk]));
152
+ }
124
153
  export function lucidAccountStore(Model, options = {}) {
125
154
  const mfaIssuer = options.mfaIssuer ?? 'AuthKit';
126
155
  const recoveryCodeCount = options.recoveryCodeCount ?? 8;
@@ -242,6 +271,7 @@ export function lucidAccountStore(Model, options = {}) {
242
271
  const row = await Model.find(accountId);
243
272
  return row?.email ?? null;
244
273
  },
274
+ listAccountIdsInAdminOrder: (accountIds) => listAccountIdsInAdminOrder(Model, accountIds),
245
275
  }),
246
276
  }
247
277
  : {}),
@@ -9,5 +9,11 @@ export interface OrgStoreContext {
9
9
  InvitationModel: any;
10
10
  /** Mapa accountId → email, para validar aceitação de convite. */
11
11
  findAccountEmail: (accountId: string) => Promise<string | null>;
12
+ /**
13
+ * Of the given account ids, the ones whose account exists, in the admin
14
+ * listing order (`listAccounts`: email asc). When present, the store exposes
15
+ * `listAllOrgsWithMemberCount` (bounded queries for the admin org list).
16
+ */
17
+ listAccountIdsInAdminOrder?: (accountIds: string[]) => Promise<string[]>;
12
18
  }
13
19
  export declare function buildOrganizations(ctx: OrgStoreContext): OrganizationsCapability;
@@ -1,5 +1,15 @@
1
1
  import { createHash, randomBytes } from 'node:crypto';
2
2
  import { DateTime } from 'luxon';
3
+ /** Max ids per `WHERE IN` (stays well below every dialect's bind-parameter limit). */
4
+ const IN_CHUNK = 500;
5
+ /** `Model.findMany` in chunks of {@link IN_CHUNK} ids. */
6
+ async function findManyChunked(Model, ids) {
7
+ const rows = [];
8
+ for (let i = 0; i < ids.length; i += IN_CHUNK) {
9
+ rows.push(...(await Model.findMany(ids.slice(i, i + IN_CHUNK))));
10
+ }
11
+ return rows;
12
+ }
3
13
  function toOrgSummary(row) {
4
14
  return {
5
15
  id: row.id,
@@ -49,8 +59,62 @@ async function countOwners(MemberModel, orgId) {
49
59
  return Number(result[0]?.$extras?.total ?? 0);
50
60
  }
51
61
  export function buildOrganizations(ctx) {
52
- const { OrgModel, MemberModel, InvitationModel, findAccountEmail } = ctx;
62
+ const { OrgModel, MemberModel, InvitationModel, findAccountEmail, listAccountIdsInAdminOrder } = ctx;
53
63
  return {
64
+ ...(listAccountIdsInAdminOrder
65
+ ? {
66
+ /**
67
+ * Admin org list in 3 queries (all memberships, the member accounts in
68
+ * admin order, the orgs) instead of walking every account. Mirrors the
69
+ * fallback in `AdminOrgsService.listOrgs` exactly: accounts in
70
+ * `listAccounts` order (email asc), each account's memberships in the
71
+ * same (natural) order `listOrgsForAccount` reads them, first sighting of
72
+ * an org wins (its `role`), and `memberCount` counts every member row
73
+ * (like `listOrgMembers(orgId).length`).
74
+ */
75
+ async listAllOrgsWithMemberCount() {
76
+ const memberships = await MemberModel.query();
77
+ if (memberships.length === 0)
78
+ return [];
79
+ const byAccount = new Map();
80
+ const memberCount = new Map();
81
+ for (const m of memberships) {
82
+ const accountId = String(m.accountId);
83
+ const orgId = String(m.organizationId);
84
+ memberCount.set(orgId, (memberCount.get(orgId) ?? 0) + 1);
85
+ const list = byAccount.get(accountId);
86
+ if (list)
87
+ list.push(m);
88
+ else
89
+ byAccount.set(accountId, [m]);
90
+ }
91
+ const accountIds = await listAccountIdsInAdminOrder([...byAccount.keys()]);
92
+ const orgs = new Map();
93
+ for (const org of await findManyChunked(OrgModel, [...memberCount.keys()])) {
94
+ orgs.set(String(org.id), org);
95
+ }
96
+ const result = [];
97
+ const seen = new Set();
98
+ for (const accountId of accountIds) {
99
+ for (const m of byAccount.get(String(accountId)) ?? []) {
100
+ const orgId = String(m.organizationId);
101
+ if (seen.has(orgId))
102
+ continue;
103
+ const org = orgs.get(orgId);
104
+ if (!org)
105
+ continue;
106
+ seen.add(orgId);
107
+ result.push({
108
+ ...toOrgSummary(org),
109
+ role: m.role,
110
+ memberCount: memberCount.get(orgId) ?? 0,
111
+ });
112
+ }
113
+ }
114
+ return result;
115
+ },
116
+ }
117
+ : {}),
54
118
  async createOrg(input) {
55
119
  const { randomUUID } = await import('node:crypto');
56
120
  const org = await OrgModel.create({
@@ -23,8 +23,31 @@ import { type RedirectUriPolicy, type ResolvedRedirectUriPolicy, type ValidateRe
23
23
  export type { AuthAccount };
24
24
  export { adapters };
25
25
  export type AuthHostRenderer = (ctx: HttpContext, view: string, props: Record<string, unknown>) => unknown;
26
+ /**
27
+ * Quando o login social pede o 2º fator da conta:
28
+ * - `'enrolled'` (default): pede se a conta tem um fator cadastrado (TOTP,
29
+ * passkey ou método custom de `mfa`). Quem ativou o fator espera que ele
30
+ * valha em qualquer porta de entrada, inclusive "Continuar com Google".
31
+ * - `'required'`: exige sempre; conta sem fator vê a tela de "configure o MFA"
32
+ * (não há o que desafiar). Cuidado: tranca quem ainda não cadastrou fator.
33
+ * - `'never'`: confia só no provider (o comportamento antigo).
34
+ */
35
+ export type SocialSecondFactorPolicy = 'enrolled' | 'required' | 'never';
26
36
  export interface AuthSocialConfig {
27
37
  providers: string[];
38
+ /**
39
+ * 2º fator no login social. Aceita uma política fixa ou uma função por conta
40
+ * (ex.: `'required'` só para papéis administrativos). Default: `'enrolled'`.
41
+ *
42
+ * `roles` vem da MESMA fonte do claim de papéis: `resolveTokenRoles` quando o
43
+ * host o configura (ex.: `@adonis-agora/authz`), senão `account.globalRoles`.
44
+ * Só é resolvido quando a política é uma função.
45
+ */
46
+ secondFactor?: SocialSecondFactorPolicy | ((account: {
47
+ id: string;
48
+ email: string;
49
+ roles: string[];
50
+ }) => SocialSecondFactorPolicy | Promise<SocialSecondFactorPolicy>);
28
51
  }
29
52
  /**
30
53
  * Hooks de e-mail plugáveis (best-effort). Quando ausentes, o host-kit cai no
@@ -62,6 +85,12 @@ export interface MailHooks {
62
85
  * hosts existentes simplesmente ignoram este campo (back-compat).
63
86
  */
64
87
  channel?: 'code' | 'link';
88
+ /**
89
+ * `'signup'`: link de confirmação do cadastro passwordless (a conta é criada
90
+ * ao abri-lo). Ausente = login por magic link. O host pode trocar o texto do
91
+ * e-mail ("confirme seu cadastro"); ignorar o campo continua funcionando.
92
+ */
93
+ kind?: 'signup';
65
94
  }) => Promise<void>;
66
95
  /**
67
96
  * Envia o link de CONFIRMAÇÃO DE IDENTIDADE (sudo). Distinto de
@@ -488,10 +517,13 @@ export interface PasswordlessConfigInput {
488
517
  passkeyFirst?: boolean;
489
518
  /**
490
519
  * Liga o cadastro público passwordless: o signup pede só e-mail + nome (sem
491
- * senha), cria a conta com uma senha aleatória inutilizável e envia um magic
492
- * link que finaliza o login (o mesmo fluxo do login por magic link). Default:
493
- * false. Exige que o accountStore implemente {@link MagicLinkCapability}; sem
494
- * ela o cadastro passwordless fica indisponível e o fluxo de senha segue valendo.
520
+ * senha) e envia um link de confirmação (`onMagicLink` com `kind: 'signup'`).
521
+ * A conta só é criada quando o dono do e-mail abre o link: nasce com o e-mail
522
+ * verificado e uma senha aleatória inutilizável, e o login segue pelo magic
523
+ * link. E-mail que já tem conta recebe um magic link de login. Default: false.
524
+ * Exige que o accountStore implemente {@link MagicLinkCapability} e o serviço de
525
+ * encryption do app (APP_KEY), que assina o link; sem a capacidade o cadastro
526
+ * passwordless fica indisponível e o fluxo de senha segue valendo.
495
527
  */
496
528
  signup?: boolean;
497
529
  }
@@ -19,8 +19,19 @@ type RoleResolverConfig = {
19
19
  * account's stored `globalRoles`. Falls back to `account.globalRoles ?? []` when no hook is set, so
20
20
  * default behavior is unchanged.
21
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`).
22
+ * Gates that are not tied to an OIDC client (console, maintenance mode, PAT introspection) call the
23
+ * hook with an empty context; callers that know the client (opaque access-token introspection) pass
24
+ * its `clientId`, matching what the token was minted with. `activeOrg` is always `null` here.
24
25
  */
25
- export declare function resolveAccountRoles(cfg: RoleResolverConfig, account: AuthAccount): Promise<string[]>;
26
+ export declare function resolveAccountRoles(cfg: RoleResolverConfig, account: AuthAccount, context?: {
27
+ clientId?: string;
28
+ }): Promise<string[]>;
29
+ /**
30
+ * true quando os papéis da conta vêm de fora do authkit: o host configurou
31
+ * `resolveTokenRoles` (ex.: `@adonis-agora/authz` ou uma tabela própria). Nesse
32
+ * caso `globalRoles` não decide nada, então o console e a Admin API mostram os
33
+ * papéis do hook e recusam editá-los — gravar na coluna diria "salvo" sem mudar
34
+ * acesso nenhum. Quem edita é o próprio app, na fonte dele.
35
+ */
36
+ export declare function rolesManagedExternally(cfg: RoleResolverConfig): boolean;
26
37
  export {};
@@ -7,12 +7,23 @@
7
7
  * account's stored `globalRoles`. Falls back to `account.globalRoles ?? []` when no hook is set, so
8
8
  * default behavior is unchanged.
9
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`).
10
+ * Gates that are not tied to an OIDC client (console, maintenance mode, PAT introspection) call the
11
+ * hook with an empty context; callers that know the client (opaque access-token introspection) pass
12
+ * its `clientId`, matching what the token was minted with. `activeOrg` is always `null` here.
12
13
  */
13
- export async function resolveAccountRoles(cfg, account) {
14
+ export async function resolveAccountRoles(cfg, account, context = {}) {
14
15
  if (cfg.resolveTokenRoles) {
15
- return cfg.resolveTokenRoles(account, { clientId: undefined, activeOrg: null });
16
+ return cfg.resolveTokenRoles(account, { clientId: context.clientId, activeOrg: null });
16
17
  }
17
18
  return account.globalRoles ?? [];
18
19
  }
20
+ /**
21
+ * true quando os papéis da conta vêm de fora do authkit: o host configurou
22
+ * `resolveTokenRoles` (ex.: `@adonis-agora/authz` ou uma tabela própria). Nesse
23
+ * caso `globalRoles` não decide nada, então o console e a Admin API mostram os
24
+ * papéis do hook e recusam editá-los — gravar na coluna diria "salvo" sem mudar
25
+ * acesso nenhum. Quem edita é o próprio app, na fonte dele.
26
+ */
27
+ export function rolesManagedExternally(cfg) {
28
+ return typeof cfg.resolveTokenRoles === 'function';
29
+ }
@@ -55,6 +55,11 @@ export class AdminOrgsService {
55
55
  const store = this.cfg.accountStore;
56
56
  if (!supportsOrganizations(store))
57
57
  return { ok: false, reason: 'not_supported' };
58
+ // Fast path: stores that can answer in a bounded number of queries (the Lucid
59
+ // store does). Same list, same order as the walk below.
60
+ if (typeof store.listAllOrgsWithMemberCount === 'function') {
61
+ return store.listAllOrgsWithMemberCount();
62
+ }
58
63
  // O store não tem listAllOrgs — acumulamos via memberships de todas as contas.
59
64
  // Para admin, fazemos um listAccounts full e coletamos todas as orgs únicas.
60
65
  // Mais eficiente: usa a tabela de organizations diretamente pelo listOrgMembers
@@ -23,6 +23,7 @@ export default class ApiUsersController {
23
23
  avatarUrl: string | null;
24
24
  globalRoles: string[];
25
25
  disabled: boolean;
26
+ roles: string[];
26
27
  }>;
27
28
  /** POST /users — { email, name?, password? | invite?:true }. */
28
29
  store(ctx: HttpContext): Promise<void | {
@@ -32,6 +33,7 @@ export default class ApiUsersController {
32
33
  avatarUrl: string | null;
33
34
  globalRoles: string[];
34
35
  disabled: boolean;
36
+ roles: string[];
35
37
  invited: boolean;
36
38
  }>;
37
39
  /** PATCH /users/:id — { globalRoles?, name?, avatarUrl? }. */
@@ -42,6 +44,7 @@ export default class ApiUsersController {
42
44
  avatarUrl: string | null;
43
45
  globalRoles: string[];
44
46
  disabled: boolean;
47
+ roles: string[];
45
48
  }>;
46
49
  /** DELETE /users/:id — deleção completa (cascade) da conta. */
47
50
  destroy(ctx: HttpContext): Promise<void | {
@@ -1,11 +1,12 @@
1
1
  import '../augmentations.js';
2
2
  import { ADMIN_LIST_HTTP_DEFAULT_SIZE, parseListPage, parseListSize } from '../../pagination.js';
3
+ import { rolesManagedExternally } from '../account_roles.js';
3
4
  import { AdminSessionsService } from '../admin_sessions_service.js';
4
5
  import { adminUserCreateValidator, adminUserUpdateValidator } from '../admin_validators.js';
5
6
  import { resolveRuntimeSettings } from '../runtime_settings.js';
6
7
  import { enrichSessionsWithContext } from '../session_context.js';
7
8
  import { AdminUsersService } from './admin_users_service.js';
8
- import { apiError, grantDto, sessionDto, userDto } from './dto.js';
9
+ import { apiError, grantDto, rolesManagedExternallyError, sessionDto, userDtoWithRoles, } from './dto.js';
9
10
  /**
10
11
  * Lê a config + monta o actor `admin-api` para auditoria. O `actorId` recebe o
11
12
  * id NÃO-SENSÍVEL da API key que autenticou (anexado pelo `adminApiGuard` em
@@ -35,7 +36,7 @@ export default class ApiUsersController {
35
36
  const size = parseListSize(ctx.request.input('size'), ADMIN_LIST_HTTP_DEFAULT_SIZE);
36
37
  const result = await cfg.accountStore.listAccounts({ search, page, size });
37
38
  const users = new AdminUsersService(cfg);
38
- const data = await Promise.all(result.data.map(async (u) => userDto(u, await users.isDisabled(u.id))));
39
+ const data = await Promise.all(result.data.map(async (u) => userDtoWithRoles(cfg, u, await users.isDisabled(u.id))));
39
40
  return { meta: { page, size, total: result.total }, data };
40
41
  }
41
42
  /** GET /users/:id */
@@ -46,7 +47,7 @@ export default class ApiUsersController {
46
47
  if (!account)
47
48
  return ctx.response.notFound(apiError('not_found', 'Usuário não encontrado.'));
48
49
  const disabled = await new AdminUsersService(cfg).isDisabled(id);
49
- return userDto(account, disabled);
50
+ return userDtoWithRoles(cfg, account, disabled);
50
51
  }
51
52
  /** POST /users — { email, name?, password? | invite?:true }. */
52
53
  async store(ctx) {
@@ -66,7 +67,7 @@ export default class ApiUsersController {
66
67
  return ctx.response.conflict(apiError('email_taken', 'Já existe uma conta com este e-mail.'));
67
68
  }
68
69
  ctx.response.status(201);
69
- return { ...userDto(result.account), invited: result.invited };
70
+ return { ...(await userDtoWithRoles(cfg, result.account)), invited: result.invited };
70
71
  }
71
72
  /** PATCH /users/:id — { globalRoles?, name?, avatarUrl? }. */
72
73
  async update(ctx) {
@@ -77,6 +78,9 @@ export default class ApiUsersController {
77
78
  return ctx.response.notFound(apiError('not_found', 'Usuário não encontrado.'));
78
79
  const users = new AdminUsersService(cfg);
79
80
  const { globalRoles: roles, name, avatarUrl, } = await ctx.request.validateUsing(adminUserUpdateValidator);
81
+ if (Array.isArray(roles) && rolesManagedExternally(cfg)) {
82
+ return ctx.response.conflict(rolesManagedExternallyError());
83
+ }
80
84
  if (Array.isArray(roles)) {
81
85
  const normalized = Array.from(new Set(roles.map((r) => r.trim()).filter(Boolean)));
82
86
  // Proteções de escalonamento/lockout: último admin + auto-rebaixamento.
@@ -107,7 +111,7 @@ export default class ApiUsersController {
107
111
  await users.updateProfile(id, patch);
108
112
  }
109
113
  const updated = await cfg.accountStore.findById(id);
110
- return userDto(updated, await users.isDisabled(id));
114
+ return userDtoWithRoles(cfg, updated, await users.isDisabled(id));
111
115
  }
112
116
  /** DELETE /users/:id — deleção completa (cascade) da conta. */
113
117
  async destroy(ctx) {
@@ -1,5 +1,6 @@
1
1
  import type { AuthAccount, OrgInvitation, OrgMember, OrgSummary } from '../../accounts/account_store.js';
2
2
  import type { StoredAuditEvent } from '../../audit/audit_sink.js';
3
+ import { resolveAccountRoles } from '../account_roles.js';
3
4
  import type { AdminClient, CreatedClient } from '../admin_clients_service.js';
4
5
  import type { AdminGrant, AdminSession } from '../admin_sessions_service.js';
5
6
  import type { OrgDetail } from './admin_orgs_service.js';
@@ -12,6 +13,21 @@ export declare function userDto(account: AuthAccount, disabled?: boolean): {
12
13
  globalRoles: string[];
13
14
  disabled: boolean;
14
15
  };
16
+ /**
17
+ * {@link userDto} + `roles`: os papéis efetivos da conta, pela mesma fonte do
18
+ * claim (`resolveAccountRoles`). Sem `resolveTokenRoles` é igual a
19
+ * `globalRoles`; com ele, é o que o app diz (ex.: authz) e `globalRoles` segue
20
+ * sendo só a coluna guardada.
21
+ */
22
+ export declare function userDtoWithRoles(cfg: Parameters<typeof resolveAccountRoles>[0], account: AuthAccount, disabled?: boolean): Promise<{
23
+ id: string;
24
+ email: string;
25
+ name: string | null;
26
+ avatarUrl: string | null;
27
+ globalRoles: string[];
28
+ disabled: boolean;
29
+ roles: string[];
30
+ }>;
15
31
  export declare function clientDto(client: AdminClient): {
16
32
  clientId: string;
17
33
  confidential: boolean;
@@ -129,3 +145,11 @@ export declare function settingDto(row: SettingRow): {
129
145
  locked: boolean;
130
146
  lockedBy?: "config" | undefined;
131
147
  };
148
+ /** 409 de quem tenta editar papéis geridos fora do authkit (`resolveTokenRoles`). */
149
+ export declare function rolesManagedExternallyError(): {
150
+ error: {
151
+ code: string;
152
+ message: string;
153
+ details?: Record<string, unknown> | undefined;
154
+ };
155
+ };
@@ -1,3 +1,4 @@
1
+ import { resolveAccountRoles } from '../account_roles.js';
1
2
  /** Projeta uma conta para a forma JSON (camelCase) da Admin REST API. */
2
3
  export function userDto(account, disabled = false) {
3
4
  return {
@@ -9,6 +10,15 @@ export function userDto(account, disabled = false) {
9
10
  disabled,
10
11
  };
11
12
  }
13
+ /**
14
+ * {@link userDto} + `roles`: os papéis efetivos da conta, pela mesma fonte do
15
+ * claim (`resolveAccountRoles`). Sem `resolveTokenRoles` é igual a
16
+ * `globalRoles`; com ele, é o que o app diz (ex.: authz) e `globalRoles` segue
17
+ * sendo só a coluna guardada.
18
+ */
19
+ export async function userDtoWithRoles(cfg, account, disabled = false) {
20
+ return { ...userDto(account, disabled), roles: await resolveAccountRoles(cfg, account) };
21
+ }
12
22
  export function clientDto(client) {
13
23
  return {
14
24
  clientId: client.clientId,
@@ -121,3 +131,7 @@ export function settingDto(row) {
121
131
  ...(locked ? { lockedBy: 'config' } : {}),
122
132
  };
123
133
  }
134
+ /** 409 de quem tenta editar papéis geridos fora do authkit (`resolveTokenRoles`). */
135
+ export function rolesManagedExternallyError() {
136
+ return apiError('roles_managed_externally', 'Os papéis desta instalação vêm do app (resolveTokenRoles) e só podem ser alterados lá.');
137
+ }
@@ -1,3 +1,4 @@
1
+ import { resolveAccountRoles } from '../account_roles.js';
1
2
  /**
2
3
  * Introspecção genérica de token usada pela Admin REST API (`POST /tokens/verify`).
3
4
  * Roteia por prefixo: tokens `pat_...` vão pelo {@link PatStore} (mesma rota do
@@ -36,7 +37,7 @@ export class TokenVerifyService {
36
37
  sub: account.id,
37
38
  email: account.email,
38
39
  name: account.name ?? null,
39
- roles: account.globalRoles ?? [],
40
+ roles: await resolveAccountRoles(this.cfg, account),
40
41
  scopes: meta.scopes,
41
42
  audience: meta.audience,
42
43
  exp: meta.exp,
@@ -64,7 +65,11 @@ export class TokenVerifyService {
64
65
  sub,
65
66
  email: account?.email ?? null,
66
67
  name: account?.name ?? null,
67
- roles: account?.globalRoles ?? [],
68
+ roles: account
69
+ ? await resolveAccountRoles(this.cfg, account, {
70
+ clientId: at.clientId ?? undefined,
71
+ })
72
+ : [],
68
73
  scopes,
69
74
  audience: at.aud ?? null,
70
75
  clientId: at.clientId ?? null,
@@ -1,7 +1,7 @@
1
1
  import { readFile } from 'node:fs/promises';
2
2
  import { extname } from 'node:path';
3
3
  import { accountPath } from '../account_paths.js';
4
- import { resolveAccountRoles } from '../account_roles.js';
4
+ import { resolveAccountRoles, rolesManagedExternally } from '../account_roles.js';
5
5
  import { getAdminPrefix } from '../admin_prefix.js';
6
6
  // ─── Shell HTML cache ─────────────────────────────────────────────────────────
7
7
  /**
@@ -141,6 +141,8 @@ export default class AdminShellController {
141
141
  features: {
142
142
  // A página de personal agents só aparece com `personalAgents` no config.
143
143
  personalAgents: !!cfg.personalAgents,
144
+ // Papéis vindos de fora (resolveTokenRoles): a tela só mostra, não edita.
145
+ rolesManagedExternally: rolesManagedExternally(cfg),
144
146
  },
145
147
  };
146
148
  const { html: rawHtml } = await loadShell();
@@ -35,6 +35,7 @@ export default class ConsoleUsersController {
35
35
  avatarUrl: string | null;
36
36
  globalRoles: string[];
37
37
  disabled: boolean;
38
+ roles: string[];
38
39
  sessionsSupported: boolean;
39
40
  sessions: {
40
41
  id: string;
@@ -70,6 +71,7 @@ export default class ConsoleUsersController {
70
71
  avatarUrl: string | null;
71
72
  globalRoles: string[];
72
73
  disabled: boolean;
74
+ roles: string[];
73
75
  invited: boolean;
74
76
  }>;
75
77
  /** PATCH {prefix}/api/users/:id/roles */
@@ -80,6 +82,7 @@ export default class ConsoleUsersController {
80
82
  avatarUrl: string | null;
81
83
  globalRoles: string[];
82
84
  disabled: boolean;
85
+ roles: string[];
83
86
  }>;
84
87
  /** POST {prefix}/api/users/:id/disable */
85
88
  disable(ctx: HttpContext): Promise<void | {}>;
@@ -91,6 +94,7 @@ export default class ConsoleUsersController {
91
94
  avatarUrl: string | null;
92
95
  globalRoles: string[];
93
96
  disabled: boolean;
97
+ roles: string[];
94
98
  }>;
95
99
  /** POST {prefix}/api/users/:id/reset-password */
96
100
  resetPassword(ctx: HttpContext): Promise<void | {}>;
@@ -1,9 +1,10 @@
1
1
  import '../augmentations.js';
2
2
  import { supportsAccountDeletion, supportsAccountStatus } from '../../accounts/account_store.js';
3
3
  import { ADMIN_LIST_HTTP_DEFAULT_SIZE, parseListPage, parseListSize } from '../../pagination.js';
4
+ import { rolesManagedExternally } from '../account_roles.js';
4
5
  import { ACCOUNT_SESSION_KEY } from '../account_session_key.js';
5
6
  import { AdminUsersService } from '../admin_api/admin_users_service.js';
6
- import { apiError, grantDto, sessionDto, userDto } from '../admin_api/dto.js';
7
+ import { apiError, grantDto, rolesManagedExternallyError, sessionDto, userDtoWithRoles, } from '../admin_api/dto.js';
7
8
  import { AdminSessionsService } from '../admin_sessions_service.js';
8
9
  import { adminUserCreateValidator, adminUserRolesValidator } from '../admin_validators.js';
9
10
  import { resolveRuntimeSettings } from '../runtime_settings.js';
@@ -61,7 +62,7 @@ export default class ConsoleUsersController {
61
62
  size,
62
63
  });
63
64
  const users = new AdminUsersService(cfg);
64
- const data = await Promise.all(result.data.map(async (u) => userDto(u, await users.isDisabled(u.id))));
65
+ const data = await Promise.all(result.data.map(async (u) => userDtoWithRoles(cfg, u, await users.isDisabled(u.id))));
65
66
  return { meta: { page, size, total: result.total }, data };
66
67
  }
67
68
  /** GET {prefix}/api/users/:id */
@@ -92,7 +93,7 @@ export default class ConsoleUsersController {
92
93
  // fail-safe
93
94
  }
94
95
  return {
95
- ...userDto(account, disabled),
96
+ ...(await userDtoWithRoles(cfg, account, disabled)),
96
97
  sessionsSupported: sessions.canList,
97
98
  sessions: enriched.map(sessionDto),
98
99
  grants: grants.map(grantDto),
@@ -122,7 +123,7 @@ export default class ConsoleUsersController {
122
123
  return ctx.response.conflict(apiError('email_taken', 'Já existe uma conta com este e-mail.'));
123
124
  }
124
125
  ctx.response.status(201);
125
- return { ...userDto(result.account), invited: result.invited };
126
+ return { ...(await userDtoWithRoles(cfg, result.account)), invited: result.invited };
126
127
  }
127
128
  /** PATCH {prefix}/api/users/:id/roles */
128
129
  async updateRoles(ctx) {
@@ -132,6 +133,11 @@ export default class ConsoleUsersController {
132
133
  const account = await cfg.accountStore.findById(id);
133
134
  if (!account)
134
135
  return ctx.response.notFound(apiError('not_found', 'Usuário não encontrado.'));
136
+ // Papéis de fora do authkit (resolveTokenRoles): a coluna não decide acesso,
137
+ // então gravar nela seria um "salvo" mentiroso. O app edita na fonte dele.
138
+ if (rolesManagedExternally(cfg)) {
139
+ return ctx.response.status(409).send(rolesManagedExternallyError());
140
+ }
135
141
  const actorId = ctx.session?.get(ACCOUNT_SESSION_KEY) ?? null;
136
142
  const { roles: rolesInput } = await ctx.request.validateUsing(adminUserRolesValidator);
137
143
  const roles = Array.from(new Set((rolesInput ?? []).map((r) => r.trim()).filter(Boolean)));
@@ -156,7 +162,7 @@ export default class ConsoleUsersController {
156
162
  }
157
163
  const updated = await cfg.accountStore.findById(id);
158
164
  const disabled = await users.isDisabled(id);
159
- return userDto(updated, disabled);
165
+ return userDtoWithRoles(cfg, updated, disabled);
160
166
  }
161
167
  /** POST {prefix}/api/users/:id/disable */
162
168
  async disable(ctx) {
@@ -193,7 +199,7 @@ export default class ConsoleUsersController {
193
199
  }
194
200
  const updated = await cfg.accountStore.findById(id);
195
201
  const disabled = await users.isDisabled(id);
196
- return userDto(updated, disabled);
202
+ return userDtoWithRoles(cfg, updated, disabled);
197
203
  }
198
204
  /** POST {prefix}/api/users/:id/reset-password */
199
205
  async resetPassword(ctx) {
@@ -136,6 +136,14 @@ export default class AuthInteractionController {
136
136
  * Consome o magic link. Em sucesso finaliza o login (amr `['email']`). Token
137
137
  * inválido/expirado volta ao início do login.
138
138
  */
139
+ /**
140
+ * GET /auth/interaction/:uid/social/continue — segunda metade do login social
141
+ * quando a conta deve o 2º fator (ver `AuthSocialController#callback`). Lê o
142
+ * login pendente da sessão UMA vez, confere que é desta interaction e passa
143
+ * pelo mesmo `secondFactorGate` dos outros fluxos, com `fed` como fator
144
+ * primário no `amr`.
145
+ */
146
+ socialContinue(ctx: HttpContext): Promise<any>;
139
147
  magicLinkConsume(ctx: HttpContext): Promise<any>;
140
148
  /**
141
149
  * POST /auth/interaction/:uid/otp-verify