@c-rex/core 0.1.12 → 0.1.14
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/{api/token.d.mts → OIDC.d.mts} +7 -4
- package/dist/{api/token.d.ts → OIDC.d.ts} +7 -4
- package/dist/OIDC.js +578 -0
- package/dist/OIDC.js.map +1 -0
- package/dist/OIDC.mjs +542 -0
- package/dist/OIDC.mjs.map +1 -0
- package/dist/logger.js +356 -9
- package/dist/logger.js.map +1 -1
- package/dist/logger.mjs +356 -9
- package/dist/logger.mjs.map +1 -1
- package/dist/requests.d.mts +44 -0
- package/dist/requests.d.ts +44 -0
- package/dist/requests.js +670 -0
- package/dist/requests.js.map +1 -0
- package/dist/requests.mjs +635 -0
- package/dist/requests.mjs.map +1 -0
- package/dist/sdk.d.mts +25 -0
- package/dist/sdk.d.ts +25 -0
- package/dist/sdk.js +389 -0
- package/dist/sdk.js.map +1 -0
- package/dist/sdk.mjs +362 -0
- package/dist/sdk.mjs.map +1 -0
- package/package.json +16 -22
- package/dist/api/cookies.d.mts +0 -17
- package/dist/api/cookies.d.ts +0 -17
- package/dist/api/cookies.js +0 -66
- package/dist/api/cookies.js.map +0 -1
- package/dist/api/cookies.mjs +0 -40
- package/dist/api/cookies.mjs.map +0 -1
- package/dist/api/rpc.d.mts +0 -16
- package/dist/api/rpc.d.ts +0 -16
- package/dist/api/rpc.js +0 -231
- package/dist/api/rpc.js.map +0 -1
- package/dist/api/rpc.mjs +0 -194
- package/dist/api/rpc.mjs.map +0 -1
- package/dist/api/token.js +0 -88
- package/dist/api/token.js.map +0 -1
- package/dist/api/token.mjs +0 -63
- package/dist/api/token.mjs.map +0 -1
- package/dist/index.d.mts +0 -81
- package/dist/index.d.ts +0 -81
- package/dist/index.js +0 -418
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -377
- package/dist/index.mjs.map +0 -1
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { NextResponse } from 'next/server';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Retrieves an access token using client credentials flow from the configured OIDC provider
|
|
5
3
|
*
|
|
6
4
|
* @returns NextResponse with success status or error message
|
|
7
5
|
* @throws Error if token retrieval fails
|
|
8
6
|
*/
|
|
9
|
-
declare const
|
|
7
|
+
declare const getToken: () => Promise<{
|
|
8
|
+
token: string;
|
|
9
|
+
expiresAt: number;
|
|
10
|
+
error?: string;
|
|
11
|
+
}>;
|
|
12
|
+
declare const getIssuerMetadata: () => Promise<any>;
|
|
10
13
|
|
|
11
|
-
export {
|
|
14
|
+
export { getIssuerMetadata, getToken };
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { NextResponse } from 'next/server';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Retrieves an access token using client credentials flow from the configured OIDC provider
|
|
5
3
|
*
|
|
6
4
|
* @returns NextResponse with success status or error message
|
|
7
5
|
* @throws Error if token retrieval fails
|
|
8
6
|
*/
|
|
9
|
-
declare const
|
|
7
|
+
declare const getToken: () => Promise<{
|
|
8
|
+
token: string;
|
|
9
|
+
expiresAt: number;
|
|
10
|
+
error?: string;
|
|
11
|
+
}>;
|
|
12
|
+
declare const getIssuerMetadata: () => Promise<any>;
|
|
10
13
|
|
|
11
|
-
export {
|
|
14
|
+
export { getIssuerMetadata, getToken };
|
package/dist/OIDC.js
ADDED
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/OIDC.ts
|
|
31
|
+
var OIDC_exports = {};
|
|
32
|
+
__export(OIDC_exports, {
|
|
33
|
+
getIssuerMetadata: () => getIssuerMetadata,
|
|
34
|
+
getToken: () => getToken
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(OIDC_exports);
|
|
37
|
+
var import_openid_client = require("openid-client");
|
|
38
|
+
|
|
39
|
+
// src/logger.ts
|
|
40
|
+
var import_winston = __toESM(require("winston"));
|
|
41
|
+
|
|
42
|
+
// src/transports/matomo.ts
|
|
43
|
+
var import_winston_transport = __toESM(require("winston-transport"));
|
|
44
|
+
|
|
45
|
+
// ../constants/src/index.ts
|
|
46
|
+
var ALL = "*";
|
|
47
|
+
var LOG_LEVELS = {
|
|
48
|
+
critical: 2,
|
|
49
|
+
error: 3,
|
|
50
|
+
warning: 4,
|
|
51
|
+
info: 6,
|
|
52
|
+
debug: 7
|
|
53
|
+
};
|
|
54
|
+
var SDK_CONFIG_KEY = "crex-sdk-config";
|
|
55
|
+
var DEFAULT_COOKIE_LIMIT = 30 * 24 * 60 * 60 * 1e3;
|
|
56
|
+
|
|
57
|
+
// src/transports/matomo.ts
|
|
58
|
+
var MatomoTransport = class extends import_winston_transport.default {
|
|
59
|
+
matomoTransport;
|
|
60
|
+
configs;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a new instance of MatomoTransport.
|
|
63
|
+
*
|
|
64
|
+
* @param configs - The application configuration containing logging settings
|
|
65
|
+
*/
|
|
66
|
+
constructor(configs) {
|
|
67
|
+
super({
|
|
68
|
+
level: configs.logs.matomo.minimumLevel,
|
|
69
|
+
silent: configs.logs.matomo.silent
|
|
70
|
+
});
|
|
71
|
+
this.matomoTransport = new import_winston_transport.default();
|
|
72
|
+
this.configs = configs;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Logs a message to Matomo if the message category is included in the configured categories.
|
|
76
|
+
*
|
|
77
|
+
* @param info - The log information including level, message, and category
|
|
78
|
+
* @param callback - Callback function to execute after logging
|
|
79
|
+
*/
|
|
80
|
+
log(info, callback) {
|
|
81
|
+
const matomoCategory = this.configs.logs.matomo.categoriesLevel;
|
|
82
|
+
if (matomoCategory.includes(info.category) || matomoCategory.includes(ALL)) {
|
|
83
|
+
this.matomoTransport.log(info, callback);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/transports/graylog.ts
|
|
89
|
+
var import_winston_transport2 = __toESM(require("winston-transport"));
|
|
90
|
+
var import_winston_graylog2 = __toESM(require("winston-graylog2"));
|
|
91
|
+
var GraylogTransport = class extends import_winston_transport2.default {
|
|
92
|
+
graylogTransport;
|
|
93
|
+
configs;
|
|
94
|
+
/**
|
|
95
|
+
* Creates a new instance of GraylogTransport.
|
|
96
|
+
*
|
|
97
|
+
* @param configs - The application configuration containing logging settings
|
|
98
|
+
*/
|
|
99
|
+
constructor(configs) {
|
|
100
|
+
if (!configs.logs.graylog.hostname || configs.logs.graylog.port === void 0) {
|
|
101
|
+
throw new Error("Graylog hostname and port must be defined");
|
|
102
|
+
}
|
|
103
|
+
super({
|
|
104
|
+
level: configs.logs.graylog.minimumLevel,
|
|
105
|
+
silent: configs.logs.graylog.silent
|
|
106
|
+
});
|
|
107
|
+
this.configs = configs;
|
|
108
|
+
this.graylogTransport = new import_winston_graylog2.default({
|
|
109
|
+
name: configs.logs.graylog.app,
|
|
110
|
+
silent: configs.logs.graylog.silent,
|
|
111
|
+
handleExceptions: false,
|
|
112
|
+
graylog: {
|
|
113
|
+
servers: [
|
|
114
|
+
{ host: configs.logs.graylog.hostname, port: configs.logs.graylog.port }
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Logs a message to Graylog if the message category is included in the configured categories.
|
|
121
|
+
*
|
|
122
|
+
* @param info - The log information including level, message, and category
|
|
123
|
+
* @param callback - Callback function to execute after logging
|
|
124
|
+
*/
|
|
125
|
+
log(info, callback) {
|
|
126
|
+
const graylogCategory = this.configs.logs.graylog.categoriesLevel;
|
|
127
|
+
if (graylogCategory.includes(info.category) || graylogCategory.includes(ALL)) {
|
|
128
|
+
this.graylogTransport.log(info, callback);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// src/utils.ts
|
|
134
|
+
var formatIssuer = (issuer) => {
|
|
135
|
+
let newIssuer = issuer;
|
|
136
|
+
const lastChar = newIssuer.charAt(newIssuer.length - 1);
|
|
137
|
+
if (lastChar !== "/") {
|
|
138
|
+
newIssuer += "/";
|
|
139
|
+
}
|
|
140
|
+
newIssuer += ".well-known/openid-configuration";
|
|
141
|
+
return newIssuer;
|
|
142
|
+
};
|
|
143
|
+
var formatApiUrl = (apiUrl) => {
|
|
144
|
+
let newApiUrl = apiUrl;
|
|
145
|
+
const lastChar = newApiUrl.charAt(newApiUrl.length - 1);
|
|
146
|
+
if (lastChar !== "/") {
|
|
147
|
+
newApiUrl += "/";
|
|
148
|
+
}
|
|
149
|
+
newApiUrl += "iirds/v1/";
|
|
150
|
+
return newApiUrl;
|
|
151
|
+
};
|
|
152
|
+
var mergeConfigs = (defaultConfig, envVars) => {
|
|
153
|
+
const definedEnvVars = {};
|
|
154
|
+
for (const key in envVars) {
|
|
155
|
+
if (envVars[key] !== void 0) {
|
|
156
|
+
definedEnvVars[key] = envVars[key];
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
...defaultConfig,
|
|
161
|
+
...definedEnvVars
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
var generateWarnings = (vars) => {
|
|
165
|
+
const warnings = [];
|
|
166
|
+
vars.forEach((variable) => {
|
|
167
|
+
const value = process.env[variable.key];
|
|
168
|
+
if (value === void 0) {
|
|
169
|
+
warnings.push(`Missing environment variable ${variable.key}, using default value: ${variable.default}`);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
return warnings;
|
|
173
|
+
};
|
|
174
|
+
var createUserOIDCConfig = () => {
|
|
175
|
+
const warnings = [];
|
|
176
|
+
const userDefaults = {
|
|
177
|
+
secret: "dummy",
|
|
178
|
+
scope: "openid profile crex.ids.api crex.ids.api.public",
|
|
179
|
+
enabled: true
|
|
180
|
+
};
|
|
181
|
+
const userEnvs = {
|
|
182
|
+
id: process.env.CREX_IDS_ID,
|
|
183
|
+
secret: process.env.CREX_IDS_SECRET,
|
|
184
|
+
scope: process.env.CREX_IDS_USER_SCOPES,
|
|
185
|
+
issuer: process.env.CREX_IDS_ISSUER == void 0 ? void 0 : formatIssuer(process.env.CREX_IDS_ISSUER),
|
|
186
|
+
enabled: process.env.CREX_IDS_USER_LOGIN_ENABLE == void 0 ? void 0 : process.env.CREX_IDS_USER_LOGIN_ENABLE == "true"
|
|
187
|
+
};
|
|
188
|
+
const user = mergeConfigs(userDefaults, userEnvs);
|
|
189
|
+
if (user.enabled) {
|
|
190
|
+
const disableOIDCVars = [
|
|
191
|
+
{ key: "CREX_IDS_ID", value: process.env.CREX_IDS_ID },
|
|
192
|
+
{ key: "CREX_IDS_ISSUER", value: process.env.CREX_IDS_ISSUER }
|
|
193
|
+
];
|
|
194
|
+
disableOIDCVars.forEach((variable) => {
|
|
195
|
+
if (variable.value === void 0) {
|
|
196
|
+
user.enabled = false;
|
|
197
|
+
warnings.push(`Missing environment variable ${variable.key}, disabling client and login`);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
if (user.enabled) {
|
|
202
|
+
const userWarningVars = [
|
|
203
|
+
{ key: "CREX_IDS_USER_SCOPES", default: user.scope },
|
|
204
|
+
{ key: "CREX_IDS_SECRET", default: user.secret },
|
|
205
|
+
{ key: "CREX_IDS_USER_LOGIN_ENABLE", default: user.enabled }
|
|
206
|
+
];
|
|
207
|
+
const aux = generateWarnings(userWarningVars);
|
|
208
|
+
warnings.push(...aux);
|
|
209
|
+
}
|
|
210
|
+
return { user, warnings };
|
|
211
|
+
};
|
|
212
|
+
var createClientOIDCConfig = () => {
|
|
213
|
+
const warnings = [];
|
|
214
|
+
const clientDefault = {
|
|
215
|
+
secret: "dummy",
|
|
216
|
+
enabled: true
|
|
217
|
+
};
|
|
218
|
+
const clientEnvs = {
|
|
219
|
+
id: process.env.CREX_IDS_ID,
|
|
220
|
+
issuer: process.env.CREX_IDS_ISSUER == void 0 ? void 0 : formatIssuer(process.env.CREX_IDS_ISSUER),
|
|
221
|
+
secret: process.env.CREX_IDS_SECRET,
|
|
222
|
+
enabled: process.env.CREX_IDS_CLIENT_LOGIN_ENABLE == void 0 ? void 0 : process.env.CREX_IDS_CLIENT_LOGIN_ENABLE == "true"
|
|
223
|
+
};
|
|
224
|
+
const client = mergeConfigs(clientDefault, clientEnvs);
|
|
225
|
+
if (client.enabled) {
|
|
226
|
+
const disableOIDCVars = [
|
|
227
|
+
{ key: "CREX_IDS_ID", value: process.env.CREX_IDS_ID },
|
|
228
|
+
{ key: "CREX_IDS_ISSUER", value: process.env.CREX_IDS_ISSUER }
|
|
229
|
+
];
|
|
230
|
+
disableOIDCVars.forEach((variable) => {
|
|
231
|
+
if (variable.value === void 0) {
|
|
232
|
+
client.enabled = false;
|
|
233
|
+
warnings.push(`Missing environment variable ${variable.key}, disabling client and login`);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
if (client.enabled) {
|
|
238
|
+
const clientWarningVars = [
|
|
239
|
+
{ key: "CREX_IDS_SECRET", default: client.secret },
|
|
240
|
+
{ key: "CREX_IDS_CLIENT_LOGIN_ENABLE", default: client.enabled }
|
|
241
|
+
];
|
|
242
|
+
const aux = generateWarnings(clientWarningVars);
|
|
243
|
+
warnings.push(...aux);
|
|
244
|
+
}
|
|
245
|
+
return { client, warnings };
|
|
246
|
+
};
|
|
247
|
+
var createConsoleLoggerConfig = () => {
|
|
248
|
+
const warnings = [];
|
|
249
|
+
const consoleLoggerDefaults = {
|
|
250
|
+
minimumLevel: "info",
|
|
251
|
+
silent: false
|
|
252
|
+
};
|
|
253
|
+
const consoleLoggerEnvs = {
|
|
254
|
+
minimumLevel: process.env.LOG_CONSOLE_LEVEL,
|
|
255
|
+
silent: process.env.LOG_CONSOLE_SILENT == void 0 ? void 0 : process.env.LOG_CONSOLE_SILENT == "true"
|
|
256
|
+
};
|
|
257
|
+
const consoleLogger = mergeConfigs(consoleLoggerDefaults, consoleLoggerEnvs);
|
|
258
|
+
if (consoleLogger.silent == false) {
|
|
259
|
+
const consoleWarningsVars = [
|
|
260
|
+
{ key: "LOG_CONSOLE_SILENT", default: consoleLogger.silent },
|
|
261
|
+
{ key: "LOG_CONSOLE_LEVEL", default: consoleLogger.minimumLevel }
|
|
262
|
+
];
|
|
263
|
+
const aux = generateWarnings(consoleWarningsVars);
|
|
264
|
+
warnings.push(...aux);
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
consoleLogger,
|
|
268
|
+
warnings
|
|
269
|
+
};
|
|
270
|
+
};
|
|
271
|
+
var createGraylogLoggerConfig = () => {
|
|
272
|
+
const warnings = [];
|
|
273
|
+
const graylogDefaults = {
|
|
274
|
+
app: "app name not set",
|
|
275
|
+
minimumLevel: "info",
|
|
276
|
+
silent: false,
|
|
277
|
+
hostname: "https://log.c-rex.net",
|
|
278
|
+
port: 12202,
|
|
279
|
+
categoriesLevel: ["NoLicense", "Scenario", "Document", "Search", "Notification", "History", "UserProfile"]
|
|
280
|
+
};
|
|
281
|
+
const graylogEnvs = {
|
|
282
|
+
app: process.env.LOG_GRAYLOG_APP_NAME,
|
|
283
|
+
silent: process.env.LOG_GRAYLOG_SILENT == void 0 ? void 0 : process.env.LOG_GRAYLOG_SILENT == "true",
|
|
284
|
+
hostname: process.env.LOG_GRAYLOG_HOSTNAME,
|
|
285
|
+
port: process.env.LOG_GRAYLOG_PORT == void 0 ? void 0 : Number(process.env.LOG_GRAYLOG_PORT),
|
|
286
|
+
minimumLevel: process.env.LOG_GRAYLOG_LEVEL,
|
|
287
|
+
categoriesLevel: process.env.LOG_GRAYLOG_CATEGORIES == void 0 ? void 0 : process.env.LOG_GRAYLOG_CATEGORIES.split(",")
|
|
288
|
+
};
|
|
289
|
+
const graylog = mergeConfigs(graylogDefaults, graylogEnvs);
|
|
290
|
+
if (graylog.silent == false) {
|
|
291
|
+
const graylogWarningVars = [
|
|
292
|
+
{ key: "LOG_GRAYLOG_APP_NAME", default: graylog.app },
|
|
293
|
+
{ key: "LOG_GRAYLOG_HOSTNAME", default: graylog.hostname },
|
|
294
|
+
{ key: "LOG_GRAYLOG_LEVEL", default: graylog.minimumLevel },
|
|
295
|
+
{ key: "LOG_GRAYLOG_SILENT", default: graylog.silent },
|
|
296
|
+
{ key: "LOG_GRAYLOG_CATEGORIES", default: graylog.categoriesLevel },
|
|
297
|
+
{ key: "LOG_GRAYLOG_PORT", default: graylog.port }
|
|
298
|
+
];
|
|
299
|
+
const aux = generateWarnings(graylogWarningVars);
|
|
300
|
+
warnings.push(...aux);
|
|
301
|
+
}
|
|
302
|
+
return {
|
|
303
|
+
graylog,
|
|
304
|
+
warnings
|
|
305
|
+
};
|
|
306
|
+
};
|
|
307
|
+
var createMatomoLoggerConfig = () => {
|
|
308
|
+
const warnings = [];
|
|
309
|
+
const matomoDefaults = {
|
|
310
|
+
app: "NextJsProjectName",
|
|
311
|
+
minimumLevel: "info",
|
|
312
|
+
silent: true,
|
|
313
|
+
hostname: "",
|
|
314
|
+
port: 0,
|
|
315
|
+
categoriesLevel: ["NoLicense", "Scenario", "Document", "Search", "Notification", "History", "UserProfile"]
|
|
316
|
+
};
|
|
317
|
+
const matomoEnvs = {
|
|
318
|
+
app: process.env.LOG_MATOMO_APP_NAME,
|
|
319
|
+
silent: process.env.LOG_MATOMO_SILENT == void 0 ? void 0 : process.env.LOG_MATOMO_SILENT == "true",
|
|
320
|
+
hostname: process.env.LOG_MATOMO_HOSTNAME,
|
|
321
|
+
port: process.env.LOG_MATOMO_PORT == void 0 ? void 0 : Number(process.env.LOG_MATOMO_PORT),
|
|
322
|
+
minimumLevel: process.env.LOG_MATOMO_LEVEL,
|
|
323
|
+
categoriesLevel: process.env.LOG_MATOMO_CATEGORIES == void 0 ? void 0 : process.env.LOG_MATOMO_CATEGORIES.split(",")
|
|
324
|
+
};
|
|
325
|
+
const matomo = mergeConfigs(matomoDefaults, matomoEnvs);
|
|
326
|
+
const matomoSilentVars = [
|
|
327
|
+
{ key: "LOG_MATOMO_SILENT", value: process.env.LOG_MATOMO_SILENT },
|
|
328
|
+
{ key: "LOG_MATOMO_HOSTNAME", value: process.env.LOG_MATOMO_HOSTNAME },
|
|
329
|
+
{ key: "LOG_MATOMO_PORT", value: process.env.LOG_MATOMO_PORT }
|
|
330
|
+
];
|
|
331
|
+
matomoSilentVars.forEach((variable) => {
|
|
332
|
+
if (variable.value === void 0) {
|
|
333
|
+
matomo.silent = true;
|
|
334
|
+
warnings.push(`Missing environment variable ${variable.key}, setting matomo logger to silent`);
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
if (matomo.silent == false) {
|
|
338
|
+
const matomoWarningVars = [
|
|
339
|
+
{ key: "LOG_MATOMO_APP_NAME", default: matomo.app },
|
|
340
|
+
{ key: "LOG_MATOMO_LEVEL", default: matomo.minimumLevel },
|
|
341
|
+
{ key: "LOG_MATOMO_CATEGORIES", default: matomo.categoriesLevel }
|
|
342
|
+
];
|
|
343
|
+
const aux = generateWarnings(matomoWarningVars);
|
|
344
|
+
warnings.push(...aux);
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
matomo,
|
|
348
|
+
warnings
|
|
349
|
+
};
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
// src/sdk.ts
|
|
353
|
+
var import_headers = require("next/headers");
|
|
354
|
+
var CrexSDK = class {
|
|
355
|
+
userAuthConfig;
|
|
356
|
+
customerConfig;
|
|
357
|
+
cookiesConfig;
|
|
358
|
+
async getUserAuthConfig() {
|
|
359
|
+
if (this.userAuthConfig) {
|
|
360
|
+
return this.userAuthConfig;
|
|
361
|
+
}
|
|
362
|
+
if (!this.customerConfig) {
|
|
363
|
+
this.customerConfig = this.getServerConfig();
|
|
364
|
+
}
|
|
365
|
+
const user = this.customerConfig.OIDC.user;
|
|
366
|
+
const userInfoEndPoint = this.customerConfig.OIDC.issuerMetadata?.userinfo_endpoint;
|
|
367
|
+
if (user.enabled) {
|
|
368
|
+
this.userAuthConfig = {
|
|
369
|
+
providers: [
|
|
370
|
+
{
|
|
371
|
+
id: "crex",
|
|
372
|
+
name: "CREX",
|
|
373
|
+
type: "oauth",
|
|
374
|
+
version: "2.0",
|
|
375
|
+
clientId: user.id,
|
|
376
|
+
wellKnown: user.issuer,
|
|
377
|
+
clientSecret: user.secret,
|
|
378
|
+
authorization: {
|
|
379
|
+
params: {
|
|
380
|
+
scope: user.scope,
|
|
381
|
+
prompt: "login"
|
|
382
|
+
}
|
|
383
|
+
},
|
|
384
|
+
idToken: true,
|
|
385
|
+
checks: ["pkce", "state"],
|
|
386
|
+
async profile(_, tokens) {
|
|
387
|
+
const res = await fetch(userInfoEndPoint, {
|
|
388
|
+
headers: {
|
|
389
|
+
Authorization: `Bearer ${tokens.access_token}`
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
const userinfo = await res.json();
|
|
393
|
+
return {
|
|
394
|
+
id: userinfo.sub,
|
|
395
|
+
name: userinfo.name,
|
|
396
|
+
email: userinfo.email
|
|
397
|
+
};
|
|
398
|
+
},
|
|
399
|
+
callbacks: {
|
|
400
|
+
async jwt({ token, account }) {
|
|
401
|
+
if (account) {
|
|
402
|
+
token.id_token = account.id_token;
|
|
403
|
+
}
|
|
404
|
+
return token;
|
|
405
|
+
},
|
|
406
|
+
async session({ session, token }) {
|
|
407
|
+
session.id_token = token.id_token;
|
|
408
|
+
return session;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
]
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
;
|
|
416
|
+
return this.userAuthConfig;
|
|
417
|
+
}
|
|
418
|
+
createCustomerConfig(CUSTOMER_CONFIG, shouldLog) {
|
|
419
|
+
const requiredEnvVars = ["CREX_API_URL", "NEXT_PUBLIC_API_URL"];
|
|
420
|
+
const required = requiredEnvVars.map((key) => {
|
|
421
|
+
const value = process.env[key];
|
|
422
|
+
if (value === void 0) {
|
|
423
|
+
return `Missing required environment variable: ${key}`;
|
|
424
|
+
}
|
|
425
|
+
return "";
|
|
426
|
+
}).filter((item) => item.length > 0);
|
|
427
|
+
const { user, warnings: userWarnings } = createUserOIDCConfig();
|
|
428
|
+
const { client, warnings: clientWarnings } = createClientOIDCConfig();
|
|
429
|
+
const { matomo, warnings: matomoWarnings } = createMatomoLoggerConfig();
|
|
430
|
+
const { graylog, warnings: graylogWarnings } = createGraylogLoggerConfig();
|
|
431
|
+
const { consoleLogger, warnings: consoleWarnings } = createConsoleLoggerConfig();
|
|
432
|
+
const warnings = required.concat(
|
|
433
|
+
consoleWarnings,
|
|
434
|
+
clientWarnings,
|
|
435
|
+
userWarnings,
|
|
436
|
+
graylogWarnings,
|
|
437
|
+
matomoWarnings
|
|
438
|
+
);
|
|
439
|
+
if (shouldLog) {
|
|
440
|
+
if (warnings.length > 0) console.warn(warnings.join("\n"));
|
|
441
|
+
}
|
|
442
|
+
const cookiesConfig = {
|
|
443
|
+
publicNextApiUrl: process.env.NEXT_PUBLIC_API_URL,
|
|
444
|
+
...CUSTOMER_CONFIG,
|
|
445
|
+
OIDC: {
|
|
446
|
+
clientEnabled: client.enabled,
|
|
447
|
+
userEnabled: user.enabled
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
const config = {
|
|
451
|
+
baseUrl: process.env.CREX_API_URL == void 0 ? "" : formatApiUrl(process.env.CREX_API_URL),
|
|
452
|
+
OIDC: { client, user },
|
|
453
|
+
logs: { console: consoleLogger, graylog, matomo },
|
|
454
|
+
...CUSTOMER_CONFIG
|
|
455
|
+
};
|
|
456
|
+
return { cookiesConfig, config };
|
|
457
|
+
}
|
|
458
|
+
getClientConfig = () => {
|
|
459
|
+
const jsonConfigs = (0, import_headers.cookies)().get(SDK_CONFIG_KEY)?.value;
|
|
460
|
+
if (!jsonConfigs) {
|
|
461
|
+
throw new Error("Configs not found");
|
|
462
|
+
}
|
|
463
|
+
const configs = JSON.parse(jsonConfigs);
|
|
464
|
+
return configs;
|
|
465
|
+
};
|
|
466
|
+
getServerConfig() {
|
|
467
|
+
if (!global.__GLOBAL_CONFIG__) {
|
|
468
|
+
throw new Error("Server config not initialized");
|
|
469
|
+
}
|
|
470
|
+
return global.__GLOBAL_CONFIG__;
|
|
471
|
+
}
|
|
472
|
+
initializeConfig(config) {
|
|
473
|
+
if (global.__GLOBAL_CONFIG__) return global.__GLOBAL_CONFIG__;
|
|
474
|
+
global.__GLOBAL_CONFIG__ = config;
|
|
475
|
+
return global.__GLOBAL_CONFIG__;
|
|
476
|
+
}
|
|
477
|
+
updateConfigProp(key, value) {
|
|
478
|
+
if (!global.__GLOBAL_CONFIG__) {
|
|
479
|
+
throw new Error("Server config not initialized");
|
|
480
|
+
}
|
|
481
|
+
global.__GLOBAL_CONFIG__[key] = value;
|
|
482
|
+
return global.__GLOBAL_CONFIG__;
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
// src/logger.ts
|
|
487
|
+
var CrexLogger = class {
|
|
488
|
+
customerConfig;
|
|
489
|
+
logger;
|
|
490
|
+
/**
|
|
491
|
+
* Initializes the logger instance if it hasn't been initialized yet.
|
|
492
|
+
* Loads customer configuration and creates the logger with appropriate transports.
|
|
493
|
+
*
|
|
494
|
+
* @private
|
|
495
|
+
*/
|
|
496
|
+
async initLogger() {
|
|
497
|
+
try {
|
|
498
|
+
if (!this.customerConfig) {
|
|
499
|
+
const sdk = new CrexSDK();
|
|
500
|
+
this.customerConfig = sdk.getServerConfig();
|
|
501
|
+
}
|
|
502
|
+
if (!this.logger) {
|
|
503
|
+
this.logger = this.createLogger();
|
|
504
|
+
}
|
|
505
|
+
} catch (error) {
|
|
506
|
+
console.log("Error initializing logger:", error);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Logs a message with the specified level and optional category.
|
|
511
|
+
*
|
|
512
|
+
* @param options - Logging options
|
|
513
|
+
* @param options.level - The log level (error, warn, info, etc.)
|
|
514
|
+
* @param options.message - The message to log
|
|
515
|
+
* @param options.category - Optional category for the log message
|
|
516
|
+
*/
|
|
517
|
+
async log({ level, message, category }) {
|
|
518
|
+
await this.initLogger();
|
|
519
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
520
|
+
const newMessage = `[${timestamp}] ${message}`;
|
|
521
|
+
this.logger.log(level, newMessage, category);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Creates a new Winston logger instance with configured transports.
|
|
525
|
+
*
|
|
526
|
+
* @private
|
|
527
|
+
* @returns A configured Winston logger instance
|
|
528
|
+
*/
|
|
529
|
+
createLogger() {
|
|
530
|
+
return import_winston.default.createLogger({
|
|
531
|
+
levels: LOG_LEVELS,
|
|
532
|
+
transports: [
|
|
533
|
+
new import_winston.default.transports.Console({
|
|
534
|
+
level: this.customerConfig.logs.console.minimumLevel,
|
|
535
|
+
silent: this.customerConfig.logs.console.silent
|
|
536
|
+
}),
|
|
537
|
+
new MatomoTransport(this.customerConfig),
|
|
538
|
+
new GraylogTransport(this.customerConfig)
|
|
539
|
+
]
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
// src/OIDC.ts
|
|
545
|
+
var getToken = async () => {
|
|
546
|
+
try {
|
|
547
|
+
const sdk = new CrexSDK();
|
|
548
|
+
const config = sdk.getServerConfig();
|
|
549
|
+
const issuer = await import_openid_client.Issuer.discover(config.OIDC.client.issuer);
|
|
550
|
+
const client = new issuer.Client({
|
|
551
|
+
client_id: config.OIDC.client.id,
|
|
552
|
+
client_secret: config.OIDC.client.secret
|
|
553
|
+
});
|
|
554
|
+
const tokenSet = await client.grant({ grant_type: "client_credentials" });
|
|
555
|
+
const token = tokenSet.access_token;
|
|
556
|
+
const expiresAt = tokenSet.expires_at;
|
|
557
|
+
return { token, expiresAt };
|
|
558
|
+
} catch (error) {
|
|
559
|
+
const logger = new CrexLogger();
|
|
560
|
+
logger.log({
|
|
561
|
+
level: "error",
|
|
562
|
+
message: `getToken error: ${error}`
|
|
563
|
+
});
|
|
564
|
+
return { token: "", expiresAt: 0, error: JSON.stringify(error) };
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
var getIssuerMetadata = async () => {
|
|
568
|
+
const sdk = new CrexSDK();
|
|
569
|
+
const config = sdk.getServerConfig();
|
|
570
|
+
const issuer = await import_openid_client.Issuer.discover(config.OIDC.client.issuer);
|
|
571
|
+
return issuer.metadata;
|
|
572
|
+
};
|
|
573
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
574
|
+
0 && (module.exports = {
|
|
575
|
+
getIssuerMetadata,
|
|
576
|
+
getToken
|
|
577
|
+
});
|
|
578
|
+
//# sourceMappingURL=OIDC.js.map
|