Skip to content

Instantly share code, notes, and snippets.

/authexample.pl Secret

Created March 20, 2012 19:31
Show Gist options
  • Save anonymous/33d23edf8fa2c0f48dc0 to your computer and use it in GitHub Desktop.
Save anonymous/33d23edf8fa2c0f48dc0 to your computer and use it in GitHub Desktop.
CGI:: App authentication and authorization plugins
#!/usr/bin/perl
use strict;
use warnings;
my $webapp = WebApp->new();
$webapp->run();
package WebApp;
use parent 'CGI::Application';
use CGI::Application::Plugin::Authentication;
use CGI::Application::Plugin::Authorization;
use CGI::Application::Plugin::Authentication::Display::Classic;
sub setup {
my $self = shift;
$self->start_mode('mode1');
$self->mode_param('rm');
$self->authen->config(
DRIVER => [
'Generic',
{
user1 => '123',
user2 => '234',
user3 => '345'
},
],
);
#authentication
$self->authen->protected_runmodes(qw/mode1 mode2 mode3 logout 403/);
$self->authen->config(LOGIN_RUNMODE => 'login',);
$self->authen->config(POST_LOGIN_RUNMODE => 'mode3',);
#authorization
$self->authz->config(
FORBIDDEN_RUNMODE => '403',
DRIVER => [
'Generic',
sub {
my ($username, $group) = @_;
my $groupmap = {
user1 => [qw/all manager admin/],
user2 => [qw/all manager/],
user3 => [qw/all/],
};
return ($username && exists $groupmap->{$username}) ? (grep {/$group/} @{$groupmap->{$username}}) : undef;
}
],
);
$self->authz->authz_runmodes(
mode1 => 'admin',
mode2 => 'manager',
mode3 => 'all',
);
$self->run_modes(
AUTOLOAD => 'mode1',
login => 'login',
mode1 => 'auth_mode1',
mode2 => 'auth_mode2',
mode3 => 'auth_mode3',
logout => 'logout',
403 => 'throw_403',
);
}
sub error_mode {
my $self = shift;
return "error has occured";
}
sub _auth_template {
my $self = shift;
my $params = shift;
my $main = $params->{main};
return qq{
<a href=?rm=mode1>Mode 1</a> :: <a href=?rm=mode2>Mode 2</a> :: <a href=?rm=mode3>Mode 3</a> :: <a href=?rm=logout>logout</a>
<p/>$main
};
}
sub _unauth_template {
my $self = shift;
my $params = shift;
my $main = $params->{main};
return qq{
$main
};
}
sub throw_403 {
my $self = shift;
return $self->_auth_template({main => q{Unauthorized Access}});
}
sub login {
my $self = shift;
return $self->authen->login_box;
}
sub auth_mode1 {
my $self = shift;
return $self->_auth_template({main => q{Mode 1 - Congratulations, you have access!}});
}
sub auth_mode2 {
my $self = shift;
return $self->_auth_template({main => q{Mode 2 - Congratulations, you have access!}});
}
sub auth_mode3 {
my $self = shift;
return $self->_auth_template({main => q{Mode 3 - Congratulations, you have access!}});
}
sub logout {
my $self = shift;
$self->authen->logout;
return $self->_unauth_template({main => q{You are now logged out, <a href=?rm=login>login</a>}});
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment