-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskgat
executable file
·60 lines (44 loc) · 1.29 KB
/
skgat
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
#!/usr/bin/env bash
# Service Key - generate access token
# Pipe a JSON representation of a service key into this to get an access token
# Assumpiton
# UAA root is in the "url" property
# Test whnether JSON is valid, outputs areturn code
# (zero if valid, non-zero otherwise)
# Usage: testjson <JSON>
function testjson (){
echo $1 | jq empty 2>/dev/null
echo $?
}
# Returns the value of given property (via jq)
# Usage: getprop <service key json> <property>
function getprop (){
echo $1 | jq -r $2
}
# Get access token, using credential and endpoint
# in the supplied access key json
# Usage: get_access_token <service key json>
function get_access_token (){
local json=$1
local clientid=$(getprop "${json}" .clientid)
local url=$(getprop "${json}" .url)
local clientsecret=$(getprop "${json}" .clientsecret)
if [[ $2 == test ]]; then
echo "clientid: ${clientid}"
echo "clientsecret: ${clientsecret}"
echo "url: ${url}"
fi
curl \
--silent \
--data "grant_type=client_credentials" \
--user "${clientid}:${clientsecret}" \
"$(getprop "${json}" .url)/oauth/token" \
| jq -r '.access_token'
}
# Main
sk=`cat /dev/stdin`
if [ $(testjson "${sk}") -ne "0" ]; then
>&2 echo "invalid JSON"
exit 1
fi
get_access_token "${sk}"