clhq-auth-module 1.1.0-alpha.100 → 1.1.0-alpha.102
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/dist/auth/interfaces/auth.interface.d.ts +9 -0
- package/dist/auth/interfaces/auth.interface.js +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/middlewares/AuthMiddleware.d.ts +3 -1
- package/dist/middlewares/AuthMiddleware.js +30 -5
- package/dist/utils/uuid-resolver.d.ts +3 -0
- package/dist/utils/uuid-resolver.js +50 -0
- package/package.json +1 -1
|
@@ -24,3 +24,12 @@ export interface UserPayload {
|
|
|
24
24
|
email: string;
|
|
25
25
|
role: string;
|
|
26
26
|
}
|
|
27
|
+
export declare abstract class UserResolver {
|
|
28
|
+
abstract resolveUserId(auth0UserId: string): Promise<string | null>;
|
|
29
|
+
}
|
|
30
|
+
export interface AuthenticatedUser {
|
|
31
|
+
id: string;
|
|
32
|
+
email: string;
|
|
33
|
+
auth0Id?: string;
|
|
34
|
+
}
|
|
35
|
+
export declare function maskSensitiveId(id: string | undefined): string;
|
|
@@ -1,2 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UserResolver = void 0;
|
|
4
|
+
exports.maskSensitiveId = maskSensitiveId;
|
|
5
|
+
class UserResolver {
|
|
6
|
+
}
|
|
7
|
+
exports.UserResolver = UserResolver;
|
|
8
|
+
function maskSensitiveId(id) {
|
|
9
|
+
if (!id)
|
|
10
|
+
return '[none]';
|
|
11
|
+
if (id.length <= 10)
|
|
12
|
+
return '***';
|
|
13
|
+
return `${id.substring(0, 10)}***`;
|
|
14
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,3 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./auth"), exports);
|
|
18
18
|
__exportStar(require("./middlewares/AuthMiddleware"), exports);
|
|
19
|
+
__exportStar(require("./auth/interfaces/auth.interface"), exports);
|
|
20
|
+
__exportStar(require("./utils/uuid-resolver"), exports);
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { NestMiddleware } from '@nestjs/common';
|
|
2
2
|
import { Request, Response } from 'express';
|
|
3
3
|
import { Auth0Service } from '../auth/services/auth0.service';
|
|
4
|
+
import { UserResolver } from '../auth/interfaces/auth.interface';
|
|
4
5
|
export declare class AuthMiddleware implements NestMiddleware {
|
|
5
6
|
private readonly auth0Service;
|
|
7
|
+
private readonly userResolver?;
|
|
6
8
|
private readonly logger;
|
|
7
|
-
constructor(auth0Service: Auth0Service);
|
|
9
|
+
constructor(auth0Service: Auth0Service, userResolver?: UserResolver);
|
|
8
10
|
use(req: Request, res: Response, next: () => void): Promise<void>;
|
|
9
11
|
}
|
|
@@ -14,11 +14,14 @@ exports.AuthMiddleware = void 0;
|
|
|
14
14
|
const common_1 = require("@nestjs/common");
|
|
15
15
|
const lodash_1 = require("lodash");
|
|
16
16
|
const auth0_service_1 = require("../auth/services/auth0.service");
|
|
17
|
+
const auth_interface_1 = require("../auth/interfaces/auth.interface");
|
|
17
18
|
let AuthMiddleware = AuthMiddleware_1 = class AuthMiddleware {
|
|
18
19
|
auth0Service;
|
|
20
|
+
userResolver;
|
|
19
21
|
logger = new common_1.Logger(AuthMiddleware_1.name);
|
|
20
|
-
constructor(auth0Service) {
|
|
22
|
+
constructor(auth0Service, userResolver) {
|
|
21
23
|
this.auth0Service = auth0Service;
|
|
24
|
+
this.userResolver = userResolver;
|
|
22
25
|
}
|
|
23
26
|
async use(req, res, next) {
|
|
24
27
|
const bearerHeader = req.headers.authorization;
|
|
@@ -33,12 +36,33 @@ let AuthMiddleware = AuthMiddleware_1 = class AuthMiddleware {
|
|
|
33
36
|
this.logger.debug('requesting jwt verify token process');
|
|
34
37
|
const decodedToken = (await this.auth0Service.validateToken(accessToken));
|
|
35
38
|
const userEmail = (0, lodash_1.get)(decodedToken, 'https://clippyhq.com/email', '');
|
|
39
|
+
const auth0UserId = decodedToken?.sub;
|
|
40
|
+
this.logger.debug('token');
|
|
41
|
+
this.logger.debug({
|
|
42
|
+
sub: (0, auth_interface_1.maskSensitiveId)(auth0UserId),
|
|
43
|
+
email: userEmail,
|
|
44
|
+
});
|
|
45
|
+
let userId = auth0UserId;
|
|
46
|
+
if (this.userResolver && auth0UserId) {
|
|
47
|
+
try {
|
|
48
|
+
const resolvedId = await this.userResolver.resolveUserId(auth0UserId);
|
|
49
|
+
if (resolvedId) {
|
|
50
|
+
userId = resolvedId;
|
|
51
|
+
this.logger.debug(`Resolved Auth0 user to UUID ${userId}`);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
this.logger.warn(`Could not resolve Auth0 user to UUID`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (resolveError) {
|
|
58
|
+
this.logger.error(`Error resolving user ID for ${(0, auth_interface_1.maskSensitiveId)(auth0UserId)}:`, resolveError);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
36
61
|
const user = {
|
|
37
|
-
id:
|
|
62
|
+
id: userId,
|
|
38
63
|
email: userEmail,
|
|
64
|
+
auth0Id: auth0UserId,
|
|
39
65
|
};
|
|
40
|
-
this.logger.debug('token');
|
|
41
|
-
this.logger.debug({ sub: decodedToken.sub, email: userEmail });
|
|
42
66
|
if (user) {
|
|
43
67
|
res.locals.user = user;
|
|
44
68
|
res.locals.email = user?.email;
|
|
@@ -58,5 +82,6 @@ let AuthMiddleware = AuthMiddleware_1 = class AuthMiddleware {
|
|
|
58
82
|
exports.AuthMiddleware = AuthMiddleware;
|
|
59
83
|
exports.AuthMiddleware = AuthMiddleware = AuthMiddleware_1 = __decorate([
|
|
60
84
|
(0, common_1.Injectable)(),
|
|
61
|
-
__metadata("design:paramtypes", [auth0_service_1.Auth0Service
|
|
85
|
+
__metadata("design:paramtypes", [auth0_service_1.Auth0Service,
|
|
86
|
+
auth_interface_1.UserResolver])
|
|
62
87
|
], AuthMiddleware);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidUUID = isValidUUID;
|
|
4
|
+
exports.resolveUserId = resolveUserId;
|
|
5
|
+
exports.resolveUserIds = resolveUserIds;
|
|
6
|
+
const typeorm_1 = require("typeorm");
|
|
7
|
+
const clhq_postgres_module_1 = require("clhq-postgres-module");
|
|
8
|
+
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
9
|
+
let dataSource = null;
|
|
10
|
+
async function getDataSource() {
|
|
11
|
+
if (dataSource && dataSource.isInitialized) {
|
|
12
|
+
return dataSource;
|
|
13
|
+
}
|
|
14
|
+
const databaseUrl = process.env.PG_SESSION_POOLER_URL;
|
|
15
|
+
if (!databaseUrl) {
|
|
16
|
+
throw new Error('PG_SESSION_POOLER_URL environment variable is not set');
|
|
17
|
+
}
|
|
18
|
+
dataSource = new typeorm_1.DataSource({
|
|
19
|
+
type: 'postgres',
|
|
20
|
+
url: databaseUrl,
|
|
21
|
+
entities: [clhq_postgres_module_1.User],
|
|
22
|
+
synchronize: false,
|
|
23
|
+
ssl: { rejectUnauthorized: false },
|
|
24
|
+
});
|
|
25
|
+
await dataSource.initialize();
|
|
26
|
+
return dataSource;
|
|
27
|
+
}
|
|
28
|
+
function isValidUUID(value) {
|
|
29
|
+
return UUID_REGEX.test(value);
|
|
30
|
+
}
|
|
31
|
+
async function resolveUserId(userIdOrAuth0Id) {
|
|
32
|
+
if (isValidUUID(userIdOrAuth0Id)) {
|
|
33
|
+
return userIdOrAuth0Id;
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const ds = await getDataSource();
|
|
37
|
+
const userRepository = ds.getRepository(clhq_postgres_module_1.User);
|
|
38
|
+
const user = await userRepository.findOne({
|
|
39
|
+
where: { auth0UserId: userIdOrAuth0Id },
|
|
40
|
+
});
|
|
41
|
+
return user?.id || null;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error(`Error resolving user ID for ${userIdOrAuth0Id}:`, error);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async function resolveUserIds(userIds) {
|
|
49
|
+
return Promise.all(userIds.map(resolveUserId));
|
|
50
|
+
}
|