forked from Yara724/api
Why: - Manual curl token setup was error-prone and allowed stale appToken reuse. Changes: - Add a script that accepts tejaratno or parsian, calls GetAppToken, then Login. - Document the script flow and fill known curl variables from the codebase. Impact: - Users can generate fresh Fanavaran tokens without manually copying multi-step curl commands.
144 lines
3.9 KiB
Bash
Executable File
144 lines
3.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
usage() {
|
|
cat <<'USAGE'
|
|
Usage:
|
|
scripts/fanavaran-auth.sh <tejaratno|parsian>
|
|
|
|
Calls Fanavaran GetAppToken, then Login with the returned appToken.
|
|
Outputs the appToken and authenticationToken, and writes raw responses to:
|
|
files/fanavaran-auth/<client>/
|
|
|
|
Optional:
|
|
FANAVARAN_BASE_URL can override the default API Manager base URL.
|
|
USAGE
|
|
}
|
|
|
|
client="${1:-}"
|
|
if [[ -z "$client" || "$client" == "-h" || "$client" == "--help" ]]; then
|
|
usage
|
|
exit 0
|
|
fi
|
|
|
|
case "$client" in
|
|
tejaratno)
|
|
app_name='fanhab'
|
|
app_secret='5Fa@N#A2B'
|
|
fanavaran_username='fanhabUser'
|
|
fanavaran_password='Fan#@2U$3er'
|
|
corp_id='3539'
|
|
contract_id='263'
|
|
location='100'
|
|
;;
|
|
parsian)
|
|
app_name='ParsianService'
|
|
app_secret='P@r30@n$erv!ce'
|
|
fanavaran_username='ParsianServiceUser'
|
|
fanavaran_password='P@r30@n123'
|
|
corp_id='543'
|
|
contract_id='28'
|
|
location='210050'
|
|
;;
|
|
*)
|
|
printf 'Unknown Fanavaran client: %s\n\n' "$client" >&2
|
|
usage >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
base_url="${FANAVARAN_BASE_URL:-https://apimanager.iraneit.com/BimeApiManager/api}"
|
|
auth_dir="files/fanavaran-auth/$client"
|
|
mkdir -p "$auth_dir"
|
|
|
|
app_token_headers="$auth_dir/get-app-token.headers"
|
|
app_token_body="$auth_dir/get-app-token.body.json"
|
|
login_headers="$auth_dir/login.headers"
|
|
login_body="$auth_dir/login.body.json"
|
|
tokens_file="$auth_dir/tokens.env"
|
|
|
|
extract_header() {
|
|
local header_name="$1"
|
|
local header_file="$2"
|
|
awk -F': ' -v wanted="$header_name" '
|
|
tolower($1) == tolower(wanted) {
|
|
gsub(/\r/, "", $2)
|
|
print $2
|
|
exit
|
|
}
|
|
' "$header_file"
|
|
}
|
|
|
|
extract_authentication_token_from_body() {
|
|
local body_file="$1"
|
|
node -e '
|
|
const fs = require("fs");
|
|
const path = process.argv[1];
|
|
const body = fs.existsSync(path) ? fs.readFileSync(path, "utf8") : "";
|
|
try {
|
|
const json = JSON.parse(body || "{}");
|
|
console.log(json.authenticationToken || json.authenticationtoken || json.authentication_token || "");
|
|
} catch {
|
|
console.log("");
|
|
}
|
|
' "$body_file"
|
|
}
|
|
|
|
printf 'Fanavaran client: %s\n' "$client"
|
|
printf 'Base URL: %s\n\n' "$base_url"
|
|
|
|
printf '1. Calling GetAppToken...\n'
|
|
curl -sS -D "$app_token_headers" -o "$app_token_body" \
|
|
-X POST "$base_url/EITAuthentication/GetAppToken" \
|
|
-H "appname: $app_name" \
|
|
-H "secret: $app_secret" \
|
|
-H "Content-Length: 0"
|
|
|
|
app_token="$(extract_header "apptoken" "$app_token_headers")"
|
|
if [[ -z "$app_token" ]]; then
|
|
printf 'Failed to extract appToken from %s\n' "$app_token_headers" >&2
|
|
printf 'Response body is saved at %s\n' "$app_token_body" >&2
|
|
exit 1
|
|
fi
|
|
|
|
printf '2. Calling Login...\n'
|
|
curl -sS -D "$login_headers" -o "$login_body" \
|
|
-X POST "$base_url/EITAuthentication/Login" \
|
|
-H "appToken: $app_token" \
|
|
-H "userName: $fanavaran_username" \
|
|
-H "password: $fanavaran_password" \
|
|
-H "Content-Length: 0"
|
|
|
|
authentication_token="$(extract_header "authenticationtoken" "$login_headers")"
|
|
if [[ -z "$authentication_token" ]]; then
|
|
authentication_token="$(extract_authentication_token_from_body "$login_body")"
|
|
fi
|
|
|
|
if [[ -z "$authentication_token" ]]; then
|
|
printf 'Failed to extract authenticationToken from login response.\n' >&2
|
|
printf 'Headers: %s\n' "$login_headers" >&2
|
|
printf 'Body: %s\n' "$login_body" >&2
|
|
exit 1
|
|
fi
|
|
|
|
cat > "$tokens_file" <<TOKENS
|
|
FANAVARAN_CLIENT='$client'
|
|
FANAVARAN_BASE_URL='$base_url'
|
|
FANAVARAN_BIME_URL='$base_url/BimeApi/v2.0'
|
|
APP_TOKEN='$app_token'
|
|
AUTHENTICATION_TOKEN='$authentication_token'
|
|
CORP_ID='$corp_id'
|
|
CONTRACT_ID='$contract_id'
|
|
LOCATION='$location'
|
|
TOKENS
|
|
|
|
printf '\nDone.\n'
|
|
printf 'APP_TOKEN=%s\n' "$app_token"
|
|
printf 'AUTHENTICATION_TOKEN=%s\n' "$authentication_token"
|
|
printf 'CORP_ID=%s\n' "$corp_id"
|
|
printf 'CONTRACT_ID=%s\n' "$contract_id"
|
|
printf 'LOCATION=%s\n' "$location"
|
|
printf '\nSaved token variables: %s\n' "$tokens_file"
|
|
printf 'Saved raw GetAppToken response: %s, %s\n' "$app_token_headers" "$app_token_body"
|
|
printf 'Saved raw Login response: %s, %s\n' "$login_headers" "$login_body"
|