summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/password-policy.service.ts
blob: 295420c27dc030f336197363fce8ca645f7565ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Injectable } from '@angular/core';

import _ from 'lodash';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { SettingsService } from '../api/settings.service';
import { CdPwdPolicySettings } from '../models/cd-pwd-policy-settings';

@Injectable({
  providedIn: 'root'
})
export class PasswordPolicyService {
  constructor(private settingsService: SettingsService) {}

  getHelpText(): Observable<string> {
    return this.settingsService.getStandardSettings().pipe(
      map((resp: { [key: string]: any }) => {
        const settings = new CdPwdPolicySettings(resp);
        let helpText: string[] = [];
        if (settings.pwdPolicyEnabled) {
          helpText.push($localize`Required rules for passwords:`);
          const i18nHelp: { [key: string]: string } = {
            pwdPolicyCheckLengthEnabled: $localize`Must contain at least ${settings.pwdPolicyMinLength} characters`,
            pwdPolicyCheckOldpwdEnabled: $localize`Must not be the same as the previous one`,
            pwdPolicyCheckUsernameEnabled: $localize`Cannot contain the username`,
            pwdPolicyCheckExclusionListEnabled: $localize`Cannot contain any configured keyword`,
            pwdPolicyCheckRepetitiveCharsEnabled: $localize`Cannot contain any repetitive characters e.g. "aaa"`,
            pwdPolicyCheckSequentialCharsEnabled: $localize`Cannot contain any sequential characters e.g. "abc"`,
            pwdPolicyCheckComplexityEnabled: $localize`Must consist of characters from the following groups:
  * Alphabetic a-z, A-Z
  * Numbers 0-9
  * Special chars: !"#$%& '()*+,-./:;<=>?@[\\]^_\`{{|}}~
  * Any other characters (signs)`
          };
          helpText = helpText.concat(
            _.keys(i18nHelp)
              .filter((key) => _.get(settings, key))
              .map((key) => '- ' + _.get(i18nHelp, key))
          );
        }
        return helpText.join('\n');
      })
    );
  }

  /**
   * Helper function to map password policy credits to a CSS class.
   * @param credits The password policy credits.
   * @return The name of the CSS class.
   */
  mapCreditsToCssClass(credits: number): string {
    let result = 'very-strong';
    if (credits < 10) {
      result = 'too-weak';
    } else if (credits < 15) {
      result = 'weak';
    } else if (credits < 20) {
      result = 'ok';
    } else if (credits < 25) {
      result = 'strong';
    }
    return result;
  }
}