forked from kaf-systems/openfire-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathof-cli-uapi
120 lines (108 loc) · 3.35 KB
/
of-cli-uapi
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
# 2016 lgg https://github.com/lgg/openfire-cli
# needed jq 1.4 (apt install jq)
# https://stedolan.github.io/jq/
# usage ./of-cli-uapi ACTION USERNAME PASSWORD
## Define this variables
apiPort="9091"
apiHost="localhost"
apiKey="YOURAPIKEYHERE"
## Internal vars
ofUsage="command usage: ./of-cli-uapi ACTION USERNAME PASSWORD"
ofActions="ACTIONS: list/get/add/delete"
## Check given arguments
if [ -z "$1" ]; then
echo "ERR: no ACTION specified"
echo "$ofUsage"
echo "$ofActions"
exit 1
else
apiAction="$1"
fi
if [[ "$apiAction" != "list" && -z "$2" ]]; then
echo "ERR: no USERNAME specified"
echo "$ofUsage"
exit 1
else
apiUser="$2"
fi
if [[ "$apiAction" = "add" && -z "$3" ]]; then
echo "ERR: no PASSWORD specified"
echo "$ofUsage"
exit 1
else
apiPass="$3"
fi
## curl help:
# -k = --insecure = allow self-signed certificates
# -s = --silent = hide curl output (table with speed, downloaded, etc)
## functions
function getUser {
apiUrl="https://$apiHost:$apiPort/plugins/restapi/v1/users/$apiUser"
userExist=$(curl -sk -H "Authorization: $apiKey" -H "Accept: application/json" -H "Content-Type: application/json" -X GET "$apiUrl" | jq '.' | jq {username} | jq '.[] != null')
echo $userExist
}
function listUsers {
apiUrl="https://$apiHost:$apiPort/plugins/restapi/v1/users/"
users=$(curl -sk -H "Authorization: $apiKey" -H "Accept: application/json" -H "Content-Type: application/json" -X GET "$apiUrl" | jq -r '.user' | jq -r '.[].username')
if [ -z "$users" ]; then
echo "ERR: wrong API AUTH KEY"
exit 1
else
arrUsers=($users)
echo "OpenFire registered users: "
for i in ${arrUsers[@]}; do echo $i; done
fi
}
function addUser {
apiUrl="https://$apiHost:$apiPort/plugins/restapi/v1/users/"
apiPostData=$(jq -n --arg u "$apiUser" --arg p "$apiPass" '{"username": $u, "password": $p}')
userResult=$(curl -sk -H "Authorization: $apiKey" -H "Accept: application/json" -H "Content-Type: application/json" -d "$apiPostData" -X POST "$apiUrl" | jq '.' | jq {exception} | jq '.[] == "UserAlreadyExistsException"')
if [ "$userResult" = "true" ]
then
echo "ERR: user already registered"
exit 1
else
if [ getUser ]; then
echo "Done, user created"
else
echo "ERR: user already exists or wrong API key"
exit 1
fi
fi
}
function deleteUser {
apiUrl="https://$apiHost:$apiPort/plugins/restapi/v1/users/$apiUser"
userResult=$(curl -sk -H "Authorization: $apiKey" -H "Accept: application/json" -H "Content-Type: application/json" -X DELETE "$apiUrl" | jq '.' | jq {exception} | jq '.[] == "UserNotFoundException"')
if [ "$userResult" = "true" ]
then
echo "ERR: no user with this username found. Didn't delete."
exit 1
else
if [ !getUser ]; then
echo "Done, user deleted"
else
echo "ERR: can't delete user or wrong API key"
exit 1
fi
fi
}
## get and execute action
if [ "$apiAction" = "get" ]
then
getUser
elif [ "$apiAction" = "list" ]
then
listUsers
elif [ "$apiAction" = "add" ]
then
addUser
elif [ "$apiAction" = "delete" ]
then
deleteUser
else
echo "ERR: wrong ACTION"
echo "$ofUsage"
echo "$ofActions"
exit 1
fi