| 1 | |||
| 2 | package Net::OpenID::IndirectMessage; | ||
| 3 | BEGIN { | ||
| 4 | 2 | ▶ | $Net::OpenID::IndirectMessage::VERSION = '1.030099_004'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | } | ||
| 6 | |||
| 7 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use Net::OpenID::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $class = shift; | ||
| 13 | my $what = shift; | ||
| 14 | my %opts = @_; | ||
| 15 | |||
| 16 | my $self = bless {}, $class; | ||
| 17 | |||
| 18 | $self->{minimum_version} = delete $opts{minimum_version}; | ||
| 19 | |||
| 20 | Carp::croak("Unknown options: " . join(", ", keys %opts)) if %opts; | ||
| 21 | |||
| 22 | my $getter; | ||
| 23 | my $enumer; | ||
| 24 | if (ref $what eq "HASH") { | ||
| 25 | # In this case it's the caller's responsibility to determine | ||
| 26 | # whether the method is GET or POST. | ||
| 27 | $getter = sub { $what->{$_[0]}; }; | ||
| 28 | $enumer = sub { keys(%$what); }; | ||
| 29 | } | ||
| 30 | elsif (UNIVERSAL::isa($what, "CGI")) { | ||
| 31 | # CGI automatically does what we need when method is POST | ||
| 32 | $getter = sub { scalar $what->param($_[0]); }; | ||
| 33 | $enumer = sub { $what->param; }; | ||
| 34 | } | ||
| 35 | elsif (ref $what eq "Apache") { | ||
| 36 | my %get; | ||
| 37 | if ($what->method eq 'POST') { | ||
| 38 | %get = $what->content; | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | %get = $what->args; | ||
| 42 | } | ||
| 43 | $getter = sub { $get{$_[0]}; }; | ||
| 44 | $enumer = sub { keys(%get); }; | ||
| 45 | } | ||
| 46 | elsif ((ref $what eq "Apache::Request") || (ref $what eq "Apache2::Request")) { | ||
| 47 | # Apache::Request includes the POST and GET arguments in ->param | ||
| 48 | # when doing a POST request, which is close enough to what | ||
| 49 | # the spec requires. | ||
| 50 | $getter = sub { scalar $what->param($_[0]); }; | ||
| 51 | $enumer = sub { $what->param; }; | ||
| 52 | } | ||
| 53 | elsif (ref $what eq "CODE") { | ||
| 54 | $getter = $what; | ||
| 55 | # We can't enumerate with just a coderef. | ||
| 56 | # OpenID 2 spec only requires enumeration to support | ||
| 57 | # extension namespaces, so we don't care too much. | ||
| 58 | $enumer = sub { return (); }; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | $what = 'undef' if !defined $what; | ||
| 62 | Carp::croak("Unknown parameter type ($what)"); | ||
| 63 | } | ||
| 64 | $self->{getter} = $getter; | ||
| 65 | $self->{enumer} = $enumer; | ||
| 66 | |||
| 67 | # Now some quick pre-configuration of a few bits | ||
| 68 | |||
| 69 | # Is this an OpenID message at all? | ||
| 70 | # All OpenID messages have an openid.mode value... | ||
| 71 | return undef unless $self->get('mode'); | ||
| 72 | |||
| 73 | # Is this an OpenID 2.0 message? | ||
| 74 | my $ns = $self->get('ns'); | ||
| 75 | |||
| 76 | |||
| 77 | # The 2.0 spec section 4.1.2 requires that we support these namespace values | ||
| 78 | # but act like it's a normal 1.1 request. | ||
| 79 | # We do this by just pretending that ns wasn't set at all. | ||
| 80 | if ($ns && ($ns eq 'http://openid.net/signon/1.1' || $ns eq 'http://openid.net/signon/1.0')) { | ||
| 81 | $ns = undef; | ||
| 82 | } | ||
| 83 | |||
| 84 | if (defined($ns) && $ns eq OpenID::util::version_2_namespace()) { | ||
| 85 | $self->{protocol_version} = 2; | ||
| 86 | } | ||
| 87 | elsif (! defined($ns)) { | ||
| 88 | # No namespace at all means a 1.1 message | ||
| 89 | if (($self->{minimum_version}||0) <= 1) { | ||
| 90 | $self->{protocol_version} = 1; | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | # Pretend we don't understand the message. | ||
| 94 | return undef; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | else { | ||
| 98 | # Unknown version is the same as not being an OpenID message at all | ||
| 99 | return undef; | ||
| 100 | } | ||
| 101 | |||
| 102 | # This will be populated in on demand | ||
| 103 | $self->{extension_prefixes} = undef; | ||
| 104 | |||
| 105 | return $self; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub protocol_version { | ||
| 109 | return $_[0]->{protocol_version}; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub mode { | ||
| 113 | my $self = shift; | ||
| 114 | return $self->get('mode'); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub get { | ||
| 118 | my $self = shift; | ||
| 119 | my $key = shift or Carp::croak("No argument name supplied to get method"); | ||
| 120 | |||
| 121 | # NOTE: There is intentionally no way to get all of the keys in the core | ||
| 122 | # namespace because that means we don't need to be able to enumerate | ||
| 123 | # to support the core protocol, and there is no requirement to enumerate | ||
| 124 | # anyway. | ||
| 125 | |||
| 126 | # Arguments can only contain letters, numbers, underscores and dashes | ||
| 127 | Carp::croak("Invalid argument key $key") unless $key =~ /^[\w\-]+$/; | ||
| 128 | Carp::croak("Too many arguments") if scalar(@_); | ||
| 129 | |||
| 130 | return $self->{getter}->("openid.$key"); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub raw_get { | ||
| 134 | my $self = shift; | ||
| 135 | my $key = shift or Carp::croak("No argument name supplied to raw_get method"); | ||
| 136 | |||
| 137 | return $self->{getter}->($key); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub getter { | ||
| 141 | my $self = shift; | ||
| 142 | |||
| 143 | return $self->{getter}; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub get_ext { | ||
| 147 | my $self = shift; | ||
| 148 | my $namespace = shift or Carp::croak("No namespace URI supplied to get_ext method"); | ||
| 149 | my $key = shift; | ||
| 150 | |||
| 151 | Carp::croak("Too many arguments") if scalar(@_); | ||
| 152 | |||
| 153 | $self->_compute_extension_prefixes() unless defined($self->{extension_prefixes}); | ||
| 154 | |||
| 155 | my $alias = $self->{extension_prefixes}{$namespace}; | ||
| 156 | return $key ? undef : {} unless $alias; | ||
| 157 | |||
| 158 | if ($key) { | ||
| 159 | return $self->{getter}->("openid.$alias.$key"); | ||
| 160 | } | ||
| 161 | else { | ||
| 162 | my $prefix = "openid.$alias."; | ||
| 163 | my $prefixlen = length($prefix); | ||
| 164 | my $ret = {}; | ||
| 165 | foreach my $key ($self->{enumer}->()) { | ||
| 166 | next unless substr($key, 0, $prefixlen) eq $prefix; | ||
| 167 | $ret->{substr($key, $prefixlen)} = $self->{getter}->($key); | ||
| 168 | } | ||
| 169 | return $ret; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | sub has_ext { | ||
| 174 | my $self = shift; | ||
| 175 | my $namespace = shift or Carp::croak("No namespace URI supplied to get_ext method"); | ||
| 176 | |||
| 177 | Carp::croak("Too many arguments") if scalar(@_); | ||
| 178 | |||
| 179 | $self->_compute_extension_prefixes() unless defined($self->{extension_prefixes}); | ||
| 180 | |||
| 181 | return defined($self->{extension_prefixes}{$namespace}) ? 1 : 0; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _compute_extension_prefixes { | ||
| 185 | my ($self) = @_; | ||
| 186 | |||
| 187 | return unless $self->{enumer}; | ||
| 188 | |||
| 189 | $self->{extension_prefixes} = {}; | ||
| 190 | if ($self->protocol_version != 1) { | ||
| 191 | foreach my $key ($self->{enumer}->()) { | ||
| 192 | next unless $key =~ /^openid\.ns\.(\w+)$/; | ||
| 193 | my $alias = $1; | ||
| 194 | my $uri = $self->{getter}->($key); | ||
| 195 | $self->{extension_prefixes}{$uri} = $alias; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | # Synthesize the SREG namespace as it was used in OpenID 1.1 | ||
| 200 | $self->{extension_prefixes}{"http://openid.net/extensions/sreg/1.1"} = "sreg"; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | 1; | ||
| 205 | |||
| 206 | =head1 NAME | ||
| 207 | |||
| 208 | Net::OpenID::IndirectMessage - Class representing a collection of namespaced arguments | ||
| 209 | |||
| 210 | =head1 VERSION | ||
| 211 | |||
| 212 | version 1.030099_004 | ||
| 213 | |||
| 214 | =head1 DESCRIPTION | ||
| 215 | |||
| 216 | This class acts as an abstraction layer over a collection of flat URL arguments | ||
| 217 | which supports namespaces as defined by the OpenID Auth 2.0 specification. | ||
| 218 | |||
| 219 | It also recognises when its is given OpenID 1.1 non-namespaced arguments and | ||
| 220 | acts as if the relevant namespaces were present. In this case, it only | ||
| 221 | supports the basic OpenID 1.1 arguments and the extension arguments | ||
| 222 | for Simple Registration. | ||
| 223 | |||
| 224 | This class can operate on a normal hashref, a L<CGI> object, an L<Apache> | ||
| 225 | object, an L<Apache::Request> object, an L<Apache2::Request> object or an | ||
| 226 | arbitrary C<CODE> ref that takes a key name as its first parameter and returns | ||
| 227 | a value. However, if you use a coderef then extension arguments are not | ||
| 228 | supported. | ||
| 229 | |||
| 230 | If you pass in a hashref or a coderef it is your responsibility as the caller | ||
| 231 | to check the HTTP request method and pass in the correct set of arguments. If | ||
| 232 | you use an Apache, Apache::Request, Apache2::Request or CGI object then this | ||
| 233 | module will do the right thing automatically. | ||
| 234 | |||
| 235 | =head1 SYNOPSIS | ||
| 236 | |||
| 237 | use Net::OpenID::IndirectMessage; | ||
| 238 | |||
| 239 | # Pass in something suitable for the underlying flat dictionary. | ||
| 240 | # Will return an instance if the request arguments can be understood | ||
| 241 | # as a supported OpenID Message format. | ||
| 242 | # Will return undef if this doesn't seem to be an OpenID Auth message. | ||
| 243 | # Will croak if the $argumenty_thing is not of a suitable type. | ||
| 244 | my $args = Net::OpenID::IndirectMessage->new($argumenty_thing); | ||
| 245 | |||
| 246 | # Determine which protocol version the message is using. | ||
| 247 | # Currently this can be either 1 for 1.1 or 2 for 2.0. | ||
| 248 | # Expect larger numbers for other versions in future. | ||
| 249 | # Most callers don't really need to care about this. | ||
| 250 | my $version = $args->protocol_version(); | ||
| 251 | |||
| 252 | # Get a core argument value ("openid.mode") | ||
| 253 | my $mode = $args->get("mode"); | ||
| 254 | |||
| 255 | # Get an extension argument value | ||
| 256 | my $nickname = $args->get_ext("http://openid.net/extensions/sreg/1.1", "nickname"); | ||
| 257 | |||
| 258 | # Get hashref of all arguments in a given namespace | ||
| 259 | my $sreg = $args->get_ext("http://openid.net/extensions/sreg/1.1"); | ||
| 260 | |||
| 261 | Most of the time callers won't need to use this class directly, but will instead | ||
| 262 | access it through a L<Net::OpenID::Consumer> instance. | ||