]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/password-policy.service.ts
936b19d8293e978f87366d6cf6d7b72b505d7f6b
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / password-policy.service.ts
1 import { Injectable } from '@angular/core';
2
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import * as _ from 'lodash';
5 import { Observable } from 'rxjs';
6 import { map } from 'rxjs/operators';
7
8 import { SettingsService } from '../api/settings.service';
9 import { CdPwdPolicySettings } from '../models/cd-pwd-policy-settings';
10
11 @Injectable({
12 providedIn: 'root'
13 })
14 export class PasswordPolicyService {
15 constructor(private i18n: I18n, private settingsService: SettingsService) {}
16
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
27 }),
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"'
33 ),
34 pwdPolicyCheckSequentialCharsEnabled: this.i18n(
35 'Cannot contain any sequential characters e.g. "abc"'
36 ),
37 pwdPolicyCheckComplexityEnabled: this.i18n(
38 'Must consist of characters from the following groups:\n' +
39 ' * Alphabetic a-z, A-Z\n' +
40 ' * Numbers 0-9\n' +
41 ' * Special chars: !"#$%& \'()*+,-./:;<=>?@[\\]^_`{{|}}~\n' +
42 ' * Any other characters (signs)'
43 )
44 };
45 helpText = helpText.concat(
46 _.keys(i18nHelp)
47 .filter((key) => _.get(settings, key))
48 .map((key) => '- ' + _.get(i18nHelp, key))
49 );
50 }
51 return helpText.join('\n');
52 })
53 );
54 }
55
56 /**
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.
60 */
61 mapCreditsToCssClass(credits: number): string {
62 let result = 'very-strong';
63 if (credits < 10) {
64 result = 'too-weak';
65 } else if (credits < 15) {
66 result = 'weak';
67 } else if (credits < 20) {
68 result = 'ok';
69 } else if (credits < 25) {
70 result = 'strong';
71 }
72 return result;
73 }
74 }