-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest-token-with-client-creds.sh
62 lines (53 loc) · 1.71 KB
/
request-token-with-client-creds.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
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
#
# Obtains an Access Token via Client Credentials flow.
# Import common functions.
. ./common.sh --source-only
# Suppress Perl warnings that might occur when invoking jq.
init
# Outputs expected usage.
print_usage() {
echo
echo "Usage: ./request-token-with-client-creds.sh -option1 arg1 -option2 arg2 ..."
echo
echo "Required options:"
echo
echo "-c Client ID"
echo "-d Discovery Document URI (a.k.a. Well-known URI)"
echo "-p PEM file containing an RSA public-private key-pair"
echo "-k Signing Key ID"
echo
echo "Optional options:"
echo
echo "-s Scopes (a space-delimited list surrounded by quotes)"
echo
exit 1
}
# Parse the command-line options.
while getopts 'c:d:p:k:s:' flag; do
case "${flag}" in
c) client_id="${OPTARG}" ;;
d) discovery_doc_uri="${OPTARG}" ;;
p) pem="${OPTARG}" ;;
k) kid="${OPTARG}" ;;
s) scopes="${OPTARG}" ;;
*) print_usage ;;
esac
done
# Test for required arguments.
if [ -z "${client_id}" ] || [ -z "${discovery_doc_uri}" ] || [ -z "${pem}" ] || [ -z "${kid}" ]; then
print_usage
fi
# Download the Discovery Document and extract the Token Endpoint URI.
discovery_doc=$(download_file "${discovery_doc_uri}")
token_endpoint=$(extract_token_endpoint "${discovery_doc}")
# Assemble the POST body request query parameters.
params="grant_type=client_credentials"
params+="&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
params+="&client_assertion=$(create_jws "${client_id}" "${kid}" "${discovery_doc}" "${pem}")"
if [ -n "${scopes}" ]; then
# Scopes provided
params+="&scope="$(encode_base64url_arg "${scopes}")
fi
# POST the request to the Token Endpoint.
post_token_endpoint "${params}" "${token_endpoint}"