@rxap/nest-auth0 10.1.0-dev.0 → 10.1.0-dev.2
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/CHANGELOG.md +12 -0
- package/package.json +2 -2
- package/src/index.d.ts +4 -1
- package/src/index.js +4 -1
- package/src/lib/auth0-authentication.service.d.ts +65 -0
- package/src/lib/auth0-authentication.service.js +82 -0
- package/src/lib/auth0-management.service.d.ts +4 -0
- package/src/lib/auth0-management.service.js +19 -0
- package/src/lib/auth0-module-options.factory.d.ts +3 -3
- package/src/lib/auth0-module-options.factory.js +10 -1
- package/src/lib/auth0-options.d.ts +5 -0
- package/src/lib/auth0-options.js +2 -0
- package/src/lib/auth0-validation-schema.d.ts +1 -0
- package/src/lib/auth0-validation-schema.js +4 -1
- package/src/lib/auth0.guard.d.ts +8 -0
- package/src/lib/auth0.guard.js +50 -0
- package/src/lib/auth0.module.d.ts +9 -3
- package/src/lib/auth0.module.js +66 -12
- package/src/lib/tokens.d.ts +2 -0
- package/src/lib/tokens.js +3 -1
- package/src/lib/jwt.strategy.d.ts +0 -76
- package/src/lib/jwt.strategy.js +0 -103
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [10.1.0-dev.2](https://gitlab.com/rxap/packages/compare/@rxap/nest-auth0@10.1.0-dev.1...@rxap/nest-auth0@10.1.0-dev.2) (2025-03-17)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- add management and authentication services ([b3acc35](https://gitlab.com/rxap/packages/commit/b3acc35117e77c6ac80b2b4d9e13d66c510ccda6))
|
|
11
|
+
|
|
12
|
+
# [10.1.0-dev.1](https://gitlab.com/rxap/packages/compare/@rxap/nest-auth0@10.1.0-dev.0...@rxap/nest-auth0@10.1.0-dev.1) (2025-03-14)
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
- **auth0:** replace jwt.strategy with auth0.guard for enhanced JWT validation ([3fe0564](https://gitlab.com/rxap/packages/commit/3fe0564722c09dc1735ca4921879d02e6d6839ec))
|
|
17
|
+
|
|
6
18
|
# [10.1.0-dev.0](https://gitlab.com/rxap/packages/compare/@rxap/nest-auth0@10.0.1...@rxap/nest-auth0@10.1.0-dev.0) (2025-03-12)
|
|
7
19
|
|
|
8
20
|
### Features
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "10.1.0-dev.
|
|
2
|
+
"version": "10.1.0-dev.2",
|
|
3
3
|
"name": "@rxap/nest-auth0",
|
|
4
4
|
"description": "This package provides an Auth0 module for NestJS applications, simplifying integration with Auth0 for authentication and authorization. It includes a configurable module, JWT strategy, and service for interacting with the Auth0 Authentication API. The module also offers validation schemas for environment variables related to Auth0 configuration.\n",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"schematics": "./generators.json",
|
|
40
40
|
"type": "commonjs",
|
|
41
41
|
"types": "./src/index.d.ts",
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "41e20fb6ce24b6b639bb4f9df64a66de95e3aee6"
|
|
43
43
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
export * from './lib/auth0-authentication.service';
|
|
2
|
+
export * from './lib/auth0-management.service';
|
|
1
3
|
export * from './lib/auth0-module-options.factory';
|
|
4
|
+
export * from './lib/auth0-options';
|
|
2
5
|
export * from './lib/auth0-validation-schema';
|
|
6
|
+
export * from './lib/auth0.guard';
|
|
3
7
|
export * from './lib/auth0.module';
|
|
4
8
|
export * from './lib/auth0.service';
|
|
5
|
-
export * from './lib/jwt.strategy';
|
|
6
9
|
export * from './lib/tokens';
|
package/src/index.js
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
// region
|
|
5
|
+
tslib_1.__exportStar(require("./lib/auth0-authentication.service"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./lib/auth0-management.service"), exports);
|
|
5
7
|
tslib_1.__exportStar(require("./lib/auth0-module-options.factory"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./lib/auth0-options"), exports);
|
|
6
9
|
tslib_1.__exportStar(require("./lib/auth0-validation-schema"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./lib/auth0.guard"), exports);
|
|
7
11
|
tslib_1.__exportStar(require("./lib/auth0.module"), exports);
|
|
8
12
|
tslib_1.__exportStar(require("./lib/auth0.service"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./lib/jwt.strategy"), exports);
|
|
10
13
|
tslib_1.__exportStar(require("./lib/tokens"), exports);
|
|
11
14
|
// endregion
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { AuthenticationClient, AuthenticationClientOptions } from 'auth0';
|
|
2
|
+
/**
|
|
3
|
+
* A service that extends Auth0's AuthenticationClient to provide authentication capabilities in NestJS applications.
|
|
4
|
+
*
|
|
5
|
+
* @summary
|
|
6
|
+
* Provides Auth0 authentication services with additional token management functionality.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam T - The type of authentication client options used to configure the Auth0 service.
|
|
9
|
+
*
|
|
10
|
+
* @param options - The configuration options for the Auth0 client including domain, clientId, and clientSecret.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* @Injectable()
|
|
15
|
+
* class AppService {
|
|
16
|
+
* constructor(private readonly auth0Service: Auth0AuthenticationService) {}
|
|
17
|
+
*
|
|
18
|
+
* async getToken() {
|
|
19
|
+
* return this.auth0Service.requestToken('https://api.example.com');
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // Manual instantiation (not recommended in NestJS)
|
|
27
|
+
* const auth0Service = new Auth0AuthenticationService({
|
|
28
|
+
* domain: 'your-domain.auth0.com',
|
|
29
|
+
* clientId: 'your-client-id',
|
|
30
|
+
* clientSecret: 'your-client-secret'
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @throws {@link Error} If the Auth0 client initialization fails due to invalid configuration options.
|
|
35
|
+
*
|
|
36
|
+
* @property requestToken - Method to obtain an access token for a specific audience using client credentials flow.
|
|
37
|
+
*/
|
|
38
|
+
export declare class Auth0AuthenticationService extends AuthenticationClient {
|
|
39
|
+
constructor(options: AuthenticationClientOptions);
|
|
40
|
+
/**
|
|
41
|
+
* Requests an access token from Auth0 using the client credentials grant flow.
|
|
42
|
+
*
|
|
43
|
+
* @summary
|
|
44
|
+
* This method obtains an access token for a specific API audience using the OAuth 2.0 client credentials flow.
|
|
45
|
+
*
|
|
46
|
+
* @param audience - The unique identifier of the API for which the access token is being requested. This is typically the API's URL or identifier registered in Auth0.
|
|
47
|
+
*
|
|
48
|
+
* @returns A Promise that resolves to a string containing the JWT access token that can be used to authenticate requests to the specified API audience.
|
|
49
|
+
*
|
|
50
|
+
* @throws {@link Error} If the token request fails due to invalid credentials, network issues, or other Auth0 API errors.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const auth0Service = new Auth0AuthenticationService(options);
|
|
55
|
+
* try {
|
|
56
|
+
* const token = await auth0Service.requestToken('https://api.example.com');
|
|
57
|
+
* // Use the token for API requests
|
|
58
|
+
* console.log(token);
|
|
59
|
+
* } catch (error) {
|
|
60
|
+
* console.error('Failed to obtain access token:', error);
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
requestToken(audience: string): Promise<string>;
|
|
65
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Auth0AuthenticationService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const auth0_1 = require("auth0");
|
|
7
|
+
const tokens_1 = require("./tokens");
|
|
8
|
+
/**
|
|
9
|
+
* A service that extends Auth0's AuthenticationClient to provide authentication capabilities in NestJS applications.
|
|
10
|
+
*
|
|
11
|
+
* @summary
|
|
12
|
+
* Provides Auth0 authentication services with additional token management functionality.
|
|
13
|
+
*
|
|
14
|
+
* @typeParam T - The type of authentication client options used to configure the Auth0 service.
|
|
15
|
+
*
|
|
16
|
+
* @param options - The configuration options for the Auth0 client including domain, clientId, and clientSecret.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* @Injectable()
|
|
21
|
+
* class AppService {
|
|
22
|
+
* constructor(private readonly auth0Service: Auth0AuthenticationService) {}
|
|
23
|
+
*
|
|
24
|
+
* async getToken() {
|
|
25
|
+
* return this.auth0Service.requestToken('https://api.example.com');
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* // Manual instantiation (not recommended in NestJS)
|
|
33
|
+
* const auth0Service = new Auth0AuthenticationService({
|
|
34
|
+
* domain: 'your-domain.auth0.com',
|
|
35
|
+
* clientId: 'your-client-id',
|
|
36
|
+
* clientSecret: 'your-client-secret'
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @throws {@link Error} If the Auth0 client initialization fails due to invalid configuration options.
|
|
41
|
+
*
|
|
42
|
+
* @property requestToken - Method to obtain an access token for a specific audience using client credentials flow.
|
|
43
|
+
*/
|
|
44
|
+
let Auth0AuthenticationService = class Auth0AuthenticationService extends auth0_1.AuthenticationClient {
|
|
45
|
+
constructor(options) {
|
|
46
|
+
super(options);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Requests an access token from Auth0 using the client credentials grant flow.
|
|
50
|
+
*
|
|
51
|
+
* @summary
|
|
52
|
+
* This method obtains an access token for a specific API audience using the OAuth 2.0 client credentials flow.
|
|
53
|
+
*
|
|
54
|
+
* @param audience - The unique identifier of the API for which the access token is being requested. This is typically the API's URL or identifier registered in Auth0.
|
|
55
|
+
*
|
|
56
|
+
* @returns A Promise that resolves to a string containing the JWT access token that can be used to authenticate requests to the specified API audience.
|
|
57
|
+
*
|
|
58
|
+
* @throws {@link Error} If the token request fails due to invalid credentials, network issues, or other Auth0 API errors.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const auth0Service = new Auth0AuthenticationService(options);
|
|
63
|
+
* try {
|
|
64
|
+
* const token = await auth0Service.requestToken('https://api.example.com');
|
|
65
|
+
* // Use the token for API requests
|
|
66
|
+
* console.log(token);
|
|
67
|
+
* } catch (error) {
|
|
68
|
+
* console.error('Failed to obtain access token:', error);
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
async requestToken(audience) {
|
|
73
|
+
const { data: { access_token }, } = await this.oauth.clientCredentialsGrant({ audience });
|
|
74
|
+
return access_token;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
exports.Auth0AuthenticationService = Auth0AuthenticationService;
|
|
78
|
+
exports.Auth0AuthenticationService = Auth0AuthenticationService = tslib_1.__decorate([
|
|
79
|
+
(0, common_1.Injectable)(),
|
|
80
|
+
tslib_1.__param(0, (0, common_1.Inject)(tokens_1.AUTH0_AUTHENTICATION_OPTIONS)),
|
|
81
|
+
tslib_1.__metadata("design:paramtypes", [Object])
|
|
82
|
+
], Auth0AuthenticationService);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ManagementClient, ManagementClientOptionsWithClientCredentials, ManagementClientOptionsWithToken } from 'auth0';
|
|
2
|
+
export declare class Auth0ManagementService extends ManagementClient {
|
|
3
|
+
constructor(options: ManagementClientOptionsWithToken | ManagementClientOptionsWithClientCredentials);
|
|
4
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Auth0ManagementService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const auth0_1 = require("auth0");
|
|
7
|
+
const tokens_1 = require("./tokens");
|
|
8
|
+
let Auth0ManagementService = class Auth0ManagementService extends auth0_1.ManagementClient {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
// @ts-expect-error - constructor signature is not compatible with the base class
|
|
11
|
+
super(options);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
exports.Auth0ManagementService = Auth0ManagementService;
|
|
15
|
+
exports.Auth0ManagementService = Auth0ManagementService = tslib_1.__decorate([
|
|
16
|
+
(0, common_1.Injectable)(),
|
|
17
|
+
tslib_1.__param(0, (0, common_1.Inject)(tokens_1.AUTH0_MANAGEMENT_OPTIONS)),
|
|
18
|
+
tslib_1.__metadata("design:paramtypes", [Object])
|
|
19
|
+
], Auth0ManagementService);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ConfigurableModuleOptionsFactory, Logger } from '@nestjs/common';
|
|
2
2
|
import { ConfigService } from '@nestjs/config';
|
|
3
|
-
import {
|
|
3
|
+
import { Auth0Options } from './auth0-options';
|
|
4
4
|
/**
|
|
5
5
|
* Creates an instance of `AuthenticationClientOptions` for Auth0 configuration.
|
|
6
6
|
*
|
|
@@ -31,7 +31,7 @@ import { AuthenticationClientOptions } from 'auth0';
|
|
|
31
31
|
* const options = await auth0ModuleOptionsFactory.create();
|
|
32
32
|
* ```
|
|
33
33
|
*/
|
|
34
|
-
export declare class Auth0ModuleOptionsFactory implements ConfigurableModuleOptionsFactory<
|
|
34
|
+
export declare class Auth0ModuleOptionsFactory implements ConfigurableModuleOptionsFactory<Auth0Options, 'create'> {
|
|
35
35
|
protected readonly config: ConfigService;
|
|
36
36
|
protected readonly logger: Logger;
|
|
37
37
|
/**
|
|
@@ -49,5 +49,5 @@ export declare class Auth0ModuleOptionsFactory implements ConfigurableModuleOpti
|
|
|
49
49
|
* const options = await auth0ModuleOptionsFactory.create();
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
|
-
create(): Promise<
|
|
52
|
+
create(): Promise<Auth0Options>;
|
|
53
53
|
}
|
|
@@ -52,11 +52,20 @@ let Auth0ModuleOptionsFactory = class Auth0ModuleOptionsFactory {
|
|
|
52
52
|
*/
|
|
53
53
|
async create() {
|
|
54
54
|
this.logger.verbose('Create auth0 client options', 'Auth0ClientModuleOptionsFactory');
|
|
55
|
-
|
|
55
|
+
const authentication = {
|
|
56
56
|
domain: this.config.get('AUTH0_DOMAIN'),
|
|
57
57
|
clientId: this.config.get('AUTH0_CLIENT_ID'),
|
|
58
58
|
clientSecret: this.config.get('AUTH0_CLIENT_SECRET'),
|
|
59
59
|
};
|
|
60
|
+
const management = this.config.get('AUTH0_MANAGEMENT_TOKEN') ? {
|
|
61
|
+
domain: this.config.get('AUTH0_DOMAIN'),
|
|
62
|
+
token: this.config.get('AUTH0_MANAGEMENT_TOKEN'),
|
|
63
|
+
} : {
|
|
64
|
+
domain: this.config.get('AUTH0_DOMAIN'),
|
|
65
|
+
clientId: this.config.get('AUTH0_CLIENT_ID'),
|
|
66
|
+
clientSecret: this.config.get('AUTH0_CLIENT_SECRET'),
|
|
67
|
+
};
|
|
68
|
+
return { authentication, management };
|
|
60
69
|
}
|
|
61
70
|
};
|
|
62
71
|
exports.Auth0ModuleOptionsFactory = Auth0ModuleOptionsFactory;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { AuthenticationClientOptions, ManagementClientOptionsWithToken, ManagementClientOptionsWithClientCredentials } from 'auth0';
|
|
2
|
+
export interface Auth0Options {
|
|
3
|
+
authentication: AuthenticationClientOptions;
|
|
4
|
+
management: ManagementClientOptionsWithToken | ManagementClientOptionsWithClientCredentials;
|
|
5
|
+
}
|
|
@@ -17,8 +17,11 @@ function auth0ValidationSchema(environment, defaults = {}) {
|
|
|
17
17
|
schema['AUTH0_DOMAIN'] = Joi.string().domain();
|
|
18
18
|
schema['AUTH0_CLIENT_ID'] = Joi.string();
|
|
19
19
|
schema['AUTH0_CLIENT_SECRET'] = Joi.string();
|
|
20
|
+
schema['AUTH0_MANAGEMENT_TOKEN'] = Joi.string();
|
|
20
21
|
for (const [key, value] of Object.keys(defaults)) {
|
|
21
|
-
|
|
22
|
+
if (value) {
|
|
23
|
+
schema[key] = schema[key].default(value);
|
|
24
|
+
}
|
|
22
25
|
}
|
|
23
26
|
return schema;
|
|
24
27
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
3
|
+
export declare class Auth0Guard implements CanActivate {
|
|
4
|
+
private readonly jwtService;
|
|
5
|
+
private readonly logger;
|
|
6
|
+
protected readonly reflector: Reflector;
|
|
7
|
+
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Auth0Guard = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const core_1 = require("@nestjs/core");
|
|
7
|
+
const jwt_1 = require("@nestjs/jwt");
|
|
8
|
+
const nest_utilities_1 = require("@rxap/nest-utilities");
|
|
9
|
+
const passport_jwt_1 = require("passport-jwt");
|
|
10
|
+
let Auth0Guard = class Auth0Guard {
|
|
11
|
+
async canActivate(context) {
|
|
12
|
+
const isPublic = this.reflector.getAllAndOverride(nest_utilities_1.IS_PUBLIC_KEY, [
|
|
13
|
+
context.getHandler(),
|
|
14
|
+
context.getClass(),
|
|
15
|
+
]);
|
|
16
|
+
if (isPublic) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
const request = context.switchToHttp().getRequest();
|
|
20
|
+
const jwt = passport_jwt_1.ExtractJwt.fromAuthHeaderAsBearerToken()(request);
|
|
21
|
+
if (!jwt) {
|
|
22
|
+
this.logger.debug('No Bearer token found in authorization header', 'Auth0Guard');
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const payload = await this.jwtService.verifyAsync(jwt);
|
|
26
|
+
if (!payload) {
|
|
27
|
+
this.logger.warn('JWT verification failed', 'Auth0Guard');
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
this.logger.verbose(`JWT verification succeeded for user ${payload.sub}`, 'Auth0Guard');
|
|
31
|
+
request.user = payload;
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
exports.Auth0Guard = Auth0Guard;
|
|
36
|
+
tslib_1.__decorate([
|
|
37
|
+
(0, common_1.Inject)(jwt_1.JwtService),
|
|
38
|
+
tslib_1.__metadata("design:type", jwt_1.JwtService)
|
|
39
|
+
], Auth0Guard.prototype, "jwtService", void 0);
|
|
40
|
+
tslib_1.__decorate([
|
|
41
|
+
(0, common_1.Inject)(common_1.Logger),
|
|
42
|
+
tslib_1.__metadata("design:type", common_1.Logger)
|
|
43
|
+
], Auth0Guard.prototype, "logger", void 0);
|
|
44
|
+
tslib_1.__decorate([
|
|
45
|
+
(0, common_1.Inject)(core_1.Reflector),
|
|
46
|
+
tslib_1.__metadata("design:type", core_1.Reflector)
|
|
47
|
+
], Auth0Guard.prototype, "reflector", void 0);
|
|
48
|
+
exports.Auth0Guard = Auth0Guard = tslib_1.__decorate([
|
|
49
|
+
(0, common_1.Injectable)()
|
|
50
|
+
], Auth0Guard);
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { DynamicModule } from '@nestjs/common';
|
|
2
|
-
import {
|
|
3
|
-
export declare const ConfigurableModuleClass: import("@nestjs/common").ConfigurableModuleCls<
|
|
2
|
+
import { Auth0Options } from './auth0-options';
|
|
3
|
+
export declare const ConfigurableModuleClass: import("@nestjs/common").ConfigurableModuleCls<Auth0Options, "register", "create", {
|
|
4
|
+
global: boolean;
|
|
5
|
+
}>, MODULE_OPTIONS_TOKEN: string | symbol, OPTIONS_TYPE: Auth0Options & Partial<{
|
|
6
|
+
global: boolean;
|
|
7
|
+
}>, ASYNC_OPTIONS_TYPE: import("@nestjs/common").ConfigurableModuleAsyncOptions<Auth0Options, "create"> & Partial<{
|
|
8
|
+
global: boolean;
|
|
9
|
+
}>;
|
|
4
10
|
/**
|
|
5
11
|
* A configurable module that provides Auth0 authentication services for NestJS applications.
|
|
6
12
|
*
|
|
@@ -48,7 +54,7 @@ export declare const ConfigurableModuleClass: import("@nestjs/common").Configura
|
|
|
48
54
|
* @property registerAsync - Static method to configure the module with asynchronous options
|
|
49
55
|
* @property updateProviders - Private static method to update the module's providers and exports
|
|
50
56
|
*
|
|
51
|
-
* @returns A dynamic module configured with Auth0 authentication capabilities, including the
|
|
57
|
+
* @returns A dynamic module configured with Auth0 authentication capabilities, including the Auth0AuthenticationService and JWT strategy.
|
|
52
58
|
*
|
|
53
59
|
* @throws {@link Error} If the required Auth0 configuration options are invalid or missing.
|
|
54
60
|
*/
|
package/src/lib/auth0.module.js
CHANGED
|
@@ -4,12 +4,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.Auth0Module = exports.ASYNC_OPTIONS_TYPE = exports.OPTIONS_TYPE = exports.MODULE_OPTIONS_TOKEN = exports.ConfigurableModuleClass = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const common_1 = require("@nestjs/common");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
7
|
+
const config_1 = require("@nestjs/config");
|
|
8
|
+
const jwt_1 = require("@nestjs/jwt");
|
|
9
|
+
const jwks_rsa_1 = require("jwks-rsa");
|
|
10
|
+
const auth0_management_service_1 = require("./auth0-management.service");
|
|
11
|
+
const auth0_authentication_service_1 = require("./auth0-authentication.service");
|
|
12
|
+
const auth0_guard_1 = require("./auth0.guard");
|
|
10
13
|
const tokens_1 = require("./tokens");
|
|
11
|
-
_a = new common_1.ConfigurableModuleBuilder()
|
|
12
|
-
.build(), exports.ConfigurableModuleClass = _a.ConfigurableModuleClass, exports.MODULE_OPTIONS_TOKEN = _a.MODULE_OPTIONS_TOKEN, exports.OPTIONS_TYPE = _a.OPTIONS_TYPE, exports.ASYNC_OPTIONS_TYPE = _a.ASYNC_OPTIONS_TYPE;
|
|
14
|
+
_a = new common_1.ConfigurableModuleBuilder().setExtras({ global: true }).build(), exports.ConfigurableModuleClass = _a.ConfigurableModuleClass, exports.MODULE_OPTIONS_TOKEN = _a.MODULE_OPTIONS_TOKEN, exports.OPTIONS_TYPE = _a.OPTIONS_TYPE, exports.ASYNC_OPTIONS_TYPE = _a.ASYNC_OPTIONS_TYPE;
|
|
13
15
|
/**
|
|
14
16
|
* A configurable module that provides Auth0 authentication services for NestJS applications.
|
|
15
17
|
*
|
|
@@ -57,7 +59,7 @@ _a = new common_1.ConfigurableModuleBuilder()
|
|
|
57
59
|
* @property registerAsync - Static method to configure the module with asynchronous options
|
|
58
60
|
* @property updateProviders - Private static method to update the module's providers and exports
|
|
59
61
|
*
|
|
60
|
-
* @returns A dynamic module configured with Auth0 authentication capabilities, including the
|
|
62
|
+
* @returns A dynamic module configured with Auth0 authentication capabilities, including the Auth0AuthenticationService and JWT strategy.
|
|
61
63
|
*
|
|
62
64
|
* @throws {@link Error} If the required Auth0 configuration options are invalid or missing.
|
|
63
65
|
*/
|
|
@@ -123,21 +125,73 @@ let Auth0Module = class Auth0Module extends exports.ConfigurableModuleClass {
|
|
|
123
125
|
}
|
|
124
126
|
static updateProviders(module) {
|
|
125
127
|
module.providers ??= [];
|
|
126
|
-
module.providers.push(auth0_service_1.Auth0Service);
|
|
127
|
-
module.exports ??= [];
|
|
128
|
-
module.exports.push(auth0_service_1.Auth0Service);
|
|
129
128
|
module.providers.push({
|
|
130
129
|
provide: tokens_1.AUTH0_OPTIONS,
|
|
131
130
|
useExisting: exports.MODULE_OPTIONS_TOKEN
|
|
132
131
|
});
|
|
132
|
+
module.providers.push({
|
|
133
|
+
provide: tokens_1.AUTH0_AUTHENTICATION_OPTIONS,
|
|
134
|
+
useFactory: (options) => options.authentication ?? null,
|
|
135
|
+
inject: [tokens_1.AUTH0_OPTIONS]
|
|
136
|
+
});
|
|
137
|
+
module.providers.push({
|
|
138
|
+
provide: tokens_1.AUTH0_MANAGEMENT_OPTIONS,
|
|
139
|
+
useFactory: (options) => options.management ?? null,
|
|
140
|
+
inject: [tokens_1.AUTH0_OPTIONS]
|
|
141
|
+
});
|
|
142
|
+
module.providers.push({
|
|
143
|
+
provide: auth0_authentication_service_1.Auth0AuthenticationService,
|
|
144
|
+
useFactory: (options) => options ? new auth0_authentication_service_1.Auth0AuthenticationService(options) : null,
|
|
145
|
+
inject: [tokens_1.AUTH0_AUTHENTICATION_OPTIONS]
|
|
146
|
+
});
|
|
147
|
+
module.providers.push({
|
|
148
|
+
provide: auth0_management_service_1.Auth0ManagementService,
|
|
149
|
+
useFactory: (options) => options ? new auth0_management_service_1.Auth0ManagementService(options) : null,
|
|
150
|
+
inject: [tokens_1.AUTH0_MANAGEMENT_OPTIONS]
|
|
151
|
+
});
|
|
152
|
+
module.exports ??= [];
|
|
153
|
+
module.exports.push(auth0_authentication_service_1.Auth0AuthenticationService);
|
|
154
|
+
module.exports.push(auth0_management_service_1.Auth0ManagementService);
|
|
133
155
|
return module;
|
|
134
156
|
}
|
|
135
157
|
};
|
|
136
158
|
exports.Auth0Module = Auth0Module;
|
|
137
159
|
exports.Auth0Module = Auth0Module = tslib_1.__decorate([
|
|
160
|
+
(0, common_1.Global)(),
|
|
138
161
|
(0, common_1.Module)({
|
|
139
|
-
imports: [
|
|
140
|
-
|
|
141
|
-
|
|
162
|
+
imports: [
|
|
163
|
+
jwt_1.JwtModule.registerAsync({
|
|
164
|
+
useFactory: (config) => ({
|
|
165
|
+
secretOrKeyProvider: (_, tokenOrPayload) => {
|
|
166
|
+
return new Promise((resolve, reject) => {
|
|
167
|
+
(0, jwks_rsa_1.passportJwtSecret)({
|
|
168
|
+
cache: true,
|
|
169
|
+
rateLimit: true,
|
|
170
|
+
jwksRequestsPerMinute: 5,
|
|
171
|
+
jwksUri: `${config.get('AUTH0_ISSUER_URL')}.well-known/jwks.json`,
|
|
172
|
+
})(null, tokenOrPayload, (err, secret) => {
|
|
173
|
+
if (err) {
|
|
174
|
+
reject(err);
|
|
175
|
+
}
|
|
176
|
+
else if (secret) {
|
|
177
|
+
resolve(secret);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
reject(new Error('No secret found'));
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
},
|
|
185
|
+
verifyOptions: {
|
|
186
|
+
audience: config.get('AUTH0_AUDIENCE'),
|
|
187
|
+
issuer: config.get('AUTH0_ISSUER_URL'),
|
|
188
|
+
algorithms: [config.get('AUTH0_ALGORITHM', 'RS256')],
|
|
189
|
+
},
|
|
190
|
+
}),
|
|
191
|
+
inject: [config_1.ConfigService],
|
|
192
|
+
}),
|
|
193
|
+
],
|
|
194
|
+
providers: [auth0_guard_1.Auth0Guard],
|
|
195
|
+
exports: [auth0_guard_1.Auth0Guard],
|
|
142
196
|
})
|
|
143
197
|
], Auth0Module);
|
package/src/lib/tokens.d.ts
CHANGED
package/src/lib/tokens.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AUTH0_OPTIONS = void 0;
|
|
3
|
+
exports.AUTH0_MANAGEMENT_OPTIONS = exports.AUTH0_AUTHENTICATION_OPTIONS = exports.AUTH0_OPTIONS = void 0;
|
|
4
4
|
exports.AUTH0_OPTIONS = Symbol('AUTH0_OPTIONS');
|
|
5
|
+
exports.AUTH0_AUTHENTICATION_OPTIONS = Symbol('AUTH0_AUTHENTICATION_OPTIONS');
|
|
6
|
+
exports.AUTH0_MANAGEMENT_OPTIONS = Symbol('AUTH0_MANAGEMENT_OPTIONS');
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { ConfigService } from '@nestjs/config';
|
|
2
|
-
import { Strategy } from 'passport-jwt';
|
|
3
|
-
declare const JwtStrategy_base: new (...args: [opt: import("passport-jwt").StrategyOptionsWithRequest] | [opt: import("passport-jwt").StrategyOptionsWithoutRequest]) => Strategy & {
|
|
4
|
-
validate(...args: any[]): unknown;
|
|
5
|
-
};
|
|
6
|
-
/**
|
|
7
|
-
* Here's the TSDoc documentation for the `JwtStrategy` class:
|
|
8
|
-
*
|
|
9
|
-
* A JWT (JSON Web Token) strategy implementation for authentication using Passport.js in NestJS applications.
|
|
10
|
-
*
|
|
11
|
-
* @summary
|
|
12
|
-
* This class implements the JWT strategy for authentication, integrating with Auth0's JWKS (JSON Web Key Set) for token validation.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* import { JwtStrategy } from './jwt.strategy';
|
|
17
|
-
*
|
|
18
|
-
* @Module({
|
|
19
|
-
* providers: [JwtStrategy],
|
|
20
|
-
* })
|
|
21
|
-
* export class AuthModule {
|
|
22
|
-
* constructor(private readonly jwtStrategy: JwtStrategy) {}
|
|
23
|
-
* }
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* // Using the strategy in a guard
|
|
29
|
-
* @Injectable()
|
|
30
|
-
* export class JwtAuthGuard extends AuthGuard('jwt') {
|
|
31
|
-
* constructor(private jwtStrategy: JwtStrategy) {
|
|
32
|
-
* super();
|
|
33
|
-
* }
|
|
34
|
-
* }
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* @throws {@link Error} If the configuration parameters for Auth0 are invalid or missing
|
|
38
|
-
* @throws {@link Error} If the JWT token validation fails
|
|
39
|
-
* @throws {@link Error} If the JWKS endpoint is unreachable or returns invalid data
|
|
40
|
-
*
|
|
41
|
-
* @property logger - A NestJS logger instance for verbose logging of validation operations
|
|
42
|
-
* @property validate - Method to validate the JWT payload and return the verified token contents
|
|
43
|
-
*
|
|
44
|
-
* @returns An instance of the JWT strategy that can be used for token validation in passport-based authentication
|
|
45
|
-
*/
|
|
46
|
-
export declare class JwtStrategy extends JwtStrategy_base {
|
|
47
|
-
private readonly logger;
|
|
48
|
-
constructor(config: ConfigService);
|
|
49
|
-
/**
|
|
50
|
-
* Here's the TSDoc documentation for the `validate` method:
|
|
51
|
-
*
|
|
52
|
-
* Validates the JWT payload received from the authentication process.
|
|
53
|
-
*
|
|
54
|
-
* @summary
|
|
55
|
-
* Performs validation of the decoded JWT payload and logs the validation process for debugging purposes.
|
|
56
|
-
*
|
|
57
|
-
* @param payload - The decoded JWT token payload to be validated. This can contain any JWT claims such as subject, issuer, or custom claims.
|
|
58
|
-
*
|
|
59
|
-
* @returns The validated payload object that will be attached to the request object for use in subsequent request handlers.
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```typescript
|
|
63
|
-
* const jwtStrategy = new JwtStrategy(configService);
|
|
64
|
-
* const payload = await jwtStrategy.validate({
|
|
65
|
-
* sub: '123456789',
|
|
66
|
-
* email: 'user@example.com',
|
|
67
|
-
* permissions: ['read:users']
|
|
68
|
-
* });
|
|
69
|
-
* // payload will be available in your request object
|
|
70
|
-
* ```
|
|
71
|
-
*
|
|
72
|
-
* @throws {@link Error} If the payload fails validation or contains invalid claims.
|
|
73
|
-
*/
|
|
74
|
-
validate(payload: unknown): unknown;
|
|
75
|
-
}
|
|
76
|
-
export {};
|
package/src/lib/jwt.strategy.js
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JwtStrategy = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const common_1 = require("@nestjs/common");
|
|
6
|
-
const config_1 = require("@nestjs/config");
|
|
7
|
-
const passport_1 = require("@nestjs/passport");
|
|
8
|
-
const passport_jwt_1 = require("passport-jwt");
|
|
9
|
-
const jwks_rsa_1 = require("jwks-rsa");
|
|
10
|
-
/**
|
|
11
|
-
* Here's the TSDoc documentation for the `JwtStrategy` class:
|
|
12
|
-
*
|
|
13
|
-
* A JWT (JSON Web Token) strategy implementation for authentication using Passport.js in NestJS applications.
|
|
14
|
-
*
|
|
15
|
-
* @summary
|
|
16
|
-
* This class implements the JWT strategy for authentication, integrating with Auth0's JWKS (JSON Web Key Set) for token validation.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* import { JwtStrategy } from './jwt.strategy';
|
|
21
|
-
*
|
|
22
|
-
* @Module({
|
|
23
|
-
* providers: [JwtStrategy],
|
|
24
|
-
* })
|
|
25
|
-
* export class AuthModule {
|
|
26
|
-
* constructor(private readonly jwtStrategy: JwtStrategy) {}
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* ```typescript
|
|
32
|
-
* // Using the strategy in a guard
|
|
33
|
-
* @Injectable()
|
|
34
|
-
* export class JwtAuthGuard extends AuthGuard('jwt') {
|
|
35
|
-
* constructor(private jwtStrategy: JwtStrategy) {
|
|
36
|
-
* super();
|
|
37
|
-
* }
|
|
38
|
-
* }
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @throws {@link Error} If the configuration parameters for Auth0 are invalid or missing
|
|
42
|
-
* @throws {@link Error} If the JWT token validation fails
|
|
43
|
-
* @throws {@link Error} If the JWKS endpoint is unreachable or returns invalid data
|
|
44
|
-
*
|
|
45
|
-
* @property logger - A NestJS logger instance for verbose logging of validation operations
|
|
46
|
-
* @property validate - Method to validate the JWT payload and return the verified token contents
|
|
47
|
-
*
|
|
48
|
-
* @returns An instance of the JWT strategy that can be used for token validation in passport-based authentication
|
|
49
|
-
*/
|
|
50
|
-
let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(passport_jwt_1.Strategy) {
|
|
51
|
-
constructor(config) {
|
|
52
|
-
super({
|
|
53
|
-
secretOrKeyProvider: (0, jwks_rsa_1.passportJwtSecret)({
|
|
54
|
-
cache: true,
|
|
55
|
-
rateLimit: true,
|
|
56
|
-
jwksRequestsPerMinute: 5,
|
|
57
|
-
jwksUri: `${config.get('AUTH0_ISSUER_URL')}.well-known/jwks.json`,
|
|
58
|
-
}),
|
|
59
|
-
jwtFromRequest: passport_jwt_1.ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
60
|
-
audience: config.get('AUTH0_AUDIENCE'),
|
|
61
|
-
issuer: `${config.get('AUTH0_ISSUER_URL')}`,
|
|
62
|
-
algorithms: ['RS256'],
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Here's the TSDoc documentation for the `validate` method:
|
|
67
|
-
*
|
|
68
|
-
* Validates the JWT payload received from the authentication process.
|
|
69
|
-
*
|
|
70
|
-
* @summary
|
|
71
|
-
* Performs validation of the decoded JWT payload and logs the validation process for debugging purposes.
|
|
72
|
-
*
|
|
73
|
-
* @param payload - The decoded JWT token payload to be validated. This can contain any JWT claims such as subject, issuer, or custom claims.
|
|
74
|
-
*
|
|
75
|
-
* @returns The validated payload object that will be attached to the request object for use in subsequent request handlers.
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```typescript
|
|
79
|
-
* const jwtStrategy = new JwtStrategy(configService);
|
|
80
|
-
* const payload = await jwtStrategy.validate({
|
|
81
|
-
* sub: '123456789',
|
|
82
|
-
* email: 'user@example.com',
|
|
83
|
-
* permissions: ['read:users']
|
|
84
|
-
* });
|
|
85
|
-
* // payload will be available in your request object
|
|
86
|
-
* ```
|
|
87
|
-
*
|
|
88
|
-
* @throws {@link Error} If the payload fails validation or contains invalid claims.
|
|
89
|
-
*/
|
|
90
|
-
validate(payload) {
|
|
91
|
-
this.logger.verbose('validate: %JSON', payload, 'JwtStrategy');
|
|
92
|
-
return payload;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
exports.JwtStrategy = JwtStrategy;
|
|
96
|
-
tslib_1.__decorate([
|
|
97
|
-
(0, common_1.Inject)(common_1.Logger),
|
|
98
|
-
tslib_1.__metadata("design:type", common_1.Logger)
|
|
99
|
-
], JwtStrategy.prototype, "logger", void 0);
|
|
100
|
-
exports.JwtStrategy = JwtStrategy = tslib_1.__decorate([
|
|
101
|
-
(0, common_1.Injectable)(),
|
|
102
|
-
tslib_1.__metadata("design:paramtypes", [config_1.ConfigService])
|
|
103
|
-
], JwtStrategy);
|