@adonis-agora/authkit-sdk 0.6.1 → 0.6.2
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/README.md +102 -0
- package/build/src/embedded_driver.js +28 -11
- package/build/src/remote_driver.js +3 -2
- package/package.json +7 -7
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# @adonis-agora/authkit-sdk
|
|
2
|
+
|
|
3
|
+
Backend SDK for the [AuthKit](https://github.com/DavideCarvalho/adonis-agora-authkit) Admin API:
|
|
4
|
+
manage users, sessions, OIDC clients, organizations, settings, signing keys, and the audit
|
|
5
|
+
log from server-side code — with **one typed interface** and **two interchangeable drivers**.
|
|
6
|
+
|
|
7
|
+
- **`remote`** — HTTP against the Admin REST API (`/api/authkit/v1`), authenticated with a
|
|
8
|
+
Bearer API key. Use it from any backend service.
|
|
9
|
+
- **`embedded`** — in-process calls that resolve the server services straight from the
|
|
10
|
+
AdonisJS container. Use it when the IdP runs in the **same** app: zero HTTP, zero API key.
|
|
11
|
+
|
|
12
|
+
Both drivers return the **exact same shapes**, so you can start embedded and split out to
|
|
13
|
+
remote later without touching call sites.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pnpm add @adonis-agora/authkit-sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`@adonis-agora/authkit-server` is an **optional** peer dependency — only the `embedded`
|
|
22
|
+
driver needs it (imported lazily). Remote-only consumers can skip it entirely.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
`createAuthkit` always returns a `Promise<Authkit>`.
|
|
27
|
+
|
|
28
|
+
### Remote
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { createAuthkit } from '@adonis-agora/authkit-sdk'
|
|
32
|
+
|
|
33
|
+
const authkit = await createAuthkit({
|
|
34
|
+
mode: 'remote',
|
|
35
|
+
baseUrl: 'https://idp.example.com',
|
|
36
|
+
apiKey: process.env.AUTHKIT_ADMIN_KEY!,
|
|
37
|
+
// apiPrefix: '/api/authkit/v1', // default; must match registerAuthHost
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Embedded (same app)
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import app from '@adonisjs/core/services/app'
|
|
45
|
+
import { createAuthkit } from '@adonis-agora/authkit-sdk'
|
|
46
|
+
|
|
47
|
+
const authkit = await createAuthkit({ mode: 'embedded', app })
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### The interface
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// Users
|
|
54
|
+
await authkit.users.list({ search: 'jane' })
|
|
55
|
+
const user = await authkit.users.create({ email: 'jane@acme.dev' })
|
|
56
|
+
await authkit.users.delete(user.id) // cascades sessions, grants, tokens (LGPD/GDPR)
|
|
57
|
+
|
|
58
|
+
// Sessions
|
|
59
|
+
await authkit.sessions.list(user.id)
|
|
60
|
+
await authkit.sessions.revokeAll(user.id)
|
|
61
|
+
|
|
62
|
+
// OIDC clients
|
|
63
|
+
await authkit.clients.create({ /* client input */ })
|
|
64
|
+
await authkit.clients.regenerateSecret(clientId)
|
|
65
|
+
|
|
66
|
+
// Organizations, members & invitations
|
|
67
|
+
await authkit.organizations.create({ name: 'Acme' })
|
|
68
|
+
await authkit.organizations.members.add(orgId, { accountId, role: 'admin' })
|
|
69
|
+
|
|
70
|
+
// Audit log, stats, settings, signing keys
|
|
71
|
+
await authkit.audit.list({ action: 'user.login' })
|
|
72
|
+
await authkit.stats()
|
|
73
|
+
await authkit.settings.set('branding.name', 'Acme')
|
|
74
|
+
await authkit.keys.rotate({ retire: false })
|
|
75
|
+
|
|
76
|
+
// Token verification
|
|
77
|
+
const result = await authkit.tokens.verify(bearerToken)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Errors surface as `AuthkitApiError`:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { AuthkitApiError } from '@adonis-agora/authkit-sdk'
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
await authkit.users.get('missing')
|
|
87
|
+
} catch (err) {
|
|
88
|
+
if (err instanceof AuthkitApiError) {
|
|
89
|
+
console.error(err.status, err.message)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Documentation
|
|
95
|
+
|
|
96
|
+
Full guide and the Admin REST API reference live in the
|
|
97
|
+
[AuthKit docs](https://github.com/DavideCarvalho/adonis-agora-authkit) (`apps/docs` → Backend SDK
|
|
98
|
+
and Admin API).
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT
|
|
@@ -190,7 +190,11 @@ export async function createEmbeddedAuthkit(opts) {
|
|
|
190
190
|
const admin = new AdminSessionsService(service);
|
|
191
191
|
const sessions = await enrichSessionsWithContext(cfg, userId, await admin.listSessions(userId));
|
|
192
192
|
const grants = await admin.listGrants(userId);
|
|
193
|
-
return {
|
|
193
|
+
return {
|
|
194
|
+
canList: admin.canList,
|
|
195
|
+
sessions: sessions.map(sessionDto),
|
|
196
|
+
grants: grants.map(grantDto),
|
|
197
|
+
};
|
|
194
198
|
},
|
|
195
199
|
async revokeAll(userId) {
|
|
196
200
|
const result = await new AdminSessionsService(service).revokeAll(userId);
|
|
@@ -290,7 +294,7 @@ export async function createEmbeddedAuthkit(opts) {
|
|
|
290
294
|
return verifier.verify(token);
|
|
291
295
|
},
|
|
292
296
|
},
|
|
293
|
-
organizations: (
|
|
297
|
+
organizations: (() => {
|
|
294
298
|
const orgsService = new AdminOrgsService(cfg);
|
|
295
299
|
const ACTOR = { actorId: null, ip: null, source: 'admin-api' };
|
|
296
300
|
const origin = (cfg.issuer ?? '').replace(/\/+$/, '');
|
|
@@ -306,7 +310,12 @@ export async function createEmbeddedAuthkit(opts) {
|
|
|
306
310
|
};
|
|
307
311
|
}
|
|
308
312
|
function memberDto(m) {
|
|
309
|
-
return {
|
|
313
|
+
return {
|
|
314
|
+
accountId: m.accountId,
|
|
315
|
+
email: m.email ?? null,
|
|
316
|
+
role: m.role,
|
|
317
|
+
joinedAt: m.joinedAt,
|
|
318
|
+
};
|
|
310
319
|
}
|
|
311
320
|
function invitationDto(inv) {
|
|
312
321
|
return {
|
|
@@ -330,17 +339,25 @@ export async function createEmbeddedAuthkit(opts) {
|
|
|
330
339
|
async create(input) {
|
|
331
340
|
const result = await orgsService.createOrg(input, ACTOR);
|
|
332
341
|
if ('ok' in result && result.ok === false) {
|
|
333
|
-
throw new Error(result.reason === 'not_supported'
|
|
342
|
+
throw new Error(result.reason === 'not_supported'
|
|
343
|
+
? 'Organizations não suportado.'
|
|
344
|
+
: 'Slug já em uso.');
|
|
334
345
|
}
|
|
335
346
|
return orgDto(result);
|
|
336
347
|
},
|
|
337
348
|
async get(id) {
|
|
338
349
|
const result = await orgsService.getOrg(id);
|
|
339
350
|
if ('ok' in result && result.ok === false) {
|
|
340
|
-
throw new Error(result.reason === 'not_supported'
|
|
351
|
+
throw new Error(result.reason === 'not_supported'
|
|
352
|
+
? 'Organizations não suportado.'
|
|
353
|
+
: 'Organização não encontrada.');
|
|
341
354
|
}
|
|
342
355
|
const detail = result;
|
|
343
|
-
return {
|
|
356
|
+
return {
|
|
357
|
+
...orgDto(detail),
|
|
358
|
+
members: detail.members.map(memberDto),
|
|
359
|
+
pendingInvitations: detail.pendingInvitations.map(invitationDto),
|
|
360
|
+
};
|
|
344
361
|
},
|
|
345
362
|
async update(id, input) {
|
|
346
363
|
const result = await orgsService.updateOrg(id, input, ACTOR);
|
|
@@ -405,7 +422,7 @@ export async function createEmbeddedAuthkit(opts) {
|
|
|
405
422
|
})(),
|
|
406
423
|
settings: {
|
|
407
424
|
async list() {
|
|
408
|
-
const { RuntimeSettings } = await import('@adonis-agora/authkit-server');
|
|
425
|
+
const { RuntimeSettings } = (await import('@adonis-agora/authkit-server'));
|
|
409
426
|
let db;
|
|
410
427
|
try {
|
|
411
428
|
db = await app.container.make('lucid.db');
|
|
@@ -426,7 +443,7 @@ export async function createEmbeddedAuthkit(opts) {
|
|
|
426
443
|
};
|
|
427
444
|
},
|
|
428
445
|
async get(key) {
|
|
429
|
-
const { RuntimeSettings } = await import('@adonis-agora/authkit-server');
|
|
446
|
+
const { RuntimeSettings } = (await import('@adonis-agora/authkit-server'));
|
|
430
447
|
let db;
|
|
431
448
|
try {
|
|
432
449
|
db = await app.container.make('lucid.db');
|
|
@@ -442,7 +459,7 @@ export async function createEmbeddedAuthkit(opts) {
|
|
|
442
459
|
return { key, value, updatedAt: null, updatedBy: null };
|
|
443
460
|
},
|
|
444
461
|
async set(key, value) {
|
|
445
|
-
const { RuntimeSettings } = await import('@adonis-agora/authkit-server');
|
|
462
|
+
const { RuntimeSettings } = (await import('@adonis-agora/authkit-server'));
|
|
446
463
|
let db;
|
|
447
464
|
try {
|
|
448
465
|
db = await app.container.make('lucid.db');
|
|
@@ -463,7 +480,7 @@ export async function createEmbeddedAuthkit(opts) {
|
|
|
463
480
|
return { key, value: saved, updatedAt: new Date().toISOString(), updatedBy: null };
|
|
464
481
|
},
|
|
465
482
|
async delete(key) {
|
|
466
|
-
const { RuntimeSettings } = await import('@adonis-agora/authkit-server');
|
|
483
|
+
const { RuntimeSettings } = (await import('@adonis-agora/authkit-server'));
|
|
467
484
|
let db;
|
|
468
485
|
try {
|
|
469
486
|
db = await app.container.make('lucid.db');
|
|
@@ -486,7 +503,7 @@ export async function createEmbeddedAuthkit(opts) {
|
|
|
486
503
|
keys: {
|
|
487
504
|
async status() {
|
|
488
505
|
// Build a settings capability for the policy (optional; falls back to defaults when absent).
|
|
489
|
-
const { RuntimeSettings } = await import('@adonis-agora/authkit-server');
|
|
506
|
+
const { RuntimeSettings } = (await import('@adonis-agora/authkit-server'));
|
|
490
507
|
let settingsSvc = null;
|
|
491
508
|
try {
|
|
492
509
|
const db = await app.container.make('lucid.db');
|
|
@@ -21,7 +21,7 @@ export function createRemoteAuthkit(opts) {
|
|
|
21
21
|
const baseUrl = opts.baseUrl.replace(/\/+$/, '');
|
|
22
22
|
const rawApiPrefix = (opts.apiPrefix ?? '/api/authkit/v1').trim();
|
|
23
23
|
// Normalize: ensure leading slash, strip trailing slash.
|
|
24
|
-
const normalizedApiPrefix = (rawApiPrefix.startsWith('/') ? rawApiPrefix :
|
|
24
|
+
const normalizedApiPrefix = (rawApiPrefix.startsWith('/') ? rawApiPrefix : `/${rawApiPrefix}`).replace(/\/+$/, '') || '/';
|
|
25
25
|
const root = `${baseUrl}${normalizedApiPrefix}`;
|
|
26
26
|
const doFetch = opts.fetchImpl ?? globalThis.fetch;
|
|
27
27
|
if (typeof doFetch !== 'function') {
|
|
@@ -47,7 +47,8 @@ export function createRemoteAuthkit(opts) {
|
|
|
47
47
|
const text = await res.text();
|
|
48
48
|
const payload = text ? safeJson(text) : undefined;
|
|
49
49
|
if (!res.ok) {
|
|
50
|
-
const envelope = payload
|
|
50
|
+
const envelope = payload
|
|
51
|
+
?.error;
|
|
51
52
|
throw new AuthkitApiError(res.status, envelope?.code ?? 'http_error', envelope?.message ?? `Request failed with status ${res.status}.`);
|
|
52
53
|
}
|
|
53
54
|
return payload;
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonis-agora/authkit-sdk",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Backend SDK for the AuthKit Admin API with one typed interface and two drivers: remote (HTTP/Bearer) and embedded (in-process when the IdP runs in the same AdonisJS app).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "dudousxd",
|
|
7
|
-
"homepage": "https://github.com/DavideCarvalho/adonis-authkit/tree/main/packages/authkit-sdk#readme",
|
|
7
|
+
"homepage": "https://github.com/DavideCarvalho/adonis-agora-authkit/tree/main/packages/authkit-sdk#readme",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/DavideCarvalho/adonis-authkit.git",
|
|
10
|
+
"url": "https://github.com/DavideCarvalho/adonis-agora-authkit.git",
|
|
11
11
|
"directory": "packages/authkit-sdk"
|
|
12
12
|
},
|
|
13
13
|
"bugs": {
|
|
14
|
-
"url": "https://github.com/DavideCarvalho/adonis-authkit/issues"
|
|
14
|
+
"url": "https://github.com/DavideCarvalho/adonis-agora-authkit/issues"
|
|
15
15
|
},
|
|
16
16
|
"keywords": [
|
|
17
17
|
"adonisjs",
|
|
@@ -47,18 +47,18 @@
|
|
|
47
47
|
}
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@adonisjs/core": "7.
|
|
50
|
+
"@adonisjs/core": "7.4.0",
|
|
51
51
|
"@adonisjs/tsconfig": "2.0.0",
|
|
52
52
|
"@japa/assert": "4.2.0",
|
|
53
53
|
"@japa/runner": "5.3.0",
|
|
54
54
|
"@poppinss/ts-exec": "1.4.4",
|
|
55
55
|
"@types/node": "25.9.1",
|
|
56
56
|
"typescript": "6.0.3",
|
|
57
|
-
"@adonis-agora/authkit-server": "0.
|
|
57
|
+
"@adonis-agora/authkit-server": "0.57.1"
|
|
58
58
|
},
|
|
59
59
|
"scripts": {
|
|
60
60
|
"build": "tsc",
|
|
61
|
-
"typecheck": "tsc --noEmit",
|
|
61
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.tests.json",
|
|
62
62
|
"test": "node --import=@poppinss/ts-exec bin/test.ts"
|
|
63
63
|
}
|
|
64
64
|
}
|