]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/openid.pm
* Group passwordauth fields with a fieldset as well. Add a new
[ikiwiki.git] / IkiWiki / Plugin / openid.pm
1 #!/usr/bin/perl
2 # OpenID support.
3 package IkiWiki::Plugin::openid;
4
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
8
9 sub import { #{{{
10         hook(type => "getopt", id => "openid", call => \&getopt);
11         hook(type => "auth", id => "openid", call => \&auth);
12         hook(type => "formbuilder_setup", id => "openid",
13                 call => \&formbuilder_setup, last => 1);
14 } # }}}
15
16 sub getopt () { #{{{
17         eval q{use Getopt::Long};
18         error($@) if $@;
19         Getopt::Long::Configure('pass_through');
20         GetOptions("openidsignup=s" => \$config{openidsignup});
21 } #}}}
22
23 sub formbuilder_setup (@) { #{{{
24         my %params=@_;
25
26         my $form=$params{form};
27         my $session=$params{session};
28         my $cgi=$params{cgi};
29
30         if ($form->title eq "signin") {
31                 $form->field(
32                         name => "openid_url",
33                         label => "",
34                         fieldset => gettext("Log in with")." ".htmllink("", "", "OpenID", noimageinline => 1),
35                         size => 30,
36                         comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
37                 );
38
39                 # Handle submission of an OpenID as validation.
40                 if ($form->submitted && $form->submitted eq "Login" &&
41                     defined $form->field("openid_url") && 
42                     length $form->field("openid_url")) {
43                         $form->field(
44                                 name => "openid_url",
45                                 validate => sub {
46                                         validate($cgi, $session, shift, $form);
47                                 },
48                         );
49                         # Skip all other required fields in this case.
50                         foreach my $field ($form->field) {
51                                 next if $field eq "openid_url";
52                                 $form->field(name => $field, required => 0,
53                                         validate => '/.*/');
54                         }
55                 }
56         }
57         elsif ($form->title eq "preferences") {
58                 if (! defined $form->field(name => "name")) {
59                         $form->field(name => "OpenID", disabled => 1,
60                                 value => $session->param("name"), 
61                                 size => 50, force => 1,
62                                 fieldset => "login");
63                 }
64         }
65 }
66
67 sub validate ($$$;$) { #{{{
68         my $q=shift;
69         my $session=shift;
70         my $openid_url=shift;
71         my $form=shift;
72
73         my $csr=getobj($q, $session);
74
75         my $claimed_identity = $csr->claimed_identity($openid_url);
76         if (! $claimed_identity) {
77                 if ($form) {
78                         # Put the error in the form and fail validation.
79                         $form->field(name => "openid_url", comment => $csr->err);
80                         return 0;
81                 }
82                 else {
83                         error($csr->err);
84                 }
85         }
86
87         my $check_url = $claimed_identity->check_url(
88                 return_to => IkiWiki::cgiurl(do => "postsignin"),
89                 trust_root => $config{cgiurl},
90                 delayed_return => 1,
91         );
92         # Redirect the user to the OpenID server, which will
93         # eventually bounce them back to auth()
94         IkiWiki::redirect($q, $check_url);
95         exit 0;
96 } #}}}
97
98 sub auth ($$) { #{{{
99         my $q=shift;
100         my $session=shift;
101
102         if (defined $q->param('openid.mode')) {
103                 my $csr=getobj($q, $session);
104
105                 if (my $setup_url = $csr->user_setup_url) {
106                         IkiWiki::redirect($q, $setup_url);
107                 }
108                 elsif ($csr->user_cancel) {
109                         IkiWiki::redirect($q, $config{url});
110                 }
111                 elsif (my $vident = $csr->verified_identity) {
112                         $session->param(name => $vident->url);
113                 }
114                 else {
115                         error("OpenID failure: ".$csr->err);
116                 }
117         }
118         elsif (defined $q->param('openid_identifier')) {
119                 # myopenid.com affiliate support
120                 validate($q, $session, $q->param('openid_identifier'));
121         }
122 } #}}}
123
124 sub getobj ($$) { #{{{
125         my $q=shift;
126         my $session=shift;
127
128         eval q{use Net::OpenID::Consumer};
129         error($@) if $@;
130
131         my $ua;
132         eval q{use LWPx::ParanoidAgent};
133         if (! $@) {
134                 $ua=LWPx::ParanoidAgent->new;
135         }
136         else {
137                 $ua=LWP::UserAgent->new;
138         }
139
140         # Store the secret in the session.
141         my $secret=$session->param("openid_secret");
142         if (! defined $secret) {
143                 $secret=rand;
144                 $session->param(openid_secret => $secret);
145         }
146
147         return Net::OpenID::Consumer->new(
148                 ua => $ua,
149                 args => $q,
150                 consumer_secret => sub { return shift()+$secret },
151                 required_root => $config{cgiurl},
152         );
153 } #}}}
154
155 1