All files / modules/33-auth-settings/pages/Configuration/AccountAndOAuth/HarnessAccount HarnessAccount.tsx

100% Statements 34/34
87.5% Branches 21/24
100% Functions 5/5
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143              3x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x                           3x               7x 7x     7x   7x 13x     7x 7x     7x   7x             1x 1x         7x 3x 1x 1x     2x   2x 1x 1x     1x           7x                                                                                                         3x  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import React, { Dispatch, SetStateAction } from 'react'
import { Layout, Switch, Collapse, useConfirmationDialog } from '@wings-software/uicore'
import { Color, Intent } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { useToaster } from '@common/components'
import { AuthenticationMechanisms } from '@auth-settings/constants/utils'
import type { AuthenticationSettingsResponse, UsernamePasswordSettings } from 'services/cd-ng'
import PasswordStrength from '@auth-settings/pages/Configuration/AccountAndOAuth/HarnessAccount/PasswordStrength/PasswordStrength'
import PasswordExpire from '@auth-settings/pages/Configuration/AccountAndOAuth/HarnessAccount/PasswordExpire/PasswordExpire'
import LockoutPolicy from '@auth-settings/pages/Configuration/AccountAndOAuth/HarnessAccount/LockoutPolicy/LockoutPolicy'
import TwoFactorAuthentication from '@auth-settings/pages/Configuration/AccountAndOAuth/HarnessAccount/TwoFactorAuthentication/TwoFactorAuthentication'
import cssConfiguration from '@auth-settings/pages/Configuration/Configuration.module.scss'
 
interface Props {
  authSettings: AuthenticationSettingsResponse
  refetchAuthSettings: () => void
  submitUserPasswordUpdate: (
    authenticationMechanism: keyof typeof AuthenticationMechanisms,
    message: string
  ) => Promise<void>
  updatingAuthMechanism: boolean
  canEdit: boolean
  setUpdating: Dispatch<SetStateAction<boolean>>
}
 
const HarnessAccount: React.FC<Props> = ({
  authSettings,
  refetchAuthSettings,
  submitUserPasswordUpdate,
  updatingAuthMechanism,
  canEdit,
  setUpdating
}) => {
  const { getString } = useStrings()
  const { showWarning } = useToaster()
 
  const userPasswordAuthMechanismEnabled =
    authSettings.authenticationMechanism === AuthenticationMechanisms.USER_PASSWORD
 
  const oauthEnabled = !!authSettings.ngAuthSettings?.find(
    settings => settings.settingsType === AuthenticationMechanisms.OAUTH
  )
 
  const userPasswordSettings = authSettings.ngAuthSettings?.find(
    ({ settingsType }) => settingsType === AuthenticationMechanisms.USER_PASSWORD
  ) as UsernamePasswordSettings | undefined
 
  const loginSettings = userPasswordSettings?.loginSettings
 
  const { openDialog: confirmUserPasswordDisable } = useConfirmationDialog({
    titleText: getString('authSettings.disableUserPasswordLogin'),
    contentText: getString('authSettings.confirmDisableUserPasswordLogin'),
    confirmButtonText: getString('confirm'),
    cancelButtonText: getString('cancel'),
    intent: Intent.WARNING,
    onCloseDialog: isConfirmed => {
      /* istanbul ignore else */ if (isConfirmed) {
        submitUserPasswordUpdate(AuthenticationMechanisms.OAUTH, getString('authSettings.loginSettingsHaveBeenUpdated'))
      }
    }
  })
 
  const toggleUsernamePassword = async (e: React.FormEvent<HTMLInputElement>): Promise<void> => {
    if (userPasswordAuthMechanismEnabled && !oauthEnabled) {
      showWarning(getString('authSettings.enableAtLeastOneSsoBeforeDisablingUserPasswordLogin'))
      return
    }
 
    const enable = e.currentTarget.checked
 
    if (!enable) {
      confirmUserPasswordDisable()
      return
    }
 
    submitUserPasswordUpdate(
      AuthenticationMechanisms.USER_PASSWORD,
      getString('authSettings.loginSettingsHaveBeenUpdated')
    )
  }
 
  return (
    <Collapse
      isOpen={userPasswordAuthMechanismEnabled}
      collapseHeaderClassName={cssConfiguration.collapseHeaderClassName}
      collapseClassName={cssConfiguration.collapseClassName}
      collapsedIcon="main-chevron-down"
      expandedIcon="main-chevron-up"
      heading={
        <Switch
          label={getString('authSettings.useHarnessUsernameAndPassword')}
          checked={userPasswordAuthMechanismEnabled}
          onChange={toggleUsernamePassword}
          disabled={
            !canEdit || updatingAuthMechanism || authSettings.authenticationMechanism === AuthenticationMechanisms.SAML
          }
          font={{ weight: 'semi-bold', size: 'normal' }}
          color={Color.GREY_800}
          data-testid="toggle-user-password-login"
        />
      }
    >
      {loginSettings && (
        <Layout.Vertical spacing="medium">
          <PasswordStrength
            loginSettings={loginSettings}
            refetchAuthSettings={refetchAuthSettings}
            canEdit={canEdit}
            setUpdating={setUpdating}
          />
          <PasswordExpire
            loginSettings={loginSettings}
            refetchAuthSettings={refetchAuthSettings}
            canEdit={canEdit}
            setUpdating={setUpdating}
          />
          <LockoutPolicy
            loginSettings={loginSettings}
            refetchAuthSettings={refetchAuthSettings}
            canEdit={canEdit}
            setUpdating={setUpdating}
          />
          <TwoFactorAuthentication
            twoFactorEnabled={!!authSettings.twoFactorEnabled}
            onSuccess={refetchAuthSettings}
            canEdit={canEdit}
            setUpdating={setUpdating}
          />
        </Layout.Vertical>
      )}
    </Collapse>
  )
}
 
export default HarnessAccount