@solidxai/core 0.1.17 → 0.1.18-beta.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/CHANGELOG.md +1345 -0
- package/CLAUDE.md +26 -0
- package/dist/constants/error-messages.d.ts +2 -0
- package/dist/constants/error-messages.d.ts.map +1 -1
- package/dist/constants/error-messages.js +2 -0
- package/dist/constants/error-messages.js.map +1 -1
- package/dist/decorators/login-extension-provider.decorator.d.ts +3 -0
- package/dist/decorators/login-extension-provider.decorator.d.ts.map +1 -0
- package/dist/decorators/login-extension-provider.decorator.js +8 -0
- package/dist/decorators/login-extension-provider.decorator.js.map +1 -0
- package/dist/helpers/field-crud-managers/SelectionStaticFieldCrudManager.d.ts +2 -0
- package/dist/helpers/field-crud-managers/SelectionStaticFieldCrudManager.d.ts.map +1 -1
- package/dist/helpers/field-crud-managers/SelectionStaticFieldCrudManager.js +31 -7
- package/dist/helpers/field-crud-managers/SelectionStaticFieldCrudManager.js.map +1 -1
- package/dist/helpers/solid-core-error-codes-provider.service.d.ts.map +1 -1
- package/dist/helpers/solid-core-error-codes-provider.service.js +18 -0
- package/dist/helpers/solid-core-error-codes-provider.service.js.map +1 -1
- package/dist/helpers/solid-registry.d.ts +5 -1
- package/dist/helpers/solid-registry.d.ts.map +1 -1
- package/dist/helpers/solid-registry.js +16 -0
- package/dist/helpers/solid-registry.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +24 -0
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/interfaces.js +6 -1
- package/dist/interfaces.js.map +1 -1
- package/dist/services/authentication.service.d.ts +11 -1
- package/dist/services/authentication.service.d.ts.map +1 -1
- package/dist/services/authentication.service.js +70 -4
- package/dist/services/authentication.service.js.map +1 -1
- package/dist/services/crud.service.d.ts.map +1 -1
- package/dist/services/crud.service.js +7 -3
- package/dist/services/crud.service.js.map +1 -1
- package/dist/services/solid-introspect.service.d.ts +1 -0
- package/dist/services/solid-introspect.service.d.ts.map +1 -1
- package/dist/services/solid-introspect.service.js +11 -0
- package/dist/services/solid-introspect.service.js.map +1 -1
- package/dist/subscribers/audit.subscriber.d.ts.map +1 -1
- package/dist/subscribers/audit.subscriber.js +11 -6
- package/dist/subscribers/audit.subscriber.js.map +1 -1
- package/package.json +1 -1
- package/src/constants/error-messages.ts +2 -0
- package/src/decorators/login-extension-provider.decorator.ts +6 -0
- package/src/helpers/field-crud-managers/SelectionStaticFieldCrudManager.ts +32 -7
- package/src/helpers/solid-core-error-codes-provider.service.ts +21 -0
- package/src/helpers/solid-registry.ts +19 -1
- package/src/index.ts +2 -0
- package/src/interfaces.ts +93 -0
- package/src/services/authentication.service.ts +135 -3
- package/src/services/crud.service.ts +11 -3
- package/src/services/solid-introspect.service.ts +13 -0
- package/src/subscribers/audit.subscriber.ts +15 -8
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
InternalServerErrorException,
|
|
9
9
|
Logger,
|
|
10
10
|
NotFoundException,
|
|
11
|
+
ServiceUnavailableException,
|
|
11
12
|
UnauthorizedException,
|
|
12
13
|
} from "@nestjs/common";
|
|
13
14
|
import { EventEmitter2 } from "@nestjs/event-emitter";
|
|
@@ -38,8 +39,15 @@ import { SignInDto } from "../dtos/sign-in.dto";
|
|
|
38
39
|
import { SignUpDto } from "../dtos/sign-up.dto";
|
|
39
40
|
import { SignupIntent } from "../enums/signup-intent.enum";
|
|
40
41
|
import { User } from "../entities/user.entity";
|
|
41
|
-
import {
|
|
42
|
+
import {
|
|
43
|
+
EventDetails,
|
|
44
|
+
EventType,
|
|
45
|
+
ILoginExtensionContext,
|
|
46
|
+
ILoginExtensionProvider,
|
|
47
|
+
LoginExtensionChannel,
|
|
48
|
+
} from "../interfaces";
|
|
42
49
|
import { ActiveUserData } from "../interfaces/active-user-data.interface";
|
|
50
|
+
import { SolidRegistry } from "../helpers/solid-registry";
|
|
43
51
|
import { ActiveSessionStorageService } from "./active-session-storage.service";
|
|
44
52
|
import { AccessTokenDenylistService } from "./access-token-denylist.service";
|
|
45
53
|
import { HashingService } from "./hashing.service";
|
|
@@ -121,6 +129,19 @@ const SIGNUP_POLICY: Record<
|
|
|
121
129
|
},
|
|
122
130
|
};
|
|
123
131
|
|
|
132
|
+
/** Deadline for a single login extension provider, unless it overrides it. */
|
|
133
|
+
const DEFAULT_LOGIN_EXTENSION_TIMEOUT_MS = 5000;
|
|
134
|
+
|
|
135
|
+
/** A registered login extension provider denied this attempt. */
|
|
136
|
+
export class LoginExtensionDeniedException extends UnauthorizedException {}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* A login extension provider malfunctioned or timed out while its failure
|
|
140
|
+
* policy was fail-closed. 503, not 401: the credential was fine, the
|
|
141
|
+
* dependency was not, and the client should retry.
|
|
142
|
+
*/
|
|
143
|
+
export class LoginExtensionUnavailableException extends ServiceUnavailableException {}
|
|
144
|
+
|
|
124
145
|
@Injectable()
|
|
125
146
|
export class AuthenticationService {
|
|
126
147
|
private readonly logger = new Logger(AuthenticationService.name);
|
|
@@ -146,6 +167,7 @@ export class AuthenticationService {
|
|
|
146
167
|
private readonly userActivityHistoryService: UserActivityHistoryService,
|
|
147
168
|
private readonly ssoCodeStorage: SsoCodeStorageService,
|
|
148
169
|
private readonly metadataValidationService: MetadataValidationService,
|
|
170
|
+
private readonly solidRegistry: SolidRegistry,
|
|
149
171
|
|
|
150
172
|
@InjectDataSource()
|
|
151
173
|
private readonly dataSource: DataSource,
|
|
@@ -207,6 +229,103 @@ export class AuthenticationService {
|
|
|
207
229
|
});
|
|
208
230
|
}
|
|
209
231
|
|
|
232
|
+
/**
|
|
233
|
+
* Runs the registered login extension chain for a user whose primary
|
|
234
|
+
* credential has already been proven.
|
|
235
|
+
*
|
|
236
|
+
* Every call site invokes this after that path's credential check has
|
|
237
|
+
* passed and before any token is minted, so the chain can only convert a
|
|
238
|
+
* successful login into a failure - never the other way round.
|
|
239
|
+
*/
|
|
240
|
+
async runLoginExtensions(
|
|
241
|
+
user: User,
|
|
242
|
+
ctxt: Omit<ILoginExtensionContext, "attemptId" | "startedAt">,
|
|
243
|
+
): Promise<void> {
|
|
244
|
+
const wrappers = this.solidRegistry.getLoginExtensionProviders();
|
|
245
|
+
|
|
246
|
+
// Guardrail: with no provider registered this is the entire cost of the
|
|
247
|
+
// feature - one array read. No log line, nothing observable in any login
|
|
248
|
+
// path. Do not "simplify" this early return away: it is what makes
|
|
249
|
+
// absence structurally different from a provider that errors.
|
|
250
|
+
if (wrappers.length === 0) return;
|
|
251
|
+
|
|
252
|
+
const context: ILoginExtensionContext = {
|
|
253
|
+
attemptId: randomUUID(),
|
|
254
|
+
startedAt: new Date(),
|
|
255
|
+
...ctxt,
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const providers = wrappers
|
|
259
|
+
.map((w) => w.instance as ILoginExtensionProvider)
|
|
260
|
+
.filter(Boolean)
|
|
261
|
+
.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
262
|
+
|
|
263
|
+
for (const provider of providers) {
|
|
264
|
+
const providerName =
|
|
265
|
+
provider.name?.() ?? provider.constructor?.name ?? "unknown";
|
|
266
|
+
try {
|
|
267
|
+
const result = await this.withLoginExtensionTimeout(
|
|
268
|
+
provider.verifyLogin(user, context),
|
|
269
|
+
provider.timeoutMs ?? DEFAULT_LOGIN_EXTENSION_TIMEOUT_MS,
|
|
270
|
+
providerName,
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
if (result?.allow === false) {
|
|
274
|
+
this.logger.warn(
|
|
275
|
+
`Login denied by ${providerName} for user ${user.id} ` +
|
|
276
|
+
`[attempt=${context.attemptId} channel=${context.channel}]: ` +
|
|
277
|
+
`${result.reason ?? "no reason given"}`,
|
|
278
|
+
);
|
|
279
|
+
throw new LoginExtensionDeniedException(
|
|
280
|
+
result.message ?? ERROR_MESSAGES.LOGIN_DENIED_BY_EXTENSION,
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
} catch (e: any) {
|
|
284
|
+
// Our own denial is a decision, not a malfunction, and is never
|
|
285
|
+
// fail-opened.
|
|
286
|
+
if (e instanceof LoginExtensionDeniedException) throw e;
|
|
287
|
+
|
|
288
|
+
const policy = provider.failurePolicy ?? "fail-closed";
|
|
289
|
+
this.logger.error(
|
|
290
|
+
`Login extension ${providerName} failed ` +
|
|
291
|
+
`[attempt=${context.attemptId} policy=${policy}]: ${e?.message}`,
|
|
292
|
+
e?.stack,
|
|
293
|
+
);
|
|
294
|
+
if (policy === "fail-open") continue;
|
|
295
|
+
throw new LoginExtensionUnavailableException(
|
|
296
|
+
ERROR_MESSAGES.LOGIN_EXTENSION_UNAVAILABLE,
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* A provider that ignores its own timeouts must not be able to hang login,
|
|
304
|
+
* so the deadline is enforced here rather than trusted to the provider.
|
|
305
|
+
*/
|
|
306
|
+
private withLoginExtensionTimeout<T>(
|
|
307
|
+
work: Promise<T>,
|
|
308
|
+
ms: number,
|
|
309
|
+
providerName: string,
|
|
310
|
+
): Promise<T> {
|
|
311
|
+
if (!(ms > 0)) return work;
|
|
312
|
+
let timer: NodeJS.Timeout;
|
|
313
|
+
return Promise.race([
|
|
314
|
+
work.finally(() => clearTimeout(timer)),
|
|
315
|
+
new Promise<never>((_, reject) => {
|
|
316
|
+
timer = setTimeout(
|
|
317
|
+
() =>
|
|
318
|
+
reject(
|
|
319
|
+
new Error(
|
|
320
|
+
`Login extension ${providerName} timed out after ${ms}ms`,
|
|
321
|
+
),
|
|
322
|
+
),
|
|
323
|
+
ms,
|
|
324
|
+
);
|
|
325
|
+
}),
|
|
326
|
+
]);
|
|
327
|
+
}
|
|
328
|
+
|
|
210
329
|
private async validateUserForPasswordLogin(
|
|
211
330
|
user: User,
|
|
212
331
|
password: string,
|
|
@@ -1237,10 +1356,14 @@ export class AuthenticationService {
|
|
|
1237
1356
|
if (!user) {
|
|
1238
1357
|
throw new UnauthorizedException(ERROR_MESSAGES.INVALID_CREDENTIALS);
|
|
1239
1358
|
}
|
|
1240
|
-
await this.validateUserForPasswordLogin(user, signInDto.password);
|
|
1359
|
+
await this. validateUserForPasswordLogin(user, signInDto.password);
|
|
1241
1360
|
await this.rehashPasswordIfRequired(user, signInDto.password);
|
|
1242
1361
|
await this.resetFailedAttempts(user);
|
|
1243
1362
|
|
|
1363
|
+
await this.runLoginExtensions(user, {
|
|
1364
|
+
channel: LoginExtensionChannel.PASSWORD,
|
|
1365
|
+
});
|
|
1366
|
+
|
|
1244
1367
|
const tokens = await this.generateTokens(user);
|
|
1245
1368
|
|
|
1246
1369
|
await this.userActivityHistoryService.logEvent("login", user);
|
|
@@ -1550,6 +1673,9 @@ export class AuthenticationService {
|
|
|
1550
1673
|
if (otp !== dummyOtp) {
|
|
1551
1674
|
throw new UnauthorizedException(ERROR_MESSAGES.INVALID_OTP);
|
|
1552
1675
|
}
|
|
1676
|
+
await this.runLoginExtensions(user, {
|
|
1677
|
+
channel: LoginExtensionChannel.OTP,
|
|
1678
|
+
});
|
|
1553
1679
|
return this.buildLoginTokenResponse(user);
|
|
1554
1680
|
}
|
|
1555
1681
|
|
|
@@ -1561,8 +1687,14 @@ export class AuthenticationService {
|
|
|
1561
1687
|
}
|
|
1562
1688
|
|
|
1563
1689
|
await this.clearLoginOtp(user, type);
|
|
1564
|
-
|
|
1690
|
+
// Reset before the chain runs, matching signIn: failedLoginAttempts counts
|
|
1691
|
+
// attempts against the *credential*, and the OTP was correct. A denial from
|
|
1692
|
+
// an external system is not a wrong OTP and must not leave the counter up.
|
|
1565
1693
|
await this.resetFailedAttempts(user);
|
|
1694
|
+
await this.runLoginExtensions(user, {
|
|
1695
|
+
channel: LoginExtensionChannel.OTP,
|
|
1696
|
+
});
|
|
1697
|
+
await this.userActivityHistoryService.logEvent("login", user);
|
|
1566
1698
|
return this.buildLoginTokenResponse(user);
|
|
1567
1699
|
}
|
|
1568
1700
|
|
|
@@ -300,15 +300,23 @@ private async prepareManyToManyAuditSnapshot(entity: T,id: number,modelSingularN
|
|
|
300
300
|
field.relationType !== 'one-to-many'
|
|
301
301
|
);
|
|
302
302
|
if (auditRelationFields.length > 0) {
|
|
303
|
-
|
|
304
|
-
|
|
303
|
+
// Fetch each audit-tracked relation independently rather than joining all of
|
|
304
|
+
// them into a single query: for an entity with several many-to-many
|
|
305
|
+
// relations, one combined query multiplies row counts across every joined
|
|
306
|
+
// relation at once and can blow past the statement timeout as data grows.
|
|
305
307
|
const auditBeforeEntity = await this.repo.findOne({
|
|
306
308
|
where: {
|
|
307
309
|
id: id,
|
|
308
310
|
} as unknown as FindOptionsWhere<T>,
|
|
309
|
-
relations: relations as any,
|
|
310
311
|
});
|
|
311
312
|
if (auditBeforeEntity) {
|
|
313
|
+
for (const field of auditRelationFields) {
|
|
314
|
+
(auditBeforeEntity as any)[field.name] = await this.repo.manager
|
|
315
|
+
.createQueryBuilder()
|
|
316
|
+
.relation(this.repo.target, field.name)
|
|
317
|
+
.of(id)
|
|
318
|
+
.loadMany();
|
|
319
|
+
}
|
|
312
320
|
Object.defineProperty(entity, AUDIT_BEFORE_SNAPSHOT, {
|
|
313
321
|
configurable: true,
|
|
314
322
|
enumerable: false,
|
|
@@ -13,6 +13,7 @@ import { IS_SECURITY_RULE_CONFIG_PROVIDER } from 'src/decorators/security-rule-c
|
|
|
13
13
|
import { IS_SELECTION_PROVIDER } from 'src/decorators/selection-provider.decorator';
|
|
14
14
|
import { IS_DASHBOARD_WIDGET_DATA_PROVIDER } from 'src/decorators/dashboard-widget-data-provider.decorator';
|
|
15
15
|
import { IS_EXTENSION_USER_CREATION_PROVIDER } from 'src/decorators/extension-user-creation-provider.decorator';
|
|
16
|
+
import { IS_LOGIN_EXTENSION_PROVIDER } from 'src/decorators/login-extension-provider.decorator';
|
|
16
17
|
import { IS_SOLID_DATABASE_MODULE } from 'src/decorators/solid-database-module.decorator';
|
|
17
18
|
import { IS_WA_PROVIDER } from 'src/decorators/whatsapp-provider.decorator';
|
|
18
19
|
import { IS_WORKFLOW_FIELD_DATA_PROVIDER } from 'src/decorators/workflow-field-data-provider.decorator';
|
|
@@ -64,6 +65,12 @@ export class SolidIntrospectService implements OnApplicationBootstrap {
|
|
|
64
65
|
this.solidRegistry.registerErrorCodeProvider(errorCodeProvider);
|
|
65
66
|
});
|
|
66
67
|
|
|
68
|
+
// Register all ILoginExtensionProvider implementations
|
|
69
|
+
const loginExtensionProviders = this.discoveryService.getProviders().filter((provider) => this.isLoginExtensionProvider(provider));
|
|
70
|
+
loginExtensionProviders.forEach((loginExtensionProvider) => {
|
|
71
|
+
this.solidRegistry.registerLoginExtensionProvider(loginExtensionProvider);
|
|
72
|
+
});
|
|
73
|
+
|
|
67
74
|
// Register all ISelectionProvider implementations
|
|
68
75
|
const selectionProviders = this.discoveryService.getProviders().filter((provider) => this.isSelectionProvider(provider));
|
|
69
76
|
selectionProviders.forEach((selectionProvider) => {
|
|
@@ -314,6 +321,12 @@ export class SolidIntrospectService implements OnApplicationBootstrap {
|
|
|
314
321
|
return !!this.reflector.get<boolean>(IS_EXTENSION_USER_CREATION_PROVIDER, instance.constructor);
|
|
315
322
|
}
|
|
316
323
|
|
|
324
|
+
private isLoginExtensionProvider(provider: InstanceWrapper): boolean {
|
|
325
|
+
const { instance } = provider;
|
|
326
|
+
if (!instance) return false;
|
|
327
|
+
return !!this.reflector.get<boolean>(IS_LOGIN_EXTENSION_PROVIDER, instance.constructor);
|
|
328
|
+
}
|
|
329
|
+
|
|
317
330
|
private isSettingsProvider(provider: InstanceWrapper) {
|
|
318
331
|
const { instance } = provider;
|
|
319
332
|
if (!instance) return false;
|
|
@@ -98,19 +98,26 @@ export class AuditSubscriber implements EntitySubscriberInterface {
|
|
|
98
98
|
return null;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
const relations: Record<string, boolean> = {};
|
|
102
|
-
|
|
103
|
-
auditRelationFields.forEach(field => {
|
|
104
|
-
relations[field.name] = true;
|
|
105
|
-
});
|
|
106
|
-
|
|
107
101
|
const relationBefore = event.entity?.[AUDIT_BEFORE_SNAPSHOT] ?? null;
|
|
108
102
|
|
|
109
|
-
|
|
103
|
+
// Same fix as CRUDService.prepareManyToManyAuditSnapshot: load each audit-tracked
|
|
104
|
+
// relation independently instead of joining them all into one query, so the row
|
|
105
|
+
// count of one relation doesn't multiply against every other joined relation.
|
|
106
|
+
const targetRepo = event.queryRunner.manager.getRepository(event.metadata.target as any);
|
|
107
|
+
const relationAfter = await targetRepo.findOne({
|
|
110
108
|
where: { id: entityId } as any,
|
|
111
|
-
relations: relations as any,
|
|
112
109
|
});
|
|
113
110
|
|
|
111
|
+
if (relationAfter) {
|
|
112
|
+
for (const field of auditRelationFields) {
|
|
113
|
+
(relationAfter as any)[field.name] = await event.queryRunner.manager
|
|
114
|
+
.createQueryBuilder()
|
|
115
|
+
.relation(event.metadata.target as any, field.name)
|
|
116
|
+
.of(entityId)
|
|
117
|
+
.loadMany();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
114
121
|
if (relationBefore && relationAfter) {
|
|
115
122
|
auditRelationFields.forEach(field => {
|
|
116
123
|
const oldIds = Array.isArray(relationBefore[field.name])
|