Catalyst-Plugin-Authentication-Basic-Remote

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/Authentication/Basic/Remote.pm  view on Meta::CPAN

  MyApp->setup(qw/Authentication::Basic::Remote Session::FastMmap/);

  MyApp->config(
      authentication => {
          auth_url => 'http://example.com/',
	  
	  # Use Template when unauthorized. (option)
	  view_tt  => 'MyApp::V::TT',
	  template => '401.tt',

          # text in Authentication dialog (default="Require Authorization")
          auth_name => 'Require Authorization',
      },
  );

=head1 DEPRECATION NOTICE

This module has been deprecated. The use of a new Authentication style is recommended.

See L<Catalyst::Plugin::Authetnication> for detail.

=head1 DESCRIPTION

Catalyst authentication plugin that use remote host's Basic authentication.

It is only first time that plugin request to remote host for authentication.
After that, user infomation keeps in sessions.

=head1 METHODS

=over 4

=item prepare

=cut

sub prepare {
    my $c = shift;

    $c = $c->NEXT::prepare(@_);

    if ( $c->session->{user} and $c->session->{password} ) {
        $c->log->debug("Auth info found in Session:");
        $c->log->debug("user: ".$c->session->{user});
        $c->log->debug("pass: ".$c->session->{password});

        $c->req->{user}     = $c->session->{user};
        $c->req->{password} = $c->session->{password};
        return $c;
    }

    if ( $c->config->{authentication}->{auth_url} ) {
        if ( $c->req->header('Authorization') and  my ($tokens) = ( $c->req->header('Authorization') =~ /^Basic (.+)$/) ) {
            my ( $username, $password ) = split /:/, decode_base64($tokens);

            $c->log->debug("Authentication via ". $c->config->{authentication}->{auth_url} );
            $c->log->debug("user: $username");
            $c->log->debug("pass: $password");

            my $ua = LWP::UserAgent->new;
            my $req = HTTP::Request->new( HEAD => $c->config->{authentication}->{auth_url} );
            $req->header( 'Authorization' => $c->req->header('Authorization') );

            my $res = $ua->request($req);

            if ( $res->code ne '401' ) {
                $c->log->debug("Authorization successful.");
                $c->req->{user}         = $username;
                $c->session->{user}     = $username;
                $c->req->{password}     = $password;
                $c->session->{password} = $password;
                $c->_login(1);
            } else {
                $c->log->debug("Authorization failed.");
                $c->log->debug("Remote status line: " . $res->status_line);
            }
        }

        unless ( $c->req->{user} ) {
            $c->log->debug("return 401 Unauthorized.");
            $c->res->status(401);
            $c->res->header( 'WWW-Authenticate' =>
                  qq!Basic realm="@{[ $c->config->{authentication}->{auth_name} || 'Require Authorization' ]}"!
            );
        }
    }

    return $c;
}

=item dispatch

=cut

sub dispatch {
    my $c = shift;

    if ( $c->config->{authentication}->{template} ) {
        my $view = $c->config->{authentication}->{view_tt} || $c->config->{name};

        if ($view and $c->res->status eq '401') {
            $c->stash->{template} = $c->config->{authentication}->{template};
            $c->forward($view);
            return;
        }
    }

    return $c->NEXT::dispatch(@_);
}

=item login

=cut

sub login {
    my $c = shift;

    return unless $c->session->{user};
    return if ($c->_login);

    if ($c->config->{authentication}->{auth_url}) {



( run in 0.930 second using v1.01-cache-2.11-cpan-5735350b133 )