feat: init and fork some code from angular-auth-oidc-client
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run

This commit is contained in:
2025-01-18 01:05:00 +08:00
parent 276d9fbda8
commit 983254164b
201 changed files with 35689 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
import { inject, Injectable } from 'injection-js';
import { LoggerService } from '../../logging/logger.service';
import { OpenIdConfiguration } from '../openid-configuration';
import { Level, RuleValidationResult } from './rule';
import { allMultipleConfigRules, allRules } from './rules';
@Injectable()
export class ConfigValidationService {
private readonly loggerService = inject(LoggerService);
validateConfigs(passedConfigs: OpenIdConfiguration[]): boolean {
return this.validateConfigsInternal(
passedConfigs ?? [],
allMultipleConfigRules
);
}
validateConfig(passedConfig: OpenIdConfiguration): boolean {
return this.validateConfigInternal(passedConfig, allRules);
}
private validateConfigsInternal(
passedConfigs: OpenIdConfiguration[],
allRulesToUse: ((
passedConfig: OpenIdConfiguration[]
) => RuleValidationResult)[]
): boolean {
if (passedConfigs.length === 0) {
return false;
}
const allValidationResults = allRulesToUse.map((rule) =>
rule(passedConfigs)
);
let overallErrorCount = 0;
passedConfigs.forEach((passedConfig) => {
const errorCount = this.processValidationResultsAndGetErrorCount(
allValidationResults,
passedConfig
);
overallErrorCount += errorCount;
});
return overallErrorCount === 0;
}
private validateConfigInternal(
passedConfig: OpenIdConfiguration,
allRulesToUse: ((
passedConfig: OpenIdConfiguration
) => RuleValidationResult)[]
): boolean {
const allValidationResults = allRulesToUse.map((rule) =>
rule(passedConfig)
);
const errorCount = this.processValidationResultsAndGetErrorCount(
allValidationResults,
passedConfig
);
return errorCount === 0;
}
private processValidationResultsAndGetErrorCount(
allValidationResults: RuleValidationResult[],
config: OpenIdConfiguration
): number {
const allMessages = allValidationResults.filter(
(x) => x.messages.length > 0
);
const allErrorMessages = this.getAllMessagesOfType('error', allMessages);
const allWarnings = this.getAllMessagesOfType('warning', allMessages);
allErrorMessages.forEach((message) =>
this.loggerService.logError(config, message)
);
allWarnings.forEach((message) =>
this.loggerService.logWarning(config, message)
);
return allErrorMessages.length;
}
private getAllMessagesOfType(
type: Level,
results: RuleValidationResult[]
): string[] {
const allMessages = results
.filter((x) => x.level === type)
.map((result) => result.messages);
return allMessages.reduce((acc, val) => acc.concat(val), []);
}
}