import React from 'react'; import Input from '@/_ui/Input'; import Oauth from './Oauth'; const OpenapiAuth = ({ username, password, bearer_token, optionchanged, authObject, api_keys, access_token_url, client_id, client_secret, client_auth, custom_auth_params, custom_query_params, add_token_to, header_prefix, grant_type, scopes, auth_url, auth_type, access_token_custom_headers, }) => { const apiKeyChanges = (key, value) => { const apiKeys = api_keys ?? []; const updatedKeys = apiKeys.map((item) => { if (Array.isArray(authObject)) { if (item.parentKey === authObject[0].key) { item.fields.map((field) => { if (field.key === key) { field.value = value; } return field; }); } } else { if (item.key === key) { item.value = value; } } return item; }); optionchanged('api_keys', updatedKeys); }; const getCurrentKey = (key) => { let currentValue; if (!api_keys) return ''; api_keys.map((item) => { if (Array.isArray(authObject) && item.parentKey === authObject[0].key) { item.fields.map((field) => { if (field.key === key) { currentValue = field.value; return; } }); if (currentValue) return; } if (item.key === key) { currentValue = item.value; return; } }); return currentValue; }; const renderApiKeyField = (auth, index) => { if (auth) { const value = getCurrentKey(auth.key); return (
apiKeyChanges(auth.key, e.target.value)} value={value} />
); } else return null; }; switch (auth_type) { case 'basic': { return (
optionchanged('username', e.target.value)} value={username} />
optionchanged('password', e.target.value)} value={password} />
); } case 'bearer': { return (
optionchanged('bearer_token', e.target.value)} value={bearer_token} />
); } case 'apiKey': { if (Array.isArray(authObject)) { return (
{authObject.map((auth, index) => { return renderApiKeyField(auth, index); })}
); } else { return renderApiKeyField(authObject); } } case 'oauth2': { return ( ); } default: return null; } }; export default OpenapiAuth;