« blib/lib/Lemonldap/NG/Portal/Auth/GitHub.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Plugins/BasePasswordPolicy.pm »
| 1 | ## @file | ||
| 2 | # Subclass of Net::OpenID::Server that manage OpenID extensions | ||
| 3 | |||
| 4 | ## @class | ||
| 5 | # Subclass of Net::OpenID::Server that manage OpenID extensions | ||
| 6 | package Lemonldap::NG::Portal::Lib::OpenID::Server; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use base qw(Net::OpenID::Server); | ||
| 10 | use fields qw(_extensions); | ||
| 11 | use Net::OpenID::Server; | ||
| 12 | use Lemonldap::NG::Common::Regexp; | ||
| 13 | |||
| 14 | use constant DEBUG => 0; | ||
| 15 | |||
| 16 | our $VERSION = '2.23.0'; | ||
| 17 | |||
| 18 | use constant OPENID2_NS => 'http://specs.openid.net/auth/2.0'; | ||
| 19 | |||
| 20 | use constant OPENID2_ID_SELECT => | ||
| 21 | 'http://specs.openid.net/auth/2.0/identifier_select'; | ||
| 22 | |||
| 23 | *_push_url_arg = | ||
| 24 | ( $Net::OpenID::Server::VERSION >= 1.09 ) | ||
| 25 | ? *OpenID::util::push_url_arg | ||
| 26 | : *Net::OpenID::Server::_push_url_arg; | ||
| 27 | |||
| 28 | ## @cmethod Lemonldap::NG::Portal::Lib::OpenID::Server new(hash opts) | ||
| 29 | # Call Net::OpenID::Server::new() and store extensions | ||
| 30 | # @param %opts Net::OpenID::Server options | ||
| 31 | # @return Lemonldap::NG::Portal::Lib::OpenID::Server new object | ||
| 32 | sub new { | ||
| 33 | my $class = shift; | ||
| 34 | my $self = fields::new($class); | ||
| 35 | my %opts = @_; | ||
| 36 | $self->$_( delete $opts{$_} ) foreach (qw(extensions)); | ||
| 37 | $self->SUPER::new(%opts); | ||
| 38 | |||
| 39 | #$self->{get_args} = sub { $self->param(@_) }; | ||
| 40 | } | ||
| 41 | |||
| 42 | ## @method protected void extensions() | ||
| 43 | # Manage "extensions" constructor parameter | ||
| 44 | sub extensions { | ||
| 45 | my $self = shift; | ||
| 46 | $self->{_extensions} = shift; | ||
| 47 | } | ||
| 48 | |||
| 49 | ## @method protected list _mode_checkid(string mode, boolean redirect_for_setup) | ||
| 50 | # Overload Net::OpenID::Server::_mode_checkid to call extensions hook | ||
| 51 | # @param $mode OpenID mode | ||
| 52 | # @param $redirect_for_setup indicates that user must be redirected or not for | ||
| 53 | # setup | ||
| 54 | # @return (string $type, hashref parameters) | ||
| 55 | sub _mode_checkid { | ||
| 56 | my Lemonldap::NG::Portal::Lib::OpenID::Server $self = shift; | ||
| 57 | my ( $mode, $redirect_for_setup ) = @_; | ||
| 58 | |||
| 59 | my $return_to = $self->args("openid.return_to"); | ||
| 60 | return $self->_fail("no_return_to") | ||
| 61 | unless ( $return_to | ||
| 62 | and $return_to =~ m!^https?://! ); | ||
| 63 | |||
| 64 | my $trust_root = $self->args("openid.trust_root") || $return_to; | ||
| 65 | $trust_root = $self->args("openid.realm") | ||
| 66 | if $self->args('openid.ns') eq OPENID2_NS; | ||
| 67 | return $self->_fail("invalid_trust_root") | ||
| 68 | unless ( $trust_root =~ Lemonldap::NG::Common::Regexp::HTTP_URI | ||
| 69 | and Net::OpenID::Server::_url_is_under( $trust_root, $return_to ) ); | ||
| 70 | |||
| 71 | my $identity = $self->args("openid.identity"); | ||
| 72 | |||
| 73 | # chop off the query string, in case our trust_root came from the return_to URL | ||
| 74 | $trust_root =~ s/\?.*//; | ||
| 75 | |||
| 76 | my $u = $self->_proxy("get_user"); | ||
| 77 | if ( $self->args('openid.ns') eq OPENID2_NS | ||
| 78 | && $identity eq OPENID2_ID_SELECT ) | ||
| 79 | { | ||
| 80 | $identity = $self->_proxy( "get_identity", $u, $identity ); | ||
| 81 | } | ||
| 82 | my $is_identity = $self->_proxy( "is_identity", $u, $identity ); | ||
| 83 | my $is_trusted = | ||
| 84 | $self->_proxy( "is_trusted", $u, $trust_root, $is_identity ); | ||
| 85 | |||
| 86 | my ( %extVars, %is_ext_trusted ); | ||
| 87 | my $is_exts_trusted = 1; | ||
| 88 | if ( ref( $self->{_extensions} ) ) { | ||
| 89 | my @list = $self->args->(); | ||
| 90 | my %extArgs; | ||
| 91 | foreach my $arg (@list) { | ||
| 92 | if ( my ( $tmp, $val ) = $arg =~ /^openid\.(\w+)\.([\w\.]+)?/ ) { | ||
| 93 | $extArgs{$tmp}->{$val} = scalar $self->args->($arg); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | foreach my $ns ( keys %{ $self->{_extensions} } ) { | ||
| 97 | print STDERR "Launching OpenIP $ns hook\n" if (DEBUG); | ||
| 98 | my $h; | ||
| 99 | ( $is_ext_trusted{$ns}, $h ) = $self->{_extensions}->{$ns}->( | ||
| 100 | $u, $trust_root, $is_identity, $is_trusted, | ||
| 101 | delete( $extArgs{$ns} ) || {} | ||
| 102 | ); | ||
| 103 | if ($h) { | ||
| 104 | while ( my ( $k, $v ) = each %$h ) { | ||
| 105 | print STDERR "$ns returned data: $k => $v\n" if (DEBUG); | ||
| 106 | $extVars{"$ns.$k"} = $v; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | $is_exts_trusted &&= $is_ext_trusted{$ns}; | ||
| 110 | } | ||
| 111 | |||
| 112 | # TODO: warn if keys(%extArgs) | ||
| 113 | } | ||
| 114 | |||
| 115 | # assertion path: | ||
| 116 | if ( $is_identity && $is_trusted && $is_exts_trusted ) { | ||
| 117 | my %sArgs = ( | ||
| 118 | identity => $identity, | ||
| 119 | claimed_id => $self->args('openid.claimed_id'), | ||
| 120 | return_to => $return_to, | ||
| 121 | assoc_handle => $self->args("openid.assoc_handle"), | ||
| 122 | ns => $self->args('openid.ns'), | ||
| 123 | ); | ||
| 124 | $sArgs{additional_fields} = \%extVars if (%extVars); | ||
| 125 | my $ret_url = $self->signed_return_url(%sArgs); | ||
| 126 | return ( "redirect", $ret_url ); | ||
| 127 | } | ||
| 128 | |||
| 129 | # Assertion could not be made, so user requires setup (login/trust... | ||
| 130 | # something). Two ways that can happen: caller might have asked us for an | ||
| 131 | # immediate return with a setup URL (the default), or explictly said that | ||
| 132 | # we're in control of the user-agent's full window, and we can do whatever | ||
| 133 | # we want with them now. | ||
| 134 | |||
| 135 | # TODO: call extension sub for setup | ||
| 136 | my %setup_args = ( | ||
| 137 | $self->_setup_map("trust_root"), $trust_root, | ||
| 138 | $self->_setup_map("realm"), $trust_root, | ||
| 139 | $self->_setup_map("return_to"), $return_to, | ||
| 140 | $self->_setup_map("identity"), $identity, | ||
| 141 | $self->_setup_map("assoc_handle"), $self->args("openid.assoc_handle"), | ||
| 142 | %extVars, | ||
| 143 | ); | ||
| 144 | $setup_args{ $self->_setup_map('ns') } = $self->args('openid.ns') | ||
| 145 | if $self->args('openid.ns'); | ||
| 146 | |||
| 147 | my $setup_url = $self->{setup_url} | ||
| 148 | or Carp::croak("No setup_url defined."); | ||
| 149 | _push_url_arg( \$setup_url, %setup_args ); | ||
| 150 | |||
| 151 | if ( $mode eq "checkid_immediate" ) { | ||
| 152 | my $ret_url = $return_to; | ||
| 153 | if ( $self->args('openid.ns') eq OPENID2_NS ) { | ||
| 154 | _push_url_arg( \$ret_url, "openid.ns", $self->args('openid.ns') ); | ||
| 155 | _push_url_arg( \$ret_url, "openid.mode", "setup_needed" ); | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | _push_url_arg( \$ret_url, "openid.mode", "id_res" ); | ||
| 159 | _push_url_arg( \$ret_url, "openid.user_setup_url", $setup_url ); | ||
| 160 | } | ||
| 161 | return ( "redirect", $ret_url ); | ||
| 162 | } | ||
| 163 | else { | ||
| 164 | |||
| 165 | # the "checkid_setup" mode, where we take control of the user-agent | ||
| 166 | # and return to their return_to URL later. | ||
| 167 | |||
| 168 | if ($redirect_for_setup) { | ||
| 169 | return ( "redirect", $setup_url ); | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | return ( "setup", \%setup_args ); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | 1; | ||
| 178 | __END__ |