@zola_do/authorization 0.2.8 → 0.2.9
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 +38 -38
- package/dist/guards/jwt.guard.js.map +1 -1
- package/dist/helper/auth.helper.js.map +1 -1
- package/dist/helper/jwt.type.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,8 +57,8 @@ JWT_REFRESH_TOKEN_EXPIRES=7d
|
|
|
57
57
|
### 2. Register Module
|
|
58
58
|
|
|
59
59
|
```typescript
|
|
60
|
-
import { Module } from
|
|
61
|
-
import { AuthorizationModule } from
|
|
60
|
+
import { Module } from '@nestjs/common';
|
|
61
|
+
import { AuthorizationModule } from '@zola_do/authorization';
|
|
62
62
|
|
|
63
63
|
@Module({
|
|
64
64
|
imports: [AuthorizationModule],
|
|
@@ -69,10 +69,10 @@ export class AppModule {}
|
|
|
69
69
|
### 3. Access User in Controller
|
|
70
70
|
|
|
71
71
|
```typescript
|
|
72
|
-
import { Controller, Get } from
|
|
73
|
-
import { CurrentUser, JwtGuard, UseGuards } from
|
|
72
|
+
import { Controller, Get } from '@nestjs/common';
|
|
73
|
+
import { CurrentUser, JwtGuard, UseGuards } from '@zola_do/authorization';
|
|
74
74
|
|
|
75
|
-
@Controller(
|
|
75
|
+
@Controller('profile')
|
|
76
76
|
export class ProfileController {
|
|
77
77
|
@Get()
|
|
78
78
|
@UseGuards(JwtGuard) // Optional - global guard is active by default
|
|
@@ -161,7 +161,7 @@ Default global guard that validates Bearer tokens:
|
|
|
161
161
|
|
|
162
162
|
```typescript
|
|
163
163
|
@UseGuards(JwtGuard)
|
|
164
|
-
@Controller(
|
|
164
|
+
@Controller('protected')
|
|
165
165
|
export class ProtectedController {}
|
|
166
166
|
```
|
|
167
167
|
|
|
@@ -171,14 +171,14 @@ Allows anonymous access but extracts user if token is present:
|
|
|
171
171
|
|
|
172
172
|
```typescript
|
|
173
173
|
@UseGuards(OptionalJwtGuard)
|
|
174
|
-
@Controller(
|
|
174
|
+
@Controller('optional')
|
|
175
175
|
export class OptionalController {
|
|
176
176
|
@Get()
|
|
177
177
|
getData(@CurrentUser() user?: any) {
|
|
178
178
|
if (user) {
|
|
179
|
-
return { message:
|
|
179
|
+
return { message: 'Authenticated', user };
|
|
180
180
|
}
|
|
181
|
-
return { message:
|
|
181
|
+
return { message: 'Anonymous' };
|
|
182
182
|
}
|
|
183
183
|
}
|
|
184
184
|
```
|
|
@@ -188,16 +188,16 @@ export class OptionalController {
|
|
|
188
188
|
Requires specific permissions. Supports pipe-separated OR logic:
|
|
189
189
|
|
|
190
190
|
```typescript
|
|
191
|
-
@UseGuards(JwtGuard, PermissionsGuard(
|
|
192
|
-
@Controller(
|
|
191
|
+
@UseGuards(JwtGuard, PermissionsGuard('product:create'))
|
|
192
|
+
@Controller('products')
|
|
193
193
|
export class ProductsController {
|
|
194
194
|
@Post()
|
|
195
195
|
createProduct() {
|
|
196
196
|
// Requires 'product:create' permission
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
@Patch(
|
|
200
|
-
@UseGuards(PermissionsGuard(
|
|
199
|
+
@Patch(':id')
|
|
200
|
+
@UseGuards(PermissionsGuard('product:update|product:manage'))
|
|
201
201
|
updateProduct() {
|
|
202
202
|
// Requires 'product:update' OR 'product:manage'
|
|
203
203
|
}
|
|
@@ -210,7 +210,7 @@ Validates `X-API-Key` header against `process.env.API_KEY`:
|
|
|
210
210
|
|
|
211
211
|
```typescript
|
|
212
212
|
@UseGuards(ApiKeyGuard)
|
|
213
|
-
@Controller(
|
|
213
|
+
@Controller('external')
|
|
214
214
|
export class ExternalController {}
|
|
215
215
|
```
|
|
216
216
|
|
|
@@ -220,7 +220,7 @@ Checks vendor registration status (custom implementation required):
|
|
|
220
220
|
|
|
221
221
|
```typescript
|
|
222
222
|
@UseGuards(JwtGuard, VendorGuard())
|
|
223
|
-
@Controller(
|
|
223
|
+
@Controller('vendor')
|
|
224
224
|
export class VendorController {}
|
|
225
225
|
```
|
|
226
226
|
|
|
@@ -279,7 +279,7 @@ interface JwtPayload {
|
|
|
279
279
|
Utility service for token operations:
|
|
280
280
|
|
|
281
281
|
```typescript
|
|
282
|
-
import { AuthHelper } from
|
|
282
|
+
import { AuthHelper } from '@zola_do/authorization';
|
|
283
283
|
|
|
284
284
|
@Injectable()
|
|
285
285
|
export class AuthService {
|
|
@@ -324,14 +324,14 @@ export class AuthService {
|
|
|
324
324
|
## Token Generation Example
|
|
325
325
|
|
|
326
326
|
```typescript
|
|
327
|
-
import { AuthHelper } from
|
|
328
|
-
import { JwtPayload } from
|
|
327
|
+
import { AuthHelper } from '@zola_do/authorization';
|
|
328
|
+
import { JwtPayload } from '@zola_do/authorization/helper';
|
|
329
329
|
|
|
330
|
-
@Controller(
|
|
330
|
+
@Controller('auth')
|
|
331
331
|
export class AuthController {
|
|
332
332
|
constructor(private readonly authHelper: AuthHelper) {}
|
|
333
333
|
|
|
334
|
-
@Post(
|
|
334
|
+
@Post('login')
|
|
335
335
|
async login(@Body() dto: LoginDto) {
|
|
336
336
|
const user = await this.userService.validateUser(dto);
|
|
337
337
|
|
|
@@ -348,8 +348,8 @@ export class AuthController {
|
|
|
348
348
|
return this.authHelper.generateTokens(payload);
|
|
349
349
|
}
|
|
350
350
|
|
|
351
|
-
@Post(
|
|
352
|
-
async refresh(@Body(
|
|
351
|
+
@Post('refresh')
|
|
352
|
+
async refresh(@Body('refreshToken') refreshToken: string) {
|
|
353
353
|
return this.authHelper.refreshTokens(refreshToken);
|
|
354
354
|
}
|
|
355
355
|
}
|
|
@@ -360,8 +360,8 @@ export class AuthController {
|
|
|
360
360
|
Override the default JWT validation logic:
|
|
361
361
|
|
|
362
362
|
```typescript
|
|
363
|
-
import { Injectable, ExecutionContext } from
|
|
364
|
-
import { JwtGuard } from
|
|
363
|
+
import { Injectable, ExecutionContext } from '@nestjs/common';
|
|
364
|
+
import { JwtGuard } from '@zola_do/authorization';
|
|
365
365
|
|
|
366
366
|
@Injectable()
|
|
367
367
|
export class AppJwtGuard extends JwtGuard {
|
|
@@ -375,8 +375,8 @@ export class AppJwtGuard extends JwtGuard {
|
|
|
375
375
|
const user = request.user;
|
|
376
376
|
|
|
377
377
|
// Check additional conditions
|
|
378
|
-
if (user.status ===
|
|
379
|
-
throw new ForbiddenException(
|
|
378
|
+
if (user.status === 'suspended') {
|
|
379
|
+
throw new ForbiddenException('Account suspended');
|
|
380
380
|
}
|
|
381
381
|
|
|
382
382
|
return true;
|
|
@@ -387,8 +387,8 @@ export class AppJwtGuard extends JwtGuard {
|
|
|
387
387
|
Register in module:
|
|
388
388
|
|
|
389
389
|
```typescript
|
|
390
|
-
import { APP_GUARD } from
|
|
391
|
-
import { AuthorizationModule, JwtGuard } from
|
|
390
|
+
import { APP_GUARD } from '@nestjs/core';
|
|
391
|
+
import { AuthorizationModule, JwtGuard } from '@zola_do/authorization';
|
|
392
392
|
|
|
393
393
|
@Module({
|
|
394
394
|
imports: [AuthorizationModule],
|
|
@@ -408,7 +408,7 @@ export class AppModule {}
|
|
|
408
408
|
Passport strategy for access token validation:
|
|
409
409
|
|
|
410
410
|
```typescript
|
|
411
|
-
import { JwtStrategy } from
|
|
411
|
+
import { JwtStrategy } from '@zola_do/authorization';
|
|
412
412
|
|
|
413
413
|
// Already registered by AuthorizationModule
|
|
414
414
|
// Customization via override (see above)
|
|
@@ -419,7 +419,7 @@ import { JwtStrategy } from "@zola_do/authorization";
|
|
|
419
419
|
Passport strategy for refresh token validation:
|
|
420
420
|
|
|
421
421
|
```typescript
|
|
422
|
-
import { JwtRefreshTokenStrategy } from
|
|
422
|
+
import { JwtRefreshTokenStrategy } from '@zola_do/authorization';
|
|
423
423
|
|
|
424
424
|
// Used for token refresh endpoints
|
|
425
425
|
// Validates against JWT_REFRESH_TOKEN_SECRET
|
|
@@ -467,18 +467,18 @@ Permissions use pipe-separated OR logic:
|
|
|
467
467
|
Subpath imports are recommended for tree-shaking:
|
|
468
468
|
|
|
469
469
|
```typescript
|
|
470
|
-
import { AuthorizationModule } from
|
|
470
|
+
import { AuthorizationModule } from '@zola_do/authorization/module';
|
|
471
471
|
import {
|
|
472
472
|
JwtGuard,
|
|
473
473
|
PermissionsGuard,
|
|
474
474
|
ApiKeyGuard,
|
|
475
|
-
} from
|
|
476
|
-
import { AllowAnonymous, CurrentUser } from
|
|
477
|
-
import { AuthHelper } from
|
|
475
|
+
} from '@zola_do/authorization/guards';
|
|
476
|
+
import { AllowAnonymous, CurrentUser } from '@zola_do/authorization/decorators';
|
|
477
|
+
import { AuthHelper } from '@zola_do/authorization/helper';
|
|
478
478
|
import {
|
|
479
479
|
JwtStrategy,
|
|
480
480
|
JwtRefreshTokenStrategy,
|
|
481
|
-
} from
|
|
481
|
+
} from '@zola_do/authorization/strategy';
|
|
482
482
|
```
|
|
483
483
|
|
|
484
484
|
Root import is supported for backward compatibility:
|
|
@@ -489,7 +489,7 @@ import {
|
|
|
489
489
|
JwtGuard,
|
|
490
490
|
AllowAnonymous,
|
|
491
491
|
AuthHelper,
|
|
492
|
-
} from
|
|
492
|
+
} from '@zola_do/authorization';
|
|
493
493
|
```
|
|
494
494
|
|
|
495
495
|
## Optional Features
|
|
@@ -499,10 +499,10 @@ import {
|
|
|
499
499
|
Rate limiting guard for applications behind a reverse proxy:
|
|
500
500
|
|
|
501
501
|
```typescript
|
|
502
|
-
import { ThrottlerBehindProxyGuard } from
|
|
502
|
+
import { ThrottlerBehindProxyGuard } from '@zola_do/authorization/optional/throttler';
|
|
503
503
|
|
|
504
504
|
@UseGuards(ThrottlerBehindProxyGuard)
|
|
505
|
-
@Controller(
|
|
505
|
+
@Controller('api')
|
|
506
506
|
export class ApiController {}
|
|
507
507
|
```
|
|
508
508
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.guard.js","sourceRoot":"","sources":["../../src/guards/jwt.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAKwB;AAExB,uCAAyC;AACzC,uFAAmF;AAC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AC,IAAI,GAAG,IAAI,CAAC;YACpB,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAEO,sBAAsB,CAAC,OAAgB;;QAC7C,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,MAAA,MAAA,OAAO,CAAC,OAAO,CAAC,aAAa,0CAAE,KAAK,CAAC,GAAG,CAAC,mCAAI,EAAE,CAAC;QACtE,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;IAC/C,CAAC;CACF,CAAA;AAnDY,4BAAQ;mBAAR,QAAQ;IADpB,IAAA,mBAAU,GAAE;qCAE6B,gBAAS;GADtC,QAAQ,CAmDpB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.helper.js","sourceRoot":"","sources":["../../src/helper/auth.helper.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAmE;AACnE,qCAAyC;AACzC,iCAAiC;AACjC,mCAAmC;
|
|
1
|
+
{"version":3,"file":"auth.helper.js","sourceRoot":"","sources":["../../src/helper/auth.helper.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAmE;AACnE,qCAAyC;AACzC,iCAAiC;AACjC,mCAAmC;AAI5B,IAAM,UAAU,GAAhB,MAAM,UAAU;IACrB,YAA6B,GAAe;QAAf,QAAG,GAAH,GAAG,CAAY;IAAG,CAAC;IAEzC,KAAK,CAAC,MAAM,CAAC,KAAa,EAAE,MAAc;QAC/C,MAAM,OAAO,GAAQ,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QAExD,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,8BAAqB,CAAC,eAAe,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IA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ms';
|
|
2
2
|
type UnitAnyCase = Unit | Uppercase<Unit> | Lowercase<Unit>;
|
|
3
3
|
export type StringValue = `${number}` | `${number}${UnitAnyCase}` | `${number} ${UnitAnyCase}`;
|
|
4
4
|
export {};
|