| 1 | package Net::OpenID::Yadis; | ||
| 2 | BEGIN { | ||
| 3 | 1 | ▶ | $Net::OpenID::Yadis::VERSION = '1.030099_004'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | } | ||
| 5 | |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Net::OpenID::URIFetch; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use XML::Simple; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Net::OpenID::Yadis::Service; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use Net::OpenID::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | |||
| 16 | our @EXPORT = qw(YR_HEAD YR_GET YR_XRDS); | ||
| 17 | |||
| 18 | use constant YR_GET => 1; | ||
| 19 | use constant YR_XRDS => 2; | ||
| 20 | |||
| 21 | use fields ( | ||
| 22 | 'last_errcode', # last error code we got | ||
| 23 | 'last_errtext', # last error code we got | ||
| 24 | 'debug', # debug flag or codeblock | ||
| 25 | 'consumer', # consumer object | ||
| 26 | 'identity_url', # URL to be identified | ||
| 27 | 'xrd_url', # URL of XRD file | ||
| 28 | 'xrd_objects', # Yadis XRD decoded objects | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | my $self = shift; | ||
| 33 | $self = fields::new( $self ) unless ref $self; | ||
| 34 | my %opts = @_; | ||
| 35 | |||
| 36 | $self->consumer(delete($opts{consumer})); | ||
| 37 | |||
| 38 | $self->{debug} = delete $opts{debug}; | ||
| 39 | |||
| 40 | Carp::croak("Unknown options: " . join(", ", keys %opts)) if %opts; | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub consumer { &_getset; } | ||
| 46 | |||
| 47 | sub identity_url { &_getset; } | ||
| 48 | sub xrd_url { &_getset; } | ||
| 49 | sub xrd_objects { _pack_array(&_getset); } | ||
| 50 | sub _getset { | ||
| 51 | my $self = shift; | ||
| 52 | my $param = (caller(1))[3]; | ||
| 53 | $param =~ s/.+:://; | ||
| 54 | |||
| 55 | if (@_) { | ||
| 56 | my $val = shift; | ||
| 57 | Carp::croak("Too many parameters") if @_; | ||
| 58 | $self->{$param} = $val; | ||
| 59 | } | ||
| 60 | return $self->{$param}; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _debug { | ||
| 64 | my $self = shift; | ||
| 65 | return unless $self->{debug}; | ||
| 66 | |||
| 67 | if (ref $self->{debug} eq "CODE") { | ||
| 68 | $self->{debug}->($_[0]); | ||
| 69 | } else { | ||
| 70 | print STDERR "[DEBUG Net::OpenID::Yadis] $_[0]\n"; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _fail { | ||
| 75 | my $self = shift; | ||
| 76 | my ($code, $text) = @_; | ||
| 77 | |||
| 78 | $text ||= { | ||
| 79 | 'xrd_parse_error' => "Error occured since parsing yadis document.", | ||
| 80 | 'xrd_format_error' => "This is not yadis document (not xrds format).", | ||
| 81 | 'too_many_hops' => 'Too many hops by X-XRDS-Location.', | ||
| 82 | 'empty_url' => 'Empty URL', | ||
| 83 | 'no_yadis_document' => 'Cannot find yadis Document', | ||
| 84 | 'url_gone' => 'URL is no longer available', | ||
| 85 | }->{$code}; | ||
| 86 | |||
| 87 | $self->{last_errcode} = $code; | ||
| 88 | $self->{last_errtext} = $text; | ||
| 89 | |||
| 90 | $self->_debug("fail($code) $text"); | ||
| 91 | wantarray ? () : undef; | ||
| 92 | } | ||
| 93 | sub err { | ||
| 94 | my $self = shift; | ||
| 95 | $self->{last_errcode} . ": " . $self->{last_errtext}; | ||
| 96 | } | ||
| 97 | sub errcode { | ||
| 98 | my $self = shift; | ||
| 99 | $self->{last_errcode}; | ||
| 100 | } | ||
| 101 | sub errtext { | ||
| 102 | my $self = shift; | ||
| 103 | $self->{last_errtext}; | ||
| 104 | } | ||
| 105 | sub _clear_err { | ||
| 106 | my $self = shift; | ||
| 107 | $self->{last_errtext} = ''; | ||
| 108 | $self->{last_errcode} = ''; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _get_contents { | ||
| 112 | my $self = shift; | ||
| 113 | my ($url, $final_url_ref, $content_ref, $headers_ref) = @_; | ||
| 114 | |||
| 115 | my $res = Net::OpenID::URIFetch->fetch($url, $self->consumer, \&OpenID::util::_extract_head_markup_only); | ||
| 116 | |||
| 117 | if ($res) { | ||
| 118 | $$final_url_ref = $res->final_uri; | ||
| 119 | my $headers = $res->headers; | ||
| 120 | foreach my $k (keys %$headers) { | ||
| 121 | $headers_ref->{$k} ||= $headers->{$k}; | ||
| 122 | } | ||
| 123 | $$content_ref = $res->content; | ||
| 124 | return 1; | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | return undef; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | sub discover { | ||
| 132 | my $self = shift; | ||
| 133 | my $url = shift or return $self->_fail("empty_url"); | ||
| 134 | my $count = shift || YR_GET; | ||
| 135 | Carp::croak("Too many parameters") if @_; | ||
| 136 | |||
| 137 | # trim whitespace | ||
| 138 | $url =~ s/^\s+//; | ||
| 139 | $url =~ s/\s+$//; | ||
| 140 | return $self->_fail("empty_url") unless $url; | ||
| 141 | |||
| 142 | my $final_url; | ||
| 143 | my %headers; | ||
| 144 | |||
| 145 | my $xrd; | ||
| 146 | $self->_get_contents($url, \$final_url, \$xrd, \%headers) or return; | ||
| 147 | |||
| 148 | $self->identity_url($final_url) if ($count < YR_XRDS); | ||
| 149 | |||
| 150 | my $doc_url; | ||
| 151 | if (($doc_url = $headers{'x-yadis-location'} || $headers{'x-xrds-location'}) && ($count < YR_XRDS)) { | ||
| 152 | return $self->discover($doc_url, YR_XRDS); | ||
| 153 | } | ||
| 154 | elsif ( (split /;\s*/, $headers{'content-type'})[0] eq 'application/xrds+xml') { | ||
| 155 | $self->xrd_url($final_url); | ||
| 156 | return $self->parse_xrd($xrd); | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | return $self->_fail($count == YR_GET ? "no_yadis_document" : "too_many_hops"); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | sub parse_xrd { | ||
| 164 | my $self = shift; | ||
| 165 | my $xrd = shift; | ||
| 166 | Carp::croak("Too many parameters") if @_; | ||
| 167 | |||
| 168 | my $xs_hash = XMLin($xrd) or return $self->_fail("xrd_parse_error"); | ||
| 169 | ($xs_hash->{'xmlns'} and $xs_hash->{'xmlns'} eq 'xri://$xrd*($v*2.0)') or $self->_fail("xrd_format_error"); | ||
| 170 | my %xmlns; | ||
| 171 | foreach (map { /^(xmlns:(.+))$/ and [$1,$2] } keys %$xs_hash) { | ||
| 172 | next unless ($_); | ||
| 173 | $xmlns{$_->[1]} = $xs_hash->{$_->[0]}; | ||
| 174 | } | ||
| 175 | my @priority; | ||
| 176 | my @nopriority; | ||
| 177 | foreach my $service (_pack_array($xs_hash->{'XRD'}{'Service'})) { | ||
| 178 | bless $service, "Net::OpenID::Yadis::Service"; | ||
| 179 | $service->{'Type'} or next; | ||
| 180 | $service->{'URI'} ||= $self->identity_url; | ||
| 181 | |||
| 182 | foreach my $sname (keys %$service) { | ||
| 183 | foreach my $ns (keys %xmlns) { | ||
| 184 | $service->{"{$xmlns{$ns}}$1"} = delete $service->{$sname} if ($sname =~ /^${ns}:(.+)$/); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | defined($service->{'priority'}) ? push(@priority,$service) : push(@nopriority,$service); | ||
| 188 | # Services without priority fields are lowest priority | ||
| 189 | } | ||
| 190 | my @service = sort {$a->{'priority'} <=> $b->{'priority'}} @priority; | ||
| 191 | push (@service,@nopriority); | ||
| 192 | foreach (grep {/^_protocol/} keys %$self) { delete $self->{$_} } | ||
| 193 | |||
| 194 | $self->xrd_objects(\@service); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub _pack_array { wantarray ? ref($_[0]) eq 'ARRAY' ? @{$_[0]} : ($_[0]) : $_[0] } | ||
| 198 | |||
| 199 | sub services { | ||
| 200 | my $self = shift; | ||
| 201 | my %protocols; | ||
| 202 | my @protocols; | ||
| 203 | my $code_ref; | ||
| 204 | my $protocol = undef; | ||
| 205 | |||
| 206 | Carp::croak("You haven't called the discover method yet") unless $self->xrd_objects; | ||
| 207 | |||
| 208 | foreach my $option (@_) { | ||
| 209 | Carp::croak("No further arguments allowed after code reference argument") if $code_ref; | ||
| 210 | my $ref = ref($option); | ||
| 211 | if ($ref eq 'CODE') { | ||
| 212 | $code_ref = $option; | ||
| 213 | } else { | ||
| 214 | my $default = {versionarray => []}; | ||
| 215 | |||
| 216 | $protocols{$option} = $default; | ||
| 217 | $protocol = $option; | ||
| 218 | push @protocols, $option; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | my @servers; | ||
| 223 | @servers = $self->xrd_objects if (keys %protocols == 0); | ||
| 224 | foreach my $key (@protocols) { | ||
| 225 | my $regex = $protocols{$key}->{urlregex} || $key; | ||
| 226 | my @ver = @{$protocols{$key}->{versionarray}}; | ||
| 227 | my $ver_regex = @ver ? '('.join('|',map { $_ =~ s/\./\\./g; $_ } @ver).')' : '.+' ; | ||
| 228 | $regex =~ s/\\ver/$ver_regex/; | ||
| 229 | |||
| 230 | push (@servers,map { $protocols{$key}->{objectclass} ? bless($_ , $protocols{$key}->{objectclass}) : $_ } grep {join(",",$_->Type) =~ /$regex/} $self->xrd_objects); | ||
| 231 | } | ||
| 232 | |||
| 233 | @servers = $code_ref->(@servers) if ($code_ref); | ||
| 234 | |||
| 235 | wantarray ? @servers : \@servers; | ||
| 236 | } | ||
| 237 | |||
| 238 | 1; | ||
| 239 | __END__ | ||