@adonis-agora/authkit-client 0.17.3 → 0.17.4

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,25 @@
1
+ import type { AuthkitClientManager } from './providers/authkit_client_provider.js';
2
+ /**
3
+ * SINGLE SOURCE of this package's container bindings.
4
+ *
5
+ * Deliberately separate from `types.ts`. `types.ts` augments
6
+ * `HttpContext.auth`, and that augmentation is the HOST'S to choose: an app that
7
+ * types its own `auth: Authenticator<AppUser>` cannot also receive the library's
8
+ * non-parameterised `auth: Authenticator` without the two merging into a
9
+ * conflict — one that `skipLibCheck` (on in every AdonisJS app) hides, leaving
10
+ * whichever declaration wins by include order.
11
+ *
12
+ * The container binding has no such problem: it names a key only this package
13
+ * owns. So it lives here, where the provider and the middleware can pull it in
14
+ * without dragging `HttpContext.auth` into the consumer's program.
15
+ *
16
+ * If you add an augmentation here, ask first whether a host could legitimately
17
+ * want to declare it differently. If yes, it belongs in `types.ts`, behind the
18
+ * host's explicit opt-in.
19
+ */
20
+ declare module '@adonisjs/core/types' {
21
+ interface ContainerBindings {
22
+ 'authkit.client': AuthkitClientManager;
23
+ }
24
+ }
25
+ export type { AuthkitClientManager };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
+ import '../container_bindings.js';
1
2
  import type { HttpContext } from '@adonisjs/core/http';
2
3
  import type { ApplicationService } from '@adonisjs/core/types';
3
4
  import { Authenticator } from '../src/authenticator.js';
@@ -107,11 +108,6 @@ export declare class AuthkitClientManager {
107
108
  startSession(ctx: HttpContext, tokenSet: TokenSet): void;
108
109
  createAuthenticator(ctx: HttpContext): Promise<Authenticator>;
109
110
  }
110
- declare module '@adonisjs/core/types' {
111
- interface ContainerBindings {
112
- 'authkit.client': AuthkitClientManager;
113
- }
114
- }
115
111
  export default class AuthkitClientProvider {
116
112
  protected app: ApplicationService;
117
113
  constructor(app: ApplicationService);
@@ -1,3 +1,6 @@
1
+ // Single source of this package's container bindings (see container_bindings.ts).
2
+ // A bare side-effect import so the declaration survives into the emitted `.d.ts`.
3
+ import '../container_bindings.js';
1
4
  import { randomUUID } from 'node:crypto';
2
5
  import { configProvider } from '@adonisjs/core';
3
6
  import { RuntimeException } from '@adonisjs/core/exceptions';
@@ -0,0 +1,6 @@
1
+ import type { HttpContext } from '@adonisjs/core/http';
2
+ import type { Authenticator } from '../authenticator.js';
3
+ /** Read the authenticator the middleware placed on the context. */
4
+ export declare function ctxAuth(ctx: HttpContext): Authenticator;
5
+ /** Place the authenticator on the context. */
6
+ export declare function setCtxAuth(ctx: HttpContext, authenticator: Authenticator): void;
@@ -0,0 +1,8 @@
1
+ /** Read the authenticator the middleware placed on the context. */
2
+ export function ctxAuth(ctx) {
3
+ return ctx.auth;
4
+ }
5
+ /** Place the authenticator on the context. */
6
+ export function setCtxAuth(ctx, authenticator) {
7
+ ctx.auth = authenticator;
8
+ }
@@ -1,10 +1,11 @@
1
+ import { ctxAuth } from '../host/ctx_auth.js';
1
2
  /**
2
3
  * Middleware de autenticação do AuthKit: exige sessão válida. Autorização por role saiu daqui — use
3
4
  * `@adonis-agora/authz` (ou Bouncer) na rota. AuthKit só autentica.
4
5
  */
5
6
  export default class AuthkitAuthMiddleware {
6
7
  async handle(ctx, next, options = {}) {
7
- const ok = await ctx.auth.check();
8
+ const ok = await ctxAuth(ctx).check();
8
9
  if (!ok)
9
10
  return ctx.response.redirect(options.redirectTo ?? '/auth/login');
10
11
  return next();
@@ -1,4 +1,4 @@
1
- import '../../types.js';
1
+ import '../../container_bindings.js';
2
2
  import type { HttpContext } from '@adonisjs/core/http';
3
3
  import type { NextFn } from '@adonisjs/core/types/http';
4
4
  export default class AuthkitMiddleware {
@@ -1,22 +1,24 @@
1
- // Side-effect import: pulls the `HttpContext.auth` module augmentation (declared
2
- // in the package root `types.ts`) into this module's declaration graph, so an app
3
- // that registers this middleware gets `ctx.auth` typed as AuthKit's `Authenticator`.
4
- // It lives here — and not in `index.ts` — because this middleware is what actually
5
- // assigns `ctx.auth`. Apps that instead hand `ctx.auth` to `@adonisjs/auth` via
6
- // `authkitClientGuard` never register this middleware, so the two owners of
7
- // `ctx.auth` can never collide. Same pattern as `@adonisjs/auth`, which augments
8
- // `HttpContext` from its own `initialize_auth_middleware`.
9
- // It must be a bare side-effect import, not `import type {} from`: the type-only form
10
- // is erased from the emitted `.d.ts` as well, which would drop the augmentation again.
11
- // The runtime cost is nil — `build/types.js` is this package's own file and compiles
12
- // down to `export {}`.
13
- import '../../types.js';
1
+ // Side-effect import: carries this package's `ContainerBindings` declaration into
2
+ // the emitted `.d.ts`, so `containerResolver.make('authkit.client')` is typed in a
3
+ // consumer's program. It must be a bare side-effect import — the
4
+ // `import type {} from` form is erased from the declaration output.
5
+ //
6
+ // It points at `container_bindings.js`, NOT at the root `types.js`. `types.js`
7
+ // also augments `HttpContext.auth`, and importing it here pushed that
8
+ // augmentation into every app that registers this middleware, where it collided
9
+ // with the app's own `auth: Authenticator<AppUser>`. `skipLibCheck` hid the
10
+ // conflict and the library's non-parameterised declaration won, collapsing
11
+ // `auth.getUser()` to `unknown` across the consumer's codebase. Typing `ctx.auth`
12
+ // is the host's decision, taken by importing
13
+ // `@adonis-agora/authkit-client/types`; it is not this middleware's to impose.
14
+ import '../../container_bindings.js';
15
+ import { setCtxAuth } from '../host/ctx_auth.js';
14
16
  export default class AuthkitMiddleware {
15
17
  async handle(ctx, next) {
16
18
  const manager = await ctx.containerResolver.make('authkit.client');
17
19
  // Renova proativamente o TokenSet (rotação de refresh token) antes de resolver.
18
20
  await manager.maybeRefresh(ctx);
19
- ctx.auth = await manager.createAuthenticator(ctx);
21
+ setCtxAuth(ctx, await manager.createAuthenticator(ctx));
20
22
  return next();
21
23
  }
22
24
  }
@@ -1,3 +1,4 @@
1
+ import { ctxAuth } from '../host/ctx_auth.js';
1
2
  /**
2
3
  * Resolve a identidade da sessão (popula `ctx.auth`) SEM exigir login — útil em
3
4
  * rotas públicas que mostram estado logado/deslogado. Nunca redireciona.
@@ -8,7 +9,7 @@
8
9
  */
9
10
  export default class AuthkitSilentAuthMiddleware {
10
11
  async handle(ctx, next) {
11
- await ctx.auth.check();
12
+ await ctxAuth(ctx).check();
12
13
  return next();
13
14
  }
14
15
  }
package/build/types.d.ts CHANGED
@@ -1,13 +1,43 @@
1
+ import './container_bindings.js';
1
2
  import type { AuthkitClientManager } from './providers/authkit_client_provider.js';
2
3
  import type { Authenticator } from './src/authenticator.js';
4
+ /**
5
+ * OPT-IN typing for `ctx.auth`.
6
+ *
7
+ * Reference this module from your app when you want `ctx.auth` typed as
8
+ * AuthKit's `Authenticator` and you are NOT declaring the property yourself:
9
+ *
10
+ * ```ts
11
+ * // any file in your app, e.g. app/types.ts
12
+ * import '@adonis-agora/authkit-client/types'
13
+ * ```
14
+ *
15
+ * NOTHING in this package imports this module on your behalf, and that is the
16
+ * point. `Authenticator` is generic (`Authenticator<TUser>`), and most apps
17
+ * declare their own augmentation so `auth.getUser()` returns their user model:
18
+ *
19
+ * ```ts
20
+ * declare module '@adonisjs/core/http' {
21
+ * interface HttpContext {
22
+ * auth: Authenticator<AppUser>
23
+ * }
24
+ * }
25
+ * ```
26
+ *
27
+ * Two declarations of the same property with different types do not merge —
28
+ * they conflict, and because `skipLibCheck` is on in every AdonisJS app the
29
+ * conflict is never reported: one simply wins by include order. When the
30
+ * library's won, `auth.getUser()` collapsed to `unknown` across the whole app.
31
+ * So the library ships the declaration and lets you decide whether to load it,
32
+ * rather than forcing it through a middleware import.
33
+ *
34
+ * The container bindings come along with it (see `container_bindings.ts`); they
35
+ * are also available on their own, which is what the provider and the middleware
36
+ * use.
37
+ */
3
38
  declare module '@adonisjs/core/http' {
4
39
  interface HttpContext {
5
40
  auth: Authenticator;
6
41
  }
7
42
  }
8
- declare module '@adonisjs/core/types' {
9
- interface ContainerBindings {
10
- 'authkit.client': AuthkitClientManager;
11
- }
12
- }
13
43
  export type { Authenticator, AuthkitClientManager };
package/build/types.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ import './container_bindings.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonis-agora/authkit-client",
3
- "version": "0.17.3",
3
+ "version": "0.17.4",
4
4
  "description": "AdonisJS OIDC/OAuth2 relying-party (client) toolkit: session-based authentication against an OpenID Connect identity provider, with JWT/PAT user resolvers and OpenTelemetry metrics.",
5
5
  "license": "MIT",
6
6
  "author": "dudousxd",
@@ -36,6 +36,7 @@
36
36
  "exports": {
37
37
  ".": "./build/index.js",
38
38
  "./types": "./build/types.js",
39
+ "./container_bindings": "./build/container_bindings.js",
39
40
  "./services/main": "./build/services/main.js",
40
41
  "./authkit_client_provider": "./build/providers/authkit_client_provider.js",
41
42
  "./auth": "./build/src/host/authkit_client_guard.js",
@@ -91,7 +92,7 @@
91
92
  "ioredis-mock": "8.13.1",
92
93
  "@types/node": "25.9.1",
93
94
  "typescript": "6.0.3",
94
- "@adonis-agora/authkit-server": "0.58.1"
95
+ "@adonis-agora/authkit-server": "0.58.2"
95
96
  },
96
97
  "scripts": {
97
98
  "build": "tsc && node -e \"require('node:fs').cpSync('stubs','build/stubs',{recursive:true,filter:(s)=>!s.endsWith('.ts')})\"",