I have the next code to get the Thycotic token in the file GetToken.sh.
This code defines a function called get_token, this function accepts 3 mandatory parameters USER_NAME, PASSWORD, and MFA_TOKEN. The function makes a Curl request using this information, saves the response into a txt file, and HTTP response into a variable.
But I need to execute twice the command using the same MFA Code to get the Auth token.
#!/bin/bash
export THYCOTIC_URL="https://mythycoticserver.com"
export TOKEN_PATH="/oauth2/token"
export CONTENT_TYPE_XWWWFORM="application/x-www-form-urlencoded"
function get_current_date {
date +%Y%m%d%H%M%S%N
}
function get_token {
local USER_NAME="${1}"
local PASSWORD="${2}"
local MFA_TOKEN="${3}"
local DOMAIN="${4:-ANY}"
local GRANT_TYPE="${5:-password}"
REST_COMMAND=(--header "Content-Type:${CONTENT_TYPE_XWWWFORM}")
if [[ -n "${MFA_TOKEN}" ]]; then
REST_COMMAND+=(--header "OTP:${MFA_TOKEN}")
fi
if [[ -n "${USER_NAME}" ]]; then
REST_COMMAND+=(--data-urlencode "username=${USER_NAME}")
fi
if [[ -n "${PASSWORD}" ]]; then
REST_COMMAND+=(--data-urlencode "password=${PASSWORD}")
fi
if [[ -n "${DOMAIN}" ]]; then
REST_COMMAND+=(--data-urlencode "domain=${DOMAIN}")
fi
if [[ -n "${GRANT_TYPE}" ]]; then
REST_COMMAND+=(--data-urlencode "grant_type=${GRANT_TYPE}")
fi
REST_COMMAND+=(--url "${THYCOTIC_URL}${TOKEN_PATH}")
FILE_NAME="${TMP}/response_$(get_current_date).txt"
HTTP_RESPONSE=$(curl --no-progress-meter --insecure -o "${FILE_NAME}" -w "%{http_code}" -X POST "${REST_COMMAND[@]}")
STATUS_CODE="$?"
echo "HTTP RESPONSE: ${HTTP_RESPONSE} - ${STATUS_CODE}"
if [[ "${HTTP_RESPONSE}" -ne 200 ]]; then
echo "Process failed with code ${STATUS_CODE}."
cat "${FILE_NAME}"
else
cat "${FILE_NAME}"
fi
}
This is the execution secuence: This is the execution secuence
Do you know if I missed some Curl parameters, or do I need to execute additional requests first to save the server cache?
I don't want to execute twice the command for getting the token.
I will appreciate your suggestions