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