@adonis-agora/authkit-client 0.12.0 → 0.14.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,10 @@
|
|
|
1
|
+
import type { AuthkitClientManager } from "../providers/authkit_client_provider.js";
|
|
2
|
+
/**
|
|
3
|
+
* Singleton accessor for the {@link AuthkitClientManager}, following the Adonis `services/main`
|
|
4
|
+
* convention (like `db`, `mail`, `drive`). Lets an app write
|
|
5
|
+
* `import authkit from "@adonis-agora/authkit-client/services/main"` and read `authkit.clientConfig`
|
|
6
|
+
* / call `authkit.getIdToken(ctx)` etc., instead of resolving the string-keyed `"authkit.client"`
|
|
7
|
+
* binding through the container by hand.
|
|
8
|
+
*/
|
|
9
|
+
declare let manager: AuthkitClientManager;
|
|
10
|
+
export { manager as default };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import app from "@adonisjs/core/services/app";
|
|
2
|
+
/**
|
|
3
|
+
* Singleton accessor for the {@link AuthkitClientManager}, following the Adonis `services/main`
|
|
4
|
+
* convention (like `db`, `mail`, `drive`). Lets an app write
|
|
5
|
+
* `import authkit from "@adonis-agora/authkit-client/services/main"` and read `authkit.clientConfig`
|
|
6
|
+
* / call `authkit.getIdToken(ctx)` etc., instead of resolving the string-keyed `"authkit.client"`
|
|
7
|
+
* binding through the container by hand.
|
|
8
|
+
*/
|
|
9
|
+
let manager;
|
|
10
|
+
await app.booted(async () => {
|
|
11
|
+
manager = await app.container.make("authkit.client");
|
|
12
|
+
});
|
|
13
|
+
export { manager as default };
|
|
@@ -28,6 +28,14 @@ export declare class Authenticator<TUser = unknown> {
|
|
|
28
28
|
check(): Promise<boolean>;
|
|
29
29
|
hasGlobalRole(role: string): boolean;
|
|
30
30
|
getUser(): Promise<TUser | null>;
|
|
31
|
+
/**
|
|
32
|
+
* O `TUser` autenticado, NÃO-nulo. Fail-closed: lança quando não há sessão (ou quando `resolveUser`
|
|
33
|
+
* não devolve usuário) — pensado para rotas atrás do middleware de auth, onde `null` seria uma
|
|
34
|
+
* rota mal-configurada, não um visitante. Espelha `authenticate()` (que devolve a `Identity`); este
|
|
35
|
+
* devolve o usuário do app. Substitui o wrapper `currentUser(auth)` que cada app reescrevia sobre
|
|
36
|
+
* `getUser()`. Para rotas que aceitam visitante, use `getUser()` (devolve `TUser | null`).
|
|
37
|
+
*/
|
|
38
|
+
getUserOrFail(): Promise<TUser>;
|
|
31
39
|
/**
|
|
32
40
|
* Monta o objeto pronto para compartilhar com o frontend (ex.: Inertia share),
|
|
33
41
|
* casando com o `AuthSharedProps` que o `@adonis-agora/authkit-react` consome.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AUTHKIT_METRICS, NoopRecorder } from "@adonis-agora/authkit-core";
|
|
1
|
+
import { AUTHKIT_METRICS, NoopRecorder, } from "@adonis-agora/authkit-core";
|
|
2
2
|
import { populateContext } from "./observability/context_bridge.js";
|
|
3
3
|
/**
|
|
4
4
|
* Autenticador do request. O parâmetro `TUser` é o tipo do usuário do app que `resolveUser`
|
|
@@ -64,7 +64,9 @@ export class Authenticator {
|
|
|
64
64
|
const accessToken = this.deps.getAccessToken?.() ?? undefined;
|
|
65
65
|
// `resolveUser` é tipado `Promise<unknown>` (a lib não conhece o model do app); o `TUser`
|
|
66
66
|
// é a asserção do consumidor, feita UMA vez aqui em vez de em cada call-site de `getUser()`.
|
|
67
|
-
this.#user = (await this.deps.resolveUser(identity, {
|
|
67
|
+
this.#user = (await this.deps.resolveUser(identity, {
|
|
68
|
+
accessToken,
|
|
69
|
+
}));
|
|
68
70
|
}
|
|
69
71
|
else {
|
|
70
72
|
this.#user = null;
|
|
@@ -72,6 +74,20 @@ export class Authenticator {
|
|
|
72
74
|
this.#userResolved = true;
|
|
73
75
|
return this.#user;
|
|
74
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* O `TUser` autenticado, NÃO-nulo. Fail-closed: lança quando não há sessão (ou quando `resolveUser`
|
|
79
|
+
* não devolve usuário) — pensado para rotas atrás do middleware de auth, onde `null` seria uma
|
|
80
|
+
* rota mal-configurada, não um visitante. Espelha `authenticate()` (que devolve a `Identity`); este
|
|
81
|
+
* devolve o usuário do app. Substitui o wrapper `currentUser(auth)` que cada app reescrevia sobre
|
|
82
|
+
* `getUser()`. Para rotas que aceitam visitante, use `getUser()` (devolve `TUser | null`).
|
|
83
|
+
*/
|
|
84
|
+
async getUserOrFail() {
|
|
85
|
+
const user = await this.getUser();
|
|
86
|
+
if (user === null) {
|
|
87
|
+
throw new Error("Not authenticated: no user for the current session");
|
|
88
|
+
}
|
|
89
|
+
return user;
|
|
90
|
+
}
|
|
75
91
|
/**
|
|
76
92
|
* Monta o objeto pronto para compartilhar com o frontend (ex.: Inertia share),
|
|
77
93
|
* casando com o `AuthSharedProps` que o `@adonis-agora/authkit-react` consome.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonis-agora/authkit-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
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/index.js",
|
|
39
|
+
"./services/main": "./build/services/main.js",
|
|
39
40
|
"./authkit_client_provider": "./build/providers/authkit_client_provider.js",
|
|
40
41
|
"./authkit_middleware": "./build/src/middleware/authkit_middleware.js",
|
|
41
42
|
"./backchannel_revocation_middleware": "./build/src/middleware/backchannel_revocation_middleware.js",
|