|
| 1 | +import * as React from 'react'; |
| 2 | +import { formatPrometheusDuration } from '@openshift-console/plugin-shared/src/datetime/prometheus'; |
| 3 | +import { Alert } from '@patternfly/react-core'; |
| 4 | +import { |
| 5 | + Dropdown as DropdownDeprecated, |
| 6 | + DropdownItem as DropdownItemDeprecated, |
| 7 | + DropdownToggle as DropdownToggleDeprecated, |
| 8 | +} from '@patternfly/react-core/deprecated'; |
| 9 | +import { CaretDownIcon } from '@patternfly/react-icons/dist/esm/icons/caret-down-icon'; |
| 10 | +import * as _ from 'lodash'; |
| 11 | +import { useTranslation } from 'react-i18next'; |
| 12 | +import { Link, useNavigate } from 'react-router-dom-v5-compat'; |
| 13 | +import { |
| 14 | + resourcePathFromModel, |
| 15 | + ResourceSummary, |
| 16 | + SectionHeading, |
| 17 | +} from '@console/internal/components/utils'; |
| 18 | +import { ClusterOperatorModel } from '@console/internal/models'; |
| 19 | +import { OAuthKind } from '@console/internal/module/k8s'; |
| 20 | +import { IDP_TYPES } from '@console/shared/src/constants/auth'; |
| 21 | +import { useQueryParams } from '@console/shared/src/hooks/useQueryParams'; |
| 22 | +import { IdentityProviders } from './IdentityProviders'; |
| 23 | + |
| 24 | +// Convert to ms for formatPrometheusDuration |
| 25 | +const tokenDuration = (seconds: number) => |
| 26 | + _.isNil(seconds) ? '-' : formatPrometheusDuration(seconds * 1000); |
| 27 | + |
| 28 | +export const OAuthConfigDetails: React.FC<OAuthDetailsProps> = ({ obj }: { obj: OAuthKind }) => { |
| 29 | + const navigate = useNavigate(); |
| 30 | + const [isIDPOpen, setIDPOpen] = React.useState(false); |
| 31 | + const { identityProviders, tokenConfig } = obj.spec; |
| 32 | + const { t } = useTranslation(); |
| 33 | + const queryParams = useQueryParams(); |
| 34 | + const idpAdded = queryParams.get('idpAdded'); |
| 35 | + |
| 36 | + const getAddIDPItemLabels = (type: string) => { |
| 37 | + switch (type) { |
| 38 | + case 'Basic Authentication': |
| 39 | + return t('console-app~Basic Authentication'); |
| 40 | + case 'GitHub': |
| 41 | + return t('console-app~GitHub'); |
| 42 | + case 'GitLab': |
| 43 | + return t('console-app~GitLab'); |
| 44 | + case 'Google': |
| 45 | + return t('console-app~Google'); |
| 46 | + case 'HTPasswd': |
| 47 | + return t('console-app~HTPasswd'); |
| 48 | + case 'Keystone': |
| 49 | + return t('console-app~Keystone'); |
| 50 | + case 'LDAP': |
| 51 | + return t('console-app~LDAP'); |
| 52 | + case 'OpenID Connect': |
| 53 | + return t('console-app~OpenID Connect'); |
| 54 | + case 'Request Header': |
| 55 | + return t('console-app~Request Header'); |
| 56 | + default: |
| 57 | + return type; |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const IDPDropdownItems = Object.entries(IDP_TYPES).map((idp) => { |
| 62 | + const [key, value] = idp; |
| 63 | + |
| 64 | + return ( |
| 65 | + <DropdownItemDeprecated |
| 66 | + key={`idp-${key}`} |
| 67 | + component="button" |
| 68 | + id={key} |
| 69 | + data-test-id={key} |
| 70 | + onClick={(e) => navigate(`/settings/idp/${e.currentTarget.id}`)} |
| 71 | + > |
| 72 | + {getAddIDPItemLabels(value)} |
| 73 | + </DropdownItemDeprecated> |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + return ( |
| 78 | + <> |
| 79 | + <div className="co-m-pane__body"> |
| 80 | + <SectionHeading text={t('console-app~OAuth details')} /> |
| 81 | + <div className="row"> |
| 82 | + <div className="col-md-6"> |
| 83 | + <ResourceSummary resource={obj}> |
| 84 | + {tokenConfig && ( |
| 85 | + <> |
| 86 | + <dt>{t('console-app~Access token max age')}</dt> |
| 87 | + <dd>{tokenDuration(tokenConfig.accessTokenMaxAgeSeconds)}</dd> |
| 88 | + </> |
| 89 | + )} |
| 90 | + </ResourceSummary> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + <div className="co-m-pane__body"> |
| 95 | + <SectionHeading text={t('console-app~Identity providers')} /> |
| 96 | + <p className="co-m-pane__explanation co-m-pane__explanation--alt"> |
| 97 | + {t('console-app~Identity providers determine how users log into the cluster.')} |
| 98 | + </p> |
| 99 | + {idpAdded === 'true' && ( |
| 100 | + <Alert |
| 101 | + isInline |
| 102 | + className="co-alert" |
| 103 | + variant="info" |
| 104 | + title={t('console-app~New identity provider added.')} |
| 105 | + > |
| 106 | + <> |
| 107 | + {t( |
| 108 | + 'console-app~Authentication is being reconfigured. The new identity provider will be available once reconfiguration is complete.', |
| 109 | + )}{' '} |
| 110 | + <Link to={resourcePathFromModel(ClusterOperatorModel, 'authentication')}> |
| 111 | + {t('console-app~View authentication conditions for reconfiguration status.')} |
| 112 | + </Link> |
| 113 | + </> |
| 114 | + </Alert> |
| 115 | + )} |
| 116 | + <DropdownDeprecated |
| 117 | + className="co-m-pane__dropdown" |
| 118 | + toggle={ |
| 119 | + <DropdownToggleDeprecated |
| 120 | + id="idp-dropdown" |
| 121 | + onToggle={() => setIDPOpen(!isIDPOpen)} |
| 122 | + toggleIndicator={CaretDownIcon} |
| 123 | + data-test-id="dropdown-button" |
| 124 | + > |
| 125 | + {t('console-app~Add')} |
| 126 | + </DropdownToggleDeprecated> |
| 127 | + } |
| 128 | + isOpen={isIDPOpen} |
| 129 | + dropdownItems={IDPDropdownItems} |
| 130 | + onSelect={() => setIDPOpen(false)} |
| 131 | + id="idp" |
| 132 | + /> |
| 133 | + <IdentityProviders identityProviders={identityProviders} /> |
| 134 | + </div> |
| 135 | + </> |
| 136 | + ); |
| 137 | +}; |
| 138 | + |
| 139 | +type OAuthDetailsProps = { |
| 140 | + obj: OAuthKind; |
| 141 | +}; |
0 commit comments