Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ApiKey visibility #797

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,74 @@

import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';

import { isRestricted } from 'AppData/AuthManager';
import { FormattedMessage, useIntl } from 'react-intl';
import { Icon, TextField, Tooltip, InputAdornment, } from '@mui/material';

import { Icon, TextField, Tooltip, InputAdornment, IconButton } from '@mui/material';
import CONSTS from 'AppData/Constants';

export default function AIEndpointAuth(props) {
const { api, saveEndpointSecurityConfig, apiKeyParamConfig, isProduction } = props;

const intl = useIntl();

const [apiKeyIdentifier] = useState(apiKeyParamConfig.authHeader || apiKeyParamConfig.authQueryParam);
const [apiKeyIdentifierType] = useState(apiKeyParamConfig.authHeader ? 'HEADER' : 'QUERY_PARAMETER');

const [apiKeyValue, setApiKeyValue] =
useState(api.endpointConfig?.endpoint_security?.[isProduction ? 'production' : 'sandbox']?.apiKeyValue === '' ?
'********' : null);

const [apiKeyValue, setApiKeyValue] = useState(
api.endpointConfig?.endpoint_security?.[isProduction ? 'production' : 'sandbox']?.apiKeyValue === ''
? '********'
: null
);
const [isHeaderParameter] = useState(!!apiKeyParamConfig.authHeader);
const [showApiKey, setShowApiKey] = useState(false);

useEffect(() => {
saveEndpointSecurityConfig({
...CONSTS.DEFAULT_ENDPOINT_SECURITY,
type: 'apikey',
apiKeyIdentifier,
apiKeyIdentifierType,
apiKeyValue: api.endpointConfig?.endpoint_security?.[isProduction ? 'production' : 'sandbox']?.apiKeyValue
=== '' ? '' : null,
apiKeyValue: api.endpointConfig?.endpoint_security?.[isProduction ?
'production' : 'sandbox']?.apiKeyValue === '' ? '' : null,
enabled: true,
}, isProduction ? 'production' : 'sandbox');
}, []);

const handleApiKeyChange = (event) => {
setApiKeyValue(event.target.value);
};

const handleApiKeyBlur = (event) => {
saveEndpointSecurityConfig({
...CONSTS.DEFAULT_ENDPOINT_SECURITY,
type: 'apikey',
apiKeyIdentifier,
apiKeyIdentifierType,
apiKeyValue: event.target.value === '********' ? '' : event.target.value,
enabled: true,
}, isProduction ? 'production' : 'sandbox');
};

const handleToggleApiKeyVisibility = () => {
if (apiKeyValue !== '********') {
setShowApiKey((prev) => !prev);
}
};

return (
<>
<TextField
disabled
label={isHeaderParameter ? <FormattedMessage
id='Apis.Details.Endpoints.Security.api.key.header'
defaultMessage='Authorization Header'
/> : <FormattedMessage
id='Apis.Details.Endpoints.Security.api.key.query.param'
defaultMessage='Authorization Query Param'
/>}
label={isHeaderParameter ? (
<FormattedMessage
id='Apis.Details.Endpoints.Security.api.key.header'
defaultMessage='Authorization Header'
/>
) : (
<FormattedMessage
id='Apis.Details.Endpoints.Security.api.key.query.param'
defaultMessage='Authorization Query Param'
/>
)}
id={'api-key-id-' + (isProduction ? '-production' : '-sandbox')}
sx={{ width: '49%', mr: 2 }}
value={apiKeyIdentifier}
Expand All @@ -83,46 +107,41 @@ export default function AIEndpointAuth(props) {
id: 'Apis.Details.Endpoints.Security.api.key.value.placeholder',
defaultMessage: 'Enter API Key',
})}
onChange={(event) => setApiKeyValue(event.target.value)}
onBlur={(event) => {
saveEndpointSecurityConfig({
...CONSTS.DEFAULT_ENDPOINT_SECURITY,
type: 'apikey',
apiKeyIdentifier,
apiKeyIdentifierType,
apiKeyValue: event.target.value === '********' ? '' : event.target.value,
enabled: true,
}, isProduction ? 'production' : 'sandbox');
}}
onChange={handleApiKeyChange}
onBlur={handleApiKeyBlur}
error={!apiKeyValue}
helperText={!apiKeyValue
? (
<FormattedMessage
id='Apis.Details.Endpoints.Security.no.api.key.value.error'
defaultMessage='API Key should not be empty'
/>
) : ''}
helperText={!apiKeyValue ? (
<FormattedMessage
id='Apis.Details.Endpoints.Security.no.api.key.value.error'
defaultMessage='API Key should not be empty'
/>
) : ''}
variant='outlined'
margin='normal'
required
type='password'
type={showApiKey ? 'text' : 'password'}
InputProps={{
endAdornment: <InputAdornment position='end'>
<Tooltip
placement='top-start'
interactive
title={(
<FormattedMessage
id='Apis.Details.Endpoints.Security.api.key.value.tooltip'
defaultMessage='API Key for the AI API'
/>
)}
>
<Icon>
security
</Icon>
</Tooltip>
</InputAdornment>
endAdornment: (
<InputAdornment position='end'>
<Tooltip
placement='top-start'
interactive
title={(
<FormattedMessage
id='Apis.Details.Endpoints.Security.api.key.value.tooltip'
defaultMessage='API Key for the AI API'
/>
)}
>
<Icon>security</Icon>
</Tooltip>
<IconButton onClick={handleToggleApiKeyVisibility} edge='end'>
<Icon>
{showApiKey ? 'visibility' : 'visibility_off'}
</Icon>
</IconButton>
</InputAdornment>
),
}}
/>
</>
Expand All @@ -133,4 +152,4 @@ AIEndpointAuth.propTypes = {
api: PropTypes.shape({}).isRequired,
saveEndpointSecurityConfig: PropTypes.func.isRequired,
isProduction: PropTypes.bool.isRequired,
};
};
Loading