|
NAMEDancer::Plugin::Auth::Extensible - extensible authentication framework for Dancer appsDESCRIPTIONA user authentication and authorisation framework plugin for Dancer apps.Makes it easy to require a user to be logged in to access certain routes, provides role-based access control, and supports various authentication methods/sources (config file, database, Unix system users, etc). Designed to support multiple authentication realms and to be as extensible as possible, and to make secure password handling easy. The base class for auth providers makes handling "RFC2307"-style hashed passwords really simple, so you have no excuse for storing plain-text passwords. A simple script to generate RFC2307-style hashed passwords is included, or you can use Crypt::SaltedHash yourself to do so, or use the "slappasswd" utility if you have it installed. SYNOPSISConfigure the plugin to use the authentication provider class you wish to use:plugins: Auth::Extensible: realms: users: provider: Example .... The configuration you provide will depend on the authentication provider module in use. For a simple example, see Dancer::Plugin::Auth::Extensible::Provider::Config. Define that a user must be logged in and have the proper permissions to access a route: get '/secret' => require_role Confidant => sub { tell_secrets(); }; Define that a user must be logged in to access a route - and find out who is logged in with the "logged_in_user" keyword: get '/users' => require_login sub { my $user = logged_in_user; return "Hi there, $user->{username}"; }; AUTHENTICATION PROVIDERSFor flexibility, this authentication framework uses simple authentication provider classes, which implement a simple interface and do whatever is required to authenticate a user against the chosen source of authentication.For an example of how simple provider classes are, so you can build your own if required or just try out this authentication framework plugin easily, see Dancer::Plugin::Auth::Extensible::Provider::Example. This framework supplies the following providers out-of-the-box:
Need to write your own? Just subclass Dancer::Plugin::Auth::Extensible::Provider::Base and implement the required methods, and you're good to go! CONTROLLING ACCESS TO ROUTESKeywords are provided to check if a user is logged in / has appropriate roles.
Replacing the Default " /login " and " /login/denied " RoutesBy default, the plugin adds a route to present a simple login form at that URL. If you would rather add your own, set the "no_default_pages" setting to a true value, and define your own route which responds to "/login" with a login page. Alternatively you can let DPAE add the routes and handle the status codes, etc. and simply define the setting "login_page_handler" and/or "permission_denied_page_handler" with the name of a subroutine to be called to handle the route. Note that it must be a fully qualified sub. E.g.plugins: Auth::Extensible: login_page_handler: 'My::App:login_page_handler' permission_denied_page_handler: 'My::App:permission_denied_page_handler' Then in your code you might simply use a template: sub permission_denied_page_handler { template 'account/login'; } If the user is logged in, but tries to access a route which requires a specific role they don't have, they will be redirected to the "permission denied" page URL, which defaults to "/login/denied" but may be changed using the "denied_page" option. Again, by default a route is added to respond to that URL with a default page; again, you can disable this by setting "no_default_pages" and creating your own. This would still leave the routes "post '/login'" and "any '/logout'" routes in place. To disable them too, set the option "no_login_handler" to a true value. In this case, these routes should be defined by the user, and should do at least the following: post '/login' => sub { my ($success, $realm) = authenticate_user( params->{username}, params->{password} ); if ($success) { session logged_in_user => params->{username}; session logged_in_user_realm => $realm; # other code here } else { # authentication failed } }; any '/logout' => sub { session->destroy; }; If you want to use the default "post '/login'" and "any '/logout'" routes you can configure them. See below. Keywords
SAMPLE CONFIGURATIONIn your application's configuation file:session: simple plugins: Auth::Extensible: # Set to 1 if you want to disable the use of roles (0 is default) disable_roles: 0 # After /login: If no return_url is given: land here ('/' is default) user_home_page: '/user' # After /logout: If no return_url is given: land here (no default) exit_page: '/' # List each authentication realm, with the provider to use and the # provider-specific settings (see the documentation for the provider # you wish to use) realms: realm_one: provider: Database db_connection_name: 'foo' Please note that you must have a session provider configured. The authentication framework requires sessions in order to track information about the currently logged in user. Please see Dancer::Session for information on how to configure session management within your application. AUTHORDavid Precious, "<davidp at preshweb.co.uk>"BUGS / FEATURE REQUESTSThis is an early version; there may still be bugs present or features missing.This is developed on GitHub - please feel free to raise issues or pull requests against the repo at: <https://github.com/bigpresh/Dancer-Plugin-Auth-Extensible> ACKNOWLEDGEMENTSValuable feedback on the early design of this module came from many people, including Matt S Trout (mst), David Golden (xdg), Damien Krotkine (dams), Daniel Perrett, and others.Configurable login/logout URLs added by Rene (hertell) Regex support for require_role by chenryn Support for user_roles looking in other realms by Colin Ewen (casao) Config options for default login/logout handlers by Henk van Oers (hvoers) LICENSE AND COPYRIGHTCopyright 2012-16 David Precious.This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information.
Visit the GSP FreeBSD Man Page Interface. |