Skip to content

Commit

Permalink
Merge pull request #607 from Kirishikesan/main
Browse files Browse the repository at this point in the history
Fixes optional authentication not working
  • Loading branch information
lasanthaS committed Mar 26, 2024
2 parents f7797d4 + 7e02086 commit 70a2a7c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ import {
API_SECURITY_BASIC_AUTH,
API_SECURITY_API_KEY,
API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY,
API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_OPTIONAL,
API_SECURITY_MUTUAL_SSL_MANDATORY,
API_SECURITY_MUTUAL_SSL_OPTIONAL,
API_SECURITY_MUTUAL_SSL,
} from './components/APISecurity/components/apiSecurityConstants';
import WebSubConfiguration from './components/WebSubConfiguration';
Expand Down Expand Up @@ -245,29 +247,46 @@ export default function RuntimeConfiguration() {
return { ...copyAPIConfig(state), [action]: value };
case 'securityScheme':
// If event came from mandatory selector of either Application level or Transport level
if (
[API_SECURITY_MUTUAL_SSL_MANDATORY, API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY]
.includes(event.name)
) {
// If user select not mandatory (optional) , Remove the respective schema, else add it
if (event.value === 'optional') {
return {
...copyAPIConfig(state),
[action]: state[action].filter((schema) => schema !== event.name),
};
} else if (state[action].includes(event.name)) {
return state; // Add for completeness, Ideally there couldn't exist this state
} else {
return { ...copyAPIConfig(state), [action]: [...state[action], event.name] };
}
if (API_SECURITY_MUTUAL_SSL_MANDATORY === event.name) {
const filteredArray = state[action]
.filter((schema) => (schema !== API_SECURITY_MUTUAL_SSL_MANDATORY
&& schema !== API_SECURITY_MUTUAL_SSL_OPTIONAL))
.concat(event.value);
const newState = {
...copyAPIConfig(state),
[action]: filteredArray,
};
return newState;
} else if (API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY === event.name) {
const filteredArray = state[action]
.filter((schema) => (schema !== API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY
&& schema !== API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_OPTIONAL))
.concat(event.value);
const newState = {
...copyAPIConfig(state),
[action]: filteredArray,
};
return newState;
}
// User checked on one of api security schemas (either OAuth, Basic, ApiKey or Mutual SSL)
if (event.checked) {
if (state[action].includes(event.value)) {
return state; // Add for completeness, Ideally there couldn't exist this state
} else {
return { ...copyAPIConfig(state), [action]: [...state[action], event.value] };
} else if (event.value === API_SECURITY_MUTUAL_SSL
&& event.value !== DEFAULT_API_SECURITY_OAUTH2
&& event.value !== API_SECURITY_BASIC_AUTH
&& event.value !== API_SECURITY_API_KEY) {
return { ...copyAPIConfig(state),
[action]: [...state[action], event.value, API_SECURITY_MUTUAL_SSL_MANDATORY] };
} else if (event.value !== API_SECURITY_MUTUAL_SSL
&& (event.value === DEFAULT_API_SECURITY_OAUTH2
|| event.value === API_SECURITY_BASIC_AUTH
|| event.value === API_SECURITY_API_KEY)) {
return { ...copyAPIConfig(state),
[action]: [...state[action], event.value,
API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY] };
}
return { ...copyAPIConfig(state), [action]: [...state[action], event.value] };
} else if (state[action].includes(event.value)) {
// User has unchecked a security schema type
const newState = {
Expand All @@ -282,17 +301,31 @@ export default function RuntimeConfiguration() {
)
) {
const noMandatoryOAuthBasicAuth = newState[action]
.filter((schema) => schema !== API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY);
.filter((schema) => (schema !== API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY
&& schema !== API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_OPTIONAL
&& schema !== API_SECURITY_MUTUAL_SSL_MANDATORY
&& schema !== API_SECURITY_MUTUAL_SSL_OPTIONAL));
const newSecurityScheme = newState[action].includes(API_SECURITY_MUTUAL_SSL) ?
[...noMandatoryOAuthBasicAuth, API_SECURITY_MUTUAL_SSL_MANDATORY]
: noMandatoryOAuthBasicAuth;
return {
...newState,
[action]: noMandatoryOAuthBasicAuth,
[action]: newSecurityScheme,
};
} else if (!newState[action].includes(API_SECURITY_MUTUAL_SSL)) {
const noMandatoryMutualSSL = newState[action]
.filter((schema) => schema !== API_SECURITY_MUTUAL_SSL_MANDATORY);
.filter((schema) => (schema !== API_SECURITY_MUTUAL_SSL_MANDATORY
&& schema !== API_SECURITY_MUTUAL_SSL_OPTIONAL
&& schema !== API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY
&& schema !== API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_OPTIONAL));
const newSecurityScheme = (newState[action].includes(DEFAULT_API_SECURITY_OAUTH2)
|| newState[action].includes(API_SECURITY_BASIC_AUTH)
|| newState[action].includes(API_SECURITY_API_KEY)) ?
[...noMandatoryMutualSSL, API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY]
: noMandatoryMutualSSL;
return {
...newState,
[action]: noMandatoryMutualSSL,
[action]: newSecurityScheme,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export default function ApplicationLevel(props) {

const [mandatoryValueRef, setMandatoryValueRef] = useState(mandatoryValue);

useEffect(() => {
setMandatoryValueRef(mandatoryValue);
});

return (
(<Root>
<Grid item xs={12}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ function TransportLevel(props) {

const [mandatoryValueRef, setMandatoryValueRef] = useState(mandatoryValue);

useEffect(() => {
setMandatoryValueRef(mandatoryValue);
});

return (
(<Root>
<Grid item xs={12}>
Expand Down

0 comments on commit 70a2a7c

Please sign in to comment.