import React, { useRef, forwardRef } from 'react'; import get from 'lodash/get'; import { useTheme } from 'providers/Theme'; import { useDispatch } from 'react-redux'; import { IconCaretDown, IconSettings, IconKey, IconAdjustmentsHorizontal, IconHelp } from '@tabler/icons'; import SingleLineEditor from 'components/SingleLineEditor'; import StyledWrapper from './StyledWrapper'; import { inputsConfig } from './inputsConfig'; import Dropdown from 'components/Dropdown'; import Oauth2TokenViewer from '../Oauth2TokenViewer/index'; import Oauth2ActionButtons from '../Oauth2ActionButtons/index'; const OAuth2ClientCredentials = ({ save, item = {}, request, handleRun, updateAuth, collection }) => { const dispatch = useDispatch(); const { storedTheme } = useTheme(); const dropdownTippyRef = useRef(); const onDropdownCreate = (ref) => (dropdownTippyRef.current = ref); const oAuth = get(request, 'auth.oauth2', {}); const { accessTokenUrl, clientId, clientSecret, scope, credentialsPlacement, credentialsId, tokenPlacement, tokenHeaderPrefix, tokenQueryKey, refreshTokenUrl, autoRefreshToken, autoFetchToken } = oAuth; const refreshTokenUrlAvailable = refreshTokenUrl?.trim() !== ''; const isAutoRefreshDisabled = !refreshTokenUrlAvailable; const handleSave = () => { save(); }; const TokenPlacementIcon = forwardRef((props, ref) => { return (
{tokenPlacement == 'url' ? 'URL' : 'Headers'}
); }); const CredentialsPlacementIcon = forwardRef((props, ref) => { return (
{credentialsPlacement == 'body' ? 'Request Body' : 'Basic Auth Header'}
); }); const handleChange = (key, value) => { dispatch( updateAuth({ mode: 'oauth2', collectionUid: collection.uid, itemUid: item.uid, content: { grantType: 'client_credentials', accessTokenUrl, clientId, clientSecret, scope, credentialsPlacement, credentialsId, tokenPlacement, tokenHeaderPrefix, tokenQueryKey, refreshTokenUrl, autoRefreshToken, autoFetchToken, [key]: value } }) ); }; return (
Configuration
{inputsConfig.map((input) => { const { key, label, isSecret } = input; return (
handleChange(key, val)} onRun={handleRun} collection={collection} item={item} isSecret={isSecret} />
); })}
} placement="bottom-end">
{ dropdownTippyRef.current.hide(); handleChange('credentialsPlacement', 'body'); }} > Request Body
{ dropdownTippyRef.current.hide(); handleChange('credentialsPlacement', 'basic_auth_header'); }} > Basic Auth Header
Token
handleChange('credentialsId', val)} onRun={handleRun} collection={collection} item={item} />
} placement="bottom-end">
{ dropdownTippyRef.current.hide(); handleChange('tokenPlacement', 'header'); }} > Header
{ dropdownTippyRef.current.hide(); handleChange('tokenPlacement', 'url'); }} > URL
{ tokenPlacement === 'header' ?
handleChange('tokenHeaderPrefix', val)} onRun={handleRun} collection={collection} />
:
handleChange('tokenQueryKey', val)} onRun={handleRun} collection={collection} />
}
Advanced Settings
handleChange("refreshTokenUrl", val)} collection={collection} item={item} />
Settings
{/* Automatically Fetch Token */}
handleChange('autoFetchToken', e.target.checked)} className="cursor-pointer ml-1" />
Automatically fetch a new token when you try to access a resource and don't have one.
{/* Auto Refresh Token (With Refresh URL) */}
handleChange('autoRefreshToken', e.target.checked)} className={`cursor-pointer ml-1 ${isAutoRefreshDisabled ? 'opacity-50 cursor-not-allowed' : ''}`} disabled={isAutoRefreshDisabled} />
Automatically refresh your token using the refresh URL when it expires.
); }; export default OAuth2ClientCredentials;