forked from jpanesar07/delius-oauth2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token-functions.sh
executable file
·47 lines (41 loc) · 1.34 KB
/
token-functions.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
#!/bin/bash
calculateHostname() {
local ENV=$1
# Set the environment-specific hostname for the oauth2 service
if [[ "$ENV" == "t3" ]]; then
echo "https://sign-in-dev.hmpps.service.justice.gov.uk"
elif [[ "$ENV" == "t2" ]]; then
echo "https://sign-in-stage.hmpps.service.justice.gov.uk"
elif [[ "$ENV" == "preprod" ]]; then
echo "https://sign-in-preprod.hmpps.service.justice.gov.uk"
elif [[ "$ENV" == "prod" ]]; then
echo "https://sign-in.hmpps.service.justice.gov.uk"
elif [[ "$ENV" =~ localhost* ]]; then
echo "http://$ENV"
fi
}
checkFile() {
local FILE=$1
# Check whether the file exists and is readable
if [[ ! -f "$FILE" ]]; then
echo "Unable to find file $FILE"
exit 1
fi
}
authenticate() {
local CLIENT=$1
local USER=$2
# Get token for the client name / secret and store it in the environment variable TOKEN
if echo | base64 -w0 >/dev/null 2>&1; then
AUTH=$(echo -n "$CLIENT" | base64 -w0)
else
AUTH=$(echo -n "$CLIENT" | base64)
fi
if ! TOKEN_RESPONSE=$(curl -sS -d "" -X POST "$HOST/auth/oauth/token?grant_type=client_credentials&username=$USER" -H "Authorization: Basic $AUTH"); then
echo "Failed to read token from credentials response"
echo "$TOKEN_RESPONSE"
exit 1
fi
TOKEN=$(echo "$TOKEN_RESPONSE" | jq -er .access_token)
echo "Authorization: Bearer $TOKEN"
}