@authyon/auth 0.2.0-beta.0 → 0.2.0-beta.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MutualPay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,22 +2,24 @@
2
2
 
3
3
  SDK JS/TS para o [Authyon](https://authyon.com) — autenticação, sessões, multi-tenant e 2FA, com armazenamento e refresh de tokens transparentes.
4
4
 
5
- Cobre todos os endpoints públicos documentados em [authyon.com/docs](https://authyon.com/docs), usando a **publishable key** (`pk_...`) — segura para expor no navegador.
5
+ Cobre os endpoints públicos do Authyon usando a **publishable key** (`pk_...`) — segura para identificar o ambiente no navegador, sem conceder privilégios administrativos.
6
6
 
7
- Para gestão de organização/membros (secret key) e verificação de token no backend, veja [`@authyon/server`](../server).
7
+ Para gestão de organização/membros (secret key) e verificação de token no backend, use `@authyon/server`.
8
8
 
9
9
  ## Instalação
10
10
 
11
11
  ```bash
12
- npm install @authyon/auth
12
+ npm install --save-exact @authyon/auth@0.2.0-beta.1
13
13
  ```
14
14
 
15
+ Durante a beta, `npm install @authyon/auth@beta` acompanha o prerelease mais recente. Fixar a versão exata é recomendado para builds reproduzíveis.
16
+
15
17
  ## Uso rápido
16
18
 
17
19
  ```ts
18
20
  import { createClient } from "@authyon/auth";
19
21
 
20
- const authyon = createClient({ envKey: "pk_live_..." });
22
+ const authyon = createClient({ envKey: "pk_live_..." }); // tokens somente em memória por padrão
21
23
 
22
24
  // Login (com suporte a 2FA)
23
25
  const result = await authyon.login({
@@ -39,13 +41,47 @@ if (result.twoFactorRequired) {
39
41
  const user = await authyon.user.me();
40
42
  ```
41
43
 
44
+ ## Next.js e React
45
+
46
+ Use o entrypoint dedicado `@authyon/auth/react`:
47
+
48
+ ```tsx
49
+ <AuthyonProvider client={authyon}>
50
+ <SessionGuard loadingFallback={<Loading />}>
51
+ <PermissionGuard action="read" subject="reports">
52
+ <Reports />
53
+ </PermissionGuard>
54
+ </SessionGuard>
55
+ </AuthyonProvider>
56
+ ```
57
+
58
+ O provider faz refresh automático, valida a sessão por `GET /auth/me`, atualiza usuário e permissions, revalida quando a aba volta ao foco e não libera guards enquanto a validação inicial estiver pendente. Use `useAuthyon()`, `useAuthyonAbility()` e `useCan()` para fluxos programáticos.
59
+
60
+ Para configuração progressiva, use o builder:
61
+
62
+ ```ts
63
+ const authyon = new AuthyonClientBuilder("pk_live_...")
64
+ .withStorage(createMemoryStorage())
65
+ .withTimeout(10_000)
66
+ .build();
67
+ ```
68
+
42
69
  ## Sessão e tokens
43
70
 
44
- - Tokens persistem em `localStorage` por padrão (`memoryStorage()` ou um `TokenStorage` próprio via opção `storage`).
71
+ - Tokens ficam somente em memória por padrão, reduzindo o impacto de XSS e sendo removidos ao recarregar a página.
72
+ - Persistência é opt-in com `storage: createLocalStorage()`. Isso melhora conveniência, mas qualquer XSS na aplicação poderá ler os tokens; criptografar o valor não elimina esse risco porque o JavaScript também precisa acessar a chave.
45
73
  - `getAccessToken()` renova o token automaticamente antes de expirar (refresh token é single-use e rotacionado, com single-flight para evitar corridas).
46
74
  - Chamadas autenticadas que retornam 401 fazem um refresh e uma retentativa automática.
47
75
 
48
76
  ```ts
77
+ import { createClient, createLocalStorage } from "@authyon/auth";
78
+
79
+ // Use somente quando a persistência for um requisito aceito conscientemente.
80
+ const persistentClient = createClient({
81
+ envKey: "pk_live_...",
82
+ storage: createLocalStorage(),
83
+ });
84
+
49
85
  const token = await authyon.getAccessToken(); // sempre válido, ou null se deslogado
50
86
 
51
87
  const unsubscribe = authyon.onAuthStateChange((event) => {
@@ -54,19 +90,109 @@ const unsubscribe = authyon.onAuthStateChange((event) => {
54
90
  });
55
91
  ```
56
92
 
93
+ Para aplicações de maior risco, prefira um BFF que mantenha o refresh token em cookie `HttpOnly`, `Secure` e `SameSite`, com proteção CSRF. O SDK também exige HTTPS para APIs remotas e aplica timeout de 15 segundos por padrão (`timeoutMs: 0` desabilita).
94
+
95
+ ## HTTP Adapter
96
+
97
+ Use `httpAdapter` para integrar mocks, tracing ou outra biblioteca HTTP. O mesmo contrato está disponível em `@authyon/auth` e `@authyon/server`:
98
+
99
+ ```ts
100
+ import { createClient, type HttpAdapter } from "@authyon/auth";
101
+
102
+ const httpAdapter: HttpAdapter = {
103
+ async request(request) {
104
+ // tracing, métricas ou adaptação para sua stack HTTP
105
+ return fetch(request.url, {
106
+ method: request.method,
107
+ headers: request.headers,
108
+ body: request.body,
109
+ signal: request.signal,
110
+ });
111
+ },
112
+ };
113
+
114
+ const authyon = createClient({ envKey: "pk_live_...", httpAdapter });
115
+ ```
116
+
117
+ `FetchHttpAdapter` é a implementação padrão e também pode encapsular um `fetch` customizado. Não configure `httpAdapter` e `fetch` ao mesmo tempo.
118
+
119
+ O logger HTTP é desabilitado por padrão. Passe uma configuração mutável para observar e manipular eventos de request, response e erro; altere `enabled` a qualquer momento sem recriar o client:
120
+
121
+ ```ts
122
+ import { createClient, type HttpLoggerOptions } from "@authyon/auth";
123
+
124
+ const httpLogger: HttpLoggerOptions = {
125
+ enabled: true,
126
+ logger(event) {
127
+ observability.track(event.type, event);
128
+ },
129
+ };
130
+
131
+ const authyon = createClient({ envKey: "pk_live_...", httpLogger });
132
+
133
+ httpLogger.enabled = false; // desabilita
134
+ httpLogger.enabled = true; // habilita novamente
135
+ ```
136
+
137
+ Sem um `logger` customizado, os eventos usam `console.debug`. Headers, bodies e valores de query string são removidos dos eventos para evitar vazamento de tokens, senhas e dados pessoais. Falhas do logger são isoladas e não interrompem a autenticação. Para decorar um adapter manualmente, use `LoggingHttpAdapter`.
138
+
57
139
  ## API
58
140
 
141
+ ## Autorização baseada em permissões
142
+
143
+ Crie uma ability diretamente do usuário da sessão. Permissões Authyon seguem `subject:action`, por exemplo `tickets:read` e `documents:update`:
144
+
145
+ ```ts
146
+ import { createAuthyonAbility } from "@authyon/auth";
147
+
148
+ const ability = createAuthyonAbility(session.user, {
149
+ rules: [
150
+ {
151
+ action: "update",
152
+ subject: "documents",
153
+ inverted: true,
154
+ conditions: { locked: true },
155
+ reason: "Documento bloqueado",
156
+ },
157
+ ],
158
+ });
159
+
160
+ ability.can("read", "tickets");
161
+ ability.can("update", { __type: "documents", locked: false });
162
+ ability.cannot("update", { __type: "documents", locked: true });
163
+ ```
164
+
165
+ Também estão disponíveis `AuthyonAbilityBuilder`, regras por role, campos, condições com operadores (`$eq`, `$ne`, `$in`, `$nin`, `$gt`, `$gte`, `$lt`, `$lte`, `$exists`, `$and`, `$or`), `rulesFor()`, `update()` e o evento `updated`. A autorização é local e nega por padrão; o backend continua responsável por validar toda ação sensível.
166
+
167
+ ### Next.js Client Side
168
+
169
+ `@authyon/auth` e o motor de abilities são isomórficos e podem ser usados em Client Components:
170
+
171
+ ```tsx
172
+ "use client";
173
+
174
+ import { createAuthyonAbility } from "@authyon/auth";
175
+
176
+ export function EditButton({ user, document }) {
177
+ const ability = createAuthyonAbility(user);
178
+ if (ability.cannot("update", { __type: "documents", ...document })) return null;
179
+ return <button>Editar</button>;
180
+ }
181
+ ```
182
+
183
+ Essa verificação controla a interface, não a segurança do backend. Nunca importe `@authyon/server` em Client Components nem envie `clientSecret` ao navegador.
184
+
59
185
  Métodos de sessão/auth ficam soltos no client; os que giram em torno de um recurso específico ficam agrupados em namespaces (`user`, `organization`, `twoFactor`, `webauthn`, `sso`).
60
186
 
61
- | Método | Endpoint |
62
- | ------------------------------------------------------------------------ | ----------------------- |
63
- | `register({ email, username, password })` | `POST /auth/register` |
64
- | `login({ email \| username, password, organizationSlug? })` | `POST /auth/login` |
65
- | `verifyTwoFactor({ challengeToken, method, code?, webAuthnAssertion? })` | `POST /auth/2fa/verify` |
66
- | `refresh()` | `POST /auth/refresh` |
67
- | `logout({ everywhere? })` | `POST /auth/logout` |
68
- | `introspect(token?)` | `POST /auth/introspect` |
69
- | `validate(token?)` | `POST /auth/validate` |
187
+ | Método | Endpoint |
188
+ | ------------------------------------------------------------------------ | --------------------------------- |
189
+ | `register({ email, username, password })` | `POST /auth/register` |
190
+ | `login({ email \| username, password, organizationSlug? })` | `POST /auth/login` |
191
+ | `verifyTwoFactor({ challengeToken, method, code?, webAuthnAssertion? })` | `POST /auth/2fa/verify` |
192
+ | `refresh()` | `POST /auth/refresh` |
193
+ | `logout({ everywhere? })` | `POST /auth/logout` |
194
+ | `introspect(token?)` | Deprecated: use `@authyon/server` |
195
+ | `validate(token?)` | Deprecated: use `@authyon/server` |
70
196
 
71
197
  ### `authyon.user`
72
198
 
@@ -137,6 +263,23 @@ Métodos de sessão/auth ficam soltos no client; os que giram em torno de um rec
137
263
 
138
264
  Toda resposta não-2xx vira um `AuthyonError` (problem+json). Compare pelo `code` legível por máquina, nunca pelo `title`:
139
265
 
266
+ Falhas de transporte usam `request.network_error` ou `request.timeout`. Quando presentes, `requestId` e `retryAfter` ajudam suporte e tratamento de rate limit sem expor o corpo da requisição.
267
+
268
+ `interpret()` converte códigos e status em uma decisão estável para a interface:
269
+
270
+ ```ts
271
+ try {
272
+ await authyon.login({ email, password });
273
+ } catch (cause) {
274
+ if (!(cause instanceof AuthyonError)) throw cause;
275
+ const { category, action, retryable, retryAfter } = cause.interpret();
276
+ // category: authentication | validation | rate_limit | network | ...
277
+ // action: reauthenticate | fix_input | retry | ...
278
+ }
279
+ ```
280
+
281
+ Veja o catálogo completo em `ERRORS.md` no repositório.
282
+
140
283
  ```ts
141
284
  import { AuthyonError, ErrorCodes } from "@authyon/auth";
142
285