[Catalyst] Re: Announcement - New Authen/Authz plugins

Uwe Voelker uwe.voelker at gmx.de
Wed Nov 9 11:23:43 CET 2005


Here is an example for working "http auth" code:


# Catalyst plugin: basic authentication (for all pages)

use MIME::Base64;


MyApp->config(
              name    => 'MyApp',
              #                     USER    PASS     KUNDE (0 = alle)
              auth    => {user => [['uwe',  'uwe',    0],
                                   ['uwe2', 'secret', 1],
                                  ],
                         },
             );

sub auto : Private {
    my ($self, $c) = @_;

    # Authorization-Header hinzufuegen
    $c->res->header('WWW-Authenticate' => 'Basic realm="My realm"');

    # wurden bereits Username und Passwort uebergeben?
    my $auth = $c->req->header('Authorization') || '';
    if ($auth =~ /Basic (.+)/) {
        my ($user, $pass) = split(/:/, decode_base64($1));

        foreach my $row (@{$c->config->{auth}->{user}}) {
            if ($user eq $row->[0] and $pass eq $row->[1]) {
                $c->stash->{auth_kunde} = $row->[2];
                return 1;
            }
        }
    }

    $c->res->status(401);
    $c->res->body('Authentication required');

    return 0;
}


It only stores the customer number in 'auth_kunde'. It need's to be
polished up (configure realm and so on). But it was working very well.


Bye,
Uwe



More information about the Catalyst mailing list