@tc-libs/authentication 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 +116 -6
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,11 +1,121 @@
|
|
|
1
|
-
# authentication
|
|
1
|
+
# @tc-libs/authentication
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Layer di autenticazione/autorizzazione per NestJS basato su JWT, permission token e OAuth opzionale Google/Apple.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Il package esporta:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- `AuthModule`
|
|
8
|
+
- `AuthService`
|
|
9
|
+
- decorator per protezione endpoint
|
|
10
|
+
- guard e strategy
|
|
11
|
+
- enum `ENUM_AUTH_ACCESS_FOR`
|
|
8
12
|
|
|
9
|
-
##
|
|
13
|
+
## Prerequisito importante
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
`AuthService` dipende da `PasswordService` e `TokenService` di `@tc-libs/helper`. In pratica questo package va usato insieme a `HelperModule`.
|
|
16
|
+
|
|
17
|
+
## Registrazione
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
AuthModule.register(
|
|
21
|
+
{
|
|
22
|
+
jwt: {
|
|
23
|
+
defaultSecretKey: process.env.JWT_DEFAULT_SECRET!,
|
|
24
|
+
defaultExpirationTime: 3600,
|
|
25
|
+
},
|
|
26
|
+
auth: {
|
|
27
|
+
prefixAuthorization: 'Bearer',
|
|
28
|
+
subject: 'access',
|
|
29
|
+
audience: 'my-api',
|
|
30
|
+
issuer: 'my-service',
|
|
31
|
+
payloadEncryption: true,
|
|
32
|
+
accessToken: {
|
|
33
|
+
secretKey: process.env.ACCESS_TOKEN_SECRET!,
|
|
34
|
+
expirationTime: 3600,
|
|
35
|
+
notBeforeExpirationTime: 0,
|
|
36
|
+
encryptKey: process.env.ACCESS_TOKEN_ENCRYPT_KEY!,
|
|
37
|
+
encryptIv: process.env.ACCESS_TOKEN_ENCRYPT_IV!,
|
|
38
|
+
},
|
|
39
|
+
refreshToken: {
|
|
40
|
+
secretKey: process.env.REFRESH_TOKEN_SECRET!,
|
|
41
|
+
expirationTime: 86400,
|
|
42
|
+
expirationTimeRememberMe: 2592000,
|
|
43
|
+
notBeforeExpirationTime: 0,
|
|
44
|
+
encryptKey: process.env.REFRESH_TOKEN_ENCRYPT_KEY!,
|
|
45
|
+
encryptIv: process.env.REFRESH_TOKEN_ENCRYPT_IV!,
|
|
46
|
+
},
|
|
47
|
+
permissionToken: {
|
|
48
|
+
headerName: 'x-permission-token',
|
|
49
|
+
secretKey: process.env.PERMISSION_TOKEN_SECRET!,
|
|
50
|
+
expirationTime: 900,
|
|
51
|
+
notBeforeExpirationTime: 0,
|
|
52
|
+
encryptKey: process.env.PERMISSION_TOKEN_ENCRYPT_KEY!,
|
|
53
|
+
encryptIv: process.env.PERMISSION_TOKEN_ENCRYPT_IV!,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
true,
|
|
58
|
+
);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Le strategy Google e Apple vengono registrate solo se la relativa configurazione e completa.
|
|
62
|
+
|
|
63
|
+
## `AuthService`
|
|
64
|
+
|
|
65
|
+
Metodi principali:
|
|
66
|
+
|
|
67
|
+
- `getAuthToken(payload, rememberMe)`
|
|
68
|
+
- `getRefreshToken(payload, refreshToken, rememberMe, loginDate)`
|
|
69
|
+
- `validateUser(password, hash)`
|
|
70
|
+
- `createPassword(password)`
|
|
71
|
+
- getter per scadenze, issuer, audience, subject
|
|
72
|
+
|
|
73
|
+
Esempio login:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
const token = await this.authService.getAuthToken(
|
|
77
|
+
{ _id: user._id.toString(), email: user.email, role: user.role },
|
|
78
|
+
false,
|
|
79
|
+
);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Decorator utili
|
|
83
|
+
|
|
84
|
+
- `AuthJwtAccessProtected()`
|
|
85
|
+
- `AuthJwtRefreshProtected()`
|
|
86
|
+
- `AuthJwtPublicAccessProtected()`
|
|
87
|
+
- `AuthJwtAdminAccessProtected()`
|
|
88
|
+
- `AuthPermissionProtected(group, ...permissions)`
|
|
89
|
+
- `CurrentUser()`
|
|
90
|
+
- `User()`
|
|
91
|
+
- `AuthJwtPayload()`
|
|
92
|
+
- `AuthJwtToken()`
|
|
93
|
+
|
|
94
|
+
Esempio:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
@Get('me')
|
|
98
|
+
@AuthJwtAccessProtected()
|
|
99
|
+
profile(@CurrentUser() user: any) {
|
|
100
|
+
return user;
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Access levels
|
|
105
|
+
|
|
106
|
+
Il package definisce:
|
|
107
|
+
|
|
108
|
+
- `SUPER_ADMIN`
|
|
109
|
+
- `USER`
|
|
110
|
+
- `ADMIN`
|
|
111
|
+
- `EDITOR`
|
|
112
|
+
- `LOGISTIC`
|
|
113
|
+
|
|
114
|
+
Questi valori vengono riusati da `role`, `api-key` e altri package.
|
|
115
|
+
|
|
116
|
+
## Sviluppo
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
nx build authentication
|
|
120
|
+
nx test authentication
|
|
121
|
+
```
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tc-libs/authentication",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.0",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@tc-libs/errors": "3.
|
|
6
|
-
"@tc-libs/helper": "3.
|
|
5
|
+
"@tc-libs/errors": "3.8.0",
|
|
6
|
+
"@tc-libs/helper": "3.8.0",
|
|
7
7
|
"@nestjs/common": "^11.0.12",
|
|
8
8
|
"@nestjs/passport": "^11.0.5",
|
|
9
9
|
"@nestjs/jwt": "^11.0.0",
|