All files / modules/35-user-profile/modals/EnableTwoFactorAuth/views EnableTwoFactorView.tsx

93.1% Statements 27/29
55.56% Branches 10/18
75% Functions 3/4
93.1% Lines 27/29

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              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x 4x 4x 4x 4x           4x       4x 1x 1x       1x 1x 1x 1x             4x 4x                                                                                   1x                           1x  
/*
 * Copyright 2021 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 from 'react'
import { Button, Container, Layout, Text, TextInput, ButtonVariation } from '@wings-software/uicore'
import QRCode from 'react-qr-code'
import { Color } from '@harness/design-system'
import { TwoFactorAuthSettingsInfo, useEnableTwoFactorAuth, useGetTwoFactorAuthSettings } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { PageSpinner, useToaster } from '@common/components'
import { CopyText } from '@common/components/CopyText/CopyText'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import css from '../useEnableTwoFactorAuthModal.module.scss'
 
interface EnableTwoFactorAuthViewProps {
  isReset: boolean
  onEnable: () => void
  onCancel: () => void
}
 
const EnableTwoFactorAuthView: React.FC<EnableTwoFactorAuthViewProps> = ({ isReset, onCancel, onEnable }) => {
  const { getString } = useStrings()
  const { showSuccess, showError } = useToaster()
  const { mutate: enableTwoFactorAuth } = useEnableTwoFactorAuth({})
  const { updateAppStore } = useAppStore()
 
  const {
    data,
    refetch: refetchTwoFactorAuthSettings,
    loading
  } = useGetTwoFactorAuthSettings({
    authMechanism: 'TOTP'
  })
 
  const handleEnableTwoFactorAuth = async (settings?: TwoFactorAuthSettingsInfo): Promise<void> => {
    try {
      const enabled = await enableTwoFactorAuth({
        ...settings,
        twoFactorAuthenticationEnabled: true
      })
      Eif (enabled) {
        showSuccess(getString('userProfile.twoFactor.enableSuccess'))
        updateAppStore({ currentUserInfo: enabled.data })
        onEnable()
      }
    } catch (e) {
      showError(e.data.message || e.message)
    }
  }
 
  const authSettings = data?.data
  return (
    <>
      <Container className={css.view}>
        {authSettings ? (
          <>
            <Layout.Horizontal padding={{ top: 'large', bottom: 'huge' }}>
              <Layout.Vertical>
                <Text padding={{ bottom: 'large' }} color={Color.BLACK_100}>
                  {getString('userProfile.qrCode')}
                </Text>
                <Layout.Horizontal padding="medium" className={css.qrCode}>
                  <QRCode value={authSettings.totpqrurl || ''} />
                </Layout.Horizontal>
              </Layout.Vertical>
              <Layout.Vertical padding="huge" className={css.description}>
                <Text color={Color.BLACK}>{getString('userProfile.twoFactor.description')}</Text>
                <Layout.Vertical spacing="small" padding={{ top: 'large' }}>
                  <Text color={Color.BLACK} font={{ weight: 'semi-bold' }}>
                    {getString('common.secretKey')}
                  </Text>
                  <TextInput
                    readOnly
                    value={authSettings.totpSecretKey}
                    rightElement={
                      (
                        <CopyText
                          className={css.copy}
                          iconName="duplicate"
                          textToCopy={authSettings.totpSecretKey || ''}
                          iconAlwaysVisible
                        />
                      ) as any
                    }
                  />
                </Layout.Vertical>
              </Layout.Vertical>
            </Layout.Horizontal>
            <Layout.Horizontal flex>
              <Layout.Horizontal spacing="small">
                <Button
                  variation={ButtonVariation.PRIMARY}
                  text={isReset ? getString('save') : getString('enable')}
                  onClick={() => handleEnableTwoFactorAuth(authSettings)}
                />
                <Button text={getString('cancel')} onClick={onCancel} variation={ButtonVariation.TERTIARY} />
              </Layout.Horizontal>
              <Button icon="reset" minimal onClick={() => refetchTwoFactorAuthSettings()} />
            </Layout.Horizontal>
          </>
        ) : null}
      </Container>
      {loading ? <PageSpinner /> : null}
    </>
  )
}
 
export default EnableTwoFactorAuthView