SSO and messages
* removes unused third party package from requirements.txt * adds KonovaSSOClient as subclass of sso.Client for more control over login, e.g. which user data shall not be updated (found in konoa/sso/sso.py) * adds Messenger class for communicating with SSO server (found in konova/utils/messenger.py)pull/9/head
parent
74f71cfd1c
commit
1a4a4a16c6
@ -0,0 +1,43 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 17.08.21
|
||||||
|
|
||||||
|
"""
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from simple_sso.sso_client.client import Client
|
||||||
|
|
||||||
|
|
||||||
|
class KonovaSSOClient(Client):
|
||||||
|
""" Konova specialized derivate of general sso.Client.
|
||||||
|
|
||||||
|
Adds some custom behaviour for konova usage.
|
||||||
|
|
||||||
|
"""
|
||||||
|
def build_user(self, user_data):
|
||||||
|
""" Creates a user or updates user data
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_data ():
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
user = User.objects.get(username=user_data['username'])
|
||||||
|
# Update user data, excluding some changes
|
||||||
|
skipable_attrs = {
|
||||||
|
"username",
|
||||||
|
"is_staff",
|
||||||
|
"is_superuser",
|
||||||
|
}
|
||||||
|
for _attr, _val in user_data.items():
|
||||||
|
if _attr in skipable_attrs:
|
||||||
|
continue
|
||||||
|
setattr(user, _attr, _val)
|
||||||
|
except User.DoesNotExist:
|
||||||
|
user = User(**user_data)
|
||||||
|
user.set_unusable_password()
|
||||||
|
user.save()
|
||||||
|
return user
|
@ -0,0 +1,62 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 17.08.21
|
||||||
|
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
from konova.settings import SSO_SERVER_BASE, SSO_PUBLIC_KEY
|
||||||
|
from konova.sub_settings.context_settings import BASE_TITLE_SHORT
|
||||||
|
|
||||||
|
|
||||||
|
class Messenger:
|
||||||
|
""" Used to send messages to the SSO server.
|
||||||
|
|
||||||
|
Messages can be seen by the user the next time they login on their SSO dashboard.
|
||||||
|
Documentation for SSO Server-Client communication can be found here:
|
||||||
|
https://git.naturschutz.rlp.de/SGD-Nord/arnova/wiki/Messages
|
||||||
|
|
||||||
|
"""
|
||||||
|
server_url = "{}communication/message/".format(SSO_SERVER_BASE)
|
||||||
|
|
||||||
|
def __init__(self, user: User, subject: str = None, body: str = None, type: str = None):
|
||||||
|
self.user = user
|
||||||
|
self.msg_subject = subject
|
||||||
|
self.msg_body = body
|
||||||
|
self.msg_type = type
|
||||||
|
|
||||||
|
def send(self) -> bool:
|
||||||
|
""" Sends the message
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
result (bool): True if successfully send, False otherwise
|
||||||
|
"""
|
||||||
|
if self.msg_body is None or len(self.msg_body) == 0:
|
||||||
|
raise AttributeError("No message body set")
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"x-services-public-key": SSO_PUBLIC_KEY
|
||||||
|
}
|
||||||
|
data = {
|
||||||
|
"type": self.msg_type,
|
||||||
|
"sender": BASE_TITLE_SHORT,
|
||||||
|
"receiver": self.user.username,
|
||||||
|
"subject": self.msg_subject,
|
||||||
|
"body": self.msg_body,
|
||||||
|
}
|
||||||
|
result = requests.post(
|
||||||
|
self.server_url,
|
||||||
|
data=data,
|
||||||
|
headers=headers
|
||||||
|
)
|
||||||
|
if result.status_code == 200:
|
||||||
|
result_content = json.loads(result.content)
|
||||||
|
success = result_content.get("success")
|
||||||
|
return success
|
||||||
|
else:
|
||||||
|
return False
|
Loading…
Reference in New Issue