Fixed authcallbacks

This commit is contained in:
Adolfo Gómez García 2022-02-23 21:54:23 +01:00
parent 1fba4d3f9f
commit ee30ab4604
2 changed files with 27 additions and 18 deletions

View File

@ -32,6 +32,7 @@ import logging
import typing import typing
from django.urls import reverse from django.urls import reverse
from django.db.models import Q
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.views.decorators.cache import never_cache from django.views.decorators.cache import never_cache
@ -79,10 +80,19 @@ def authCallback(request: HttpRequest, authName: str) -> HttpResponse:
an authenticator that has an authCallback an authenticator that has an authCallback
""" """
try: try:
authenticator = Authenticator.objects.get(name=authName) authenticator = Authenticator.objects.filter(Q(name=authName) | Q(small_name=authName)).order_by('priority').first()
params = request.GET.copy() if not authenticator:
params.update(request.POST) raise Exception('Authenticator not found')
params['_query'] = request.META.get('QUERY_STRING', '')
params = {
'https': request.is_secure(),
'http_host': request.META['HTTP_HOST'],
'path_info': request.META['PATH_INFO'],
'server_port': request.META['SERVER_PORT'],
'get_data': request.GET.copy(),
'post_data': request.POST.copy(),
'query_string': request.META['QUERY_STRING'],
}
logger.debug( logger.debug(
'Auth callback for %s with params %s', authenticator, params.keys() 'Auth callback for %s with params %s', authenticator, params.keys()
@ -103,15 +113,6 @@ def authCallback_stage2(
params: typing.Dict[str, typing.Any] = ticket['params'] params: typing.Dict[str, typing.Any] = ticket['params']
auth_uuid: str = ticket['auth'] auth_uuid: str = ticket['auth']
authenticator = Authenticator.objects.get(uuid=auth_uuid) authenticator = Authenticator.objects.get(uuid=auth_uuid)
params['_request'] = request
# params['_session'] = request.session
# params['_user'] = request.user
logger.debug(
'Request session:%s -> %s, %s',
request.ip,
request.session.keys(),
request.session.session_key,
)
user = authenticateViaCallback(authenticator, params) user = authenticateViaCallback(authenticator, params)

View File

@ -36,7 +36,7 @@ from django.shortcuts import render
from django.http import HttpRequest, HttpResponse, JsonResponse, HttpResponseRedirect from django.http import HttpRequest, HttpResponse, JsonResponse, HttpResponseRedirect
from django.urls import reverse from django.urls import reverse
from uds.core.util.request import ExtendedHttpRequest, ExtendedHttpRequestWithUser from uds.core.util.request import ExtendedHttpRequest, ExtendedHttpRequestWithUser
from uds.core.auths import auth from uds.core.auths import auth, exceptions
from uds.web.util import errors from uds.web.util import errors
from uds.web.forms.LoginForm import LoginForm from uds.web.forms.LoginForm import LoginForm
@ -103,10 +103,18 @@ def login(
def logout(request: ExtendedHttpRequestWithUser) -> HttpResponse: def logout(request: ExtendedHttpRequestWithUser) -> HttpResponse:
auth.authLogLogout(request) auth.authLogLogout(request)
request.session['restricted'] = False # Remove restricted request.session['restricted'] = False # Remove restricted
logoutUrl = request.user.logout() try:
if logoutUrl is None: logoutUrl = request.user.logout()
logoutUrl = request.session.get('logouturl', None) if logoutUrl is None:
return auth.webLogout(request, logoutUrl) logoutUrl = request.session.get('logouturl', None)
return auth.webLogout(request, logoutUrl)
except exceptions.Redirect as e:
return HttpResponseRedirect(
request.build_absolute_uri(str(e)) if e.args and e.args[0] else '/'
)
except Exception as e:
logger.exception('Error logging out user')
return auth.webLogout(request, None)
def js(request: ExtendedHttpRequest) -> HttpResponse: def js(request: ExtendedHttpRequest) -> HttpResponse: