Add Discourse auth

This commit is contained in:
Tanner Collin 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( data = dict(
username=self.user.username, username=self.user.username,
password=self.data['new_password1'], 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.wiki_is_configured():
if utils_auth.set_wiki_password(data) != 200: 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) utils.alert_tanner(msg)
logger.info(msg) logger.info(msg)
raise ValidationError(dict(non_field_errors=msg)) raise ValidationError(dict(non_field_errors=msg))
@ -602,11 +611,20 @@ class MyPasswordResetConfirmSerializer(PasswordResetConfirmSerializer):
data = dict( data = dict(
username=self.user.username, username=self.user.username,
password=self.data['new_password1'], 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.wiki_is_configured():
if utils_auth.set_wiki_password(data) != 200: 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) utils.alert_tanner(msg)
logger.info(msg) logger.info(msg)
raise ValidationError(dict(non_field_errors=msg)) raise ValidationError(dict(non_field_errors=msg))
@ -653,10 +671,14 @@ class HistorySerializer(serializers.ModelSerializer):
class SpaceportAuthSerializer(LoginSerializer): class SpaceportAuthSerializer(LoginSerializer):
def authenticate(self, **kwargs): 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_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(): def wiki_is_configured():
return bool(secrets.WIKI_AUTH_API_URL and secrets.AUTH_API_KEY) 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): def auth_api(url, data):
try: try:
@ -25,3 +28,12 @@ def set_wiki_password(data):
password=data['password'], password=data['password'],
) )
return auth_api(secrets.WIKI_AUTH_API_URL + 'set-wiki-password', auth_data) 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 # with trailing slash
WIKI_AUTH_API_URL = '' 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 # Auth API key
# should be equal to the auth token value set in # should be equal to the auth token value set in
# spaceport/authserver/secrets.py # spaceport/authserver/secrets.py

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

@ -18,7 +18,7 @@ export function AuthForm(props) {
const handleSubmit = (e) => { const handleSubmit = (e) => {
if (input.username.includes('@')) { if (input.username.includes('@')) {
setError({ username: 'Username, not email.' }); setError({ username: 'Spaceport username, not email.' });
} else { } else {
if (loading) return; if (loading) return;
setLoading(true); setLoading(true);
@ -104,12 +104,43 @@ export function AuthWiki(props) {
<p>would like to request Spaceport authentication.</p> <p>would like to request Spaceport authentication.</p>
<p>URL: <a href='http://wiki.protospace.ca/Welcome_to_Protospace' target='_blank' rel='noopener noreferrer'>wiki.protospace.ca</a></p> <p>URL: <a href='https://wiki.protospace.ca/Welcome_to_Protospace' target='_blank' rel='noopener noreferrer'>wiki.protospace.ca</a></p>
<AuthForm user={user}> <AuthForm user={user}>
<Header size='small'>Success!</Header> <Header size='small'>Success!</Header>
<p>You can now log into the wiki:</p> <p>You can now log into the Wiki:</p>
<p><a href='http://wiki.protospace.ca/index.php?title=Special:UserLogin&returnto=Welcome+to+Protospace' rel='noopener noreferrer'>Protospace Wiki</a></p> <p>
Username: {user.username}<br/>
Password: [this Spaceport password]
</p>
<p><a href='https://wiki.protospace.ca/index.php?title=Special:UserLogin&returnto=Welcome+to+Protospace' rel='noopener noreferrer'>Protospace Wiki</a></p>
</AuthForm>
</Segment>
);
}
export function AuthDiscourse(props) {
const { user } = props;
return (
<Segment compact padded>
<Header size='medium'>
<Image src={'/discourselogo.png'} />
Protospace Discourse
</Header>
<p>would like to request Spaceport authentication.</p>
<p>URL: <a href='https://forum.protospace.ca' target='_blank' rel='noopener noreferrer'>forum.protospace.ca</a></p>
<AuthForm user={user}>
<Header size='small'>Success!</Header>
<p>You can now log into the Discourse:</p>
<p>
Username: {user.username}<br/>
Password: [this Spaceport password]
</p>
<p><a href='https://forum.protospace.ca' rel='noopener noreferrer'>Protospace Discourse</a></p>
</AuthForm> </AuthForm>
</Segment> </Segment>
); );
@ -127,6 +158,10 @@ export function Auth(props) {
<Route path='/auth/wiki'> <Route path='/auth/wiki'>
<AuthWiki user={user} /> <AuthWiki user={user} />
</Route> </Route>
<Route path='/auth/discourse'>
<AuthDiscourse user={user} />
</Route>
</Container> </Container>
); );
} }