.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Mojolicious::Plugin::Authentication 3pm" .TH Mojolicious::Plugin::Authentication 3pm "2015-10-28" "perl v5.20.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojolicious::Plugin::Authentication \- A plugin to make authentication a bit easier .SH "VERSION" .IX Header "VERSION" version 1.29 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Mojolicious::Plugin::Authentication \& \& $self\->plugin(\*(Aqauthentication\*(Aq => { \& \*(Aqautoload_user\*(Aq => 1, \& \*(Aqsession_key\*(Aq => \*(Aqwickedapp\*(Aq, \& \*(Aqload_user\*(Aq => sub { ... }, \& \*(Aqvalidate_user\*(Aq => sub { ... }, \& \*(Aqcurrent_user_fn\*(Aq => \*(Aquser\*(Aq, # compatibility with old code \& }); \& \& if ($self\->authenticate(\*(Aqusername\*(Aq, \*(Aqpassword\*(Aq, { optional => \*(Aqextra data stuff\*(Aq })) { \& ... \& } .Ve .SH "METHODS" .IX Header "METHODS" .ie n .SS "authenticate($username, $password, $extra_data_hashref)" .el .SS "authenticate($username, \f(CW$password\fP, \f(CW$extra_data_hashref\fP)" .IX Subsection "authenticate($username, $password, $extra_data_hashref)" Authenticate will use the supplied \f(CW\*(C`load_user\*(C'\fR and \f(CW\*(C`validate_user\*(C'\fR subroutine refs to see whether a user exists with the given username and password, and will set up the session accordingly. Returns true when the user has been successfully authenticated, false otherwise. You can pass additional data along in the extra_data hashref, it will be passed to your \f(CW\*(C`validate_user\*(C'\fR subroutine as-is. If the extra data hash contains a key 'auto_validate', the value of that key will be used as the \s-1UID,\s0 and authenticate will not call your validate_user callback; this can be used when working with oAuth tokens or other authentication mechanisms that do not use a local username and password form. .SS "is_user_authenticated" .IX Subsection "is_user_authenticated" Returns true if \fIcurrent_user()\fR returns some valid object, false otherwise. .SS "current_user" .IX Subsection "current_user" Returns the user object as it was returned from the supplied \f(CW\*(C`load_user\*(C'\fR subroutine ref. .SS "reload_user" .IX Subsection "reload_user" Flushes the current user object and then returns \fIuser()\fR. .SS "signature_exists" .IX Subsection "signature_exists" Returns true if uid signature exist on the client side (in cookies), false otherwise. .PP Warning: non-secure check! Use this method only for a \*(L"fast & dirty\*(R" lookup to see if the client has the proper cookies. May be helpful in some cases (for example \- in counting 'guest'/'logged users' or for additional non-confidential information for 'logged users' but not for 'guest'). .SS "logout" .IX Subsection "logout" Removes the session data for authentication, and effectively logs a user out. .SH "CONFIGURATION" .IX Header "CONFIGURATION" The following options can be set for the plugin: .ie n .IP "load_user (\s-1REQUIRED\s0) A coderef for user loading (see ""\s-1USER LOADING""\s0)" 4 .el .IP "load_user (\s-1REQUIRED\s0) A coderef for user loading (see ``\s-1USER LOADING''\s0)" 4 .IX Item "load_user (REQUIRED) A coderef for user loading (see USER LOADING)" .PD 0 .ie n .IP "validate_user (\s-1REQUIRED\s0) A coderef for user validation (see ""\s-1USER VALIDATION""\s0)" 4 .el .IP "validate_user (\s-1REQUIRED\s0) A coderef for user validation (see ``\s-1USER VALIDATION''\s0)" 4 .IX Item "validate_user (REQUIRED) A coderef for user validation (see USER VALIDATION)" .IP "session_key (optional) The name of the session key" 4 .IX Item "session_key (optional) The name of the session key" .IP "autoload_user (optional) Turn on/off automatic loading of user data \- user data can be loaded only if it be used. May reduce site latency in some cases." 4 .IX Item "autoload_user (optional) Turn on/off automatic loading of user data - user data can be loaded only if it be used. May reduce site latency in some cases." .IP "current_user_fn (optional) Set the name for the \fIcurrent_user()\fR helper function" 4 .IX Item "current_user_fn (optional) Set the name for the current_user() helper function" .PD .PP In order to set the session expiry time, use the following in your startup routine: .PP .Vb 3 \& $app\->plugin(\*(Aqauthentication\*(Aq, { ... }); \& $app\->sessions\->default_expiration(86400); # set expiry to 1 day \& $app\->sessions\->default_expiration(3600); # set expiry to 1 hour .Ve .SH "USER LOADING" .IX Header "USER LOADING" The coderef you pass to the load_user configuration key has the following signature: .PP .Vb 5 \& sub { \& my ($app, $uid) = @_; \& ... \& return $user; \& } .Ve .PP The uid is the value that was originally returned from the \f(CW\*(C`validate_user\*(C'\fR coderef. You must return either a user object (it can be a hashref, arrayref, or a blessed object) or undef. .SH "USER VALIDATION" .IX Header "USER VALIDATION" User validation is what happens when we need to authenticate someone. The coderef you pass to the \f(CW\*(C`validate_user\*(C'\fR configuration key has the following signature: .PP .Vb 5 \& sub { \& my ($app, $username, $password, $extradata) = @_; \& ... \& return $uid; \& } .Ve .PP You must return either a user id or undef. The user id can be numerical or a string. Do not return hashrefs, arrayrefs or objects, since the behaviour of this plugin could get a little bit on the odd side of weird if you do that. .SH "EXAMPLES" .IX Header "EXAMPLES" For a code example using this, see the \fIt/01\-functional.t\fR and \fIt/02\-functional_lazy.t\fR tests, it uses Mojolicious::Lite and this plugin. .SH "ROUTING VIA CONDITION" .IX Header "ROUTING VIA CONDITION" This plugin also exports a routing condition you can use in order to limit access to certain documents to only authenticated users. .PP .Vb 1 \& $r\->route(\*(Aq/foo\*(Aq)\->over(authenticated => 1)\->to(\*(Aqmycontroller#foo\*(Aq); \& \& my $authenticated_only = $r\->route(\*(Aq/members\*(Aq)\->over(authenticated => 1)\->to(\*(Aqmembers#index\*(Aq); \& $authenticated_only\->route(\*(Aqonline\*(Aq)\->to(\*(Aqmembers#online\*(Aq); .Ve .PP If someone is not authenticated, these routes will not be considered by the dispatcher and unless you have set up a catch-all route, a 404 Not Found will be generated instead. .PP And another condition for fast and unsecured checking for users, having a signature (without validating it). This method just checks client cookies for uid data existing. .PP .Vb 1 \& $r\->route(\*(Aq/foo\*(Aq)\->over(signed => 1)\->to(\*(Aqmycontroller#foo\*(Aq); .Ve .PP This behavior is similar to the \*(L"authenticated\*(R" condition. .SH "ROUTING VIA CALLBACK" .IX Header "ROUTING VIA CALLBACK" If you want to be able to send people to a login page, you will have to use the following: .PP .Vb 2 \& my $members_only = $r\->route(\*(Aq/members\*(Aq)\->to(cb => sub { \& my $self = shift; \& \& $self\->redirect_to(\*(Aq/login\*(Aq) and return 0 unless($self\->is_user_authenticated); \& return 1; \& }); \& \& $members_only\->route(\*(Aqonline\*(Aq)\->to(\*(Aqmembers#online\*(Aq); .Ve .PP Lazy and unsecured methods: .PP .Vb 2 \& my $members_only = $r\->route(\*(Aq/unimportant\*(Aq)\->to(cb => sub { \& my $self = shift; \& \& $self\->redirect_to(\*(Aq/login\*(Aq) and return 0 unless($self\->signature_exists); \& return 1; \& }); \& \& $members_only\->route(\*(Aqpages\*(Aq)\->to(\*(Aqunimportant#pages\*(Aq); .Ve .SH "ROUTING VIA BRIDGE" .IX Header "ROUTING VIA BRIDGE" If you want to be able to send people to a login page, you will have to use the following: .PP .Vb 2 \& my $auth_bridge = $r\->under(\*(Aq/members\*(Aq)\->to(\*(Aqauth#check\*(Aq); \& $auth_bridge\->route(\*(Aq/list\*(Aq)\->to(\*(Aqmembers#list\*(Aq); # only visible to logged in users .Ve .PP And in your Auth controller you would put: .PP .Vb 5 \& sub check { \& my $self = shift; \& $self\->redirect_to(\*(Aq/login\*(Aq) and return 0 unless($self\->is_user_authenticated); \& return 1; \& }); .Ve .PP Lazy and unsecured methods: .PP .Vb 5 \& sub check { \& my $self = shift; \& $self\->redirect_to(\*(Aq/login\*(Aq) and return 0 unless($self\->signature_exists); \& return 1; \& }); .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious::Sessions, Mojocast 3: Authentication .SH "AUTHOR" .IX Header "AUTHOR" Ben van Staveren, \f(CW\*(C`\*(C'\fR .SH "BUGS / CONTRIBUTING" .IX Header "BUGS / CONTRIBUTING" Please report any bugs or feature requests through the web interface at . .SH "SUPPORT" .IX Header "SUPPORT" You can find documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc Mojolicious::Plugin::Authentication .Ve .PP You can also look for information at: .IP "\(bu" 4 AnnoCPAN: Annotated \s-1CPAN\s0 documentation .Sp .IP "\(bu" 4 \&\s-1CPAN\s0 Ratings .Sp .IP "\(bu" 4 Search \s-1CPAN\s0 .Sp .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" Andrew Parker \- For pointing out some bugs that crept in; a silent reminder not to code while sleepy .PP Mirko Westermeier (memowe) \- For doing some (much needed) code cleanup .PP Terrence Brannon (metaperl) \- Documentation patches .PP Karpich Dmitry (meettya) \- lazy_mode and signature_exists functionality, including a test and documentation .PP Ivo Welch \- For donating his first ever Mojolicious application that shows an example of how to use this module .PP Ed W \- Adding the \fIcurrent_user()\fR functionality, as well as some method renaming to make things a bit more sane. .PP SailingYYC (Github) \- For reporting an issue with routing conditions; I really should not code while sleepy, brainfarts imminent! .PP carragom (Github) \- For fixing the bug that'd consider an uid of 0 or \*(L"0\*(R" to be a problem .SH "LICENSE AND COPYRIGHT" .IX Header "LICENSE AND COPYRIGHT" Copyright 2011\-2015 Ben van Staveren. .PP This program is free software; you can redistribute it and/or modify it under the terms of either: the \s-1GNU\s0 General Public License as published by the Free Software Foundation; or the Artistic License. .PP See http://dev.perl.org/licenses/ for more information.