@tc-libs/api-key 3.6.0 → 3.8.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 +70 -6
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -1,11 +1,75 @@
|
|
|
1
|
-
# api-key
|
|
1
|
+
# @tc-libs/api-key
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Gestione API key applicative con supporto guard, strategy Passport e scadenze.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Il package include:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- `ApiKeyModule`
|
|
8
|
+
- `ApiKeyService`
|
|
9
|
+
- decorator `ApiKeyProtected`
|
|
10
|
+
- guard/strategy `x-api-key`
|
|
11
|
+
- entity/repository/controller admin e public
|
|
8
12
|
|
|
9
|
-
##
|
|
13
|
+
## Registrazione
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
Il modulo richiede anche un provider del servizio utente:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
ApiKeyModule.register(
|
|
19
|
+
{
|
|
20
|
+
provide: USER_SERVICE,
|
|
21
|
+
useExisting: UserService,
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
env: 'PROD',
|
|
25
|
+
},
|
|
26
|
+
true,
|
|
27
|
+
);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## `ApiKeyService`
|
|
31
|
+
|
|
32
|
+
Metodi specifici:
|
|
33
|
+
|
|
34
|
+
- `checkIfUserIsCapped(userId)`
|
|
35
|
+
- `findOneByKey(key)`
|
|
36
|
+
- `findOneByActiveKey(key)`
|
|
37
|
+
- `createByUserId(userId, env, dto)`
|
|
38
|
+
- `createRaw(userId, dto)`
|
|
39
|
+
- `active(repository)`
|
|
40
|
+
- `inactive(repository)`
|
|
41
|
+
- `reset(repository, secret)`
|
|
42
|
+
- `validateHashApiKey(plainHeader, hash)`
|
|
43
|
+
- `createKey(env)`
|
|
44
|
+
- `createSecret()`
|
|
45
|
+
- `createHashApiKey(key, secret)`
|
|
46
|
+
- `update(repository, dto)`
|
|
47
|
+
- `inactiveManyByEndDate()`
|
|
48
|
+
|
|
49
|
+
## Come funziona la coppia key/secret
|
|
50
|
+
|
|
51
|
+
- `key` viene generata con prefisso ambiente
|
|
52
|
+
- `secret` viene restituito solo in fase creazione/reset
|
|
53
|
+
- nel database viene salvato l'hash SHA256 di `key:secret`
|
|
54
|
+
- il client invia la chiave via header `x-api-key`
|
|
55
|
+
|
|
56
|
+
## Protezione endpoint
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
@Get('protected')
|
|
60
|
+
@ApiKeyProtected()
|
|
61
|
+
handler(@ApiKeyPayload() payload: any) {}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Decorator utili:
|
|
65
|
+
|
|
66
|
+
- `ApiKeyProtected()`
|
|
67
|
+
- `ApiKeyPayload()`
|
|
68
|
+
- `GetApiKey()`
|
|
69
|
+
|
|
70
|
+
## Sviluppo
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
nx build api-key
|
|
74
|
+
nx test api-key
|
|
75
|
+
```
|
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tc-libs/api-key",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.0",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@tc-libs/pagination": "3.
|
|
6
|
-
"@tc-libs/authentication": "3.
|
|
7
|
-
"@tc-libs/errors": "3.
|
|
8
|
-
"@tc-libs/request": "3.
|
|
9
|
-
"@tc-libs/response": "3.
|
|
10
|
-
"@tc-libs/doc": "3.
|
|
11
|
-
"@tc-libs/helper": "3.
|
|
12
|
-
"@tc-libs/user": "3.
|
|
13
|
-
"@tc-libs/database": "3.
|
|
14
|
-
"@tc-libs/app-cache": "3.
|
|
15
|
-
"@tc-libs/service": "3.
|
|
5
|
+
"@tc-libs/pagination": "3.8.0",
|
|
6
|
+
"@tc-libs/authentication": "3.8.0",
|
|
7
|
+
"@tc-libs/errors": "3.8.0",
|
|
8
|
+
"@tc-libs/request": "3.8.0",
|
|
9
|
+
"@tc-libs/response": "3.8.0",
|
|
10
|
+
"@tc-libs/doc": "3.8.0",
|
|
11
|
+
"@tc-libs/helper": "3.8.0",
|
|
12
|
+
"@tc-libs/user": "3.8.0",
|
|
13
|
+
"@tc-libs/database": "3.8.0",
|
|
14
|
+
"@tc-libs/app-cache": "3.8.0",
|
|
15
|
+
"@tc-libs/service": "3.8.0",
|
|
16
16
|
"@nestjs/common": "^11.0.12",
|
|
17
17
|
"@faker-js/faker": "^9.6.0",
|
|
18
18
|
"@nestjs/swagger": "^11.1.0",
|