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.""")))
###
# API related settings below:
# API backend related settings below:
###
conf.registerGroup(Keycloak, 'backend')
conf.registerGlobalValue(Keycloak.backend, 'server',
@ -90,4 +90,27 @@ conf.registerGlobalValue(Keycloak.replies, 'error',
, 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:

View File

@ -32,7 +32,7 @@ import re
import requests
import secrets
import string
from supybot import utils, plugins, ircutils, callbacks
from supybot import utils, plugins, ircutils, callbacks, ircmsgs
from supybot.commands import *
from supybot.ircmsgs import nick
try:
@ -56,20 +56,30 @@ class Keycloak(callbacks.Plugin):
realm = self.registryValue('backend.realm')
tokenurl = self.registryValue('backend.token')
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:
tokendl = requests.get(tokenurl)
tokendata = tokendl.json()
token = tokendata['access_token']
url = server + '/auth/admin/realms/' + realm + '/users'
except:
print("ERROR: Keycloak token could not be installed.")
irc.error(usererr)
if re.match(r"[^@]+@[^@]+\.[^@]+", email):
pw = random
payload = {
"firstName": "Foo",
"lastName": "Bar",
"firstName": firstname,
"lastName": lastname,
"email": email,
"enabled": "true",
"username": msg.nick,
"credentials": [{"type": "password", "value": "test123", "temporary": "true"}]
"credentials": [{"type": "password", "value": pw, "temporary": emailverified,}],
"emailVerified": "false"
}
response = requests.post(
url,
@ -77,15 +87,19 @@ class Keycloak(callbacks.Plugin):
json = payload
)
print("Keycloak: HTTP Status ", response.status_code)
if response.text:
try:
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())
except:
print("Keycloak: No or invalid response JSON. This it not an error.")
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:
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:
print("ERROR: Keycloak indicated that the request is invalid.")
irc.error(usererr)
@ -109,9 +123,6 @@ class Keycloak(callbacks.Plugin):
irc.error(usererr)
else:
irc.error("Is that a valid email address?")
except:
print("ERROR: Keycloak token could not be installed.")
irc.error(usererr)
register = wrap(register, ['anything'])