From c5a41c61460fa90fa95259655cfb0b173035f776 Mon Sep 17 00:00:00 2001 From: Dave Menninger Date: Wed, 12 Nov 2014 22:16:03 -0500 Subject: [PATCH] move Authentication into separate package --- Readme.markdown | 6 ++++-- lib/Lrrr.pm | 28 +++------------------------- lib/Lrrr/Authentication.pm | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 lib/Lrrr/Authentication.pm diff --git a/Readme.markdown b/Readme.markdown index 2963104..3d5eb03 100644 --- a/Readme.markdown +++ b/Readme.markdown @@ -10,14 +10,14 @@ This is a test app for trying new things in Mojolicious. [![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy) -# Roadmap +## Roadmap * ~~Add simplest authentication~~ * ~~move authentication routes into own controller~~ * ~~create login page ( post )~~ * ~~connect authentication to external database ( mongo )~~ * wait for November 18th, 2014 for mongolab to upgrade default version to 2.6, argh! -* create authentication plugin +* fix travis hack after travis upgrades default mongodb version to 2.6 * make authentication more secure ( hash, salt ) * tests for authentication * add authorization ( logged in user can see some things, but not others ) @@ -26,3 +26,5 @@ This is a test app for trying new things in Mojolicious. * roles: admin, author, reader? * more templates/routes: ~~home~~, ~~user~~, user/:username, "posts" ... * Dockerfile; add mongodb setup? fixtures? + +## Credits diff --git a/lib/Lrrr.pm b/lib/Lrrr.pm index 210703b..84405ac 100644 --- a/lib/Lrrr.pm +++ b/lib/Lrrr.pm @@ -5,7 +5,7 @@ use warnings; use Mojo::Base 'Mojolicious'; use Mango; -use Mango::BSON; +use Lrrr::Authentication; # This method will run once at server start sub startup { @@ -17,30 +17,8 @@ sub startup { # auth $self->plugin( authentication => { autoload_user => 1, - load_user => sub { - my $self = shift; - my $username = shift; - - my $collection = $self->mango->db('test')->collection('users'); - my $user = $collection->find_one( {username => $username} ); - - return { - 'username' => $user->{username} - } if ( defined $user->{username} ); - return undef; - }, - validate_user => sub { - my $self = shift; - my $username = shift || ''; - my $password = shift || ''; - my $extradata = shift || {}; - - my $collection = $self->mango->db('test')->collection('users'); - my $user = $collection->find_one( {username => $username} ); - - return $user->{username} if ( $password eq $user->{password} ); - return undef; - }, + load_user => sub { return Lrrr::Authentication->load_user(@_); }, + validate_user => sub { return Lrrr::Authentication->validate_user(@_); } }); # Router diff --git a/lib/Lrrr/Authentication.pm b/lib/Lrrr/Authentication.pm new file mode 100644 index 0000000..cf4345a --- /dev/null +++ b/lib/Lrrr/Authentication.pm @@ -0,0 +1,35 @@ +package Lrrr::Authentication; + +sub load_user { + my ($class, $app, $username) = @_; + + #return 1; + #my $self = shift; + #my $username = shift; + + my $collection = $app->mango->db('test')->collection('users'); + my $user = $collection->find_one( {username => $username} ); + + return { + 'username' => $user->{username} + } if ( defined $user->{username} ); + return undef; +} + +sub validate_user { + my ($class, $app, $username, $password, $extas) = @_; + + #return 1; + #my $self = shift; + #my $username = shift || ''; + #my $password = shift || ''; + #my $extradata = shift || {}; + + my $collection = $app->mango->db('test')->collection('users'); + my $user = $collection->find_one( {username => $username} ); + + return $user->{username} if ( $password eq $user->{password} ); + return undef; +} + +1;