Apache-AuthTkt

 view release on metacpan or  search on metacpan

AuthTkt.pm  view on Meta::CPAN

my %DEFAULTS = (
    digest_type                 => 'MD5',
    cookie_name                 => 'auth_tkt',
    back_arg_name               => 'back',
    timeout                     => 2 * 60 * 60,
    timeout_min                 => 2 * 60,
    timeout_refresh             => 0.5,
    guest_login                 => 0,
    guest_user                  => 'guest',
    ignore_ip                   => 0,
    require_ssl                 => 0,
    cookie_secure               => 0,
);
my %BOOLEAN = map { $_ => 1 } qw(
    TKTAuthGuestLogin TKTAuthIgnoreIP TKTAuthRequireSSL TKTAuthCookieSecure
);
# Default TKTAuthDomain to host part of HTTP_HOST, or SERVER_NAME
($DEFAULTS{TKTAuthDomain}) = split /:/, $ENV{HTTP_HOST} || '';
$DEFAULTS{TKTAuthDomain} ||= $ENV{SERVER_NAME};
my %ATTR = map { $_ => 1 } qw(
    conf secret secret_old digest_type
    cookie_name back_cookie_name back_arg_name domain cookie_expires
    login_url timeout_url post_timeout_url unauth_url 
    timeout timeout_min timeout_refresh token debug
    guest_login guest_user ignore_ip require_ssl cookie_secure
);
#my %TICKET_ARGS = map { $_ => 1 } 

# digest_type => [ module, function ]
my %DIGEST_TYPE = (
    MD5     => [ 'Digest::MD5', 'md5_hex' ],
    SHA256  => [ 'Digest::SHA', 'sha256_hex' ],
    SHA512  => [ 'Digest::SHA', 'sha512_hex' ],
);

AuthTkt.pm  view on Meta::CPAN

    $cookie = $at->cookie(
        uid => $username, 
        cookie_name => 'auth_tkt',
        cookie_domain => 'www.openfusion.com.au',
    );

    # Access the shared secret
    $secret = $at->secret();
    # If using the 'conf' constructor above, all other TKTAuth attributes 
    #   are also available e.g.:
    print $at->cookie_name(), $at->ignore_ip(), $at->request_ssl();

    # Report error string
    print $at->errstr;


=head1 INTRODUCTION

Apache::AuthTkt is a module for generating and validating 
authentication tickets used with the 'mod_auth_tkt' apache module. 
Tickets are typically generated by a login web page of some kind 

AuthTkt.pm  view on Meta::CPAN

    $at->domain()
    $at->cookie_expires()
    $at->login_url()
    $at->timeout_url()
    $at->unauth_url()
    $at->timeout()
    $at->timeout_refresh()
    $at->token ()
    $at->guest_login()
    $at->ignore_ip()
    $at->require_ssl()


=head2 TICKET GENERATION

Tickets are generated using the ticket() method with named parameters:

    # Generate ticket
    $ticket = $at->ticket(uid => $username);

Ticket returns undef on error, with error information available via

AuthTkt.pm  view on Meta::CPAN

Cookie domain. Should match the TKTAuthDomain directive, if you're
using it. Default: $at->domain.

=item cookie_path

Cookie path. Default: '/'.

=item cookie_secure

Flag whether to set the 'secure' cookie flag, so that the cookie is 
returned only in HTTPS contexts. Default: $at->require_ssl, or 0.

=back

=head2 TICKET PARSING AND VALIDATION

You may parse and validate existing tickets with the validate_ticket() 
method. It takes as its first parameter the ticket to be validated, and
then an optional list of named parameter overrides 
(e.g. ip_addr => 'x.x.x.x'). If the ticket is valid, validate_ticket 
returns a hashref with the following key/value pairs:

t/01_basic.t  view on Meta::CPAN

    domain => '.openfusion.com.au',
    login_url => 'http://www.openfusion.com.au/auth/login.cgi',
    timeout_url => 'http://www.openfusion.com.au/auth/login.cgi?timeout=1',
    post_timeout_url => 'http://www.openfusion.com.au/auth/login.cgi?post_timeout=1',
    unauth_url => 'http://www.openfusion.com.au/auth/login.cgi?unauth=1',
    timeout => '2d',
    timeout_refresh => 0.33,
    guest_login => 0,
    guest_user => 'visitor',
    ignore_ip => 1,
    require_ssl => 1,
    cookie_secure => 1,
    debug => 1,
);
ok($at = Apache::AuthTkt->new(%arg), 'non-conf constructor with args ok');
is($at->$_(), $arg{$_}, "$_ accessor value ok") for keys %arg;


# vim:ft=perl

t/04_parse_conf.t  view on Meta::CPAN

    domain => undef,
    cookie_expires => undef,
    login_url => => 'https://www.example.com/pub/login.cgi',
    timeout_url => undef,
    unauth_url => undef,
    timeout => 2 * 60 * 60,
    timeout_refresh => 0.5,
    token => undef,
    guest_login => 0,
    ignore_ip => 1,
    require_ssl => 0,
);
for (sort keys %attr) {
  is(eval "\$at->$_", $attr{$_}, "$_() ok");
}
ok(! defined eval { $at->foo }, "die on invalid method ok");

# auth_tkt2.conf - most attributes defined
ok($at = Apache::AuthTkt->new(conf => "$dir/t04/auth_tkt2.conf"),
    'conf constructor ok');
%attr = (

t/04_parse_conf.t  view on Meta::CPAN

    domain => 'www.example.com',
    cookie_expires => 86400,
    login_url => => 'https://www.example.com/pub/login.cgi',
    timeout_url => undef,
    unauth_url => undef,
    timeout => 60 * 60,
    timeout_refresh => 0.33,
    token => undef,
    guest_login => 1,
    ignore_ip => 1,
    require_ssl => 1,
);
for (sort keys %attr) {
  is(eval "\$at->$_", $attr{$_}, "$_() ok");
}
ok(! defined eval { $at->foo }, "die on invalid method ok");


# vim:ft=perl

t/07_mutator.t  view on Meta::CPAN

    domain => '.openfusion.com.au',
    login_url => 'http://www.openfusion.com.au/auth/login.cgi',
    timeout_url => 'http://www.openfusion.com.au/auth/login.cgi?timeout=1',
    post_timeout_url => 'http://www.openfusion.com.au/auth/login.cgi?post_timeout=1',
    unauth_url => 'http://www.openfusion.com.au/auth/login.cgi?unauth=1',
    timeout => '2d',
    timeout_refresh => 0.33,
    guest_login => 0,
    guest_user => 'visitor',
    ignore_ip => 1,
    require_ssl => 1,
    cookie_secure => 1,
);
my %arg2 = (
    timeout => '1d',
    timeout_refresh => 0.66,
    guest_login => 1,
    ignore_ip => 0,
    require_ssl => 0,
    cookie_secure => 0,
);
# Uppercase any missing arg2 args
for (keys %arg) {
  next if exists $arg2{$_};
  $arg2{$_} = uc $arg{$_};
}

ok($at = Apache::AuthTkt->new(%arg), 'non-conf constructor with args ok');
is($at->$_(), $arg{$_}, "$_ accessor value ok") for keys %arg;



( run in 1.663 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )