Catalyst::Plugin::Authentication::CDBI::Basic - (DEPRECATED) Basic Authorization with Catalyst


Catalyst-Plugin-Authentication-CDBI-Basic documentation Contained in the Catalyst-Plugin-Authentication-CDBI-Basic distribution.

Index


Code Index:

NAME

Top

Catalyst::Plugin::Authentication::CDBI::Basic - (DEPRECATED) Basic Authorization with Catalyst

SYNOPSIS

Top

    use Catalyst qw/Session::FastMmap Authentication::CDBI Authentication::CDBI::Basic/;

    __PACKAGE__->config(
        authentication => {
            # Configure Autentication::CDBI
            :
            # and
            basic => {
                realm => 'Require Authorization', # Basic realm

                no_session => 1,                  # disable auth caching (optional)

                # auto error responsing
                #   use View::TT
                template => '401.tt',
                view     => 'MyApp::V::TT',

                #   or plain text
                error_msg => 'Authentication Failed !',
            },
        },
    );

DEPRECATION NOTICE

Top

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

See Catalyst::Plugin::Authetnication for detail.

DESCRIPTION

Top

This plugin privide Basic Authorization mechanism for Catalyst Application.

This plugin is required C::P::Authentication::CDBI, for users info. And also use Session Plugin for authorization caching (optional but recommanded).

METHODS

Top

prepare

AUTHOR

Top

Daisuke Murase <typester@cpan.org>

COPYRIGHT

Top

SEE ALSO

Top

Catalyst::Plugin::Authentication::CDBI


Catalyst-Plugin-Authentication-CDBI-Basic documentation Contained in the Catalyst-Plugin-Authentication-CDBI-Basic distribution.
package Catalyst::Plugin::Authentication::CDBI::Basic;
use strict;
use NEXT;
use MIME::Base64;

our $VERSION = '0.02';

sub prepare {
    my $c = shift;
    $c = $c->NEXT::prepare(@_);

    my $auth_header = $c->req->header('Authorization') || '';
    $c->log->debug("Authorization: $auth_header") if $auth_header;
    if ($auth_header =~ /^Basic (.+)$/) {
        my ( $username, $password ) = split q{:}, decode_base64($1);

        $c->log->debug("username: $username");
        $c->log->debug("password: $password");

        ( $c->can('session_login') and !$c->config->{authentication}->{basic}->{no_session} )
            ? $c->session_login($username, $password)
            : $c->login($username, $password)
                if $username and $password;
    }

    unless ( $c->req->{user} ) {
        $c->res->header('WWW-Authenticate' => q[Basic realm="]
                                            . ( $c->config->{authentication}->{basic}->{realm} || 'Require Authorization' )
                                            . q["] );

        if ( $c->config->{authentication}->{basic}->{error_msg}
                 or $c->config->{authentication}->{basic}->{view} & $c->config->{authentication}->{basic}->{template} ) {
            $c->res->status(401);

            if ( $c->config->{authentication}->{basic}->{error_msg} ) {
                $c->res->body( $c->config->{authentication}->{basic}->{error_msg} );
            }
            else {
                $c->stash->{template} = $c->config->{authentication}->{basic}->{template};
                $c->forward( $c->config->{authentication}->{basic}->{view} );
            }
        }
    }

    $c;
}

1;