First fully functional user registration.

Signed-off-by: Georg <georg@lysergic.dev>
This commit is contained in:
Georg Pfuetzenreuter 2021-09-01 02:24:51 +02:00
parent b747836374
commit 474d16ba94
Signed by: Georg
GPG Key ID: 1DAF57F49F8E8F22
2 changed files with 83 additions and 49 deletions

View File

@ -53,7 +53,7 @@ Keycloak = conf.registerPlugin('Keycloak')
# registry.Boolean(False, _("""Help for someConfigVariableName."""))) # registry.Boolean(False, _("""Help for someConfigVariableName.""")))
### ###
# API related settings below: # API backend related settings below:
### ###
conf.registerGroup(Keycloak, 'backend') conf.registerGroup(Keycloak, 'backend')
conf.registerGlobalValue(Keycloak.backend, 'server', conf.registerGlobalValue(Keycloak.backend, 'server',
@ -90,4 +90,27 @@ conf.registerGlobalValue(Keycloak.replies, 'error',
, private=False , private=False
)) ))
###
# API call settings below:
###
conf.registerGroup(Keycloak, 'options')
conf.registerGlobalValue(Keycloak.options, 'emailVerified',
registry.Boolean(False,
"""
Keycloak: Whether to set newly created users email addresses to having been verified \(true, default\) or not \(false\)
"""
))
conf.registerGlobalValue(Keycloak.options, 'firstName',
registry.String('Foo',
"""
Keycloak: What to set as the firstName value for newly created users.
"""
))
conf.registerGlobalValue(Keycloak.options, 'lastName',
registry.String('Bar',
"""
Keycloak: What to set as the lastName value for newly created users.
"""
))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -32,7 +32,7 @@ import re
import requests import requests
import secrets import secrets
import string import string
from supybot import utils, plugins, ircutils, callbacks from supybot import utils, plugins, ircutils, callbacks, ircmsgs
from supybot.commands import * from supybot.commands import *
from supybot.ircmsgs import nick from supybot.ircmsgs import nick
try: try:
@ -56,20 +56,30 @@ class Keycloak(callbacks.Plugin):
realm = self.registryValue('backend.realm') realm = self.registryValue('backend.realm')
tokenurl = self.registryValue('backend.token') tokenurl = self.registryValue('backend.token')
usererr = self.registryValue('replies.error') usererr = self.registryValue('replies.error')
emailverified = self.registryValue('options.emailVerified')
firstname = self.registryValue('options.firstName')
lastname = self.registryValue('options.lastName')
alphabet = string.ascii_letters + string.digits
random = ''.join(secrets.choice(alphabet) for i in range(64))
try: try:
tokendl = requests.get(tokenurl) tokendl = requests.get(tokenurl)
tokendata = tokendl.json() tokendata = tokendl.json()
token = tokendata['access_token'] token = tokendata['access_token']
url = server + '/auth/admin/realms/' + realm + '/users' url = server + '/auth/admin/realms/' + realm + '/users'
except:
print("ERROR: Keycloak token could not be installed.")
irc.error(usererr)
if re.match(r"[^@]+@[^@]+\.[^@]+", email): if re.match(r"[^@]+@[^@]+\.[^@]+", email):
pw = random
payload = { payload = {
"firstName": "Foo", "firstName": firstname,
"lastName": "Bar", "lastName": lastname,
"email": email, "email": email,
"enabled": "true", "enabled": "true",
"username": msg.nick, "username": msg.nick,
"credentials": [{"type": "password", "value": "test123", "temporary": "true"}] "credentials": [{"type": "password", "value": pw, "temporary": emailverified,}],
"emailVerified": "false"
} }
response = requests.post( response = requests.post(
url, url,
@ -77,15 +87,19 @@ class Keycloak(callbacks.Plugin):
json = payload json = payload
) )
print("Keycloak: HTTP Status ", response.status_code) print("Keycloak: HTTP Status ", response.status_code)
if response.text: try:
print("Keycloak: Response Text: ", response.text) print("Keycloak: Response Text: ", response.text)
except:
print("Keycloak: No or invalid response text. This is not an error.")
try:
print("Keycloak: Response JSON: ", response.json()) print("Keycloak: Response JSON: ", response.json())
except:
print("Keycloak: No or invalid response JSON. This it not an error.")
status = response.status_code status = response.status_code
#To-Do: figure out why this needs to bere instead of being fed from the usererr config variable defined above
#usererr = irc.error("Something went wrong. Please contact an administrator.")
if status == 201: if status == 201:
print(" SSO User " + msg.nick + " created.") print(" SSO User " + msg.nick + " created.")
irc.reply("OK, please log in and change your password NOW.") irc.queueMsg(msg=ircmsgs.IrcMsg(command='PRIVMSG', args=(msg.nick, f'{pw}')))
irc.reply("OK, I sent you a private message.")
if status == 400: if status == 400:
print("ERROR: Keycloak indicated that the request is invalid.") print("ERROR: Keycloak indicated that the request is invalid.")
irc.error(usererr) irc.error(usererr)
@ -109,9 +123,6 @@ class Keycloak(callbacks.Plugin):
irc.error(usererr) irc.error(usererr)
else: else:
irc.error("Is that a valid email address?") irc.error("Is that a valid email address?")
except:
print("ERROR: Keycloak token could not be installed.")
irc.error(usererr)
register = wrap(register, ['anything']) register = wrap(register, ['anything'])