]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/openid.pm
web commit by http://dmarti.myopenid.com/
[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;
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 => "OpenID",
34                         size => 30,
35                         comment => '('.
36                                 htmllink("", "", "OpenID", 1, 0, "What's this?")
37                                 .($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">Get an OpenID</a>" : "")
38                                 .')'
39                 );
40
41                 # Handle submission of an OpenID as validation.
42                 if ($form->submitted && $form->submitted eq "Login" &&
43                     defined $form->field("openid_url") && 
44                     length $form->field("openid_url")) {
45                         $form->field(
46                                 name => "openid_url",
47                                 validate => sub {
48                                         validate($cgi, $session, shift, $form);
49                                 },
50                         );
51                         # Skip all other required fields in this case.
52                         foreach my $field ($form->field) {
53                                 next if $field eq "openid_url";
54                                 $form->field(name => $field, required => 0,
55                                         validate => '/.*/');
56                         }
57                 }
58         }
59 }
60
61 sub validate ($$$;$) { #{{{
62         my $q=shift;
63         my $session=shift;
64         my $openid_url=shift;
65         my $form=shift;
66
67         my $csr=getobj($q, $session);
68
69         my $claimed_identity = $csr->claimed_identity($openid_url);
70         if (! $claimed_identity) {
71                 if ($form) {
72                         # Put the error in the form and fail validation.
73                         $form->field(name => "openid_url", comment => $csr->err);
74                         return 0;
75                 }
76                 else {
77                         error($csr->err);
78                 }
79         }
80
81         my $check_url = $claimed_identity->check_url(
82                 return_to => IkiWiki::cgiurl(do => "postsignin"),
83                 trust_root => $config{cgiurl},
84                 delayed_return => 1,
85         );
86         # Redirect the user to the OpenID server, which will
87         # eventually bounce them back to auth()
88         IkiWiki::redirect($q, $check_url);
89         exit 0;
90 } #}}}
91
92 sub auth ($$) { #{{{
93         my $q=shift;
94         my $session=shift;
95
96         if (defined $q->param('openid.mode')) {
97                 my $csr=getobj($q, $session);
98
99                 if (my $setup_url = $csr->user_setup_url) {
100                         IkiWiki::redirect($q, $setup_url);
101                 }
102                 elsif ($csr->user_cancel) {
103                         IkiWiki::redirect($q, $config{url});
104                 }
105                 elsif (my $vident = $csr->verified_identity) {
106                         $session->param(name => $vident->url);
107                 }
108                 else {
109                         error("OpenID failure: ".$csr->err);
110                 }
111         }
112         elsif (defined $q->param('openid_identifier')) {
113                 # myopenid.com affiliate support
114                 validate($q, $session, $q->param('openid_identifier'));
115         }
116 } #}}}
117
118 sub getobj ($$) { #{{{
119         my $q=shift;
120         my $session=shift;
121
122         eval q{use Net::OpenID::Consumer};
123         error($@) if $@;
124
125         my $ua;
126         eval q{use LWPx::ParanoidAgent};
127         if (! $@) {
128                 $ua=LWPx::ParanoidAgent->new;
129         }
130         else {
131                 $ua=LWP::UserAgent->new;
132         }
133
134         # Store the secret in the session.
135         my $secret=$session->param("openid_secret");
136         if (! defined $secret) {
137                 $secret=$session->param(openid_secret => time);
138         }
139
140         return Net::OpenID::Consumer->new(
141                 ua => $ua,
142                 args => $q,
143                 consumer_secret => $secret,
144                 required_root => $config{cgiurl},
145         );
146 } #}}}
147
148 1