| 1 | package OpenID::Login::Extension; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 4 | $OpenID::Login::Extension::VERSION = '0.1.2'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Storage and methods for OpenId extensions, both requesting information and receiving data. | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Moose 0.51; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | has ns => ( | |||||||||||||||||||||||||||
| 12 | is => 'rw', | ||||||||||||||||||||||||||||
| 13 | isa => 'Str', | ||||||||||||||||||||||||||||
| 14 | required => 1, | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | has uri => ( | |||||||||||||||||||||||||||
| 19 | is => 'rw', | ||||||||||||||||||||||||||||
| 20 | isa => 'Str', | ||||||||||||||||||||||||||||
| 21 | required => 1, | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | has attributes => ( | |||||||||||||||||||||||||||
| 26 | is => 'rw', | ||||||||||||||||||||||||||||
| 27 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 28 | required => 1, | ||||||||||||||||||||||||||||
| 29 | trigger => \&_flatten_attributes, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||
| 33 | 3 | my $orig = shift; | |||||||||||||||||||||||||||
| 34 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | my $args; | |||||||||||||||||||||||||||
| 37 | 3 | ▶ | if ( @_ == 1 && ref $_[0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 38 | 2 | $args = $_[0]; | |||||||||||||||||||||||||||
| 39 | } else { | ||||||||||||||||||||||||||||
| 40 | 1 | $args = {@_}; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 3 | ▶ | if ( $args->{cgi} or $args->{cgi_params} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args->{"cgi"} or $args->{"cgi_params"}
MC/DC50%1/2
$args->{"cgi"} or $args->{"cgi_params"}
$args->{"cgi"}
$args->{"cgi_params"}
Truth table66%2/3
A or B
A= $args->{"cgi"}B= $args->{"cgi_params"}
| |||||||||||||||||||||||||||||
| 43 | 1 | my $new_args; | |||||||||||||||||||||||||||
| 44 | 1 | ▶ | if ( $args->{uri} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | 1 | $new_args = _extract_attributes_by_uri($args); | |||||||||||||||||||||||||||
| 46 | } elsif ( $args->{ns} ) { | ||||||||||||||||||||||||||||
| 47 | 0 | $new_args = _extract_attributes_by_ns($args); | |||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||
| 49 | 0 | die 'Unable to determine extension details'; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 1 | return $class->$orig($new_args); | |||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 2 | return $class->$orig(@_); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 4 | }; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub _extract_attributes_by_uri { | ||||||||||||||||||||||||||
|
Subroutine
_extract_attributes_by_uri: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 58 | 1 | my $args = shift; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | my $cgi = $args->{cgi}; | |||||||||||||||||||||||||||
| 61 | 1 | my $cgi_params = $args->{cgi_params}; | |||||||||||||||||||||||||||
| 62 | 1 | my $uri = $args->{uri}; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | my @openid_params = grep {/^openid\./} $cgi ? $cgi->param() : keys %$cgi_params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | ( my $ns_param ) = grep { ( $cgi ? $cgi->param($_) : $cgi_params->{$_} ) eq $uri } grep {/^openid\.ns\./} @openid_params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | if ($ns_param) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | $args->{ns} = substr( $ns_param, 10 ); | |||||||||||||||||||||||||||
| 68 | 1 | return _extract_attributes_by_ns($args); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | return $args; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | sub _extract_attributes_by_ns { | ||||||||||||||||||||||||||
|
Subroutine
_extract_attributes_by_ns: called CC=6 SCAR=21.4
| |||||||||||||||||||||||||||||
| 75 | 1 | my $args = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | my $cgi = $args->{cgi}; | |||||||||||||||||||||||||||
| 78 | 1 | my $cgi_params = $args->{cgi_params}; | |||||||||||||||||||||||||||
| 79 | 1 | my $ns = $args->{ns}; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | $args->{uri} ||= $cgi ? $cgi->param("openid.ns.$ns") : $cgi_params->{"openid.ns.$ns"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"uri"} ||= $cgi ? $cgi->param("openid.ns.$ns") : $$cgi_params{"openid.ns.$ns"}
MC/DC0%0/2
$args->{"uri"} ||= $cgi ? $cgi->param("openid.ns.$ns") : $$cgi_params{"openid.ns.$ns"}
$args->{"uri"}
$cgi ? $cgi->param("openid.ns.$ns") : $$cgi_params{"openid.ns.$ns"}
Truth table33%1/3
A ||= B
A= $args->{"uri"}B= $cgi ? $cgi->param("openid.ns.$ns") : $$cgi_params{"openid.ns.$ns"}
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | my $prefix = "openid.$ns."; | |||||||||||||||||||||||||||
| 84 | 1 | my $prefix_len = length($prefix); | |||||||||||||||||||||||||||
| 85 | 1 | my %attributes; | |||||||||||||||||||||||||||
| 86 | 1 | ▶ | if ($cgi) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 1 | my %signed_params = map { ( "openid.$_" => 1 ) } split /,/, $cgi->param('openid.signed'); | |||||||||||||||||||||||||||
| 88 | 1 | ▶ | %attributes = ( map { substr( $_, $prefix_len ) => scalar $cgi->param($_) } grep { /^\Q$prefix\E/ and $signed_params{$_} } $cgi->param() ); | ||||||||||||||||||||||||||
|
Condition66%2/3
/^\Q$prefix\E/ && $signed_params{$_}
MC/DC50%1/2
/^\Q$prefix\E/ && $signed_params{$_}
/^\Q$prefix\E/
$signed_params{$_}
Truth table66%2/3
A && B
A= /^\Q$prefix\E/B= $signed_params{$_}
| |||||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||
| 90 | 0 | my %signed_params = map { ( "openid.$_" => 1 ) } split /,/, $cgi_params->{'openid.signed'}; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | %attributes = ( map { substr( $_, $prefix_len ) => $cgi_params->{$_} } grep { /^\Q$prefix\E/ and $signed_params{$_} } keys %$cgi_params ); | ||||||||||||||||||||||||||
|
Condition0%0/3
/^\Q$prefix\E/ && $signed_params{$_}
MC/DC0%0/2
/^\Q$prefix\E/ && $signed_params{$_}
/^\Q$prefix\E/
$signed_params{$_}
Truth table0%0/3
A && B
A= /^\Q$prefix\E/B= $signed_params{$_}
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 1 | $args->{attributes} = \%attributes; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | return $args; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 6 | ▶ | sub get_parameter_string { | ||||||||||||||||||||||||||
|
Subroutine
get_parameter_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 6 | my $ns = $self->ns; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 6 | my $params = sprintf 'openid.ns.%s=%s', $ns, $self->uri; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 6 | my $attributes = $self->attributes; | |||||||||||||||||||||||||||
| 106 | 6 | $params .= _parameterise_hash( "openid.$ns", $attributes ); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 6 | return $params; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 6 | ▶ | sub _parameterise_hash { | ||||||||||||||||||||||||||
|
Subroutine
_parameterise_hash: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 112 | 6 | my $prefix = shift; | |||||||||||||||||||||||||||
| 113 | 6 | my $hash = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 6 | my $params = ''; | |||||||||||||||||||||||||||
| 116 | 6 | $params .= sprintf '&%s.%s=%s', $prefix, $_, $hash->{$_} foreach ( sort keys %$hash ); | |||||||||||||||||||||||||||
| 117 | 6 | return $params; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | sub get_parameter { | ||||||||||||||||||||||||||
|
Subroutine
get_parameter: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | 3 | my $param = shift; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3 | ▶ | die "$param is not an available parameter" unless exists $self->attributes->{$param}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 3 | return $self->attributes->{$param}; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | ▶ | sub set_parameter { | ||||||||||||||||||||||||||
|
Subroutine
set_parameter: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 132 | 3 | my %params = @_; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 3 | _flatten_hash( \%params ); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 3 | $self->attributes->{$_} = $params{$_} foreach keys %params; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 3 | ▶ | sub _flatten_attributes { | ||||||||||||||||||||||||||
|
Subroutine
_flatten_attributes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 140 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 3 | my $attributes = shift; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 3 | _flatten_hash($attributes); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 8 | ▶ | sub _flatten_hash { | ||||||||||||||||||||||||||
|
Subroutine
_flatten_hash: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 147 | 8 | my $hash = shift; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 8 | foreach my $key ( keys %$hash ) { | |||||||||||||||||||||||||||
| 150 | 18 | my $value = $hash->{$key}; | |||||||||||||||||||||||||||
| 151 | 18 | ▶ | if ( ref $value eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 2 | _flatten_hash($value); | |||||||||||||||||||||||||||
| 153 | 2 | $hash->{"$key.$_"} = $value->{$_} foreach keys %$value; | |||||||||||||||||||||||||||
| 154 | 2 | delete $hash->{$key}; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 4 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 161 | 4 | 1; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =pod | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head1 NAME | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | OpenID::Login::Extension - Storage and methods for OpenId extensions, both requesting information and receiving data. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 VERSION | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | version 0.1.2 | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head2 ns | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | The namespace to use for this extension (eg 'ax' is the one usually used in documentation about attribute exchange). | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head2 uri | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | The type URI for the extension (eg attribute exchange has L<http://openid.net/srv/ax/1.0>). | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head2 attributes | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | The attributes for the extension (everything under openid.[ns].*), stored as a hashref. Internally this | ||||||||||||||||||||||||||||
| 188 | is flattened to a single hashref, but a tree structure can be passed in, and intermediate keys will be | ||||||||||||||||||||||||||||
| 189 | linked together with '.'. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 METHODS | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 get_parameter_string | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Collect the internal attributes, and create a single string representing the query of this extension object, | ||||||||||||||||||||||||||||
| 196 | usable for an OpenID request. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head2 get_parameter | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Get a single extension parameter, this is most likely to be used for extensions that are | ||||||||||||||||||||||||||||
| 201 | the result of a request (rather than when creating a request). | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 set_parameter | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Set an extension parameter (or several parameters). Nested parameters are allowed, i.e. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | C<< $extension->set_parameter(type => {firstname => 'q1', lastname => 'q2'}); >> | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | is equivalent to: | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | C<< $extension->set_parameter('type.firstname' => 'q1', 'type.lastname' => 'q2'); >> | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | and neither approach will clear any other C<type.*> values that may already be set. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Holger Eiboeck <realholgi@cpan.org> | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | This software is copyright (c) 2013 by Holger Eiboeck. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 224 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | __END__ | ||||||||||||||||||||||||||||