-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathozorkauth-cli
executable file
·88 lines (84 loc) · 2.51 KB
/
ozorkauth-cli
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /bin/bash
## Make sure http exists
command -v http >/dev/null 2>&1 || { echo >&2 "This script requires that httpie be installed. On Mac, you can install it with: brew install httpie"; exit 1; }
BASE_URL=https://ozorkauth.herokuapp.com
COMMAND=
EMAIL=
PASSWORD=
FIRST_NAME=
LAST_NAME=
REQUEST=
TOKEN=
while getopts ":b:c:f:l:e:p:r:t:" opt; do
case $opt in
b)
BASE_URL=$OPTARG
;;
c)
COMMAND=$OPTARG
;;
f)
FIRST_NAME=$OPTARG
;;
l)
LAST_NAME=$OPTARG
;;
e)
EMAIL=$OPTARG
;;
p)
PASSWORD=$OPTARG
;;
r)
REQUEST=$OPTARG
;;
t)
TOKEN=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [ "$COMMAND" = "auth" ]; then
OAUTH_RESPONSE=`http -f POST $BASE_URL/v1/a Origin:$BASE_URL grant_type=password username=$EMAIL password=$PASSWORD`
REGEX='"access_token":"(.*)","refresh_token"'
if [[ $OAUTH_RESPONSE =~ $REGEX ]]; then
TOKEN=${BASH_REMATCH[1]}
echo $TOKEN
else
echo "Something's done horribly wrong. Please check to make sure that you've supplied and email and password. You can also try hitting the endpoint directly: "
echo "\thttp -f POST $BASE_URL/v1/a Origin:$BASE_URL grant_type=password username=<your email address> password=<your password>"
fi
elif [ "$COMMAND" = "game" ]; then
if [ -n "$REQUEST" ]; then
http POST $BASE_URL/v1/c Authorization:"Bearer $TOKEN" request="$REQUEST"
else
http POST $BASE_URL/v1/c Authorization:"Bearer $TOKEN"
fi
elif [ "$COMMAND" = "register" ]; then
http POST $BASE_URL/v1/r givenName=$FIRST_NAME surName=$LAST_NAME email=$EMAIL password=$PASSWORD
elif [ "$COMMAND" = "instructions" ]; then
http $BASE_URL/v1/instructions
else
echo "Usage: "
echo
echo "Instructions (similar to this but delivered from the API):"
echo " $0 -c instructions"
echo
echo "Register:"
echo " $0 -c register -f <first name> -l <last name> -e <email address> -p <password>"
echo
echo "Authenticate using OAuth2 and get an access token:"
echo " $0 -c auth -e <email address> -p <password>"
echo
echo "Play the game:"
echo " $0 -c game -t <access token> -r \"<command>\""
echo " NOTE: if you leave off the -r, the response will be the result of looking around your current surroundings."
echo " NOTE: The game is restored from where you last left off and is automatically saved after each command."
fi