-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-keycloak-token.sh
executable file
·88 lines (69 loc) · 1.94 KB
/
get-keycloak-token.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
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
# Colorize terminal
red='\e[0;31m'
no_color='\033[0m'
# Default
KC_USERNAME="admin"
# Declare script helper
TEXT_HELPER="\nThe purpose of this script is to display the keycloak user's token based on appropriate information.
Following flags are available:
-i Keycloak client id.
-k Keycloak host.
-p User password.
-r Keycloak realm.
-s Keycloak client secret.
-u Keycloak username (Default is '$KC_USERNAME').
-h Print script help.\n\n"
print_help() {
printf "$TEXT_HELPER"
}
# Parse options
while getopts hi:k:p:r:s:u: flag; do
case "${flag}" in
i)
CLIENT_ID=${OPTARG};;
k)
KC_HOST=${OPTARG};;
p)
KC_PASSWORD=${OPTARG};;
r)
KC_REALM=${OPTARG};;
s)
CLIENT_SECRET=${OPTARG};;
u)
KC_USERNAME=${OPTARG};;
h | *)
print_help
exit 0;;
esac
done
function jwt_decode(){
jq -R 'split(".") | .[1] | @base64d | fromjson' <<< "$1"
}
if [ -z "$CLIENT_ID" ]; then
printf "\n${red}Error.${no_color} Argument missing : client id (flag -i)".
exit 1
elif [ -z "$KC_HOST" ]; then
printf "\n${red}Error.${no_color} Argument missing : keycloak host (flag -k)".
exit 1
elif [ -z "$KC_PASSWORD" ]; then
printf "\n${red}Error.${no_color} Argument missing : user password (flag -p)".
exit 1
elif [ -z "$KC_REALM" ]; then
printf "\n${red}Error.${no_color} Argument missing : keycloak realm (flag -r)".
exit 1
elif [ -z "$CLIENT_SECRET" ]; then
printf "\n${red}Error.${no_color} Argument missing : client secret (flag -s)".
exit 1
elif [ -z "$KC_USERNAME" ]; then
printf "\n${red}Error.${no_color} Argument missing : username (flag -u)".
exit 1
fi
ACCESS_TOKEN=$(curl \
-d "client_id=$CLIENT_ID" \
-d "client_secret=$CLIENT_SECRET" \
-d "username=$KC_USERNAME" \
-d "password=$KC_PASSWORD" \
-d "grant_type=password" \
"$KC_HOST/realms/$KC_REALM/protocol/openid-connect/token" | jq -r '.access_token')
jwt_decode "$ACCESS_TOKEN"