@wazobiatech/auth-middleware 1.0.7 → 1.0.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 +1 -11
- package/dist/middlewares/express.helper.d.ts +1 -1
- package/dist/middlewares/express.helper.d.ts.map +1 -1
- package/dist/middlewares/express.helper.js +2 -2
- package/dist/middlewares/express.helper.js.map +1 -1
- package/dist/middlewares/gql.helper.d.ts +56 -7
- package/dist/middlewares/gql.helper.d.ts.map +1 -1
- package/dist/middlewares/gql.helper.js +177 -31
- package/dist/middlewares/gql.helper.js.map +1 -1
- package/dist/middlewares/jwt.guard.d.ts +1 -1
- package/dist/middlewares/jwt.guard.d.ts.map +1 -1
- package/dist/middlewares/jwt.guard.js +23 -22
- package/dist/middlewares/jwt.guard.js.map +1 -1
- package/dist/middlewares/project.guard.d.ts +38 -13
- package/dist/middlewares/project.guard.d.ts.map +1 -1
- package/dist/middlewares/project.guard.js +245 -95
- package/dist/middlewares/project.guard.js.map +1 -1
- package/dist/nestjs/decorators/auth.decorator.d.ts +42 -1
- package/dist/nestjs/decorators/auth.decorator.d.ts.map +1 -1
- package/dist/nestjs/decorators/auth.decorator.js +67 -2
- package/dist/nestjs/decorators/auth.decorator.js.map +1 -1
- package/dist/nestjs/guards/project.guard.d.ts +24 -22
- package/dist/nestjs/guards/project.guard.d.ts.map +1 -1
- package/dist/nestjs/guards/project.guard.js +258 -114
- package/dist/nestjs/guards/project.guard.js.map +1 -1
- package/dist/nestjs/index.d.ts +1 -1
- package/dist/nestjs/index.d.ts.map +1 -1
- package/dist/nestjs/index.js +16 -3
- package/dist/nestjs/index.js.map +1 -1
- package/dist/nestjs/jwt-auth.module.d.ts +6 -0
- package/dist/nestjs/jwt-auth.module.d.ts.map +1 -1
- package/dist/nestjs/jwt-auth.module.js +34 -7
- package/dist/nestjs/jwt-auth.module.js.map +1 -1
- package/dist/nestjs/strategies/jwt-strategy.d.ts +1 -1
- package/dist/nestjs/strategies/jwt-strategy.d.ts.map +1 -1
- package/dist/nestjs/strategies/jwt-strategy.js +31 -59
- package/dist/nestjs/strategies/jwt-strategy.js.map +1 -1
- package/dist/types/jwt-payload.d.ts +93 -20
- package/dist/types/jwt-payload.d.ts.map +1 -1
- package/dist/utils/redis.connection.d.ts +10 -0
- package/dist/utils/redis.connection.d.ts.map +1 -1
- package/dist/utils/redis.connection.js +108 -12
- package/dist/utils/redis.connection.js.map +1 -1
- package/package.json +3 -7
|
@@ -1,10 +1,75 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SERVICE_SCOPES_KEY = exports.USER_SCOPES_KEY = exports.PROJECT_SCOPES_KEY = void 0;
|
|
3
4
|
exports.ProjectAndUserAuth = ProjectAndUserAuth;
|
|
5
|
+
exports.ProjectAuth = ProjectAuth;
|
|
6
|
+
exports.UserAuth = UserAuth;
|
|
7
|
+
exports.ServiceAuth = ServiceAuth;
|
|
4
8
|
const common_1 = require("@nestjs/common");
|
|
5
9
|
const project_guard_1 = require("../guards/project.guard");
|
|
6
10
|
const jwt_guard_1 = require("../guards/jwt-guard");
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
// Metadata keys for scopes
|
|
12
|
+
exports.PROJECT_SCOPES_KEY = 'project_scopes';
|
|
13
|
+
exports.USER_SCOPES_KEY = 'user_scopes';
|
|
14
|
+
exports.SERVICE_SCOPES_KEY = 'service_scopes';
|
|
15
|
+
/**
|
|
16
|
+
* Requires both User token (Authorization header) AND Project/Platform token (x-project-token header)
|
|
17
|
+
* Use for: Operations needing both user and project context
|
|
18
|
+
*
|
|
19
|
+
* @param options - Optional scopes for project and user
|
|
20
|
+
* @example
|
|
21
|
+
* @ProjectAndUserAuth({ projectScopes: ['billing:read'], userScopes: ['invoices:create'] })
|
|
22
|
+
*/
|
|
23
|
+
function ProjectAndUserAuth(options) {
|
|
24
|
+
const decorators = [(0, common_1.UseGuards)(jwt_guard_1.JwtAuthGuard, project_guard_1.ProjectAuthGuard)];
|
|
25
|
+
if (options?.projectScopes) {
|
|
26
|
+
decorators.push((0, common_1.SetMetadata)(exports.PROJECT_SCOPES_KEY, options.projectScopes));
|
|
27
|
+
}
|
|
28
|
+
if (options?.userScopes) {
|
|
29
|
+
decorators.push((0, common_1.SetMetadata)(exports.USER_SCOPES_KEY, options.userScopes));
|
|
30
|
+
}
|
|
31
|
+
return (0, common_1.applyDecorators)(...decorators);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Requires only Project/Platform token (x-project-token header)
|
|
35
|
+
* Use for: Project management, admin operations
|
|
36
|
+
*
|
|
37
|
+
* @param scopes - Optional required scopes
|
|
38
|
+
* @example
|
|
39
|
+
* @ProjectAuth(['projects:write', 'users:manage'])
|
|
40
|
+
*/
|
|
41
|
+
function ProjectAuth(scopes) {
|
|
42
|
+
const decorators = [(0, common_1.UseGuards)(project_guard_1.ProjectAuthGuard)];
|
|
43
|
+
if (scopes && scopes.length > 0) {
|
|
44
|
+
decorators.push((0, common_1.SetMetadata)(exports.PROJECT_SCOPES_KEY, scopes));
|
|
45
|
+
}
|
|
46
|
+
return (0, common_1.applyDecorators)(...decorators);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Requires only User token (Authorization header)
|
|
50
|
+
* Use for: User profile operations (me, updateMe, deleteMe)
|
|
51
|
+
*
|
|
52
|
+
* @param scopes - Optional required permissions
|
|
53
|
+
* @example
|
|
54
|
+
* @UserAuth(['users:delete'])
|
|
55
|
+
*/
|
|
56
|
+
function UserAuth(scopes) {
|
|
57
|
+
const decorators = [(0, common_1.UseGuards)(jwt_guard_1.JwtAuthGuard)];
|
|
58
|
+
if (scopes && scopes.length > 0) {
|
|
59
|
+
decorators.push((0, common_1.SetMetadata)(exports.USER_SCOPES_KEY, scopes));
|
|
60
|
+
}
|
|
61
|
+
return (0, common_1.applyDecorators)(...decorators);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Requires service token (x-project-token header with type: 'service')
|
|
65
|
+
* Use for: Service-to-service operations
|
|
66
|
+
*
|
|
67
|
+
* @param scopes - Required service scopes
|
|
68
|
+
* @example
|
|
69
|
+
* @ServiceAuth(['tokens:create', 'users:read'])
|
|
70
|
+
*/
|
|
71
|
+
function ServiceAuth(scopes) {
|
|
72
|
+
return (0, common_1.applyDecorators)((0, common_1.UseGuards)(project_guard_1.ProjectAuthGuard), // Reuses same guard, checks for service token
|
|
73
|
+
(0, common_1.SetMetadata)(exports.SERVICE_SCOPES_KEY, scopes));
|
|
9
74
|
}
|
|
10
75
|
//# sourceMappingURL=auth.decorator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.decorator.js","sourceRoot":"","sources":["../../../src/nestjs/decorators/auth.decorator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"auth.decorator.js","sourceRoot":"","sources":["../../../src/nestjs/decorators/auth.decorator.ts"],"names":[],"mappings":";;;AAiBA,gDAeC;AAUD,kCAQC;AAUD,4BAQC;AAUD,kCAKC;AAnFD,2CAAyE;AACzE,2DAA2D;AAC3D,mDAAmD;AAEnD,2BAA2B;AACd,QAAA,kBAAkB,GAAG,gBAAgB,CAAC;AACtC,QAAA,eAAe,GAAG,aAAa,CAAC;AAChC,QAAA,kBAAkB,GAAG,gBAAgB,CAAC;AAEnD;;;;;;;GAOG;AACH,SAAgB,kBAAkB,CAAC,OAGlC;IACC,MAAM,UAAU,GAAG,CAAC,IAAA,kBAAS,EAAC,wBAAY,EAAE,gCAAgB,CAAC,CAAC,CAAC;IAE/D,IAAI,OAAO,EAAE,aAAa,EAAE,CAAC;QAC3B,UAAU,CAAC,IAAI,CAAC,IAAA,oBAAW,EAAC,0BAAkB,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC;IAC1E,CAAC;IAED,IAAI,OAAO,EAAE,UAAU,EAAE,CAAC;QACxB,UAAU,CAAC,IAAI,CAAC,IAAA,oBAAW,EAAC,uBAAe,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;IACpE,CAAC;IAED,OAAO,IAAA,wBAAe,EAAC,GAAG,UAAU,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,WAAW,CAAC,MAAiB;IAC3C,MAAM,UAAU,GAAG,CAAC,IAAA,kBAAS,EAAC,gCAAgB,CAAC,CAAC,CAAC;IAEjD,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAChC,UAAU,CAAC,IAAI,CAAC,IAAA,oBAAW,EAAC,0BAAkB,EAAE,MAAM,CAAC,CAAC,CAAC;IAC3D,CAAC;IAED,OAAO,IAAA,wBAAe,EAAC,GAAG,UAAU,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,QAAQ,CAAC,MAAiB;IACxC,MAAM,UAAU,GAAG,CAAC,IAAA,kBAAS,EAAC,wBAAY,CAAC,CAAC,CAAC;IAE7C,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAChC,UAAU,CAAC,IAAI,CAAC,IAAA,oBAAW,EAAC,uBAAe,EAAE,MAAM,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,OAAO,IAAA,wBAAe,EAAC,GAAG,UAAU,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,WAAW,CAAC,MAAgB;IAC1C,OAAO,IAAA,wBAAe,EACpB,IAAA,kBAAS,EAAC,gCAAgB,CAAC,EAAE,8CAA8C;IAC3E,IAAA,oBAAW,EAAC,0BAAkB,EAAE,MAAM,CAAC,CACxC,CAAC;AACJ,CAAC"}
|
|
@@ -1,45 +1,47 @@
|
|
|
1
1
|
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
2
3
|
export declare class ProjectAuthGuard implements CanActivate {
|
|
4
|
+
private reflector;
|
|
3
5
|
private readonly logger;
|
|
4
|
-
private
|
|
6
|
+
private serviceJwksCacheKey;
|
|
5
7
|
private jwksCacheTTL;
|
|
6
|
-
|
|
8
|
+
private readonly serviceName;
|
|
9
|
+
constructor(reflector: Reflector);
|
|
7
10
|
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
11
|
+
/**
|
|
12
|
+
* Check scopes based on decorator metadata
|
|
13
|
+
*/
|
|
14
|
+
private checkScopes;
|
|
8
15
|
/**
|
|
9
16
|
* Extract request object from different NestJS contexts
|
|
10
17
|
*/
|
|
11
18
|
private getRequest;
|
|
12
19
|
/**
|
|
13
|
-
*
|
|
20
|
+
* Handle platform token
|
|
14
21
|
*/
|
|
15
|
-
private
|
|
22
|
+
private handlePlatformToken;
|
|
16
23
|
/**
|
|
17
|
-
*
|
|
24
|
+
* Handle project token
|
|
18
25
|
*/
|
|
19
|
-
private
|
|
20
|
-
/**
|
|
21
|
-
* Get current project secret version from Redis (cached by Mercury)
|
|
22
|
-
*/
|
|
23
|
-
private getCurrentProjectSecretVersion;
|
|
26
|
+
private handleProjectToken;
|
|
24
27
|
/**
|
|
25
|
-
*
|
|
28
|
+
* Handle service token
|
|
26
29
|
*/
|
|
27
|
-
private
|
|
30
|
+
private handleServiceToken;
|
|
31
|
+
private validateToken;
|
|
32
|
+
private validatePlatformToken;
|
|
33
|
+
private validateProjectToken;
|
|
34
|
+
private validateServiceToken;
|
|
28
35
|
/**
|
|
29
|
-
*
|
|
36
|
+
* Get RSA public key from cached JWKS with auto-refresh on key miss
|
|
30
37
|
*/
|
|
38
|
+
private getPublicKeyFromCache;
|
|
39
|
+
private getCurrentSecretVersion;
|
|
40
|
+
private fetchAndCacheJWKS;
|
|
31
41
|
private decodeJwtHeader;
|
|
32
|
-
|
|
33
|
-
* Update JWKS cache TTL (can be increased beyond 5 hours)
|
|
34
|
-
*/
|
|
42
|
+
private decodeJwtPayload;
|
|
35
43
|
setCacheTTL(seconds: number): void;
|
|
36
|
-
/**
|
|
37
|
-
* Manually refresh JWKS cache
|
|
38
|
-
*/
|
|
39
44
|
refreshJWKSCache(): Promise<void>;
|
|
40
|
-
/**
|
|
41
|
-
* Cleanup Redis connection (called on app shutdown)
|
|
42
|
-
*/
|
|
43
45
|
onApplicationShutdown(): Promise<void>;
|
|
44
46
|
}
|
|
45
47
|
//# sourceMappingURL=project.guard.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"project.guard.d.ts","sourceRoot":"","sources":["../../../src/nestjs/guards/project.guard.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"project.guard.d.ts","sourceRoot":"","sources":["../../../src/nestjs/guards/project.guard.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,WAAW,EACX,gBAAgB,EAKjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAiBzC,qBACa,gBAAiB,YAAW,WAAW;IAMtC,OAAO,CAAC,SAAS;IAL7B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqC;IAC5D,OAAO,CAAC,mBAAmB,CAAwB;IACnD,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;gBAEjB,SAAS,EAAE,SAAS;IAKlC,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IAqE9D;;MAEE;IACF,OAAO,CAAC,WAAW;IA8FnB;;OAEG;IACH,OAAO,CAAC,UAAU;IAalB;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAiB3B;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAgC1B;;OAEG;IACH,OAAO,CAAC,kBAAkB;YAsBZ,aAAa;YAoCb,qBAAqB;YAsBrB,oBAAoB;YA+BpB,oBAAoB;IAYlC;;OAEG;YACW,qBAAqB;YAuErB,uBAAuB;YAmBvB,iBAAiB;IA0D/B,OAAO,CAAC,eAAe;IAUvB,OAAO,CAAC,gBAAgB;IAgBxB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAK5B,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IAIjC,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC;CAM7C"}
|