CGI::A...ization(3pm)
π CGI::Application::Plugin::Authorization π
okay bestie, this is the CGI::Application::Plugin::Authorization manpage, section 3pm, straight outta the User Contributed Perl Documentation. it's the authorization framework for CGI::Application and it's honestly giving β¨gatekeeper but make it politeβ¨ β let's get into it π«Ά
πΈ NAME πΈ
CGI::Application::Plugin::Authorization β Authorization framework for CGI::Application. yes bestie, that's literally the whole name. iconic. π
β¨ SYNOPSIS β¨
here's the vibe, copy-paste ready:
use base qw(CGI::Application);
use CGI::Application::Plugin::Authentication;
use CGI::Application::Plugin::Authorization;
# default config for runmode authorization
__PACKAGE__->authz->config(
DRIVER => [ 'HTGroup', FILE => 'htgroup' ],
);
# Using a named configuration to distinguish it from
# the above configuration
__PACKAGE__->authz('dbaccess')->config(
DRIVER => [ 'DBI',
DBH => $self->dbh,
TABLES => ['user', 'access'],
JOIN_ON => 'user.id = access.user_id',
CONSTRAINTS => {
'user.name' => '__USERNAME__',
'access.table' => '__PARAM_1__',
'access.item_id' => '__PARAM_2__'
}
],
);
sub admin_runmode {
my $self = shift;
# User must be in the admin group to have access to this runmode
return $self->authz->forbidden unless $self->authz->authorize('admin');
# rest of the runmode
...
}
sub update_widget {
my $self = shift;
my $widget = $self->query->param('widget_id');
# Can this user edit this widget in the widgets table?
return $self->authz->forbidden unless $self->authz('dbaccess')->authorize(widgets => $widget);
# save changes to the widget
...
}
π¦ DESCRIPTION π¦
CGI::Application::Plugin::Authorization adds the ability to authorize users for specific tasks. so like β once a user has been authenticated and you actually know who you're dealing with, then you whip out this plugin to control what that user has access to. π
it imports two methods (authz and authorization) into your CGI::Application module. both of these methods are interchangeable, so pick one and use it consistently throughout your code, ok? don't be chaotic about it, bestie, consistency is the whole vibe. π
through the authz method you can call all the methods of the CGI::Application::Plugin::Authorization plugin. that's the whole gateway. π
π Named Configurations π
there could be multiple ways that you may want to authorize actions in different parts of your code. and sometimes these differences conflict with each other π. for example, you may have runmode level authorization that requires that the user belongs to a certain group. but secondly, you may have row level database authorization that requires that the username column of the table contains the name of the current user. these configurations would conflict with each other since they are authorizing using different information. messy. π
to solve this you can create multiple named configurations, by specifying a unique name to the c<authz> method.
__PACKAGE__->authz('dbaccess')->config(
DRIVER => [ 'DBI', ... ],
);
# later
$self->authz('dbaccess')->authorize(widgets => $widget_id);
see? one plugin, many personalities. main character energy for every use case. π
π EXPORTED METHODS π
π authz -and- authorization π
these methods are interchangeable and provided for users that either prefer brevity, or clarity. everything is controlled through this method call, which will return a CGI::Application::Plugin::Authorization object, or just the class name if called as a class method. π«Ά
when using the plugin, you will always first call $self->authz or __PACKAGE__->authz and then the method you wish to invoke. you can create multiple named authorization modules by providing a unique name to the call to authz. this will allow you to handle different types of authorization in your modules. for example, you could use the main configuration to do runmode level authorization, and use a named configuration to manage database row level authorization. slay. π
π₯ METHODS π₯
π§ config π§
this method is used to configure the CGI::Application::Plugin::Authorization module. it can be called as an object method, or as a class method.
the following parameters are accepted:
DRIVER
here you can choose which authorization module(s) you want to use to perform the authorization. for simplicity, you can leave off the CGI::Application::Plugin::Authorization::Driver:: part when specifying the DRIVER parameter. β¨
if this module requires extra parameters, you can pass an array reference that contains as the first parameter the name of the module, and the required parameters as the rest of the array. you can provide multiple drivers which will be used, in order, to check the permissions until a valid response is received. it's giving "one of you better say yes" π
DRIVER => [ 'DBI', dbh => $self->dbh ],
- or -
DRIVER => [
[ 'HTGroup', file => '.htgroup' ],
[ 'LDAP', binddn => '...', host => 'localhost', ... ]
],
FORBIDDEN_RUNMODE
here you can specify a runmode that the user will be redirected to if they fail the authorization checks. bye bestie, wrong door. πͺ
FORBIDDEN_RUNMODE => 'forbidden'
FORBIDDEN_URL
if your forbidden page is external to this module, then you can use this option to specify a URL that the user will be redirected to when they fail the authorization checks. if both FORBIDDEN_URL and FORBIDDEN_RUNMODE are specified, then the latter will take precedence. (that's FORBIDDEN_RUNMODE winning, in case you were wondering π)
FORBIDDEN_URL => 'http://example.com/forbidden.html'
GET_USERNAME
this option allows you to provide a method that should return us the username of the currently logged in user. it will be passed the current authz objects as the only parameter. this is not a required option, and can be omitted if you use the Authentication plugin, or if your authentication system sets $ENV{REMOTE_USER}.
GET_USERNAME => sub { my $authz = shift; return $authz->cgiapp->my_username }
π authz_runmodes π
this method takes a list of runmodes that are to be authorized, and the authorization rules for said runmodes. if a user tries to access one of these runmodes, then they will be redirected to the forbidden page unless authorization is granted. π«
the runmode names can be simple strings, regular expressions, coderefs (which are passed the name of the runmode as their only parameter), or special directives that start with a colon.
the authorization rules can be simple strings representing the name of the group that the user must be a member of, as a list-ref of group names (of which the user only has to be a member of any one of the groups, or as a code-ref that will be called (with no parameters).
this method is cumulative, so if it is called multiple times, the new values are appended to the list of existing entries. it returns a list containing all of the entries that have been configured thus far. β¨
NOTE: compatibility with the interface as was defined in0.06is preserved.0.06allowed for runmodes to be passed in as a list-ref of two-element lists to specify authorization rules. although this interface is supported, the extra list-refs aren't necessary. (so like, you can still do it, but why would you, bestie π)
:all β All runmodes in this module will require authorization
# match all runmodes
__PACKAGE__->authz->authz_runmodes(
':all' => 'admin',
);
# only protect runmodes one and two
__PACKAGE__->authz->authz_runmodes(
one => 'admin',
two => 'admin',
);
# protect only runmodes that start with auth_
__PACKAGE__->authz->authz_runmodes(
qr/^authz_/ => 'admin',
);
# protect all runmodes that *do not* start with public_
__PACKAGE__->authz->authz_runmodes(
qr/^(?!public_)/ => 'admin',
);
# preserve the interface from 0.06:
__PACKAGE__->authz->authz_runmodes(
[':all' => 'admin'],
);
π is_authz_runmode π
this method accepts the name of a runmode, and if that runmode requires authorization (ie the user needs to be a member of a particular group or has to satisfy some other authorization rule) then this method returns the corresponding authorization rule which must be satisfied (which could be either a scalar, a list-ref, or a code-ref, depending on how the rules were defined). π
β new β
this method creates a new CGI::Application::Plugin::Authorization object. it requires as it's only parameter a CGI::Application object. this method should never be called directly, since the authz method that is imported into the CGI::Application module will take care of creating the CGI::Application::Plugin::Authorization object when it is required. let the plugin handle it, bestie, it's a whole adult. π
π« instance π«
this method works the same way as new, except that it returns the same Authorization object for the duration of the request. this method should never be called directly, since the authz method that is imported into the CGI::Application module will take care of creating the CGI::Application::Plugin::Authorization object when it is required. same energy as above, one object per request, periodt. π
π authorize π
this method will test to see if the current user has access to the given resource. it will take the given parameters and test them against the DRIVER classes that have been configured. a true return value means the user should have access to the given resource. β
# is the current user in the admin group
if ($self->authz->authorize('admingroup')) {
# perform an admin action
}
πΈ username πΈ
this method will return the name of the currently logged in user. it uses three different methods to figure out the username:
GET_USERNAME option
use the subroutine provided by the GET_USERNAME option to figure out the current username
CGI::Application::Plugin::Authentication
see if the CGI::Application::Plugin::Authentication plugin is being used, and retrieve the username through this plugin
REMOTE_USER
see if the REMOTE_USER environment variable is set and use that value
three backup plans, zero excuses for not knowing who's logged in. π«Ά
π drivers π
this method will return a list of driver objects that are used for this authorization instance. π―
π¦ cgiapp π¦
this will return the underlying CGI::Application object. the source of all truth, lowkey. β¨
π§ setup_runmodes π§
this method is called during the prerun stage to register some custom runmodes that the Authentication plugin requires in order to function. π οΈ
...and bestie, there's more where that came from. part 2 is coming, don't you dare close the tab π
β¨ CGI::Application CALLBACKS β¨
We're gonna auto-magically add the authz_forbidden run mode for you if you're on CGI::Application 4.0 or greater. Slay π
If you're on an older version of CGI::Application though? Bestie, you gotta add it yourself. No cap.
sub cgiapp_prerun {
my $self = shift;
$self->run_modes( authz_forbidden => \&CGI::Application::Plugin::Authorization::authz_forbidden, );
}
πΈ prerun_callback πΈ
This one is a CGI::Application prerun callback, and it gets automatically registered for you if you're running CGI::Application 4.0 or greater. On an older CGI::Application, you have to roll your own cgiapp_prerun method and make sure you call this method from inside it. You got this π«Ά
sub cgiapp_prerun {
my $self = shift;
$self->CGI::Application::Plugin::Authorization::prerun_callback();
}
π¦ redirect_to_forbidden π¦
This method gets called during the prerun stage when the current user is not authorized and they're trying to access an authz runmode. It redirects them to the page you configured as the forbidden page, based on whatever value FORBIDDEN_RUNMODE or FORBIDDEN_URL is holding. And if you configured nothing at all? No worries β the default forbidden page is what they get. π
π forbidden π
This hands you back a forbidden page. It peeks at your configuration to see if there's a custom runmode or URL to redirect to, and if there isn't, it just calls the builtin authz_forbidden runmode. Simple as. β¨
π CGI::Application RUNMODES π
π₯ authz_forbidden π₯
This runmode is here for you if you don't feel like creating your own forbidden runmode. It shows the user a lil' simple error page. That's it, that's the whole vibe.
π authz_dummy_redirect π
This runmode exists purely for convenience, for when you need to do an external redirect. It literally just returns an empty string. Not me providing an entire runmode that does nothing π iconic behaviour honestly.
π EXAMPLE π
In a CGI::Application module, it goes a lil' something like this:
package MyCGIApp;
use base qw(CGI::Application);
use CGI::Application::Plugin::AutoRunmode;
use CGI::Application::Plugin::Authentication;
use CGI::Application::Plugin::Authorization;
# Configure Authentication
MyCGIApp->authen->config(
DRIVER => 'Dummy',
);
MyCGIApp->authen->protected_runmodes(qr/^admin_/);
# Configure Authorization (manages runmode authorization)
MyCGIApp->authz->config(
DRIVER => [ 'DBI',
DBH => $self->dbh,
TABLES => ['user', 'usergroup', 'group'],
JOIN_ON => 'user.id = usergroup.user_id AND usergroup.group_id = group.id',
CONSTRAINTS => {
'user.name' => '__USERNAME__',
'group.name' => '__GROUP__',
}
],
);
MyCGIApp->authz->authz_runmodes(
[a_runmode => 'a_group'],
[qr/^admin_/ => 'admin'],
[':all' => 'all_group'],
[sub {my $rm = shift; return ($rm eq "dangerous_rm")} => 'super_group'],
);
# Configure second Authorization module using a named configuration
__PACKAGE__->authz('dbaccess')->config(
DRIVER => [ 'DBI',
DBH => $self->dbh,
TABLES => ['user', 'access'],
JOIN_ON => 'user.id = access.user_id',
CONSTRAINTS => {
'user.name' => '__USERNAME__',
'access.table' => '__PARAM_1__',
'access.item_id' => '__PARAM_2__'
}
],
);
sub start : Runmode {
my $self = shift;
}
sub admin_one : Runmode {
my $self = shift;
# The user will only get here if they are logged in and
# belong to the admin group
}
sub admin_widgets : Runmode {
my $self = shift;
# The user will only get here if they are logged in and
# belong to the admin group
# Can this user edit this widget in the widgets table?
my $widget_id = $self->query->param('widget_id');
return $self->authz->forbidden unless $self->authz('dbaccess')->authorize(widgets => $widget_id);
}
π TODO π
So like, this module is definitely in a usable state, but there are still a few things missing that the author would love to add in eventually:
- provide easy methods for authorizing runmode access automatically
- allow subroutine attributes to configure authorization for a runmode
- write a tutorial/cookbook to include with the docs
The vision is there, bestie. The follow-through is pending π
π BUGS π
Heads up: this is alpha software. As such, the features and the interface are subject to change. So please check the Changes file when you upgrade. Like, actually check it. Fr fr. π
π SEE ALSO π
CGI::Application::Plugin::AuthenticationCGI::Applicationperl(1)
π AUTHOR π
Cees Hek <ceeshek@gmail.com>
π«Ά CREDITS π«Ά
Thanks to SiteSuite (http://www.sitesuite.com.au) for funding the development of this plugin and for releasing it to the world. Genuinely iconic of them β¨
π LICENCE AND COPYRIGHT π
Copyright (c) 2005, SiteSuite. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
β οΈ DISCLAIMER OF WARRANTY β οΈ
Okay so, legal voice time, but make it dramatic π :
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
And that's a wrap on the page, babe π this lil' doc came from perl v5.30.3 and was last touched on 2020-09-25 β go forth and authorize your runmodes responsibly, mwah πβ¨
π¦ where this page comes from
- debian bookworm: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms