1.0
[auf_django_saml.git] / auf / django / saml / views.py
1 # -*- coding: utf-8 -*-
2
3 from django.http import HttpResponse
4 from django.core.urlresolvers import reverse
5 from django.contrib.auth import login as auth_login
6 from django.contrib.auth import logout as auth_logout
7 from django.template import RequestContext
8 from django.shortcuts import render_to_response, redirect
9 from forms import RemoteUserForm
10 from settings import SAML_REDIRECT_FIELD_NAME,\
11 SAML_MELLON_LOGIN_URL,\
12 SAML_MELLON_LOGOUT_URL,\
13 SAML_CHANGE_PASSWORD_URL,\
14 SAML_LOGOUT_REDIRECT_URL
15
16
17 def redirect_to_login(request, redirect_to=None, do_redirect=True):
18 if redirect_to is None:
19 redirect_to = request.get_full_path()
20 url = "%s?%s=%s" % (SAML_MELLON_LOGIN_URL,
21 SAML_REDIRECT_FIELD_NAME,
22 redirect_to,
23 )
24 if do_redirect:
25 return redirect(url)
26 else:
27 return url
28
29
30 def redirect_to_logout(request, redirect_to=None, do_redirect=True):
31 if redirect_to is None:
32 redirect_to = SAML_LOGOUT_REDIRECT_URL
33 url = "%s?%s=%s" % (reverse('local_logout'),
34 SAML_REDIRECT_FIELD_NAME,
35 redirect_to,
36 )
37 if do_redirect:
38 return redirect(url)
39 else:
40 return url
41
42
43 def login_form(request, ):
44 """
45 Page de login en mode développement
46 permet de se connecter avec un user selon son username défini localemement
47 """
48 redirect_to = request.REQUEST.get(SAML_REDIRECT_FIELD_NAME, '/')
49 if request.method == "POST":
50 form = RemoteUserForm(request, request.POST)
51 if form.is_valid():
52 auth_login(request, form.user)
53 return redirect(redirect_to)
54 else:
55 form = RemoteUserForm(request)
56
57 c = {'form': form}
58 return render_to_response("saml/login_form.html",
59 c,
60 context_instance=RequestContext(request))
61
62
63 def local_logout(request, ):
64 """
65 Logout pour SAML pour détruire la session Django
66 """
67 query_string = request.META['QUERY_STRING']
68 auth_logout(request)
69 logout_url = "%s?%s" % (SAML_MELLON_LOGOUT_URL, query_string)
70 response = HttpResponse(content="", status=303)
71 response["Location"] = logout_url
72 return response
73
74
75 def mellon_logout(request, ):
76 """
77 Simule la vue qui de mellon qui initie le logout sur le l'IdP
78 """
79 redirect_to = request.REQUEST.get(SAML_REDIRECT_FIELD_NAME, '/')
80 return redirect(redirect_to)
81
82
83 def password_change(request, ):
84 return redirect(SAML_CHANGE_PASSWORD_URL)