@objectstack/plugin-auth 3.2.7 → 3.2.8
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/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +8 -0
- package/dist/index.d.mts +38 -22
- package/dist/index.d.ts +38 -22
- package/dist/index.js +159 -139
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +159 -139
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
- package/src/auth-plugin.ts +24 -1
package/dist/index.mjs
CHANGED
|
@@ -510,145 +510,6 @@ var AuthManager = class {
|
|
|
510
510
|
}
|
|
511
511
|
};
|
|
512
512
|
|
|
513
|
-
// src/auth-plugin.ts
|
|
514
|
-
var AuthPlugin = class {
|
|
515
|
-
constructor(options = {}) {
|
|
516
|
-
this.name = "com.objectstack.auth";
|
|
517
|
-
this.type = "standard";
|
|
518
|
-
this.version = "1.0.0";
|
|
519
|
-
this.dependencies = [];
|
|
520
|
-
this.authManager = null;
|
|
521
|
-
this.options = {
|
|
522
|
-
registerRoutes: true,
|
|
523
|
-
basePath: "/api/v1/auth",
|
|
524
|
-
...options
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
async init(ctx) {
|
|
528
|
-
ctx.logger.info("Initializing Auth Plugin...");
|
|
529
|
-
if (!this.options.secret) {
|
|
530
|
-
throw new Error("AuthPlugin: secret is required");
|
|
531
|
-
}
|
|
532
|
-
const dataEngine = ctx.getService("data");
|
|
533
|
-
if (!dataEngine) {
|
|
534
|
-
ctx.logger.warn("No data engine service found - auth will use in-memory storage");
|
|
535
|
-
}
|
|
536
|
-
this.authManager = new AuthManager({
|
|
537
|
-
...this.options,
|
|
538
|
-
dataEngine
|
|
539
|
-
});
|
|
540
|
-
ctx.registerService("auth", this.authManager);
|
|
541
|
-
ctx.logger.info("Auth Plugin initialized successfully");
|
|
542
|
-
}
|
|
543
|
-
async start(ctx) {
|
|
544
|
-
ctx.logger.info("Starting Auth Plugin...");
|
|
545
|
-
if (!this.authManager) {
|
|
546
|
-
throw new Error("Auth manager not initialized");
|
|
547
|
-
}
|
|
548
|
-
if (this.options.registerRoutes) {
|
|
549
|
-
ctx.hook("kernel:ready", async () => {
|
|
550
|
-
let httpServer = null;
|
|
551
|
-
try {
|
|
552
|
-
httpServer = ctx.getService("http-server");
|
|
553
|
-
} catch {
|
|
554
|
-
}
|
|
555
|
-
if (httpServer) {
|
|
556
|
-
const serverWithPort = httpServer;
|
|
557
|
-
if (this.authManager && typeof serverWithPort.getPort === "function") {
|
|
558
|
-
const actualPort = serverWithPort.getPort();
|
|
559
|
-
if (actualPort) {
|
|
560
|
-
const configuredUrl = this.options.baseUrl || "http://localhost:3000";
|
|
561
|
-
const configuredOrigin = new URL(configuredUrl).origin;
|
|
562
|
-
const actualUrl = `http://localhost:${actualPort}`;
|
|
563
|
-
if (configuredOrigin !== actualUrl) {
|
|
564
|
-
this.authManager.setRuntimeBaseUrl(actualUrl);
|
|
565
|
-
ctx.logger.info(
|
|
566
|
-
`Auth baseUrl auto-updated to ${actualUrl} (configured: ${configuredUrl})`
|
|
567
|
-
);
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
this.registerAuthRoutes(httpServer, ctx);
|
|
572
|
-
ctx.logger.info(`Auth routes registered at ${this.options.basePath}`);
|
|
573
|
-
} else {
|
|
574
|
-
ctx.logger.warn(
|
|
575
|
-
"No HTTP server available \u2014 auth routes not registered. Auth service is still available for MSW/mock environments via HttpDispatcher."
|
|
576
|
-
);
|
|
577
|
-
}
|
|
578
|
-
});
|
|
579
|
-
}
|
|
580
|
-
try {
|
|
581
|
-
const ql = ctx.getService("objectql");
|
|
582
|
-
if (ql && typeof ql.registerMiddleware === "function") {
|
|
583
|
-
ql.registerMiddleware(async (opCtx, next) => {
|
|
584
|
-
if (opCtx.context?.userId || opCtx.context?.isSystem) {
|
|
585
|
-
return next();
|
|
586
|
-
}
|
|
587
|
-
await next();
|
|
588
|
-
});
|
|
589
|
-
ctx.logger.info("Auth middleware registered on ObjectQL engine");
|
|
590
|
-
}
|
|
591
|
-
} catch (_e) {
|
|
592
|
-
ctx.logger.debug("ObjectQL engine not available, skipping auth middleware registration");
|
|
593
|
-
}
|
|
594
|
-
ctx.logger.info("Auth Plugin started successfully");
|
|
595
|
-
}
|
|
596
|
-
async destroy() {
|
|
597
|
-
this.authManager = null;
|
|
598
|
-
}
|
|
599
|
-
/**
|
|
600
|
-
* Register authentication routes with HTTP server
|
|
601
|
-
*
|
|
602
|
-
* Uses better-auth's universal handler for all authentication requests.
|
|
603
|
-
* This forwards all requests under basePath to better-auth, which handles:
|
|
604
|
-
* - Email/password authentication
|
|
605
|
-
* - OAuth providers (Google, GitHub, etc.)
|
|
606
|
-
* - Session management
|
|
607
|
-
* - Password reset
|
|
608
|
-
* - Email verification
|
|
609
|
-
* - 2FA, passkeys, magic links (if enabled)
|
|
610
|
-
*/
|
|
611
|
-
registerAuthRoutes(httpServer, ctx) {
|
|
612
|
-
if (!this.authManager) return;
|
|
613
|
-
const basePath = this.options.basePath || "/api/v1/auth";
|
|
614
|
-
if (!("getRawApp" in httpServer) || typeof httpServer.getRawApp !== "function") {
|
|
615
|
-
ctx.logger.error("HTTP server does not support getRawApp() - wildcard routing requires Hono server");
|
|
616
|
-
throw new Error(
|
|
617
|
-
"AuthPlugin requires HonoServerPlugin for wildcard routing support. Please ensure HonoServerPlugin is loaded before AuthPlugin."
|
|
618
|
-
);
|
|
619
|
-
}
|
|
620
|
-
const rawApp = httpServer.getRawApp();
|
|
621
|
-
rawApp.all(`${basePath}/*`, async (c) => {
|
|
622
|
-
try {
|
|
623
|
-
const response = await this.authManager.handleRequest(c.req.raw);
|
|
624
|
-
if (response.status >= 500) {
|
|
625
|
-
try {
|
|
626
|
-
const body = await response.clone().text();
|
|
627
|
-
ctx.logger.error("[AuthPlugin] better-auth returned server error", new Error(`HTTP ${response.status}: ${body}`));
|
|
628
|
-
} catch {
|
|
629
|
-
ctx.logger.error("[AuthPlugin] better-auth returned server error", new Error(`HTTP ${response.status}: (unable to read body)`));
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
return response;
|
|
633
|
-
} catch (error) {
|
|
634
|
-
const err = error instanceof Error ? error : new Error(String(error));
|
|
635
|
-
ctx.logger.error("Auth request error:", err);
|
|
636
|
-
return new Response(
|
|
637
|
-
JSON.stringify({
|
|
638
|
-
success: false,
|
|
639
|
-
error: err.message
|
|
640
|
-
}),
|
|
641
|
-
{
|
|
642
|
-
status: 500,
|
|
643
|
-
headers: { "Content-Type": "application/json" }
|
|
644
|
-
}
|
|
645
|
-
);
|
|
646
|
-
}
|
|
647
|
-
});
|
|
648
|
-
ctx.logger.info(`Auth routes registered: All requests under ${basePath}/* forwarded to better-auth`);
|
|
649
|
-
}
|
|
650
|
-
};
|
|
651
|
-
|
|
652
513
|
// src/objects/sys-user.object.ts
|
|
653
514
|
import { ObjectSchema, Field } from "@objectstack/spec/data";
|
|
654
515
|
var SysUser = ObjectSchema.create({
|
|
@@ -1358,6 +1219,165 @@ var SysTwoFactor = ObjectSchema11.create({
|
|
|
1358
1219
|
mru: false
|
|
1359
1220
|
}
|
|
1360
1221
|
});
|
|
1222
|
+
|
|
1223
|
+
// src/auth-plugin.ts
|
|
1224
|
+
var AuthPlugin = class {
|
|
1225
|
+
constructor(options = {}) {
|
|
1226
|
+
this.name = "com.objectstack.auth";
|
|
1227
|
+
this.type = "standard";
|
|
1228
|
+
this.version = "1.0.0";
|
|
1229
|
+
this.dependencies = [];
|
|
1230
|
+
this.authManager = null;
|
|
1231
|
+
this.options = {
|
|
1232
|
+
registerRoutes: true,
|
|
1233
|
+
basePath: "/api/v1/auth",
|
|
1234
|
+
...options
|
|
1235
|
+
};
|
|
1236
|
+
}
|
|
1237
|
+
async init(ctx) {
|
|
1238
|
+
ctx.logger.info("Initializing Auth Plugin...");
|
|
1239
|
+
if (!this.options.secret) {
|
|
1240
|
+
throw new Error("AuthPlugin: secret is required");
|
|
1241
|
+
}
|
|
1242
|
+
const dataEngine = ctx.getService("data");
|
|
1243
|
+
if (!dataEngine) {
|
|
1244
|
+
ctx.logger.warn("No data engine service found - auth will use in-memory storage");
|
|
1245
|
+
}
|
|
1246
|
+
this.authManager = new AuthManager({
|
|
1247
|
+
...this.options,
|
|
1248
|
+
dataEngine
|
|
1249
|
+
});
|
|
1250
|
+
ctx.registerService("auth", this.authManager);
|
|
1251
|
+
ctx.registerService("app.com.objectstack.system", {
|
|
1252
|
+
id: "com.objectstack.system",
|
|
1253
|
+
name: "System",
|
|
1254
|
+
version: "1.0.0",
|
|
1255
|
+
type: "plugin",
|
|
1256
|
+
namespace: "sys",
|
|
1257
|
+
objects: [
|
|
1258
|
+
SysUser,
|
|
1259
|
+
SysSession,
|
|
1260
|
+
SysAccount,
|
|
1261
|
+
SysVerification,
|
|
1262
|
+
SysOrganization,
|
|
1263
|
+
SysMember,
|
|
1264
|
+
SysInvitation,
|
|
1265
|
+
SysTeam,
|
|
1266
|
+
SysTeamMember,
|
|
1267
|
+
SysApiKey,
|
|
1268
|
+
SysTwoFactor
|
|
1269
|
+
]
|
|
1270
|
+
});
|
|
1271
|
+
ctx.logger.info("Auth Plugin initialized successfully");
|
|
1272
|
+
}
|
|
1273
|
+
async start(ctx) {
|
|
1274
|
+
ctx.logger.info("Starting Auth Plugin...");
|
|
1275
|
+
if (!this.authManager) {
|
|
1276
|
+
throw new Error("Auth manager not initialized");
|
|
1277
|
+
}
|
|
1278
|
+
if (this.options.registerRoutes) {
|
|
1279
|
+
ctx.hook("kernel:ready", async () => {
|
|
1280
|
+
let httpServer = null;
|
|
1281
|
+
try {
|
|
1282
|
+
httpServer = ctx.getService("http-server");
|
|
1283
|
+
} catch {
|
|
1284
|
+
}
|
|
1285
|
+
if (httpServer) {
|
|
1286
|
+
const serverWithPort = httpServer;
|
|
1287
|
+
if (this.authManager && typeof serverWithPort.getPort === "function") {
|
|
1288
|
+
const actualPort = serverWithPort.getPort();
|
|
1289
|
+
if (actualPort) {
|
|
1290
|
+
const configuredUrl = this.options.baseUrl || "http://localhost:3000";
|
|
1291
|
+
const configuredOrigin = new URL(configuredUrl).origin;
|
|
1292
|
+
const actualUrl = `http://localhost:${actualPort}`;
|
|
1293
|
+
if (configuredOrigin !== actualUrl) {
|
|
1294
|
+
this.authManager.setRuntimeBaseUrl(actualUrl);
|
|
1295
|
+
ctx.logger.info(
|
|
1296
|
+
`Auth baseUrl auto-updated to ${actualUrl} (configured: ${configuredUrl})`
|
|
1297
|
+
);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
this.registerAuthRoutes(httpServer, ctx);
|
|
1302
|
+
ctx.logger.info(`Auth routes registered at ${this.options.basePath}`);
|
|
1303
|
+
} else {
|
|
1304
|
+
ctx.logger.warn(
|
|
1305
|
+
"No HTTP server available \u2014 auth routes not registered. Auth service is still available for MSW/mock environments via HttpDispatcher."
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
});
|
|
1309
|
+
}
|
|
1310
|
+
try {
|
|
1311
|
+
const ql = ctx.getService("objectql");
|
|
1312
|
+
if (ql && typeof ql.registerMiddleware === "function") {
|
|
1313
|
+
ql.registerMiddleware(async (opCtx, next) => {
|
|
1314
|
+
if (opCtx.context?.userId || opCtx.context?.isSystem) {
|
|
1315
|
+
return next();
|
|
1316
|
+
}
|
|
1317
|
+
await next();
|
|
1318
|
+
});
|
|
1319
|
+
ctx.logger.info("Auth middleware registered on ObjectQL engine");
|
|
1320
|
+
}
|
|
1321
|
+
} catch (_e) {
|
|
1322
|
+
ctx.logger.debug("ObjectQL engine not available, skipping auth middleware registration");
|
|
1323
|
+
}
|
|
1324
|
+
ctx.logger.info("Auth Plugin started successfully");
|
|
1325
|
+
}
|
|
1326
|
+
async destroy() {
|
|
1327
|
+
this.authManager = null;
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Register authentication routes with HTTP server
|
|
1331
|
+
*
|
|
1332
|
+
* Uses better-auth's universal handler for all authentication requests.
|
|
1333
|
+
* This forwards all requests under basePath to better-auth, which handles:
|
|
1334
|
+
* - Email/password authentication
|
|
1335
|
+
* - OAuth providers (Google, GitHub, etc.)
|
|
1336
|
+
* - Session management
|
|
1337
|
+
* - Password reset
|
|
1338
|
+
* - Email verification
|
|
1339
|
+
* - 2FA, passkeys, magic links (if enabled)
|
|
1340
|
+
*/
|
|
1341
|
+
registerAuthRoutes(httpServer, ctx) {
|
|
1342
|
+
if (!this.authManager) return;
|
|
1343
|
+
const basePath = this.options.basePath || "/api/v1/auth";
|
|
1344
|
+
if (!("getRawApp" in httpServer) || typeof httpServer.getRawApp !== "function") {
|
|
1345
|
+
ctx.logger.error("HTTP server does not support getRawApp() - wildcard routing requires Hono server");
|
|
1346
|
+
throw new Error(
|
|
1347
|
+
"AuthPlugin requires HonoServerPlugin for wildcard routing support. Please ensure HonoServerPlugin is loaded before AuthPlugin."
|
|
1348
|
+
);
|
|
1349
|
+
}
|
|
1350
|
+
const rawApp = httpServer.getRawApp();
|
|
1351
|
+
rawApp.all(`${basePath}/*`, async (c) => {
|
|
1352
|
+
try {
|
|
1353
|
+
const response = await this.authManager.handleRequest(c.req.raw);
|
|
1354
|
+
if (response.status >= 500) {
|
|
1355
|
+
try {
|
|
1356
|
+
const body = await response.clone().text();
|
|
1357
|
+
ctx.logger.error("[AuthPlugin] better-auth returned server error", new Error(`HTTP ${response.status}: ${body}`));
|
|
1358
|
+
} catch {
|
|
1359
|
+
ctx.logger.error("[AuthPlugin] better-auth returned server error", new Error(`HTTP ${response.status}: (unable to read body)`));
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
return response;
|
|
1363
|
+
} catch (error) {
|
|
1364
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
1365
|
+
ctx.logger.error("Auth request error:", err);
|
|
1366
|
+
return new Response(
|
|
1367
|
+
JSON.stringify({
|
|
1368
|
+
success: false,
|
|
1369
|
+
error: err.message
|
|
1370
|
+
}),
|
|
1371
|
+
{
|
|
1372
|
+
status: 500,
|
|
1373
|
+
headers: { "Content-Type": "application/json" }
|
|
1374
|
+
}
|
|
1375
|
+
);
|
|
1376
|
+
}
|
|
1377
|
+
});
|
|
1378
|
+
ctx.logger.info(`Auth routes registered: All requests under ${basePath}/* forwarded to better-auth`);
|
|
1379
|
+
}
|
|
1380
|
+
};
|
|
1361
1381
|
export {
|
|
1362
1382
|
AUTH_ACCOUNT_CONFIG,
|
|
1363
1383
|
AUTH_INVITATION_SCHEMA,
|