Generic user group query function

Signed-off-by: Georg <georg@lysergic.dev>
This commit is contained in:
Georg Pfuetzenreuter 2021-09-02 21:08:18 +02:00
parent cdd640ab9e
commit 4f9b155d66
Signed by: Georg
GPG Key ID: 1DAF57F49F8E8F22

View File

@ -128,7 +128,7 @@ class Keycloak(callbacks.Plugin):
register = wrap(register, ['anything']) register = wrap(register, ['anything'])
def ircprom(self, irc, msg, args, option): def ircprom(self, irc, msg, args, option):
"""<status> """<option>
true/on = enable authentication to your IRC account with an SSO account going by the same username -- true/on = enable authentication to your IRC account with an SSO account going by the same username --
false/off = allow authentication to your IRC account ONLY with internal IRC credentials (NickServ) -- false/off = allow authentication to your IRC account ONLY with internal IRC credentials (NickServ) --
Warning: Enabling this without having an SSO account with the same username as your IRC nickname is a security risk.""" Warning: Enabling this without having an SSO account with the same username as your IRC nickname is a security risk."""
@ -218,6 +218,48 @@ class Keycloak(callbacks.Plugin):
ircprom = wrap(ircprom, ['anything']) ircprom = wrap(ircprom, ['anything'])
def user(self, irc, msg, args, option):
"""<option>
groups = dumps the groups you are joined to."""
user = msg.nick
server = self.registryValue('backend.server')
realm = self.registryValue('backend.realm')
tokenurl = self.registryValue('backend.token')
usererr = self.registryValue('replies.error')
try:
tokendl = requests.get(tokenurl)
tokendata = tokendl.json()
token = tokendata['access_token']
url = server + '/auth/admin/realms/' + realm + '/users'
userdata = requests.get(url, params = {'username': user}, headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token})
userresp = userdata.json()
uid = userresp[0]['id']
print(user, uid)
except:
print("ERROR: Keycloak token could not be installed.")
irc.error(usererr)
if option == 'groups':
try:
url = server + '/auth/admin/realms/' + realm + '/users/' + uid + '/groups'
response = requests.get(
url,
headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token})
test = "{}"
print(url)
usergroups = response.json()
if usergroups:
for group in usergroups:
groupname = usergroups[0]['name']
irc.reply(groupname)
else:
irc.reply("No groups.")
except:
print('Operation failed.')
irc.reply(usererr)
user = wrap(user, ['anything'])
Class = Keycloak Class = Keycloak