This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
authentic2-beid/src/authentic2_beid/views.py

60 lines
2.3 KiB
Python

import logging
from django.utils.translation import ugettext as _
from django.template.loader import render_to_string
from django.template import RequestContext
from django.contrib.auth import authenticate, login
from django.contrib import messages
from authentic2.auth2_auth.auth2_ssl import models, util
from authentic2.utils import continue_to_next_url, redirect
from .backends import BeIDBackend
logger = logging.getLogger(__name__)
def handle_authentication(request, *args, **kwargs):
ssl_info = util.SSLInfo(request)
logger.debug('received SSL info: %s', ssl_info)
user = authenticate(ssl_info=ssl_info)
logger.debug('got user: %s', user)
if not request.user.is_authenticated():
login(request, user)
return continue_to_next_url(request)
def add_beid(request):
if request.user.is_authenticated:
ssl_info = util.SSLInfo(request)
if BeIDBackend().link_user(ssl_info, request.user):
logger.info('Successful linking of the SSL '
'Certificate to an account, redirection to %s' % next_url)
messages.info(request, _('BeID card successfully linked to your account'))
else:
logger.error('eid linking failed')
messages.error(request, _('BeID linking failed. Internal server error.'))
else:
return redirect(request, 'account_management',
fragment='a2-beid-certificate-profile')
def profile(request, template_name='beid/profile.html', *args, **kwargs):
context_instance = kwargs.pop('context_instance', None) or \
RequestContext(request)
certificates = models.ClientCertificate.objects.filter(user=request.user)
ctx = {'certificates': certificates}
return render_to_string(template_name, ctx,
context_instance=context_instance)
def delete_beid(request, certificate_pk):
try:
beid = models.ClientCertificate.objects.get(pk=certificate_pk)
beid.delete()
logger.info('client certificate %s deleted', beid)
messages.info(request, _('Your BeID card informations are successfully deleted'))
except models.ClientCertificate.DoesNotExist:
logger.info('no client certificate %s', certificate_pk)
messages.error(request, _('No BeID card associated to this account'))
return redirect(request, 'account_management',
fragment='a2-beid-certificate-profile')