Add a special login form for the community auth logins
authorMagnus Hagander <magnus@hagander.net>
Mon, 19 Dec 2011 17:08:13 +0000 (18:08 +0100)
committerMagnus Hagander <magnus@hagander.net>
Thu, 22 Dec 2011 18:25:32 +0000 (19:25 +0100)
This form will inform about the auth system, and also which site is
trying to perform the login - just to make it extra clear that this
login is from the outside.

pgweb/account/views.py
templates/account/communityauth.html [new file with mode: 0644]

index dd7aff23c461f24d330be14be0fc49fe96ae2614..b20e7dbdbb97f15d5d61bddde5334ce444ec5bb0 100644 (file)
@@ -167,27 +167,49 @@ def signup_complete(request):
 ####
 ## Community authentication endpoint
 ####
+from django.views.decorators.csrf import csrf_protect
+
 @ssl_required
-@login_required
+@csrf_protect
 def communityauth(request, siteid):
+       # Get whatever site the user is trying to log in to.
+       site = get_object_or_404(CommunityAuthSite, pk=siteid)
+
+       if request.GET.has_key('su'):
+               su = request.GET['su']
+               if not su.startswith('/'):
+                       su = None
+       else:
+               su = None
+
+       # Verify if the user is authenticated, and if he/she is not, generate
+       # a login form that has information about which site is being logged
+       # in to, and basic information about how the community login system
+       # works.
+       if not request.user.is_authenticated():
+               if su:
+                       suburl = "?su=%s" % su
+               else:
+                       suburl = ""
+               return render_to_response('account/communityauth.html', {
+                               'sitename': site.name,
+                               'next': '/account/auth/%s/%s' % (siteid, suburl),
+                               }, NavContext(request, 'account'))
+
+
        # When we reach this point, the user *has* already been authenticated.
        # The request variable "su" *may* contain a suburl and should in that
        # case be passed along to the site we're authenticating for. And of
        # course, we fill a structure with information about the user.
 
-       site = get_object_or_404(CommunityAuthSite, pk=siteid)
-
        info = {
                'u': request.user.username,
                'f': request.user.first_name,
                'l': request.user.last_name,
                'e': request.user.email,
                }
-       if request.GET.has_key('su'):
-               if request.GET['su'].startswith('/'):
-                       info.update({
-                                       'su': request.GET['su']
-                                       })
+       if su:
+               info['su'] = request.GET['su']
 
        # URL-encode the structure
        s = urllib.urlencode(info)
diff --git a/templates/account/communityauth.html b/templates/account/communityauth.html
new file mode 100644 (file)
index 0000000..212a89c
--- /dev/null
@@ -0,0 +1,38 @@
+{%extends "base/page.html"%}
+{%block contents%}
+<h1>Community authentication</h1>
+<p>
+The website you are trying to log in to ({{sitename}}) is using the
+postgresql.org community login system. In this system you create a
+central account that is used to log into most postgresql.org services.
+Once you are logged into this account, you will automatically be
+logged in to the associated postgresql.org services. Note that this
+single sign on is only used for official postgresql.org websites.
+</p>
+<p>
+If you do not already have an account, you may
+<a href="/account/signup/">sign up</a> for one now.
+If you have one but have lost your
+password, you can use the <a href="/account/reset/">password reset</a> form.
+</p>
+
+<form action="/account/login/" method="post" id="login-form">{% csrf_token %}
+  <div class="form-row">
+    <label for="id_username">Username:</label> <input type="text" name="username" id="id_username" />
+  </div>
+  <div class="form-row">
+    <label for="id_password">Password:</label> <input type="password" name="password" id="id_password" />
+    <input type="hidden" name="this_is_the_login_form" value="1" />
+    <input type="hidden" name="next" value="{{next}}" />
+  </div>
+  <div class="submit-row">
+    <label>&nbsp;</label><input type="submit" value="Log in" />
+  </div>
+</form>
+
+<script type="text/javascript">
+document.getElementById('id_username').focus()
+</script>
+
+{%endblock%}
+