3 package IkiWiki::Plugin::openid;
10 hook(type => "getopt", id => "openid", call => \&getopt);
11 hook(type => "getsetup", id => "openid", call => \&getsetup);
12 hook(type => "auth", id => "openid", call => \&auth);
13 hook(type => "formbuilder_setup", id => "openid",
14 call => \&formbuilder_setup, last => 1);
18 eval q{use Getopt::Long};
20 Getopt::Long::Configure('pass_through');
21 GetOptions("openidsignup=s" => \$config{openidsignup});
33 example => "http://myopenid.com/",
34 description => "an url where users can signup for an OpenID",
40 sub formbuilder_setup (@) {
43 my $form=$params{form};
44 my $session=$params{session};
47 if ($form->title eq "signin") {
48 # Give up if module is unavailable to avoid
49 # needing to depend on it.
50 eval q{use Net::OpenID::Consumer};
52 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
56 # This avoids it displaying a redundant label for the
58 $form->fieldsets("OpenID");
62 label => gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
65 comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
68 # Handle submission of an OpenID as validation.
69 if ($form->submitted && $form->submitted eq "Login" &&
70 defined $form->field("openid_url") &&
71 length $form->field("openid_url")) {
75 validate($cgi, $session, shift, $form);
78 # Skip all other required fields in this case.
79 foreach my $field ($form->field) {
80 next if $field eq "openid_url";
81 $form->field(name => $field, required => 0,
86 elsif ($form->title eq "preferences" &&
87 IkiWiki::openiduser($session->param("name"))) {
88 $form->field(name => "openid_url", disabled => 1,
89 label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
90 value => $session->param("name"),
91 size => 50, force => 1,
93 $form->field(name => "email", type => "hidden");
97 sub validate ($$$;$) {
100 my $openid_url=shift;
103 my $csr=getobj($q, $session);
105 my $claimed_identity = $csr->claimed_identity($openid_url);
106 if (! $claimed_identity) {
108 # Put the error in the form and fail validation.
109 $form->field(name => "openid_url", comment => $csr->err);
117 # Ask for client to provide a name and email, if possible.
119 if ($claimed_identity->can("set_extension_args")) {
120 $claimed_identity->set_extension_args(
121 'http://openid.net/extensions/sreg/1.1',
123 optional => 'email,fullname,nickname',
126 $claimed_identity->set_extension_args(
127 'http://openid.net/srv/ax/1.0',
129 mode => 'fetch_request',
130 'required' => 'email,fullname,nickname,firstname',
131 'type.email' => "http://schema.openid.net/contact/email",
132 'type.fullname' => "http://axschema.org/namePerson",
133 'type.nickname' => "http://axschema.org/namePerson/friendly",
134 'type.firstname' => "http://axschema.org/namePerson/first",
139 my $check_url = $claimed_identity->check_url(
140 return_to => IkiWiki::cgiurl(do => "postsignin"),
141 trust_root => $config{cgiurl},
144 # Redirect the user to the OpenID server, which will
145 # eventually bounce them back to auth()
146 IkiWiki::redirect($q, $check_url);
154 if (defined $q->param('openid.mode')) {
155 my $csr=getobj($q, $session);
157 if (my $setup_url = $csr->user_setup_url) {
158 IkiWiki::redirect($q, $setup_url);
160 elsif ($csr->user_cancel) {
161 IkiWiki::redirect($q, $config{url});
163 elsif (my $vident = $csr->verified_identity) {
164 $session->param(name => $vident->url);
167 if ($vident->can("signed_extension_fields")) {
168 @extensions=grep { defined } (
169 $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
170 $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
173 foreach my $ext (@extensions) {
174 foreach my $field (qw{value.email email}) {
175 if (exists $ext->{$field} &&
176 defined $ext->{$field} &&
177 length $ext->{$field}) {
178 $session->param(email => $ext->{$field});
182 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
183 if (exists $ext->{$field} &&
184 defined $ext->{$field} &&
185 length $ext->{$field}) {
186 $session->param(username => $ext->{$field});
193 error("OpenID failure: ".$csr->err);
196 elsif (defined $q->param('openid_identifier')) {
197 # myopenid.com affiliate support
198 validate($q, $session, $q->param('openid_identifier'));
206 eval q{use Net::OpenID::Consumer};
210 eval q{use LWPx::ParanoidAgent};
212 $ua=LWPx::ParanoidAgent->new;
215 $ua=LWP::UserAgent->new;
218 # Store the secret in the session.
219 my $secret=$session->param("openid_secret");
220 if (! defined $secret) {
222 $session->param(openid_secret => $secret);
225 return Net::OpenID::Consumer->new(
228 consumer_secret => sub { return shift()+$secret },
229 required_root => $config{cgiurl},