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

100% Statements 34/34
100% Branches 5/5
100% Functions 4/4
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              2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x                 2x 4x 4x 4x 4x   4x     4x   4x 2x     4x       1x 1x             1x 1x 1x             4x                                           1x 1x               4x                                                                                     2x  
/*
 * 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 cx from 'classnames'
import { useParams } from 'react-router-dom'
import { Radio, Container, Layout, Collapse, Text, useConfirmationDialog } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import type { AuthenticationSettingsResponse } from 'services/cd-ng'
import { useUpdateAuthMechanism } from 'services/cd-ng'
import { AuthenticationMechanisms } from '@auth-settings/constants/utils'
import { useToaster } from '@common/components'
import HarnessAccount from '@auth-settings/pages/Configuration/AccountAndOAuth/HarnessAccount/HarnessAccount'
import PublicOAuthProviders from '@auth-settings/pages/Configuration/AccountAndOAuth/OAuthProviders/PublicOAuthProviders'
import { getForgotPasswordURL } from 'framework/utils/SessionUtils'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import cssConfiguration from '@auth-settings/pages/Configuration/Configuration.module.scss'
 
interface Props {
  authSettings: AuthenticationSettingsResponse
  refetchAuthSettings: () => void
  canEdit: boolean
  setUpdating: Dispatch<SetStateAction<boolean>>
}
 
const AccountAndOAuth: React.FC<Props> = ({ authSettings, refetchAuthSettings, canEdit, setUpdating }) => {
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const { showSuccess, showError } = useToaster()
  const userPasswordOrOauthEnabled =
    authSettings.authenticationMechanism === AuthenticationMechanisms.USER_PASSWORD ||
    /* istanbul ignore next */ authSettings.authenticationMechanism === AuthenticationMechanisms.OAUTH
 
  const { mutate: updateAuthMechanism, loading: updatingAuthMechanism } = useUpdateAuthMechanism({})
 
  React.useEffect(() => {
    setUpdating(updatingAuthMechanism)
  }, [updatingAuthMechanism, setUpdating])
 
  const submitUserPasswordUpdate = async (
    authenticationMechanism: keyof typeof AuthenticationMechanisms,
    message: string
  ): Promise<void> => {
    try {
      const response = await updateAuthMechanism(undefined, {
        queryParams: {
          accountIdentifier: accountId,
          authenticationMechanism: authenticationMechanism
        }
      })
 
      /* istanbul ignore else */ if (response) {
        refetchAuthSettings()
        showSuccess(message, 5000)
      }
    } catch (e) {
      /* istanbul ignore next */ showError(getRBACErrorMessage(e), 5000)
    }
  }
 
  const { openDialog: enableAccountAndOauthLogin } = useConfirmationDialog({
    titleText: getString('authSettings.enableHarnessAccountOrOauthLogin'),
    contentText: (
      <React.Fragment>
        <Text color={Color.BLACK} padding={{ bottom: 'xlarge' }}>
          {getString('authSettings.changeLoginToHarnessAccountOrOauth')}
        </Text>
        <Text inline color={Color.BLACK} font={{ weight: 'semi-bold', size: 'normal' }}>
          {getString('common.note')}
        </Text>
        <Text inline color={Color.BLACK} font={{ size: 'normal' }}>
          : {getString('authSettings.changeLoginToHarnessAccountOrOauthDescription')}{' '}
        </Text>
        <a href={getForgotPasswordURL()} target="_blank" rel="noreferrer">
          {getString('common.link')}
        </a>
        .
      </React.Fragment>
    ),
    confirmButtonText: getString('confirm'),
    cancelButtonText: getString('cancel'),
    onCloseDialog: isConfirmed => {
      /* istanbul ignore else */ if (isConfirmed) {
        submitUserPasswordUpdate(
          AuthenticationMechanisms.USER_PASSWORD,
          getString('authSettings.accountOrOAuthLoginEnabledSuccessfully')
        )
      }
    }
  })
 
  return (
    <Container margin="xlarge" background={Color.WHITE}>
      <Collapse
        isOpen={userPasswordOrOauthEnabled}
        collapseHeaderClassName={cx(cssConfiguration.collapseHeaderClassName, cssConfiguration.height60)}
        collapseClassName={cssConfiguration.collapseClassName}
        collapsedIcon="main-chevron-down"
        expandedIcon="main-chevron-up"
        heading={
          <Container margin={{ left: 'xlarge' }}>
            <Radio
              label={getString('authSettings.accountOrOAuthLogin')}
              font={{ weight: 'bold', size: 'normal' }}
              color={Color.GREY_900}
              checked={userPasswordOrOauthEnabled}
              disabled={!canEdit || updatingAuthMechanism}
              onChange={enableAccountAndOauthLogin}
            />
          </Container>
        }
      >
        <Layout.Vertical spacing="large" margin={{ left: 'xxlarge', right: 'xxlarge' }}>
          <HarnessAccount
            authSettings={authSettings}
            refetchAuthSettings={refetchAuthSettings}
            submitUserPasswordUpdate={submitUserPasswordUpdate}
            updatingAuthMechanism={updatingAuthMechanism}
            canEdit={canEdit}
            setUpdating={setUpdating}
          />
          <PublicOAuthProviders
            authSettings={authSettings}
            refetchAuthSettings={refetchAuthSettings}
            canEdit={canEdit}
            setUpdating={setUpdating}
          />
          <Container height={10} />
        </Layout.Vertical>
      </Collapse>
    </Container>
  )
}
 
export default AccountAndOAuth