-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeycloak-list-users.sh
executable file
·70 lines (53 loc) · 1.46 KB
/
keycloak-list-users.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
#!/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 list users in a keycloak realm.
Following flags are available:
-k Keycloak host.
-p Keycloak password.
-r Keycloak realm where to list users.
-u Keycloak username (Default is '$KC_USERNAME').
-h Print script help.\n\n"
print_help() {
printf "$TEXT_HELPER"
}
# Parse options
while getopts hk:p:r:u: flag; do
case "${flag}" in
k)
KC_HOST=${OPTARG};;
p)
KC_PASSWORD=${OPTARG};;
r)
KC_REALM=${OPTARG};;
u)
KC_USERNAME=${OPTARG};;
h | *)
print_help
exit 0;;
esac
done
if [ -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
fi
ACCESS_TOKEN=$(curl -fsSL \
-X POST "$KC_HOST/realms/master/protocol/openid-connect/token" \
-d "client_id=admin-cli" \
-d "username=$KC_USERNAME" \
-d "password=$KC_PASSWORD" \
-d "grant_type=password" | jq -r '.access_token')
curl -fsSL \
-X GET "$KC_HOST/admin/realms/$KC_REALM/users?max=-1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" | jq '.'