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

92.16% Statements 47/51
78.95% Branches 15/19
85.71% Functions 6/7
92.16% Lines 47/51

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 144 145 146 147 148 149 150 151              4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x                 4x 10x 10x 10x 10x 10x 10x     10x           10x 5x     10x       1x 1x       1x 1x 1x             10x             1x 1x               10x                                       10x           1x 1x         10x 2x 2x   2x 1x 1x     1x         1x 1x       10x                               4x  
/*
 * 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 { useParams, useHistory } from 'react-router-dom'
import { Card, Text, useConfirmationDialog } from '@wings-software/uicore'
import { Color, Intent } from '@harness/design-system'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { useToaster } from '@common/components'
import { useSetTwoFactorAuthAtAccountLevel } from 'services/cd-ng'
import routes from '@common/RouteDefinitions'
import FeatureSwitch from '@rbac/components/Switch/Switch'
import { FeatureIdentifier } from 'framework/featureStore/FeatureIdentifier'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import css from './TwoFactorAuthentication.module.scss'
 
interface Props {
  twoFactorEnabled: boolean
  onSuccess: () => void
  canEdit: boolean
  setUpdating: Dispatch<SetStateAction<boolean>>
}
 
const TwoFactorAuthentication: React.FC<Props> = ({ twoFactorEnabled, onSuccess, canEdit, setUpdating }) => {
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const history = useHistory()
  const { currentUserInfo } = useAppStore()
  const { showSuccess, showError } = useToaster()
 
  const { mutate: updateTwoFactorAuthentication, loading: updatingTwoFactorAuthentication } =
    useSetTwoFactorAuthAtAccountLevel({
      queryParams: {
        accountIdentifier: accountId
      }
    })
 
  React.useEffect(() => {
    setUpdating(updatingTwoFactorAuthentication)
  }, [updatingTwoFactorAuthentication, setUpdating])
 
  const submitUpdateTwoFactorAuthentication = async (
    adminOverrideTwoFactorEnabled: boolean,
    message: string
  ): Promise<void> => {
    try {
      const response = await updateTwoFactorAuthentication({
        adminOverrideTwoFactorEnabled
      })
 
      /* istanbul ignore else */ if (response) {
        onSuccess()
        showSuccess(message, 5000)
      }
    } catch (e) {
      /* istanbul ignore next */ showError(getRBACErrorMessage(e), 5000)
    }
  }
 
  const { openDialog: confirmRedirect } = useConfirmationDialog({
    titleText: getString('authSettings.enforceTwoFA'),
    contentText: getString('authSettings.yourAccountWillBeLockedOut'),
    confirmButtonText: getString('authSettings.goToSettings'),
    cancelButtonText: getString('cancel'),
    intent: Intent.WARNING,
    onCloseDialog: isConfirmed => {
      /* istanbul ignore else */ if (isConfirmed) {
        history.push({
          pathname: routes.toUserProfile({ accountId }),
          search: 'openTwoFactorModal=true'
        })
      }
    }
  })
 
  const { openDialog: confirm2FAEnable } = useConfirmationDialog({
    titleText: getString('authSettings.enforceTwoFA'),
    contentText: (
      <React.Fragment>
        <Text color={Color.BLACK}>{getString('authSettings.doYouWantToEnforceTwoFAForAllMembers')}</Text>
        <ol type="1" className={css.listItem}>
          <li>{getString('authSettings.newMembersWillNeedToSetUpTwoFADuringSignup')}</li>
          <li>{getString('authSettings.existingMembersWillReceiveAnEmailWithQRCode')}</li>
        </ol>
      </React.Fragment>
    ),
    confirmButtonText: getString('confirm'),
    cancelButtonText: getString('cancel'),
    onCloseDialog: isConfirmed => {
      /* istanbul ignore else */ Iif (isConfirmed) {
        submitUpdateTwoFactorAuthentication(true, getString('authSettings.twoFAEnforcementEnabled'))
      }
    }
  })
 
  const { openDialog: confirm2FADisable } = useConfirmationDialog({
    titleText: getString('authSettings.disableTwoFAEnforcement'),
    contentText: getString('authSettings.sureToDisableTwoFAEnforcement'),
    confirmButtonText: getString('confirm'),
    cancelButtonText: getString('cancel'),
    onCloseDialog: isConfirmed => {
      /* istanbul ignore else */ if (isConfirmed) {
        submitUpdateTwoFactorAuthentication(false, getString('authSettings.twoFAEnforcementDisabled'))
      }
    }
  })
 
  const onChange2FASetting = (e: React.FormEvent<HTMLInputElement>): void => {
    const enable = e.currentTarget.checked
    const isTwoFactorAuthEnabledForCurrentUser = currentUserInfo.twoFactorAuthenticationEnabled
 
    if (!twoFactorEnabled && !isTwoFactorAuthEnabledForCurrentUser && enable) {
      confirmRedirect()
      return
    }
 
    Iif (!twoFactorEnabled && isTwoFactorAuthEnabledForCurrentUser && enable) {
      confirm2FAEnable()
      return
    }
 
    /* istanbul ignore else */ if (twoFactorEnabled && !enable) {
      confirm2FADisable()
    }
  }
 
  return (
    <Card className={css.twoFactorAuthentication}>
      <FeatureSwitch
        featureProps={{ featureRequest: { featureName: FeatureIdentifier.TWO_FACTOR_AUTH_SUPPORT } }}
        label={getString('authSettings.enforceTwoFA')}
        checked={twoFactorEnabled}
        font={{ weight: 'semi-bold', size: 'normal' }}
        color={Color.GREY_800}
        onChange={onChange2FASetting}
        disabled={!canEdit || updatingTwoFactorAuthentication}
        data-testid="twoFA-toggle"
      />
    </Card>
  )
}
 
export default TwoFactorAuthentication