forked from netinsight/edge-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.sh
executable file
·35 lines (30 loc) · 1.11 KB
/
login.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env bash
edge_url="${1?missing argument: edge URL}"
install_name="${edge_url}"
install_name="${install_name#https://}"
install_name="${install_name#http://}"
install_name="${install_name#/}"
cookie_jar="$HOME/.config/edge-cli/$install_name.cookie"
if [ -f "$cookie_jar" ]; then
now="$(date +%s)"
# parse the curl cookie file, reference: https://curl.se/docs/http-cookies.html
expires="$(awk "/edgetoken/"' { print $5 }' "$cookie_jar")"
if [ -z "$expires" ]; then # no cookies stored
rm "$cookie_jar"
elif (( "$expires" < "$now" )); then
echo >&2 "Cookie has expired, refreshing"
rm "$cookie_jar"
fi
fi
if ! [ -f "$cookie_jar" ]; then
mkdir -p "$(dirname "$cookie_jar")"
curl "$edge_url/api/login/" \
--silent \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
--cookie-jar "$cookie_jar" \
--data-raw '{"username":"'"${EDGE_USER-admin}"'","password":"'"${EDGE_PASSWORD?The EDGE_PASSWORD environment variable is not set, cannot authenticate.}"'"}' \
> /dev/null
fi
echo "$cookie_jar"