Issuer.pm 8.03 KB
Newer Older
Yadd's avatar
Yadd committed
1 2 3 4 5 6 7 8 9 10 11
# Base package for simple issuers plugins
#
# Issuer should just implement a run() method that will be called only for
# authenticated users when PATH_INFO starts with issuerDBXXPath
#
# run() should just return a Lemonldap::NG::Portal::Main::Constants value. It
# is called using process() method (Lemonldap::NG::Portal::Main::Process)
package Lemonldap::NG::Portal::Main::Issuer;

use strict;
use Mouse;
Yadd's avatar
Yadd committed
12
use MIME::Base64;
13
use IO::String;
Yadd's avatar
Yadd committed
14 15 16
use Lemonldap::NG::Portal::Main::Constants qw(
  PE_OK
  PE_RENEWSESSION
Yadd's avatar
Yadd committed
17
  PE_SENDRESPONSE
Yadd's avatar
Yadd committed
18
);
Yadd's avatar
Yadd committed
19 20 21 22 23 24 25 26 27

extends 'Lemonldap::NG::Portal::Main::Plugin';

our $VERSION = '2.0.0';

# PROPERTIES

has type => ( is => 'rw' );

Yadd's avatar
Yadd committed
28 29
has path => ( is => 'rw' );

30 31
has _ott => (
    is      => 'rw',
32
    lazy    => 1,
33 34 35 36 37 38 39
    default => sub {
        my $ott = $_[0]->{p}->loadModule('::Lib::OneTimeToken');
        $ott->timeout( $_[0]->{conf}->{formTimeout} );
        return $ott;
    }
);

Yadd's avatar
Yadd committed
40 41 42 43 44 45
# INTERFACE

# Only logout is called in normal use. Issuer that inherits from this
# package are called only by their path
sub beforeLogout { 'logout' }

Yadd's avatar
Yadd committed
46 47 48 49 50 51 52 53
# INITIALIZATION

sub init {
    my ($self) = @_;
    my $type = ref( $_[0] );
    $type =~ s/.*:://;
    $self->type($type);
    if ( my $path = $self->conf->{"issuerDB${type}Path"} ) {
Yadd's avatar
Yadd committed
54
        $path =~ s/^.*?(\w+).*?$/$1/;
Yadd's avatar
Yadd committed
55
        $self->path($path);
Yadd's avatar
Yadd committed
56 57 58 59 60 61 62 63
        $self->addUnauthRoute(
            $path => { '*' => '_redirect' },
            [ 'GET', 'POST' ]
        );
        $self->addAuthRoute(
            $path => { '*' => "_forAuthUser" },
            [ 'GET', 'POST' ]
        );
Yadd's avatar
Yadd committed
64 65
    }
    else {
Yadd's avatar
Yadd committed
66
        $self->logger->debug("No path declared for issuer $type. Skipping");
Yadd's avatar
Yadd committed
67 68 69 70 71 72 73 74
    }
}

# RUNNING METHODS

# Case 1: Unauthentified users are redirected to the main portal

sub _redirect {
Yadd's avatar
Yadd committed
75
    my ( $self, $req, @path ) = @_;
Yadd's avatar
Yadd committed
76
    $self->logger->debug('Processing _redirect');
77
    $self->logger->debug('Store issuer request');
Yadd's avatar
Yadd committed
78 79
    my $ir =
      $req->param( 'issuerRequest' . $self->path ) || $self->storeRequest($req);
80
    $self->p->setHiddenFormValue( $req, 'issuerRequest' . $self->path,
Yadd's avatar
Yadd committed
81
        $ir, '' );
82 83 84 85
    $req->{urldc} = $self->conf->{portal};
    $req->{urldc} =~ s#/*$##;
    $req->{urldc} .= $req->path . "?issuerRequest$self->{path}=$ir";
    $self->p->setHiddenFormValue( $req, 'issuerUrldc', $req->urldc, '', 0 );
Yadd's avatar
Yadd committed
86 87

    if ( my $t = $req->param( 'issuerRequest' . $self->path ) ) {
88
        $ir = $t;
Yadd's avatar
Yadd committed
89
    }
Yadd's avatar
Yadd committed
90

Yadd's avatar
Yadd committed
91
    # TODO: launch normal process with 'run' at the end
Yadd's avatar
Yadd committed
92 93 94 95 96 97 98 99 100
    return $self->p->do(
        $req,
        [
            'controlUrl',
            @{ $self->p->beforeAuth },
            $self->p->authProcess,
            @{ $self->p->betweenAuthAndDatas },
            $self->p->sessionDatas,
            @{ $self->p->afterDatas },
Yadd's avatar
Yadd committed
101
            sub {
102 103
                # Restore urldc if auth doesn't need to dial with browser
                $self->restoreRequest( $req, $ir );
Yadd's avatar
Yadd committed
104
                return $self->run( @_, @path );
Yadd's avatar
Tidy  
Yadd committed
105
            }
Yadd's avatar
Yadd committed
106 107
        ]
    );
Yadd's avatar
Yadd committed
108 109 110 111
}

# Case 3: authentified user, launch
sub _forAuthUser {
Yadd's avatar
Yadd committed
112
    my ( $self, $req, @path ) = @_;
Yadd's avatar
Yadd committed
113
    $self->logger->debug('Processing _forAuthUser');
Yadd's avatar
Yadd committed
114 115 116
    if ( my $r = $req->param( 'issuerRequest' . $self->path ) ) {
        $self->restoreRequest( $req, $r );
    }
Yadd's avatar
Yadd committed
117
    $req->urlNotBase64(1) if ( ref($self) =~ /::CAS$/ );
Yadd's avatar
Yadd committed
118 119 120 121 122 123 124
    return $self->p->do(
        $req,
        [
            'importHandlerDatas',
            'controlUrl',
            @{ $self->p->forAuthUser },
            sub {
Yadd's avatar
Yadd committed
125
                return $self->run( @_, @path );
Yadd's avatar
Yadd committed
126 127 128 129 130
            },
        ]
    );
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
sub storeRequest {
    my ( $self, $req ) = @_;
    my $info = {};
    $info->{content} = $req->content;
    foreach ( keys %{ $req->env } ) {
        $info->{$_} = $req->env->{$_} unless ( ref $req->env->{$_} );
    }
    return $self->_ott->createToken($info);
}

sub restoreRequest {
    my ( $self, $req, $token ) = @_;
    my $env = $self->_ott->getToken($token);
    if ($env) {
        $self->logger->debug("Restoring request from $token");
        if ( my $c = delete $env->{content} ) {
            $env->{'psgix.input.buffered'} = 0;
            $env->{'psgi.input'}           = IO::String->new($c);
        }
        $req->{env} = {};
Yadd's avatar
Yadd committed
151
        foreach ( keys %$env ) {
152
            $self->logger->debug("Restore $_");
Yadd's avatar
Yadd committed
153
            $req->env->{$_} = $env->{$_} unless /^plack/;
154 155 156 157 158
        }
    }
    return $req;
}

Yadd's avatar
Yadd committed
159 160
sub reAuth {
    my ( $self, $req ) = @_;
161
    my $url =
Yadd's avatar
Yadd committed
162
        $self->conf->{portal}
163 164 165 166 167 168 169 170 171
      . $req->path_info
      . '?issuerRequest'
      . $self->path . '='
      . $self->storeRequest($req);
    if ( $self->conf->{skipRenewConfirmation} ) {
        $req->response( [ 302, [ Location => $url ], [] ] );
        return PE_SENDRESPONSE;
    }
    $req->datas->{_url} = encode_base64( $url, '' );
Yadd's avatar
Yadd committed
172 173 174
    return PE_RENEWSESSION;
}

Yadd's avatar
Yadd committed
175
1;
Yadd's avatar
Yadd committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
__END__

=pod

=encoding utf8

=head1 NAME

Lemonldap::NG::Portal::Main::Issuer - Base class for identity providers.

=head1 SYNOPSIS

  package Lemonldap::NG::Portal::Issuer::My;
  use strict;
  use Mouse;
  extends 'Lemonldap::NG::Portal::Main::Issuer';
  use Lemonldap::NG::Portal::Main::Constants qw(PE_OK);
  
  # Optional initialization method
  sub init {
      my ($self) = @_;
      ...
      # Must return 1 (succeed) or 0 (failure)
  }
  
  # Required methods are run() and logout(), they are launched only for
  # authenticated users
  # $req is a Lemonldap::NG::Portal::Main::Request object
  # They must return a Lemonldap::NG::Portal::Main::Constants constant
  sub run {
      my ( $self, $req ) = @_
      ...
      return PE_OK
  }
  
  sub logout {
      my ( $self, $req ) = @_
      ...
      return PE_OK
  }
  1;

=head1 DESCRIPTION

Lemonldap::NG::Portal::Main::Issuer is a base class to write identity providers
for Lemonldap::NG web-SSO system. It provide several methods to write easily
an IdP and manage authentication if the identity request comes before
authentication.

=head1 WRITING AN IDENTITY PROVIDER

To write a classic identity provider, you just have to inherit this class and
write run() and logout() methods. These methods must return a
Lemonldap::NG::Portal::Main::Constants constant.

A classic identity provider needs a "issuerDBE<gt>XXXE<lt>Path" parameter in
LLNG configuration to declare its base URI path (see
L<Lemonldap::NG::Manager::Build>). Example: /saml/. All requests that starts
Yadd's avatar
Yadd committed
234
with /saml/ will call run() after authentication if needed, and no one else.
Yadd's avatar
Yadd committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

The logout() function is called when user asks for logout on this server. If
you want to write an identity provider, you must implement a single logout
system.

=head2 managing other URI path

Lemonldap::NG::Portal::Main::Issuer provides methods to bind a method to an
URI path:

=over

=item addAuthRoute() for authenticated users

=item addUnauthRoute() for unauthenticated users

=back

They must be called during initialization process (so you must write the
optional init() sub).

Example:

  sub init {
      my ($self) = @_;
      ...
      $self->addUnauthRoute( saml => { soap => 'soapServer' }, [ 'POST' ] );
      return 1;
  }
  sub soapServer {
      my ( $self, $req ) = @_;
      ...
      # You must return a valid PSGI response
      return [ 200, [ 'Content-Type' => 'application/xml' ], [] ];
  }

=head1 SEE ALSO

L<http://lemonldap-ng.org/>

Yadd's avatar
Yadd committed
275
=head1 AUTHORS
Yadd's avatar
Yadd committed
276 277 278

=over

Yadd's avatar
Yadd committed
279
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
Yadd's avatar
Yadd committed
280 281 282 283 284 285

=back

=head1 BUG REPORT

Use OW2 system to report bug or ask for features:
Yadd's avatar
Yadd committed
286
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
Yadd's avatar
Yadd committed
287 288 289 290 291 292 293 294

=head1 DOWNLOAD

Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>

=head1 COPYRIGHT AND LICENSE

Yadd's avatar
Yadd committed
295
See COPYING file for details.
Yadd's avatar
Yadd committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see L<http://www.gnu.org/licenses/>.

=cut