1 import { Injectable } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import * as _ from 'lodash';
5 import { Observable } from 'rxjs';
6 import { map } from 'rxjs/operators';
8 import { SettingsService } from '../api/settings.service';
9 import { CdPwdPolicySettings } from '../models/cd-pwd-policy-settings';
14 export class PasswordPolicyService {
15 constructor(private i18n: I18n, private settingsService: SettingsService) {}
17 getHelpText(): Observable<string> {
18 return this.settingsService.getStandardSettings().pipe(
19 map((resp: { [key: string]: any }) => {
20 const settings = new CdPwdPolicySettings(resp);
21 let helpText: string[] = [];
22 if (settings.pwdPolicyEnabled) {
23 helpText.push(this.i18n('Required rules for passwords:'));
24 const i18nHelp: { [key: string]: string } = {
25 pwdPolicyCheckLengthEnabled: this.i18n('Must contain at least {{length}} characters', {
26 length: settings.pwdPolicyMinLength
28 pwdPolicyCheckOldpwdEnabled: this.i18n('Must not be the same as the previous one'),
29 pwdPolicyCheckUsernameEnabled: this.i18n('Cannot contain the username'),
30 pwdPolicyCheckExclusionListEnabled: this.i18n('Cannot contain any configured keyword'),
31 pwdPolicyCheckRepetitiveCharsEnabled: this.i18n(
32 'Cannot contain any repetitive characters e.g. "aaa"'
34 pwdPolicyCheckSequentialCharsEnabled: this.i18n(
35 'Cannot contain any sequential characters e.g. "abc"'
37 pwdPolicyCheckComplexityEnabled: this.i18n(
38 'Must consist of characters from the following groups:\n' +
39 ' * Alphabetic a-z, A-Z\n' +
41 ' * Special chars: !"#$%& \'()*+,-./:;<=>?@[\\]^_`{{|}}~\n' +
42 ' * Any other characters (signs)'
45 helpText = helpText.concat(
47 .filter((key) => _.get(settings, key))
48 .map((key) => '- ' + _.get(i18nHelp, key))
51 return helpText.join('\n');
57 * Helper function to map password policy credits to a CSS class.
58 * @param credits The password policy credits.
59 * @return The name of the CSS class.
61 mapCreditsToCssClass(credits: number): string {
62 let result = 'very-strong';
65 } else if (credits < 15) {
67 } else if (credits < 20) {
69 } else if (credits < 25) {