Skip to content

Commit

Permalink
Merge pull request #1615 from akto-api-security/feature/multiple_head…
Browse files Browse the repository at this point in the history
…ers_auth

allow multiple headers for token
  • Loading branch information
ankush-jain-akto authored Oct 14, 2024
2 parents d6fd350 + d31385a commit 2f4484d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function TestRoleSettings() {
const [roleName, setRoleName] = useState(systemRole || "");
const [change, setChange] = useState(false);
const [currentInfo, setCurrentInfo] = useState({steps: [], authParams: {}});
const [hardCodeAuthInfo, setHardCodeAuthInfo] = useState({authHeaderKey: '',authHeaderValue: ''})
const [hardCodeAuthInfo, setHardCodeAuthInfo] = useState({authParams:[]})
const [showAuthComponent, setShowAuthComponent] = useState(false)
const [showAuthDeleteModal, setShowAuthDeleteModal] = useState(false)
const [deletedIndex, setDeletedIndex] = useState(-1);
Expand Down Expand Up @@ -169,11 +169,12 @@ function TestRoleSettings() {
}
}



const setHardCodedInfo = (obj) => {
setHardCodeAuthInfo(prev => ({
...prev,
authHeaderKey: obj.authHeaderKey,
authHeaderValue: obj.authHeaderValue,
authParams: obj.authParams
}))
}

Expand Down Expand Up @@ -290,7 +291,6 @@ function TestRoleSettings() {
steps: obj.steps,
authParams: obj.authParams
}))

}

const addAuthButton = (
Expand All @@ -304,15 +304,15 @@ function TestRoleSettings() {
setCurrentInfo({})
setHeaderKey('')
setHeaderValue('')
setHardCodeAuthInfo({})
setHardCodeAuthInfo({authParams:[]})
}

const handleSaveAuthMechanism = async() => {
const apiCond = {[headerKey] : headerValue};
let resp = {}
if(hardcodedOpen){
const automationType = "HardCoded";
const authParamData = [{key: hardCodeAuthInfo.authHeaderKey, value: hardCodeAuthInfo.authHeaderValue, where: "HEADER"}]
const authParamData = hardCodeAuthInfo.authParams
if(editableDoc > -1){
resp = await api.updateAuthInRole(initialItems.name, apiCond, editableDoc, authParamData, automationType)
}else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Dropdown from "../../../components/layouts/Dropdown";
import Store from "../../../store";


function AuthParams({ authParams, setAuthParams }) {
function AuthParams({ authParams, setAuthParams, hideTitle }) {

const setToastConfig = Store(state => state.setToastConfig)

Expand Down Expand Up @@ -49,7 +49,7 @@ function AuthParams({ authParams, setAuthParams }) {
}

return (
<LegacyCard title="Extract">
<LegacyCard title={hideTitle ? '' : "Extract"}>
<br />
<Divider />
<LegacyCard.Section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,38 @@ import api from "../api"
import Store from "../../../store";
import { useEffect } from "react";
import TestingStore from "../testingStore";
import AuthParams from './AuthParams';

function HardCoded({showOnlyApi, extractInformation, setInformation}) {

const setToastConfig = Store(state => state.setToastConfig)
const authMechanism = TestingStore(state => state.authMechanism)

const [userConfig, setUserConfig] = useState({
authHeaderKey: "",
authHeaderValue: ""
})
const [hasChanges, setHasChanges] = useState(false)
const [authParams, setAuthParams] = useState([{
key: "",
value: "",
where: "HEADER",
showHeader: true
}])

useEffect(() => {
if (authMechanism && authMechanism?.type.toUpperCase() === "HARDCODED") {
const authParam = authMechanism.authParams[0]
setUserConfig({
authHeaderKey: authParam.key,
authHeaderValue: authParam.value
})
setAuthParams(authMechanism.authParams)
}
}, [authMechanism])

useEffect(()=> {
if(extractInformation){
setInformation(userConfig)
setInformation({authParams})
}else{
return ;
}
},[userConfig])

function updateUserConfig(field, value) {
setUserConfig(prev => ({
...prev,
[field]: value
}))
setHasChanges(true)
}

async function handleSave() {
await api.addAuthMechanism(
"HARDCODED",
[],
[{
"key": userConfig.authHeaderKey,
"value": userConfig.authHeaderValue,
"where": "HEADER"
}]
)
setToastConfig({ isActive: true, isError: false, message: <div data-testid="hardcoded_saved_message">Hard coded auth token saved successfully!</div> })
}
},[authParams])

return (
<div>
<Text variant="headingMd">Inject hard-coded attacker auth token</Text>
<br />
<FormLayout>
<FormLayout.Group>
<TextField
id={"auth-header-key-field"}
label={(
<HorizontalStack gap="2">
<Text>Auth header key</Text>
<Tooltip content="Please enter name of the header which contains your auth token. This field is case-sensitive. eg Authorization" dismissOnMouseOut width="wide" preferredPosition="below">
<Icon source={InfoMinor} color="base" />
</Tooltip>
</HorizontalStack>
)}
value={userConfig.authHeaderKey} placeholder='' onChange={(authHeaderKey) => updateUserConfig("authHeaderKey", authHeaderKey)} />
<TextField
id={"auth-header-value-field"}
label={(
<HorizontalStack gap="2">
<Text>Auth header value</Text>
<Tooltip content="Please enter the value of the auth token." dismissOnMouseOut width="wide" preferredPosition="below">
<Icon source={InfoMinor} color="base" />
</Tooltip>
</HorizontalStack>
)}
value={userConfig.authHeaderValue} placeholder='' onChange={(authHeaderValue) => updateUserConfig("authHeaderValue", authHeaderValue)} />`
</FormLayout.Group>
</FormLayout>
<AuthParams authParams={authParams} setAuthParams={setAuthParams} hideTitle={true} />
<br />
{showOnlyApi ? null :<Button
id={"save-token"}
primary
disabled={!hasChanges}
onClick={handleSave}
>
<div data-testid="save_token_hardcoded">Save changes</div>
</Button>
}
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,37 @@ function LoginForm({ step, setSteps }) {
async function handleLoginFlowTest() {
setTestDisable(true)
func.setToast(true, false, "Running login flow")
const response = await api.triggerSingleStep('LOGIN_REQUEST', step.id, [{ ...step }])
if (response) {
func.setToast(true, false, <div data-testid="login_flow_ran_message">Login flow ran successfully!</div>)
const testResponse = JSON.parse(response.responses[0])
try {
const response = await api.triggerSingleStep('LOGIN_REQUEST', step.id, [{ ...step }])
if (response) {
func.setToast(true, false, <div data-testid="login_flow_ran_message">Login flow ran successfully!</div>)
const testResponse = JSON.parse(response.responses[0])

let responseBody
try {
responseBody = func.formatJsonForEditor(testResponse.body)
} catch {
responseBody = testResponse.body
}
let responseBody
try {
responseBody = func.formatJsonForEditor(testResponse.body)
} catch {
responseBody = testResponse.body
}

setSteps(prev => prev.map((s) => s.id === step.id ? {
...s,
testResponse: {
headers: { message: func.formatJsonForEditor(testResponse.headers) },
body: { message: responseBody }
setSteps(prev => prev.map((s) => s.id === step.id ? {
...s,
testResponse: {
headers: { message: func.formatJsonForEditor(testResponse.headers) },
body: { message: responseBody }
}
}
: s))
setSelectedApiResponseTab(0)
}
: s))
setSelectedApiResponseTab(0)

}
catch (Exception ) {

}

setTestDisable(false);

}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public static String executeCode(String ogPayload, Map<String, Object> valuesMap


public static String replaceVariables(String payload, Map<String, Object> valuesMap, boolean escapeString) throws Exception {
String regex = "\\$\\{(x\\d+\\.[\\w\\-\\[\\].]+|AKTO\\.changes_info\\..*?)\\}";
String regex = "\\$\\{((x|step)\\d+\\.[\\w\\-\\[\\].]+|AKTO\\.changes_info\\..*?)\\}";
Pattern p = Pattern.compile(regex);

// replace with values
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/src/main/java/com/akto/testing/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static String executeCode(String ogPayload, Map<String, Object> valuesMap


public static String replaceVariables(String payload, Map<String, Object> valuesMap, boolean escapeString) throws Exception {
String regex = "\\$\\{(x\\d+\\.[\\w\\-\\[\\].]+|AKTO\\.changes_info\\..*?)\\}";
String regex = "\\$\\{((x|step)\\d+\\.[\\w\\-\\[\\].]+|AKTO\\.changes_info\\..*?)\\}";
Pattern p = Pattern.compile(regex);

// replace with values
Expand Down

0 comments on commit 2f4484d

Please sign in to comment.