patch hidden field setting code
authorJoey Hess <joey@kitenet.net>
Fri, 7 May 2010 23:10:50 +0000 (19:10 -0400)
committerJoey Hess <joey@kitenet.net>
Fri, 7 May 2010 23:10:50 +0000 (19:10 -0400)
Fixes http://code.google.com/p/openid-selector/issues/detail?id=11#c3

IkiWiki/Plugin/openid.pm
debian/changelog
debian/copyright
doc/templates.mdwn
templates/openid-selector.tmpl [new file with mode: 0644]
underlays/javascript/ikiwiki/openid-selector/openid-jquery.js

index 7b1a17831f9155ea7034a528198ee864bb5cf6e4..0ad8697d872be8517394ea5366bb4ea55c42e6d2 100644 (file)
@@ -7,6 +7,7 @@ use strict;
 use IkiWiki 3.00;
 
 sub import {
+       hook(type => "checkconfig", id => "openid", call => \&checkconfig);
        hook(type => "getopt", id => "openid", call => \&getopt);
        hook(type => "getsetup", id => "openid", call => \&getsetup);
        hook(type => "auth", id => "openid", call => \&auth);
@@ -14,6 +15,18 @@ sub import {
                call => \&formbuilder_setup, last => 1);
 }
 
+sub checkconfig () {
+       if ($config{cgi}) {
+               # Intercept normal signin form, so the openid selector
+               # can be displayed.
+               require IkiWiki::CGI;
+               my $real_cgi_signin=\&IkiWiki::cgi_signin;
+               inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
+                               openid_selector($real_cgi_signin, @_);
+               });
+       }
+}
+
 sub getopt () {
        eval q{use Getopt::Long};
        error($@) if $@;
@@ -37,6 +50,39 @@ sub getsetup () {
                },
 }
 
+sub openid_selector {
+       my $real_cgi_signin=shift;
+        my $q=shift;
+        my $session=shift;
+
+       my $openid_url=$q->param('openid_url');
+       my $openid_error;
+
+       if (defined $q->param("action") && $q->param("action") eq "verify") {
+               validate($q, $session, $openid_url, sub {
+                       $openid_error=shift;
+               });
+       }
+       elsif ($q->param("do") eq "signin" || ! load_openid_module()) {
+               $real_cgi_signin->($q, $session);
+               exit;
+       }
+
+       my $template=IkiWiki::template("openid-selector.tmpl");
+       $template->param(
+               cgiurl => $config{cgiurl},
+               (defined $openid_error ? (openid_error => $openid_error) : ()),
+               (defined $openid_url ? (openid_url => $openid_url) : ()),
+               # TODO only if other auth methods are available
+               nonopenidurl => IkiWiki::cgiurl(do => "signin"),
+               loginlabel => loginlabel(),
+       );
+
+       IkiWiki::printheader($session);
+       print IkiWiki::misctemplate("signin", $template->output);
+       exit;
+}
+
 sub formbuilder_setup (@) {
        my %params=@_;
 
@@ -45,13 +91,7 @@ sub formbuilder_setup (@) {
        my $cgi=$params{cgi};
        
        if ($form->title eq "signin") {
-               # Give up if module is unavailable to avoid
-               # needing to depend on it.
-               eval q{use Net::OpenID::Consumer};
-               if ($@) {
-                       debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
-                       return;
-               }
+               return unless load_openid_module();
 
                # This avoids it displaying a redundant label for the
                # OpenID fieldset.
@@ -59,7 +99,7 @@ sub formbuilder_setup (@) {
 
                $form->field(
                        name => "openid_url",
-                       label => gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
+                       label => loginlabel(),
                        fieldset => "OpenID",
                        size => 30,
                        comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
@@ -72,7 +112,10 @@ sub formbuilder_setup (@) {
                        $form->field(
                                name => "openid_url",
                                validate => sub {
-                                       validate($cgi, $session, shift, $form);
+                                       validate($cgi, $session, shift, sub {
+                                               # Display error in the form.
+                                               $form->field(name => "openid_url", comment => shift);
+                                       });
                                },
                        );
                        # Skip all other required fields in this case.
@@ -98,15 +141,14 @@ sub validate ($$$;$) {
        my $q=shift;
        my $session=shift;
        my $openid_url=shift;
-       my $form=shift;
+       my $errhandler=shift;
 
        my $csr=getobj($q, $session);
 
        my $claimed_identity = $csr->claimed_identity($openid_url);
        if (! $claimed_identity) {
-               if ($form) {
-                       # Put the error in the form and fail validation.
-                       $form->field(name => "openid_url", comment => $csr->err);
+               if ($errhandler) {
+                       $errhandler->($csr->err);
                        return 0;
                }
                else {
@@ -230,4 +272,18 @@ sub getobj ($$) {
        );
 }
 
+sub load_openid_module {
+       # Give up if module is unavailable to avoid needing to depend on it.
+       eval q{use Net::OpenID::Consumer};
+       if ($@) {
+               debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
+               return;
+       }
+       return 1;
+}
+
+sub loginlabel {
+       return gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1);
+}
+
 1
index 31d64e2372b24854a3837cd47ced8cecfb529ada..c335030ed37a9cf7d37541e2e999eb35a5b7a0e6 100644 (file)
@@ -10,6 +10,8 @@ ikiwiki (3.20100505) UNRELEASED; urgency=low
   * inline: Call indexhtml when inlining internal pages, so their
     text can be indexed for searching.
   * Delete hooks are passed deleted internal pages. 
+  * openid: Incorporated a fancy openid-selector signin form
+    (http://code.google.com/p/openid-selector/).
 
  -- Joey Hess <joeyh@debian.org>  Wed, 05 May 2010 18:07:29 -0400
 
index d4bb1009f328f437470e31109d2c6ea357ebb361..b6dd3782b639720b4e69bd5163df94ae52577a0a 100644 (file)
@@ -191,6 +191,13 @@ License: GPL-2+
 
 Files: cvs.pm
 Copyright: © 2009 Amitai Schlair
+License: BSD-C2
+
+Files: underlays/javascript/ikiwiki/openid-selector/*
+Copyright: © 2008-2010 andyjm, david.j.boden
+License: BSD-C2
+  From http://code.google.com/p/openid-selector/
+
 License: BSD-C2
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
index 8f6561fcf3fff334dacbf928006bd8d75b8d75be..22b9345ef7faec93829dedd8725c437c964334f1 100644 (file)
@@ -71,7 +71,7 @@ html out of ikiwiki and in the templates.
 * `editpage.tmpl`, `editconflict.tmpl`, `editcreationconflict.tmpl`,
   `editfailedsave.tmpl`, `editpagegone.tmpl`, `pocreatepage.tmpl`,
   `editcomment.tmpl` `commentmoderation.tmpl`, `renamesummary.tmpl`,
-  `passwordmail.tmpl` - Parts of ikiwiki's user interface; do not
-  normally need to be customised.
+  `passwordmail.tmpl`, `openid-selector.tmpl` - Parts of ikiwiki's user
+  interface; do not normally need to be customised.
 
 [[!meta robots="noindex, follow"]]
diff --git a/templates/openid-selector.tmpl b/templates/openid-selector.tmpl
new file mode 100644 (file)
index 0000000..76c4051
--- /dev/null
@@ -0,0 +1,30 @@
+<script type="text/javascript" src="ikiwiki/openid-selector/jquery.js"></script>
+<script type="text/javascript" src="ikiwiki/openid-selector/openid-jquery.js"></script>
+<script type="text/javascript">
+$(document).ready(function() {
+       openid.init('openid_url');
+});
+</script>
+       
+<TMPL_IF OPENID_ERROR>
+<div class="error"><TMPL_VAR OPENID_ERROR></span>
+</TMPL_IF>
+</div>
+
+<form action="<TMPL_VAR CGIURL>" method="get" id="openid_form">
+       <input type="hidden" name="do" value="signin" />
+       <input type="hidden" name="action" value="verify" />
+       <div id="openid_choice">
+               <p><TMPL_VAR LOGINLABEL>:</p>
+               <div id="openid_btns">
+               </div>
+               <TMPL_IF NONOPENIDURL>
+               <a href="<TMPL_VAR NONOPENIDURL>">local account</a>
+               </TMPL_IF>
+       </div>
+       
+       <span id="openid_input_area">
+               <input id="openid_url" name="openid_url" type="text" value="<TMPL_VAR OPENID_URL>"/>
+               <input id="openid_submit" type="submit" value="Login"/>
+       </span>
+</form>
index 3b60827b6edb095a65ac57177e2baa1007036f91..f39fff842f97a5503c2b9d92e0bbc71bce865fda 100644 (file)
@@ -170,11 +170,11 @@ var openid = {
     },
     setOpenIdUrl: function (url) {
     
-       var hidden = document.getElementById(this.input_id);
-       if (hidden != null) {
-               hidden.value = url;
+       var hidden = $('#'+this.input_id);
+       if (hidden.length > 0) {
+               hidden.value = url;
        } else {
-               $('#openid_form').append('<input type="hidden" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
+               $('#openid_form').append('<input style="display:none" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
        }
     },
     highlight: function (box_id) {