@sustaina/iam-middleware 1.0.1
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 +61 -0
- package/dist/auth/AuthMiddleware.d.ts +51 -0
- package/dist/auth/AuthMiddleware.d.ts.map +1 -0
- package/dist/auth/AuthMiddleware.js +312 -0
- package/dist/auth/AuthMiddleware.js.map +1 -0
- package/dist/auth/ImplementModeMiddleware.d.ts +26 -0
- package/dist/auth/ImplementModeMiddleware.d.ts.map +1 -0
- package/dist/auth/ImplementModeMiddleware.js +77 -0
- package/dist/auth/ImplementModeMiddleware.js.map +1 -0
- package/dist/base/BaseMiddleware.d.ts +15 -0
- package/dist/base/BaseMiddleware.d.ts.map +1 -0
- package/dist/base/BaseMiddleware.js +50 -0
- package/dist/base/BaseMiddleware.js.map +1 -0
- package/dist/domain/entities/AggregateRoot.d.ts +17 -0
- package/dist/domain/entities/AggregateRoot.d.ts.map +1 -0
- package/dist/domain/entities/AggregateRoot.js +79 -0
- package/dist/domain/entities/AggregateRoot.js.map +1 -0
- package/dist/domain/entities/Currency.d.ts +26 -0
- package/dist/domain/entities/Currency.d.ts.map +1 -0
- package/dist/domain/entities/Currency.js +51 -0
- package/dist/domain/entities/Currency.js.map +1 -0
- package/dist/domain/entities/OutboxEvent.d.ts +17 -0
- package/dist/domain/entities/OutboxEvent.d.ts.map +1 -0
- package/dist/domain/entities/OutboxEvent.js +24 -0
- package/dist/domain/entities/OutboxEvent.js.map +1 -0
- package/dist/domain/event/DomainEvent.d.ts +38 -0
- package/dist/domain/event/DomainEvent.d.ts.map +1 -0
- package/dist/domain/event/DomainEvent.js +20 -0
- package/dist/domain/event/DomainEvent.js.map +1 -0
- package/dist/domain/repositories/OutboxRepository.d.ts +7 -0
- package/dist/domain/repositories/OutboxRepository.d.ts.map +1 -0
- package/dist/domain/repositories/OutboxRepository.js +3 -0
- package/dist/domain/repositories/OutboxRepository.js.map +1 -0
- package/dist/errors/index.d.ts +71 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +150 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/infrastructure/RedisClient.d.ts +14 -0
- package/dist/infrastructure/RedisClient.d.ts.map +1 -0
- package/dist/infrastructure/RedisClient.js +69 -0
- package/dist/infrastructure/RedisClient.js.map +1 -0
- package/dist/logging/EventLogMiddleware.d.ts +4 -0
- package/dist/logging/EventLogMiddleware.d.ts.map +1 -0
- package/dist/logging/EventLogMiddleware.js +49 -0
- package/dist/logging/EventLogMiddleware.js.map +1 -0
- package/dist/shared/config.d.ts +2 -0
- package/dist/shared/config.d.ts.map +1 -0
- package/dist/shared/config.js +5 -0
- package/dist/shared/config.js.map +1 -0
- package/dist/test.d.ts +4 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +80 -0
- package/dist/test.js.map +1 -0
- package/dist/types/AuthTypes.d.ts +43 -0
- package/dist/types/AuthTypes.d.ts.map +1 -0
- package/dist/types/AuthTypes.js +3 -0
- package/dist/types/AuthTypes.js.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RedisClient = void 0;
|
|
7
|
+
const redis_1 = require("redis");
|
|
8
|
+
const pino_1 = __importDefault(require("pino"));
|
|
9
|
+
class RedisClient {
|
|
10
|
+
static async getInstance() {
|
|
11
|
+
if (!RedisClient.instance) {
|
|
12
|
+
await RedisClient.createInstance();
|
|
13
|
+
}
|
|
14
|
+
return RedisClient.instance;
|
|
15
|
+
}
|
|
16
|
+
static async duplicate() {
|
|
17
|
+
if (!RedisClient.instance) {
|
|
18
|
+
await RedisClient.createInstance();
|
|
19
|
+
}
|
|
20
|
+
const dupInstance = RedisClient.instance.duplicate();
|
|
21
|
+
await dupInstance.connect();
|
|
22
|
+
return dupInstance;
|
|
23
|
+
}
|
|
24
|
+
static async createInstance() {
|
|
25
|
+
const redisUrl = process.env.IAM_REDIS_URL || "redis://localhost:6379";
|
|
26
|
+
RedisClient.instance = (0, redis_1.createClient)({
|
|
27
|
+
url: redisUrl,
|
|
28
|
+
socket: process.env.NODE_ENV !== "local" ? { tls: true, rejectUnauthorized: false } : undefined,
|
|
29
|
+
});
|
|
30
|
+
RedisClient.instance.on("error", (err) => {
|
|
31
|
+
RedisClient.logger.error(err, "Redis client error");
|
|
32
|
+
});
|
|
33
|
+
RedisClient.instance.on("connect", () => {
|
|
34
|
+
RedisClient.logger.info("Redis client connected");
|
|
35
|
+
});
|
|
36
|
+
RedisClient.instance.on("disconnect", () => {
|
|
37
|
+
RedisClient.logger.info("Redis client disconnected");
|
|
38
|
+
});
|
|
39
|
+
await RedisClient.instance.connect();
|
|
40
|
+
}
|
|
41
|
+
static async disconnect() {
|
|
42
|
+
if (RedisClient.instance) {
|
|
43
|
+
await RedisClient.instance.disconnect();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
static async set(key, value, ttlSeconds) {
|
|
47
|
+
const client = await RedisClient.getInstance();
|
|
48
|
+
if (ttlSeconds) {
|
|
49
|
+
await client.setEx(key, ttlSeconds, value);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
await client.set(key, value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
static async get(key) {
|
|
56
|
+
const client = await RedisClient.getInstance();
|
|
57
|
+
return client.get(key);
|
|
58
|
+
}
|
|
59
|
+
static async del(key) {
|
|
60
|
+
const client = await RedisClient.getInstance();
|
|
61
|
+
await client.del(key);
|
|
62
|
+
}
|
|
63
|
+
static generateKey(prefix, ...parts) {
|
|
64
|
+
return `${prefix}:${parts.join(":")}`;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.RedisClient = RedisClient;
|
|
68
|
+
RedisClient.logger = (0, pino_1.default)({ name: "IAM-middleware:RedisClient" });
|
|
69
|
+
//# sourceMappingURL=RedisClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RedisClient.js","sourceRoot":"","sources":["../../src/infrastructure/RedisClient.ts"],"names":[],"mappings":";;;;;;AAAA,iCAAsD;AACtD,gDAAwB;AAExB,MAAa,WAAW;IAItB,MAAM,CAAC,KAAK,CAAC,WAAW;QACtB,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;YAC1B,MAAM,WAAW,CAAC,cAAc,EAAE,CAAC;QACrC,CAAC;QACD,OAAO,WAAW,CAAC,QAAQ,CAAC;IAC9B,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,SAAS;QACpB,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;YAC1B,MAAM,WAAW,CAAC,cAAc,EAAE,CAAC;QACrC,CAAC;QACD,MAAM,WAAW,GAAG,WAAW,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;QACrD,MAAM,WAAW,CAAC,OAAO,EAAE,CAAC;QAC5B,OAAO,WAAW,CAAC;IACrB,CAAC;IAEO,MAAM,CAAC,KAAK,CAAC,cAAc;QACjC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,IAAI,wBAAwB,CAAC;QAEvE,WAAW,CAAC,QAAQ,GAAG,IAAA,oBAAY,EAAC;YAClC,GAAG,EAAE,QAAQ;YACb,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,kBAAkB,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS;SAChG,CAAC,CAAC;QAEH,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EA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,MAAM,GAAG,MAAM,WAAW,CAAC,WAAW,EAAE,CAAC;QAC/C,MAAM,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACxB,CAAC;IAED,MAAM,CAAC,WAAW,CAAC,MAAc,EAAE,GAAG,KAAe;QACnD,OAAO,GAAG,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;IACxC,CAAC;;AAtEH,kCAuEC;AArEgB,kBAAM,GAAG,IAAA,cAAI,EAAC,EAAE,IAAI,EAAE,4BAA4B,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { FastifyRequest, FastifyReply } from "fastify";
|
|
2
|
+
import { OutboxRepository } from "../domain/repositories/OutboxRepository";
|
|
3
|
+
export declare function createEventLogMiddleware(outboxRepository: OutboxRepository): (req: FastifyRequest, res: FastifyReply) => Promise<void>;
|
|
4
|
+
//# sourceMappingURL=EventLogMiddleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventLogMiddleware.d.ts","sourceRoot":"","sources":["../../src/logging/EventLogMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yCAAyC,CAAC;AAE3E,wBAAgB,wBAAwB,CAAC,gBAAgB,EAAE,gBAAgB,IAC5B,KAAK,cAAc,EAAE,KAAK,YAAY,KAAG,OAAO,CAAC,IAAI,CAAC,CA2DpG"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createEventLogMiddleware = createEventLogMiddleware;
|
|
4
|
+
const api_1 = require("@opentelemetry/api");
|
|
5
|
+
const OutboxEvent_1 = require("../domain/entities/OutboxEvent");
|
|
6
|
+
function createEventLogMiddleware(outboxRepository) {
|
|
7
|
+
return async function SaveEventLogMiddleware(req, res) {
|
|
8
|
+
const activeSpan = api_1.trace.getActiveSpan();
|
|
9
|
+
if (!activeSpan) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
const spanContext = activeSpan.spanContext();
|
|
13
|
+
const reqHeaders = req.headers;
|
|
14
|
+
const resHeaders = res.getHeaders();
|
|
15
|
+
const filteredHeaders = Object.fromEntries(Object.entries(reqHeaders)
|
|
16
|
+
.filter(([key, value]) => !["authorization", "cookie"].includes(key.toLowerCase()) && value !== undefined)
|
|
17
|
+
.map(([key, value]) => [key, value]));
|
|
18
|
+
const filteredHeadersRes = Object.fromEntries(Object.entries(resHeaders)
|
|
19
|
+
.filter(([key, value]) => !["authorization", "cookie"].includes(key.toLowerCase()) && value !== undefined)
|
|
20
|
+
.map(([key, value]) => [key, value]));
|
|
21
|
+
const metadata = {
|
|
22
|
+
traceId: spanContext.traceId,
|
|
23
|
+
spanId: spanContext.spanId,
|
|
24
|
+
tenantId: req.tenantId ?? "",
|
|
25
|
+
correlationId: req.correlationId ?? "",
|
|
26
|
+
remoteIp: req.ip ?? "",
|
|
27
|
+
request: {
|
|
28
|
+
headers: filteredHeaders,
|
|
29
|
+
method: req.method,
|
|
30
|
+
url: req.url,
|
|
31
|
+
body: req.body || undefined,
|
|
32
|
+
endpoint: req.routeOptions?.url,
|
|
33
|
+
query: req.query,
|
|
34
|
+
params: req.params,
|
|
35
|
+
},
|
|
36
|
+
response: {
|
|
37
|
+
statusCode: res.statusCode,
|
|
38
|
+
headers: filteredHeadersRes,
|
|
39
|
+
endpoint: req.routeOptions?.url,
|
|
40
|
+
responseTime: res.elapsedTime,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
await outboxRepository.save(OutboxEvent_1.OutboxEvent.create("event.logs", {
|
|
44
|
+
tenantId: req.tenantId ?? "",
|
|
45
|
+
user: req.user ?? "",
|
|
46
|
+
}, metadata));
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
//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AAY,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/shared/config.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG,gBAAgB,CAAC"}
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
declare function createApp(): Promise<import("fastify").FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>>;
|
|
3
|
+
export { createApp };
|
|
4
|
+
//# sourceMappingURL=test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAC;AAavB,iBAAe,SAAS,qUA4CvB;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
package/dist/test.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createApp = createApp;
|
|
7
|
+
require("dotenv/config");
|
|
8
|
+
const fastify_1 = __importDefault(require("fastify"));
|
|
9
|
+
const pino_1 = __importDefault(require("pino"));
|
|
10
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
11
|
+
const AuthMiddleware_1 = require("./auth/AuthMiddleware");
|
|
12
|
+
const ImplementModeMiddleware_1 = require("./auth/ImplementModeMiddleware");
|
|
13
|
+
const redis_1 = require("redis");
|
|
14
|
+
const logger = (0, pino_1.default)({
|
|
15
|
+
level: process.env.LOG_LEVEL || "info",
|
|
16
|
+
transport: { target: "pino-pretty", options: { colorize: true } },
|
|
17
|
+
});
|
|
18
|
+
async function createApp() {
|
|
19
|
+
const app = (0, fastify_1.default)({
|
|
20
|
+
logger: {
|
|
21
|
+
level: process.env.LOG_LEVEL || "info",
|
|
22
|
+
transport: { target: "pino-pretty", options: { colorize: true } },
|
|
23
|
+
},
|
|
24
|
+
genReqId: () => node_crypto_1.default.randomUUID(),
|
|
25
|
+
});
|
|
26
|
+
const redisClient = (0, redis_1.createClient)({
|
|
27
|
+
url: process.env.IAM_REDIS_URL,
|
|
28
|
+
socket: { tls: true, rejectUnauthorized: false },
|
|
29
|
+
});
|
|
30
|
+
const authMiddleware = new AuthMiddleware_1.AuthMiddleware({ redisClient, jwtSecret: process.env.IAM_JWT_SECRET });
|
|
31
|
+
const implementMode = new ImplementModeMiddleware_1.ImplementModeMiddleware({ redisClient });
|
|
32
|
+
// or through env
|
|
33
|
+
// const authMiddleware = new AuthMiddleware();
|
|
34
|
+
// const implementMode = new ImplementModeMiddleware();
|
|
35
|
+
// Register standardized routes
|
|
36
|
+
await app.register(async (appInstance) => {
|
|
37
|
+
appInstance.get("/test", {
|
|
38
|
+
preHandler: [
|
|
39
|
+
authMiddleware.authenticate.bind(authMiddleware), // required for implementMode.authorise and authMiddleware.permissionGuard
|
|
40
|
+
implementMode.authorise.bind(implementMode),
|
|
41
|
+
authMiddleware.permissionGuard([{ access: "canRead", subProgram: "test" }]).bind(authMiddleware),
|
|
42
|
+
],
|
|
43
|
+
}, async (request, reply) => {
|
|
44
|
+
return reply.send({
|
|
45
|
+
success: true,
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}, { prefix: "/api/v1" });
|
|
49
|
+
return app;
|
|
50
|
+
}
|
|
51
|
+
async function start() {
|
|
52
|
+
try {
|
|
53
|
+
const app = await createApp();
|
|
54
|
+
const port = Number(process.env.PORT) || 3000;
|
|
55
|
+
const host = "0.0.0.0";
|
|
56
|
+
await app.listen({ port, host });
|
|
57
|
+
logger.info(`Server listening on http://${host}:${port}`);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
logger.error(error, "Error starting server");
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function shutdown() {
|
|
65
|
+
logger.info("Shutting down server...");
|
|
66
|
+
try {
|
|
67
|
+
logger.info("Server shut down successfully");
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
logger.error(error, "Error during shutdown");
|
|
71
|
+
}
|
|
72
|
+
process.exit(0);
|
|
73
|
+
}
|
|
74
|
+
// Handle graceful shutdown
|
|
75
|
+
process.on("SIGINT", shutdown);
|
|
76
|
+
process.on("SIGTERM", shutdown);
|
|
77
|
+
if (require.main === module) {
|
|
78
|
+
start();
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=test.js.map
|
package/dist/test.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;;;;AA2DS,8BAAS;AA3DlB,yBAAuB;AACvB,sDAA8B;AAC9B,gDAAwB;AACxB,8DAAiC;AACjC,0DAAuD;AACvD,4EAAyE;AACzE,iCAAqC;AAErC,MAAM,MAAM,GAAG,IAAA,cAAI,EAAC;IAClB,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,MAAM;IACtC,SAAS,EAAE,EAAE,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;CAClE,CAAC,CAAC;AAEH,KAAK,UAAU,SAAS;IACtB,MAAM,GAAG,GAAG,IAAA,iBAAO,EAAC;QAClB,MAAM,EAAE;YACN,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,MAAM;YACtC,SAAS,EAAE,EAAE,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;SACl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SAAS,EAAE,CAAC;QAC9B,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC;QAC9C,MAAM,IAAI,GAAG,SAAS,CAAC;QAEvB,MAAM,GAAG,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QACjC,MAAM,CAAC,IAAI,CAAC,8BAA8B,IAAI,IAAI,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,uBAAuB,CAAC,CAAC;QAC7C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,KAAK,UAAU,QAAQ;IACrB,MAAM,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;IACvC,IAAI,CAAC;QACH,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;IAC/C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,uBAAuB,CAAC,CAAC;IAC/C,CAAC;IACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC;AAED,2BAA2B;AAC3B,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AAC/B,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;AAEhC,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;IAC5B,KAAK,EAAE,CAAC;AACV,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createClient } from "redis";
|
|
2
|
+
export interface JwtPayload {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
email: string;
|
|
6
|
+
tenantId: string;
|
|
7
|
+
type: string;
|
|
8
|
+
tenantLocale: string;
|
|
9
|
+
passwordPolicy: string;
|
|
10
|
+
isDenyPasswordChange?: boolean;
|
|
11
|
+
isPasswordSendEmail?: boolean;
|
|
12
|
+
username?: string;
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
export interface UserInfo {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
email: string;
|
|
19
|
+
type: string;
|
|
20
|
+
}
|
|
21
|
+
export type BaseOptions = ({
|
|
22
|
+
redisUrl?: string;
|
|
23
|
+
} | {
|
|
24
|
+
redisClient?: ReturnType<typeof createClient>;
|
|
25
|
+
}) & {
|
|
26
|
+
serviceName?: string;
|
|
27
|
+
};
|
|
28
|
+
export type AuthMiddlewareOptions = BaseOptions & {
|
|
29
|
+
jwtSecret?: string;
|
|
30
|
+
};
|
|
31
|
+
export type ImplementModeMiddlewareOptions = BaseOptions;
|
|
32
|
+
export interface CheckPermissionParams {
|
|
33
|
+
userId: string;
|
|
34
|
+
subProgram: string;
|
|
35
|
+
access: "canCreate" | "canRead" | "canEdit" | "canDelete" | "canNotify" | "canCreateDraft";
|
|
36
|
+
}
|
|
37
|
+
export type PermissionGuardUnit = {
|
|
38
|
+
subProgram: string;
|
|
39
|
+
access: CheckPermissionParams["access"];
|
|
40
|
+
};
|
|
41
|
+
export type PermissionGuardArray = PermissionGuardUnit[];
|
|
42
|
+
export type PermissionGuardParams = PermissionGuardUnit | PermissionGuardArray;
|
|
43
|
+
//# sourceMappingURL=AuthTypes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthTypes.d.ts","sourceRoot":"","sources":["../../src/types/AuthTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,IA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ourceRoot":"","sources":["../../src/types/AuthTypes.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sustaina/iam-middleware",
|
|
3
|
+
"author": "Sustaina Team",
|
|
4
|
+
"license": "UNLICENSED",
|
|
5
|
+
"version": "1.0.1",
|
|
6
|
+
"description": "",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"package.json",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "rimraf dist && tsc",
|
|
19
|
+
"dev": "nodemon src/test.ts",
|
|
20
|
+
"deploy:npm": "npm run build && semantic-release",
|
|
21
|
+
"lint": "eslint src/**/*.ts",
|
|
22
|
+
"lint:fix": "eslint src/**/*.ts --fix",
|
|
23
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
24
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
25
|
+
"prepare": "husky"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"@sustaina",
|
|
29
|
+
"tcc"
|
|
30
|
+
],
|
|
31
|
+
"type": "commonjs",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@semantic-release/git": "^10.0.1",
|
|
34
|
+
"@semantic-release/gitlab": "^13.2.9",
|
|
35
|
+
"@semantic-release/npm": "^12.0.2",
|
|
36
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
37
|
+
"@types/node": "^24.10.1",
|
|
38
|
+
"@types/redis": "^4.0.10",
|
|
39
|
+
"eslint": "^9.39.1",
|
|
40
|
+
"eslint-config-prettier": "^10.1.8",
|
|
41
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
42
|
+
"husky": "^9.1.7",
|
|
43
|
+
"nodemon": "^3.1.11",
|
|
44
|
+
"pino-pretty": "^13.1.3",
|
|
45
|
+
"prettier": "^3.7.4",
|
|
46
|
+
"rimraf": "^6.1.2",
|
|
47
|
+
"semantic-release": "^24.2.9",
|
|
48
|
+
"ts-node": "^10.9.2",
|
|
49
|
+
"typescript": "^5.9.3",
|
|
50
|
+
"typescript-eslint": "^8.48.1"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"fastify": "^5.4.0"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@opentelemetry/api": "^1.9.0",
|
|
57
|
+
"dotenv": "^17.2.3",
|
|
58
|
+
"jsonwebtoken": "^9.0.2",
|
|
59
|
+
"pino": "^10.1.0",
|
|
60
|
+
"redis": "^5.10.0"
|
|
61
|
+
}
|
|
62
|
+
}
|