-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth
More file actions
executable file
·62 lines (51 loc) · 1.27 KB
/
auth
File metadata and controls
executable file
·62 lines (51 loc) · 1.27 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
set -eu
set -o pipefail
if [ "$#" != 1 ]
then
echo "$0 <IP address>" >&2
echo
exit 1
fi
H="$1"
if [[ ! -v XLED_DEBUG ]]
then
if [ -t 1 ]
then
XLED_DEBUG=1
else
XLED_DEBUG=0
fi
fi
function debug()
{
if [ "$XLED_DEBUG" -ne 0 ]
then
echo "$@" >&2
fi
}
debug "Login..."
E="/xled/v1/login"
C="$(head -c 32 /dev/urandom | base64)"
debug " - challenge (b64): $C"
mapfile -t lines < <(curl --silent -X POST -H "Content-Type: application/json" -d '{"challenge":"'"$C"'"}' "http://$H$E" | jq -r '(.authentication_token, .["challenge-response"])')
if [ "${#lines[@]}" -ne 2 ]
then
echo "Failed to get login data" >&2
exit 1
fi
authentication_token="${lines[0]}"
challenge_response="${lines[1]}"
debug "Login responded with:"
debug "- authentication_token: $authentication_token"
debug "- challenge-response: $challenge_response"
debug "Verify:"
debug " - challenge-response: $challenge_response"
E="/xled/v1/verify"
code=$(curl --silent -X POST -H "Content-Type: application/json" -H "X-Auth-Token: $authentication_token" -d '{"challenge-response": "'"$challenge_response"'"}' "http://$H$E" | jq -r .code)
if [ "$code" -ne 1000 ]
then
echo "Failed to verify: $code" >&2
exit 1
fi
echo "$authentication_token"