Apache-AppSamurai

 view release on metacpan or  search on metacpan

lib/Apache/AppSamurai/AuthSimple.pm  view on Meta::CPAN

# Apache::AppSamurai::AuthSimple - AppSamurai "Simple" authentication framework
#                                  plugin

# $Id: AuthSimple.pm,v 1.2 2008/05/01 22:36:10 pauldoom Exp $

##
# Copyright (c) 2008 Paul M. Hirsch (paul@voltagenoir.org).
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the same terms as Perl itself.
##

package Apache::AppSamurai::AuthSimple;
use strict;
use warnings;

use vars qw($VERSION @ISA);
$VERSION = substr(q$Revision: 1.2 $, 10, -1);

use Carp;
use Apache::AppSamurai::AuthBase;
use Authen::Simple;

@ISA = qw( Apache::AppSamurai::AuthBase );

sub Configure {
    my $self = shift;

    # Pull defaults from AuthBase and save.
    $self->SUPER::Configure();
    my $conft = $self->{conf};
    
    # Initial configuration.  Put defaults here before the @_ args are
    # pulled in.
    $self->{conf} = { %{$conft},
		      # Send log messages to Apache::AppSamurai::AuthSimple
		      # itself (see debug, error, info, and warn methods below)
		      log => $self,
		      @_,
		  };

    return 1;
}

sub Initialize {
    my $self = shift;

   # A submodule MUST be selected at this point
    unless ($self->{conf}{SubModule}) {
	$self->AddError("Please specify a submodule for Authen::Simple to use!");
	return 0;
    }
    
    my $fullsub = 'Authen::Simple::' . $self->{conf}{SubModule};

    # Attempt to load the submodule
    unless (eval "require $fullsub") {
	$self->AddError("Unable to load Authen::Simple submodule, $fullsub: $@");
	return 0;
    }

    # Create a limited config hash with only the keys supported by the
    # adaptor submodule
    my %simpleconf;
    
    if ($fullsub->can('options')) {
	# Pull in only supported values
	foreach (keys %{$fullsub->options}) {
	    exists $self->{conf}{$_} and $simpleconf{$_} = $self->{conf}{$_};
	}
    } else {
	# Params::Validate appears not to be used for this adaptor, so
	# just copy in the full conf
	%simpleconf = %{$self->{conf}};
    }

    # Prepare an eval to create our Authen::Simple instance with our
    # chosen auth adaptor
    my $asconf = '$self->{authen} = Authen::Simple->new(' .
	$fullsub . '->new(%simpleconf))';

    unless (eval $asconf && $self->{authen}) {
	$self->AddError("Initialization of Authen::Simple failed: $@");
	return 0;
    }

    $self->{init} = 1;
    return 1;
}



( run in 0.511 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )