summaryrefslogtreecommitdiffstats
path: root/plugin.py
diff options
context:
space:
mode:
authorGeorg2021-09-01 02:24:51 +0200
committerGeorg2021-09-01 02:24:51 +0200
commit474d16ba946b31c7fa13c9365138d8245d09f724 (patch)
tree6a059a465134a1d4d6c07d2c49ad076f0974a17a /plugin.py
parentb747836374ef426079bfe3722bbc5dbba30d86a8 (diff)
downloadkeycloak-474d16ba946b31c7fa13c9365138d8245d09f724.tar.gz
keycloak-474d16ba946b31c7fa13c9365138d8245d09f724.tar.bz2
keycloak-474d16ba946b31c7fa13c9365138d8245d09f724.zip
First fully functional user registration.
Signed-off-by: Georg <georg@lysergic.dev>
Diffstat (limited to 'plugin.py')
-rw-r--r--plugin.py107
1 files changed, 59 insertions, 48 deletions
diff --git a/plugin.py b/plugin.py
index 0ca7894..561fcb0 100644
--- a/plugin.py
+++ b/plugin.py
@@ -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,62 +56,73 @@ 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'
- if re.match(r"[^@]+@[^@]+\.[^@]+", email):
- payload = {
- "firstName": "Foo",
- "lastName": "Bar",
- "email": email,
- "enabled": "true",
- "username": msg.nick,
- "credentials": [{"type": "password", "value": "test123", "temporary": "true"}]
- }
- response = requests.post(
- url,
- headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token},
- json = payload
- )
- print("Keycloak: HTTP Status ", response.status_code)
- if response.text:
- print("Keycloak: Response Text: ", response.text)
- print("Keycloak: Response JSON: ", response.json())
- 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.")
- if status == 400:
- print("ERROR: Keycloak indicated that the request is invalid.")
- irc.error(usererr)
- if status == 401:
- print("ERROR: Fix your Keycloak API credentials and/or client roles, doh.")
- irc.error(usererr)
- if status == 403:
- print("ERROR: Keycloak indicated that the authorization provided is not enough to access the resource.")
- irc.error(usererr)
- if status == 404:
- print("ERROR: Keycloak indicated that the requested resource does not exist.")
- irc.error(usererr)
- if status == 409:
- print("ERROR: Keycloak indicated that the resource already exists or \"some other coonflict when processing the request\" occured.")
- irc.reply("Your username seems to already be registerd.")
- if status == 415:
- print("ERROR: Keycloak indicated that the requested media type is not supported.")
- irc.error(usererr)
- if status == 500:
- print("ERROR: Keycloak indicated that the server could not fullfill the request due to \"some unexpected error \".")
- irc.error(usererr)
- else:
- irc.error("Is that a valid email address?")
except:
print("ERROR: Keycloak token could not be installed.")
irc.error(usererr)
+ if re.match(r"[^@]+@[^@]+\.[^@]+", email):
+ pw = random
+ payload = {
+ "firstName": firstname,
+ "lastName": lastname,
+ "email": email,
+ "enabled": "true",
+ "username": msg.nick,
+ "credentials": [{"type": "password", "value": pw, "temporary": emailverified,}],
+ "emailVerified": "false"
+ }
+ response = requests.post(
+ url,
+ headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token},
+ json = payload
+ )
+ print("Keycloak: HTTP Status ", response.status_code)
+ 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
+ if status == 201:
+ print(" SSO User " + msg.nick + " created.")
+ 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)
+ if status == 401:
+ print("ERROR: Fix your Keycloak API credentials and/or client roles, doh.")
+ irc.error(usererr)
+ if status == 403:
+ print("ERROR: Keycloak indicated that the authorization provided is not enough to access the resource.")
+ irc.error(usererr)
+ if status == 404:
+ print("ERROR: Keycloak indicated that the requested resource does not exist.")
+ irc.error(usererr)
+ if status == 409:
+ print("ERROR: Keycloak indicated that the resource already exists or \"some other coonflict when processing the request\" occured.")
+ irc.reply("Your username seems to already be registerd.")
+ if status == 415:
+ print("ERROR: Keycloak indicated that the requested media type is not supported.")
+ irc.error(usererr)
+ if status == 500:
+ print("ERROR: Keycloak indicated that the server could not fullfill the request due to \"some unexpected error \".")
+ irc.error(usererr)
+ else:
+ irc.error("Is that a valid email address?")
register = wrap(register, ['anything'])