@adonis-agora/authkit-testing 0.3.0 → 0.5.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.
- package/README.md +3 -4
- package/build/src/authenticator.d.ts +5 -3
- package/build/src/authenticator.js +12 -7
- package/build/src/jwt.js +2 -2
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -53,20 +53,19 @@ provides one locally with no external network. You can also reuse a single
|
|
|
53
53
|
keypair across tokens via `generateTestKeyPair()` + the `key` option, or get the
|
|
54
54
|
raw public JWKS object with `jwksFromKey(key)` / `testJwks(key)`.
|
|
55
55
|
|
|
56
|
-
### `fakeAuthenticator({ identity?, user
|
|
56
|
+
### `fakeAuthenticator({ identity?, user? })`
|
|
57
57
|
|
|
58
58
|
Object satisfying the client's `Authenticator` surface, for injecting into
|
|
59
|
-
`ctx.auth` in controller tests.
|
|
59
|
+
`ctx.auth` in controller tests. O AuthKit só autentica — autorização por role é
|
|
60
|
+
do `@adonis-agora/authz`, então o fake expõe só `hasGlobalRole` (roles do token).
|
|
60
61
|
|
|
61
62
|
```ts
|
|
62
63
|
import { fakeAuthenticator } from '@adonis-agora/authkit-testing'
|
|
63
64
|
|
|
64
65
|
const auth = fakeAuthenticator({
|
|
65
66
|
identity: createTestIdentity({ globalRoles: ['ADMIN'] }),
|
|
66
|
-
appRoles: ['editor'],
|
|
67
67
|
})
|
|
68
68
|
auth.hasGlobalRole('ADMIN') // true
|
|
69
|
-
await auth.hasAppRole('editor') // true
|
|
70
69
|
```
|
|
71
70
|
|
|
72
71
|
Pass `identity: null` to simulate an anonymous request.
|
|
@@ -9,16 +9,18 @@ export interface FakeAuthenticatorLike {
|
|
|
9
9
|
authenticate(): Promise<Identity>;
|
|
10
10
|
check(): Promise<boolean>;
|
|
11
11
|
hasGlobalRole(role: string): boolean;
|
|
12
|
-
hasAppRole(role: string): Promise<boolean>;
|
|
13
12
|
getUser(): Promise<unknown>;
|
|
13
|
+
getUserOrFail(): Promise<unknown>;
|
|
14
|
+
toSharedProps(): Promise<{
|
|
15
|
+
user: unknown;
|
|
16
|
+
globalRoles: string[];
|
|
17
|
+
} | null>;
|
|
14
18
|
}
|
|
15
19
|
export interface FakeAuthenticatorOptions {
|
|
16
20
|
/** identidade resolvida; default = {@link createTestIdentity}. Passe `null` para simular anônimo. */
|
|
17
21
|
identity?: Identity | null;
|
|
18
22
|
/** usuário de domínio retornado por `getUser()`. */
|
|
19
23
|
user?: unknown;
|
|
20
|
-
/** roles de app reconhecidas por `hasAppRole`. */
|
|
21
|
-
appRoles?: string[];
|
|
22
24
|
}
|
|
23
25
|
/**
|
|
24
26
|
* Cria um objeto que satisfaz a superfície do `Authenticator` do client para
|
|
@@ -5,7 +5,6 @@ import { createTestIdentity } from './identity.js';
|
|
|
5
5
|
*/
|
|
6
6
|
export function fakeAuthenticator(options = {}) {
|
|
7
7
|
const identity = options.identity === undefined ? createTestIdentity() : options.identity;
|
|
8
|
-
const appRoles = options.appRoles ?? [];
|
|
9
8
|
const user = 'user' in options ? options.user : identity;
|
|
10
9
|
return {
|
|
11
10
|
async getIdentity() {
|
|
@@ -16,7 +15,7 @@ export function fakeAuthenticator(options = {}) {
|
|
|
16
15
|
},
|
|
17
16
|
async authenticate() {
|
|
18
17
|
if (!identity)
|
|
19
|
-
throw new Error('
|
|
18
|
+
throw new Error('Not authenticated');
|
|
20
19
|
return identity;
|
|
21
20
|
},
|
|
22
21
|
async check() {
|
|
@@ -25,15 +24,21 @@ export function fakeAuthenticator(options = {}) {
|
|
|
25
24
|
hasGlobalRole(role) {
|
|
26
25
|
return identity?.globalRoles.includes(role) ?? false;
|
|
27
26
|
},
|
|
28
|
-
async hasAppRole(role) {
|
|
29
|
-
if (!identity)
|
|
30
|
-
return false;
|
|
31
|
-
return appRoles.includes(role);
|
|
32
|
-
},
|
|
33
27
|
async getUser() {
|
|
34
28
|
if (!identity)
|
|
35
29
|
return null;
|
|
36
30
|
return user;
|
|
37
31
|
},
|
|
32
|
+
async getUserOrFail() {
|
|
33
|
+
if (!identity || user === null || user === undefined) {
|
|
34
|
+
throw new Error('Not authenticated: no user for the current session');
|
|
35
|
+
}
|
|
36
|
+
return user;
|
|
37
|
+
},
|
|
38
|
+
async toSharedProps() {
|
|
39
|
+
if (!identity)
|
|
40
|
+
return null;
|
|
41
|
+
return { user: user ?? null, globalRoles: identity.globalRoles ?? [] };
|
|
42
|
+
},
|
|
38
43
|
};
|
|
39
44
|
}
|
package/build/src/jwt.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { createServer } from 'node:http';
|
|
2
1
|
import { randomUUID } from 'node:crypto';
|
|
3
|
-
import {
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { SignJWT, exportJWK, generateKeyPair } from 'jose';
|
|
4
4
|
const ALG = 'RS256';
|
|
5
5
|
/** Gera um par de chaves RS256 extraível com um `kid` estável, para minting de tokens em testes. */
|
|
6
6
|
export async function generateTestKeyPair(kid = randomUUID()) {
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonis-agora/authkit-testing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Test helpers for apps built on AuthKit: mint real signed ID tokens with a local JWKS, fake authenticators and account stores, and build valid identities — so host apps can test auth flows without booting an IdP.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "dudousxd",
|
|
7
|
-
"homepage": "https://github.com/DavideCarvalho/adonis-authkit/tree/main/packages/authkit-testing#readme",
|
|
7
|
+
"homepage": "https://github.com/DavideCarvalho/adonis-agora-authkit/tree/main/packages/authkit-testing#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-testing"
|
|
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",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"./types": "./build/index.js"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"jose": "
|
|
40
|
-
"@adonis-agora/authkit-core": "0.7.
|
|
39
|
+
"jose": "5.10.0",
|
|
40
|
+
"@adonis-agora/authkit-core": "0.7.1"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@adonisjs/tsconfig": "2.0.0",
|
|
@@ -46,11 +46,11 @@
|
|
|
46
46
|
"@poppinss/ts-exec": "1.4.4",
|
|
47
47
|
"@types/node": "25.9.1",
|
|
48
48
|
"typescript": "6.0.3",
|
|
49
|
-
"@adonis-agora/authkit-client": "0.
|
|
49
|
+
"@adonis-agora/authkit-client": "0.17.1"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsc",
|
|
53
|
-
"typecheck": "tsc --noEmit",
|
|
53
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.tests.json",
|
|
54
54
|
"test": "node --import=@poppinss/ts-exec bin/test.ts"
|
|
55
55
|
}
|
|
56
56
|
}
|