f2af6f91d736368bd43c19e0a90e7b87e88a62b3
[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 SAML_AUTH
16
17
18 def redirect_to_login(request, redirect_to=None, do_redirect=True):
19 if redirect_to is None:
20 redirect_to = request.get_full_path()
21 if SAML_AUTH:
22 base_url = SAML_MELLON_LOGIN_URL
23 else:
24 base_url = reverse('sandbox_login')
25 url = "%s?%s=%s" % (base_url,
26 SAML_REDIRECT_FIELD_NAME,
27 redirect_to,
28 )
29 if do_redirect:
30 return redirect(url)
31 else:
32 return url
33
34
35 def redirect_to_logout(request, redirect_to=None, do_redirect=True):
36 if redirect_to is None:
37 redirect_to = SAML_LOGOUT_REDIRECT_URL
38 url = "%s?%s=%s" % (reverse('local_logout'),
39 SAML_REDIRECT_FIELD_NAME,
40 redirect_to,
41 )
42 if do_redirect:
43 return redirect(url)
44 else:
45 return url
46
47
48 def login_form(request, ):
49 """
50 Page de login en mode développement
51 permet de se connecter avec un user selon son username défini localemement
52 """
53 redirect_to = request.REQUEST.get(SAML_REDIRECT_FIELD_NAME, '/')
54 if request.method == "POST":
55 form = RemoteUserForm(request, request.POST)
56 if form.is_valid():
57 auth_login(request, form.user)
58 return redirect(redirect_to)
59 else:
60 form = RemoteUserForm(request)
61
62 c = {'form': form}
63 return render_to_response("saml/login_form.html",
64 c,
65 context_instance=RequestContext(request))
66
67
68 def local_logout(request, ):
69 """
70 Logout pour SAML pour détruire la session Django
71 """
72 query_string = request.META['QUERY_STRING']
73 auth_logout(request)
74 if SAML_AUTH:
75 base_url = SAML_MELLON_LOGOUT_URL
76 else:
77 base_url = reverse('sandbox_logout')
78 logout_url = "%s?%s" % (base_url, query_string)
79 response = HttpResponse(content="", status=303)
80 response["Location"] = logout_url
81 return response
82
83
84 def mellon_logout(request, ):
85 """
86 Simule la vue qui de mellon qui initie le logout sur le l'IdP
87 """
88 redirect_to = request.REQUEST.get(SAML_REDIRECT_FIELD_NAME, '/')
89 return redirect(redirect_to)
90
91
92 def password_change(request, ):
93 return redirect(SAML_CHANGE_PASSWORD_URL)