[libnet-openid-common-perl] 02/34: Some initial work towards a proper framework for extensions. This doesn't actually do anything yet.

gregor herrmann gregoa at debian.org
Sun Feb 7 21:50:15 UTC 2016


This is an automated email from the git hooks/post-receive script.

gregoa pushed a commit to annotated tag v1.030099_001
in repository libnet-openid-common-perl.

commit 9b6a754a60a981646141645dd56581cab0bf6736
Author: mart <mart at 5176148d-0815-0410-95d9-e2102c905069>
Date:   Fri Nov 14 08:27:29 2008 +0000

    Some initial work towards a proper framework for extensions. This doesn't actually do anything yet.
    
    One problem with this design right now is that there's no support for extension response fields to be signed. I'll work on that later.
    
    
    git-svn-id: http://code.sixapart.com/svn/openid/trunk/perl/Net-OpenID-Common@160 5176148d-0815-0410-95d9-e2102c905069
---
 MANIFEST                                       |   3 +
 lib/Net/OpenID/Extension.pm                    |  88 +++++++++
 lib/Net/OpenID/Extension/SimpleRegistration.pm | 239 +++++++++++++++++++++++++
 lib/Net/OpenID/ExtensionMessage.pm             |  30 ++++
 4 files changed, 360 insertions(+)

diff --git a/MANIFEST b/MANIFEST
index 7f4c68b..a450438 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -6,6 +6,9 @@ lib/Net/OpenID/IndirectMessage.pm
 lib/Net/OpenID/URIFetch.pm
 lib/Net/OpenID/Yadis.pm
 lib/Net/OpenID/Yadis/Service.pm
+lib/Net/OpenID/Extension.pm
+lib/Net/OpenID/ExtensionMessage.pm
+lib/Net/OpenID/Extension/SimpleRegistration.pm
 t/00-use-indirectmessage.t
 t/01-use-urifetch.t
 t/02-use-yadis.t
diff --git a/lib/Net/OpenID/Extension.pm b/lib/Net/OpenID/Extension.pm
new file mode 100644
index 0000000..44fdc36
--- /dev/null
+++ b/lib/Net/OpenID/Extension.pm
@@ -0,0 +1,88 @@
+
+package Net::OpenID::Extension;
+
+use strict;
+
+=head1 NAME
+
+Net::OpenID::Extension - Base class for OpenID extensions
+
+=head1 METHODS
+
+=head2 CLASS->namespace_uris
+
+Return a hashref mapping namespace URIs to the aliases you will use
+to refer to them in the other methods. For example:
+
+    return {
+        'http://example.com/some-extension' => 'someext',
+    };
+
+=head2 CLASS->new_request(@parameters)
+
+When your extension is added to the L<Net::OpenID::ClaimedIdentity>
+object in consumer-land, this method will be called to create
+a request object. Any additional arguments passed when adding the
+extension will be passed through verbatim in C<@parameters>.
+
+The object you return here should at minimum provide the interface
+defined in L<Net::OpenID::ExtensionMessage>.
+
+You can return C<undef> here if you have nothing useful to return.
+
+=head2 CLASS->received_request(\%args)
+
+In server-land, when a caller asks for the request object for your
+extension this method will be called to create a request object.
+C<%args> maps the aliases you returned from the C<namespace_uris>
+method to a hashref of the key-value pairs provided in that namespace.
+
+The object you return here should at minimum provide the interface
+defined in L<Net::OpenID::ExtensionMessage>, and should behave
+identically to the corresponding object returned from C<new_request>.
+
+You can return C<undef> here if you have nothing useful to return.
+
+=head2 CLASS->new_response(@parameters)
+
+When your extension is added to the response in server-land, this
+method will be called to create a response object. Any additional
+arguments passed when adding the extension will be passed through
+verbatim in C<@parameters>.
+
+You can return C<undef> here if you have nothing useful to return.
+
+=head2 CLASS->received_response(\%args)
+
+In consumer-land, when a caller asks for the request object for
+your extension in L<Net::OpenID::VerifiedIdentity> this method
+will be called to create a response object.
+C<%args> maps the aliases you returned from the C<namespace_uris>
+method to a hashref of the key-value pairs provided in that namespace.
+
+You can return C<undef> here if you have nothing useful to return.
+
+=cut
+
+sub namespace_uris {
+    return {};
+}
+
+sub new_request {
+    return undef;
+}
+
+sub new_response {
+    return undef;
+}
+
+sub received_request {
+    return undef;
+}
+
+sub received_response {
+    return undef;
+}
+
+1;
+
diff --git a/lib/Net/OpenID/Extension/SimpleRegistration.pm b/lib/Net/OpenID/Extension/SimpleRegistration.pm
new file mode 100644
index 0000000..493b7e5
--- /dev/null
+++ b/lib/Net/OpenID/Extension/SimpleRegistration.pm
@@ -0,0 +1,239 @@
+
+package Net::OpenID::Extension::SimpleRegistration;
+
+use base qw(Net::OpenID::Extension);
+use strict;
+use Carp;
+
+use constant namespace_uris => {
+    'http://openid.net/extensions/sreg/1.1' => 'sreg',
+};
+
+sub new_request {
+    my ($class, %opts) = @_;
+
+    return Net::OpenID::Extension::SimpleRegistration::Request->new(%opts);
+}
+
+sub received_request {
+    my ($class, $args) = @_;
+
+    return Net::OpenID::Extension::SimpleRegistration::Request->received($args);
+}
+
+sub new_response {
+    my ($class, %opts) = @_;
+
+    return Net::OpenID::Extension::SimpleRegistration::Request->new(%opts);
+}
+
+sub received_response {
+    my ($class, $args) = @_;
+
+    return Net::OpenID::Extension::SimpleRegistration::Request->received($args);
+}
+
+package Net::OpenID::Extension::SimpleRegistration::Request;
+
+use base qw(Net::OpenID::ExtensionMessage);
+use strict;
+
+sub new {
+    my ($class, %opts) = @_;
+
+    my $self = bless {}, $class;
+
+    $self->required_fields(delete $opts{required_fields});
+    $self->optional_fields(delete $opts{optional_fields});
+    $self->policy_url(delete $opts{policy_url});
+
+    $self->{required_fields} = [ split(',', $self->{required_fields}) ] unless ref $self->{required_fields};
+    $self->{optional_fields} = [ split(',', $self->{optional_fields}) ] unless ref $self->{optional_fields};
+
+    Carp::croak("Unsupported options: ".join(',', keys %opts)) if %opts;
+
+    return $self;
+}
+
+sub received {
+    my ($class, $args) = @_;
+
+    my $self = $class->new();
+    my $args = $args->{sreg} || {};
+
+    $self->required_fields($args->{required});
+    $self->optional_fields($args->{optional});
+
+    return $self;
+}
+
+sub extension_arguments {
+    my ($self) = @_;
+
+    my $ret = {};
+
+    $ret->{required} = join(',', @{$self->required_fields}) if @{$self->required_fields};
+    $ret->{optional} = join(',', @{$self->optional_fields}) if @{$self->optional_fields};
+    $ret->{policy_url} = $self->policy_url if $self->policy_url;
+
+    return {
+        sreg => $ret,
+    };
+}
+
+sub required_fields {
+    my ($self, $value) = @_;
+
+    if (defined $value) {
+        $value = [] unless $value;
+        $value = [ split(',', $value) ] unless ref $value;
+        $self->{required_fields} = $value;
+    }
+    else {
+        return $self->{required_fields};
+    }
+}
+
+sub optional_fields {
+    my ($self, $value) = @_;
+
+    if (defined $value) {
+        $value = [] unless $value;
+        $value = [ split(',', $value) ] unless ref $value;
+        $self->{optional_fields} = $value;
+    }
+    else {
+        return $self->{optional_fields};
+    }
+}
+
+sub add_required_field {
+    my ($self, $value) = @_;
+
+    push @{$self->{required_fields}}, $value;
+}
+
+sub add_optional_field {
+    my ($self, $value) = @_;
+
+    push @{$self->{optional_fields}}, $value;
+}
+
+sub policy_url {
+    my ($self, $value) = @_;
+
+    if (defined $value) {
+        $self->{policy_url} = $value;
+    }
+    else {
+        return $self->{optional_fields};
+    }
+}
+
+package Net::OpenID::Extension::SimpleRegistration::Response;
+
+use base qw(Net::OpenID::ExtensionMessage);
+use strict;
+
+use constant FIELDS => [qw(nickname email fullname dob gender postcode country language timezone)];
+use fields FIELDS();
+
+BEGIN {
+    # Create an accessor for each of the fields
+    foreach my $field_name (@{FIELDS()}) {
+        no strict qw(refs);
+        *{'Net::OpenID::Extension::SimpleRegistration::Response::'.$field_name} = sub {
+            my ($self, $value) = @_;
+
+            if (defined $value) {
+                $self->{$field_name} = $value;
+            }
+            else {
+                return $self->{$field_name};
+            }
+        };
+    }
+}
+
+sub new {
+    my ($class, %opts) = @_;
+
+    my $self = fields::new($class);
+
+    foreach my $field_name (@{FIELDS()}) {
+        $self->{$field_name} = delete $opts{$field_name};
+    }
+
+    Carp::croak("Unrecognised options: ".join(',', %opts)) if %opts;
+
+    return $self;
+}
+
+sub received {
+    my ($class, $args) = @_;
+
+    my $args = $args->{sreg} || {};
+    my %opts = ();
+
+    foreach my $field_name (@{FIELDS()}) {
+        $opts{$field_name} = $args->{$field_name} if $args->{$field_name};
+    }
+
+    return $class->new(%opts);
+}
+
+sub extension_arguments {
+    my ($self) = @_;
+
+    # De-reference and then re-reference the hash to shake off the blessedness
+    my $ret = { %$self };
+
+    return {
+        sreg => $ret,
+    };
+}
+
+=pod
+
+=head1 NAME
+
+Net::OpenID::Extension::SimpleRegistration - Support for the Simple Registration extension (SREG)
+
+=head1 SYNOPSIS
+
+In Consumer...
+
+    my $sreg_req = $claimed_identity->add_extension_request('Net::OpenID::Extension::SimpleRegistration', (
+        required_fields => [qw(nickname email)],
+        optional_fields => [qw(country language timezone)],
+        policy_url => "http://example.com/policy.html",
+    ));
+
+Then, in Server, when handling the authentication request...
+
+    # FIXME: What object do we have in ::Server that can hold this method?
+    my $sreg_req = $something->get_extension_request('Net::OpenID::Extension::SimpleRegistration');
+    my $required_fields = $sreg_req->required_fields;
+    my $optional_fields = $sreg_req->optional_fields;
+    my $policy_url = $sreg_req->policy_url;
+
+When Server sends back its response...
+
+    # FIXME: Again, what object do we have to hold this method?
+    my $sreg_res = $something->add_extension_response('Net::OpenID::Extension::SimpleRegistration', (
+        nickname => $nickname,
+        email => $email,
+    ));
+
+And finally, when back in Consumer recieving the response:
+
+    my $sreg_res = $verified_identity->get_extension_response('Net::OpenID::Extension::SimpleRegistration');
+    my $nickname = $sreg_res->nickname;
+    my $email = $sreg_res->email;
+    my $country = $sreg_res->country;
+    my $language = $sreg_res->language;
+    my $timezone = $sreg_res->timezone;
+
+=cut
+
+1;
diff --git a/lib/Net/OpenID/ExtensionMessage.pm b/lib/Net/OpenID/ExtensionMessage.pm
new file mode 100644
index 0000000..8510184
--- /dev/null
+++ b/lib/Net/OpenID/ExtensionMessage.pm
@@ -0,0 +1,30 @@
+
+package Net::OpenID::ExtensionMessage;
+
+use strict;
+
+=head1 NAME
+
+Net::OpenID::ExtensionMessage - Base class for extension messages
+
+=head1 DESCRIPTION
+
+Instances of implementations of the interface provided by this
+package are returned from various methods in L<Net::OpenID::Extension>
+implementations.
+
+=head1 METHODS
+
+=head2 $emsg->extension_arguments
+
+Return a hashref that maps extension namespace aliases as defined
+in the corresponding L<Net::OpenID::Extension> to hashrefs of
+key-value pairs for the arguments to include in that namespace.
+
+=cut
+
+sub extension_arguments {
+    return {};
+}
+
+1;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libnet-openid-common-perl.git



More information about the Pkg-perl-cvs-commits mailing list