Add Auth API connection to apiserver

This commit is contained in:
Tanner Collin 2020-09-16 22:52:17 +00:00
parent 649b2061bc
commit b45f86e0cc
3 changed files with 56 additions and 1 deletions

View File

@ -473,6 +473,13 @@ class MyPasswordChangeSerializer(PasswordChangeSerializer):
logger.info(msg) logger.info(msg)
raise ValidationError(dict(non_field_errors=msg)) raise ValidationError(dict(non_field_errors=msg))
if utils_auth.is_configured():
if utils_auth.set_password(data) != 200:
msg = 'Problem connecting to Auth server: set.'
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
super().save() super().save()
class MyPasswordResetSerializer(PasswordResetSerializer): class MyPasswordResetSerializer(PasswordResetSerializer):
@ -490,7 +497,17 @@ class MyPasswordResetConfirmSerializer(PasswordResetConfirmSerializer):
if utils_ldap.is_configured(): if utils_ldap.is_configured():
if utils_ldap.set_password(data) != 200: if utils_ldap.set_password(data) != 200:
raise ValidationError(dict(non_field_errors='Problem connecting to LDAP server: set.')) msg = 'Problem connecting to LDAP server: set.'
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
if utils_auth.is_configured():
if utils_auth.set_password(data) != 200:
msg = 'Problem connecting to Auth server: set.'
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
super().save() super().save()

View File

@ -0,0 +1,28 @@
import logging
logger = logging.getLogger(__name__)
import requests
from apiserver import secrets
from apiserver.api import utils
def is_configured():
return bool(secrets.AUTH_API_URL and secrets.AUTH_API_KEY)
def auth_api(route, data):
try:
headers = {'Authorization': 'Token ' + secrets.AUTH_API_KEY}
url = secrets.AUTH_API_URL + route
r = requests.post(url, data=data, headers=headers, timeout=3)
return r.status_code
except BaseException as e:
logger.error('Auth {} - {} - {}'.format(url, e.__class__.__name__, str(e)))
return None
def set_password(data):
auth_data = dict(
username=data['username'],
password=data['password1'],
)
return auth_api('set-password', auth_data)

View File

@ -40,6 +40,16 @@ LDAP_API_URL = ''
# spaceport/ldapserver/secrets.py # spaceport/ldapserver/secrets.py
LDAP_API_KEY = '' LDAP_API_KEY = ''
# Auth API url
# should contain the IP and port of the script and machine connected over VPN
# with trailing slash
AUTH_API_URL = ''
# Auth API key
# should be equal to the auth token value set in
# spaceport/authserver/secrets.py
AUTH_API_KEY = ''
# Door cards API token # Door cards API token
# Set this to random characters # Set this to random characters
# For example, use the output of this: # For example, use the output of this: