Net::OpenID::Server patch to manage extensions

12 views
Skip to first unread message

xavier

unread,
Sep 30, 2010, 6:30:35 AM9/30/10
to Net::OpenID for Perl
Hi all,

here is a proposed patch to manage extensions with
Net::OpenID::Server. It is called like this :

my $s : Net::OpenID::Server->new(...
extensions => {
sreg => sub {
my ($u, $trust_root, $is_id, $is_trusted,
$sreg_args) = @_;
# ...
return {nickname => 'foo', email =>
'f...@bar.com' };
},
},
);

There is just one thing to do: get_args() called without any args must
return keys (like CGI->param() does)

Best regards,
Xavier
-------------8<--------------
--- Net-OpenID-Server-1.02/lib/Net/OpenID/Server.pm 2010-09-30
12:19:11.000000000 +0200
+++ Net-OpenID-Server-1.02-new/lib/Net/OpenID/Server.pm 2010-09-30
12:20:59.000000000 +0200
@@ -43,6 +43,8 @@
'secret_expire_age',

'compat', # version 1.0 compatibility flag
(otherwise only sends 1.1 parameters)
+
+ 'extensions' # extension hooks
);

use URI;
@@ -81,7 +83,7 @@
get_user get_identity is_identity is_trusted
endpoint_url setup_url setup_map server_secret
secret_gen_interval secret_expire_age
- compat
+ compat extensions
));

Carp::croak("Unknown options: " . join(", ", keys %opts)) if
%opts;
@@ -102,6 +104,7 @@
sub secret_gen_interval { &_getset; }
sub secret_expire_age { &_getset; }

+sub extensions { &_getset; }

# returns ($content_type, $page), where $content_type can be
"redirect"
# in which case a temporary redirect should be done to the URL in
$page
@@ -338,17 +341,46 @@
}
my $is_identity = $self->_proxy("is_identity", $u, $identity);
my $is_trusted = $self->_proxy("is_trusted", $u, $trust_root,
$is_identity);
+ my ( %extVars, %is_ext_trusted );
+ my $is_exts_trusted = 1;
+ if ( ref( $self->{extensions} ) ) {
+
+ # TODO: get_args->() called without args must return keys
(like CGI->param())
+ my @list = $self->get_args->();
+ my %extArgs;
+ foreach my $arg (@list) {
+ next unless ( $arg =~ /^openid\.(\w+)\.([\w\.]+)?/ );
+ my ( $tmp, $val ) = ( $1, $2 );
+ $extArgs{$tmp}->{$val} = scalar $self->args->($arg);
+ }
+ foreach my $ns ( keys %{ $self->{extensions} } ) {
+ die 'Invalid extension hook' unless(ref($self-
>{extensions}->{$ns}) eq 'CODE');
+ my $h;
+ ( $is_ext_trusted{$ns}, $h ) = $self->{extensions}->{$ns}-
>(
+ $u, $trust_root, $is_identity, $is_trusted,
+ delete( $extArgs{$ns} ) || {}
+ );
+ if ($h) {
+ while ( my ( $k, $v ) = each %$h ) {
+ $extVars{"$ns.$k"} = $v;
+ }
+ }
+ $is_exts_trusted &&= $is_ext_trusted{$ns};
+ }
+ }

# assertion path:
- if ($is_identity && $is_trusted) {
- my $ret_url = $self->signed_return_url(
+ if ( $is_identity && $is_trusted && $is_exts_trusted ) {
+ my %sArgs = (
identity => $identity,
claimed_id => $self-
>args('openid.claimed_id'),
return_to =>
$return_to,
assoc_handle => $self-
>args("openid.assoc_handle"),
ns => $self-
>args('openid.ns'),
);
- return ("redirect", $ret_url);
+ $sArgs{additional_fields} = \%extVars if (%extVars);
+ my $ret_url = $self->signed_return_url(%sArgs);
+ return ( "redirect", $ret_url );
}

# assertion could not be made, so user requires setup (login/
trust.. something)
@@ -361,6 +393,7 @@
$self->_setup_map("return_to"), $return_to,
$self->_setup_map("identity"), $identity,
$self->_setup_map("assoc_handle"), $self-
>args("openid.assoc_handle"),
+ %extVars,
);
$setup_args{$self->_setup_map('ns')} = $self->args('openid.ns')
if $self->args('openid.ns');
Reply all
Reply to author
Forward
0 new messages