-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathokapi-curl
executable file
·105 lines (93 loc) · 2.54 KB
/
okapi-curl
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/sh
if [ "x$1" = x-v ]; then
MAYBE_ECHO=echo
shift
fi
if [ $# = 0 ]; then
echo "Usage: $0 [-v] login|<path> [<curl-options>]" >&2
exit 1;
fi
# Save off environment variables
OKAPI_URL_TMP=$OKAPI_URL
OKAPI_TENANT_TMP=$OKAPI_TENANT
OKAPI_TOKEN_TMP=$OKAPI_TOKEN
OKAPI_USER_TMP=$OKAPI_USER
OKAPI_PW_TMP=$OKAPI_PW
if [ -r ~/.okapi ]; then
. ~/.okapi
fi
# Reset variables from env if necessary
if test ! -z "$OKAPI_URL_TMP"; then OKAPI_URL=$OKAPI_URL_TMP; fi
if test ! -z "$OKAPI_TENANT_TMP"; then OKAPI_TENANT=$OKAPI_TENANT_TMP; fi
if test ! -z "$OKAPI_TOKEN_TMP"; then OKAPI_TOKEN=$OKAPI_TOKEN_TMP; fi
if test ! -z "$OKAPI_USER_TMP"; then OKAPI_USER=$OKAPI_USER_TMP; fi
if test ! -z "$OKAPI_PW_TMP"; then OKAPI_PW=$OKAPI_PW_TMP; fi
if test -z "$OKAPI_URL"; then
echo "OKAPI_URL must be given in ~/.okapi or set in environment"
exit 1
fi
OKAPI_TENANT="${OKAPI_TENANT:-supertenant}"
trap 'rm -f tmp.okapi.out tmp.okapi.headers' 0 1 15
if test "x$1" = "xlogin"; then
/bin/echo -n 'User: '
read username
/bin/echo -n 'Password: '
read password
trap 'rm -f tmp.okapi.out tmp.okapi.headers' 0 1 15
curl -o tmp.okapi.out -D tmp.okapi.headers -s \
-H "X-Okapi-Tenant:$OKAPI_TENANT" \
-H 'Content-Type:application/json' \
-H 'Accept:*/*' \
-d"{\"username\":\"$username\",\"password\":\"$password\"}" \
"$OKAPI_URL/authn/login"
OKAPI_TOKEN=`
cat tmp.okapi.headers |
awk '/x-okapi-token/ {print $2}' |
tr -d '[:space:]'
`
if test -z "$OKAPI_TOKEN"; then
cat tmp.okapi.out
echo
exit 1
fi
echo OKAPI_URL=$OKAPI_URL >~/.okapi
echo OKAPI_TENANT=$OKAPI_TENANT >>~/.okapi
echo OKAPI_TOKEN=$OKAPI_TOKEN >>~/.okapi
exit 0
fi
if test -z "$OKAPI_TOKEN"; then
if test ! -z "$OKAPI_USER"; then
if test -z "$OKAPI_PW"; then
echo "If using OKAPI_USER, OKAPI_PW must be set in ~/.okapi or environment"
exit 1;
fi
OKAPI_TOKEN=`
curl -o tmp.okapi.out -D - -s \
-H "X-Okapi-Tenant:$OKAPI_TENANT" \
-H 'Content-Type:application/json' \
-H 'Accept:*/*' \
-d"{\"username\":\"$OKAPI_USER\",\"password\":\"$OKAPI_PW\"}" \
"$OKAPI_URL/authn/login" |
awk '/x-okapi-token/ {print $2}' |
tr -d '[:space:]'
`
if test -z "$OKAPI_TOKEN"; then
cat tmp.okapi.out 1>&2
echo
exit 1
fi
rm -f tmp.okapi.out
else
echo "Either OKAPI_TOKEN or OKAPI_USER/OKAPI_PW must be set in ~/.okapi or environment" >&2
exit 1
fi
fi
path="$1"
shift
exec $MAYBE_ECHO curl -w '\n' \
-H "X-Okapi-Tenant:$OKAPI_TENANT" \
-H "X-Okapi-Token:$OKAPI_TOKEN" \
-H "Content-Type:application/json" \
-H "Accept:*/*" \
${@+"$@"} \
"${OKAPI_URL}${path}"