Skip to content

Commit

Permalink
move Authentication into separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
davemenninger committed Nov 13, 2014
1 parent febbde6 commit c5a41c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
6 changes: 4 additions & 2 deletions Readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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
28 changes: 3 additions & 25 deletions lib/Lrrr.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions lib/Lrrr/Authentication.pm
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit c5a41c6

Please sign in to comment.