Add Discourse auth

This commit is contained in:
2021-09-05 00:47:44 +00:00
parent fa8d68ad52
commit 77def611cc
6 changed files with 84 additions and 10 deletions

View File

@@ -561,11 +561,20 @@ class MyPasswordChangeSerializer(PasswordChangeSerializer):
data = dict(
username=self.user.username,
password=self.data['new_password1'],
email=self.user.email,
first_name=self.user.member.first_name,
)
if utils_auth.wiki_is_configured():
if utils_auth.set_wiki_password(data) != 200:
msg = 'Problem connecting to Auth server: set.'
msg = 'Problem connecting to Wiki Auth server: set.'
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
if utils_auth.discourse_is_configured():
if utils_auth.set_discourse_password(data) != 200:
msg = 'Problem connecting to Discourse Auth server: set.'
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
@@ -602,11 +611,20 @@ class MyPasswordResetConfirmSerializer(PasswordResetConfirmSerializer):
data = dict(
username=self.user.username,
password=self.data['new_password1'],
email=self.user.email,
first_name=self.user.member.first_name,
)
if utils_auth.wiki_is_configured():
if utils_auth.set_wiki_password(data) != 200:
msg = 'Problem connecting to Auth server: set.'
msg = 'Problem connecting to Wiki Auth server: set.'
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
if utils_auth.discourse_is_configured():
if utils_auth.set_discourse_password(data) != 200:
msg = 'Problem connecting to Discourse Auth server: set.'
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
@@ -653,10 +671,14 @@ class HistorySerializer(serializers.ModelSerializer):
class SpaceportAuthSerializer(LoginSerializer):
def authenticate(self, **kwargs):
result = super().authenticate(**kwargs)
user = super().authenticate(**kwargs)
if user:
data = self.context['request'].data.copy()
data['email'] = user.email
data['first_name'] = user.member.first_name
if result:
data = self.context['request'].data
utils_auth.set_wiki_password(data)
utils_auth.set_discourse_password(data)
return result
return user

View File

@@ -9,6 +9,9 @@ from apiserver.api import utils
def wiki_is_configured():
return bool(secrets.WIKI_AUTH_API_URL and secrets.AUTH_API_KEY)
def discourse_is_configured():
return bool(secrets.DISCOURSE_AUTH_API_URL and secrets.AUTH_API_KEY)
def auth_api(url, data):
try:
@@ -25,3 +28,12 @@ def set_wiki_password(data):
password=data['password'],
)
return auth_api(secrets.WIKI_AUTH_API_URL + 'set-wiki-password', auth_data)
def set_discourse_password(data):
auth_data = dict(
username=data['username'],
password=data['password'],
first_name=data['first_name'],
email=data['email'],
)
return auth_api(secrets.DISCOURSE_AUTH_API_URL + 'set-discourse-password', auth_data)

View File

@@ -45,6 +45,11 @@ LDAP_API_KEY = ''
# with trailing slash
WIKI_AUTH_API_URL = ''
# Discourse Auth API url
# should contain the IP and port of the script and machine connected over VPN
# with trailing slash
DISCOURSE_AUTH_API_URL = ''
# Auth API key
# should be equal to the auth token value set in
# spaceport/authserver/secrets.py