| blib/lib/MojoX/Auth/Simple.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 12 | 49 | 24.4 |
| branch | 0 | 22 | 0.0 |
| condition | 0 | 3 | 0.0 |
| subroutine | 4 | 10 | 40.0 |
| pod | 5 | 5 | 100.0 |
| total | 21 | 89 | 23.6 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package MojoX::Auth::Simple; | ||||||
| 2 | |||||||
| 3 | 1 | 1 | 45593 | use strict; | |||
| 1 | 4 | ||||||
| 1 | 52 | ||||||
| 4 | 1 | 1 | 6 | use warnings; | |||
| 1 | 2 | ||||||
| 1 | 42 | ||||||
| 5 | 1 | 1 | 6 | use base 'Mojo::Base'; | |||
| 1 | 8 | ||||||
| 1 | 5896 | ||||||
| 6 | 1 | 1 | 31277 | use MojoX::Session; | |||
| 1 | 12467655 | ||||||
| 1 | 11 | ||||||
| 7 | |||||||
| 8 | our $VERSION = '0.04.02'; | ||||||
| 9 | |||||||
| 10 | __PACKAGE__->attr(logged_in => 0); | ||||||
| 11 | __PACKAGE__->attr(loader => sub { Mojo::Loader->new }); | ||||||
| 12 | __PACKAGE__->attr(qw/session/); | ||||||
| 13 | |||||||
| 14 | sub new { | ||||||
| 15 | 0 | 0 | 1 | my $class = shift; | |||
| 16 | 0 | my %args = @_; | |||||
| 17 | 0 | my $self = $class->SUPER::new(%args); | |||||
| 18 | |||||||
| 19 | 0 | return $self; | |||||
| 20 | } | ||||||
| 21 | |||||||
| 22 | sub log_in { | ||||||
| 23 | 0 | 0 | 1 | my $self = shift; | |||
| 24 | 0 | my $uid = shift; | |||||
| 25 | 0 | my $token = shift; | |||||
| 26 | |||||||
| 27 | 0 | 0 | die "self undefined" unless defined $self; | ||||
| 28 | 0 | 0 | die "session undefined" unless defined $self->session; | ||||
| 29 | 0 | 0 | _authenticate($self, $uid, $token) unless 'true' eq $self->session->data->{logged_in}; | ||||
| 30 | } | ||||||
| 31 | |||||||
| 32 | sub log_out { | ||||||
| 33 | 0 | 0 | 1 | my $self = shift; | |||
| 34 | |||||||
| 35 | 0 | 0 | if (is_logged_in($self)) { | ||||
| 36 | 0 | $self->session->data('logged_in' => 'false', 'uid' => ''); | |||||
| 37 | } | ||||||
| 38 | } | ||||||
| 39 | |||||||
| 40 | sub is_logged_in { | ||||||
| 41 | 0 | 0 | 1 | my $self = shift; | |||
| 42 | |||||||
| 43 | 0 | 0 | if ($self->session->data->{logged_in}) { | ||||
| 44 | 0 | return $self->session->data->{uid}; | |||||
| 45 | } | ||||||
| 46 | 0 | return ""; | |||||
| 47 | } | ||||||
| 48 | |||||||
| 49 | sub load { | ||||||
| 50 | 0 | 0 | 1 | my $self = shift; | |||
| 51 | 0 | 0 | my $session = shift || $self->session; | ||||
| 52 | |||||||
| 53 | 0 | 0 | die "session undefined" unless defined $session; | ||||
| 54 | 0 | 0 | die "session data undefined" unless defined $session->data; | ||||
| 55 | |||||||
| 56 | 0 | 0 | if ($session->load) { | ||||
| 57 | 0 | 0 | if ($session->is_expired) { | ||||
| 58 | # purge old session | ||||||
| 59 | 0 | $session->clear; | |||||
| 60 | 0 | $session->flush; | |||||
| 61 | # make a new session | ||||||
| 62 | 0 | $session->create; | |||||
| 63 | 0 | $session->data('logged_in' => 'false'); | |||||
| 64 | } else { | ||||||
| 65 | 0 | $session->extend_expires; | |||||
| 66 | } | ||||||
| 67 | } else { | ||||||
| 68 | 0 | $session->create; | |||||
| 69 | 0 | $session->data('logged_in' => 'false'); | |||||
| 70 | } | ||||||
| 71 | 0 | return $self->{session} = $session; | |||||
| 72 | } | ||||||
| 73 | |||||||
| 74 | sub _authenticate { | ||||||
| 75 | 0 | 0 | my ($self, $uid, $token) = @_; | ||||
| 76 | |||||||
| 77 | 0 | 0 | if ('guest' eq $uid ) { | ||||
| 78 | #warn "uid match"; | ||||||
| 79 | 0 | 0 | if ('guest' eq $token){ | ||||
| 80 | #warn "token match"; | ||||||
| 81 | 0 | $self->session->data('logged_in' => 'true', 'uid' => $uid); | |||||
| 82 | 0 | return 1; | |||||
| 83 | } else { | ||||||
| 84 | #warn "token match fails"; | ||||||
| 85 | } | ||||||
| 86 | } else { | ||||||
| 87 | #warn "uid match fails"; | ||||||
| 88 | } | ||||||
| 89 | 0 | return 0; | |||||
| 90 | } | ||||||
| 91 | |||||||
| 92 | =head1 NAME | ||||||
| 93 | |||||||
| 94 | MojoX::Auth::Simple - Perl extension for login authentication for Mojolicious | ||||||
| 95 | |||||||
| 96 | =head1 VERSION | ||||||
| 97 | |||||||
| 98 | Version 0.04.02 | ||||||
| 99 | |||||||
| 100 | =head1 SYNOPSIS | ||||||
| 101 | |||||||
| 102 | use MojoX::Auth::Simple; | ||||||
| 103 | use Mojolicious::Lite; | ||||||
| 104 | use MojoX::Session; | ||||||
| 105 | use DBI; | ||||||
| 106 | |||||||
| 107 | # fill in you $dbh details here... | ||||||
| 108 | my $db_host = "..."; | ||||||
| 109 | my $db_name = "..."; | ||||||
| 110 | my $dsn = "DBI:mysql:database=$db_name;host=$db_host;"; | ||||||
| 111 | my $user = "..."; | ||||||
| 112 | my $pass = "..."; | ||||||
| 113 | my $dbh = DBI->connect($dsn, $user, $pass, {'RaiseError' => '1'}); | ||||||
| 114 | |||||||
| 115 | plugin session => { | ||||||
| 116 | stash_key => 'session', | ||||||
| 117 | transport => MojoX::Session::Transport::Cookie->new, | ||||||
| 118 | store => MojoX::Session::Store::Dbi->new(dbh => $dbh), | ||||||
| 119 | expires_delta => 900, | ||||||
| 120 | }; | ||||||
| 121 | |||||||
| 122 | any [qw/get post/] => '/' => sub { | ||||||
| 123 | my $self = shift; | ||||||
| 124 | my $page_title = "Index - not logged in"; | ||||||
| 125 | my $template = "index"; | ||||||
| 126 | my $layout = "default"; | ||||||
| 127 | my $session = $self->stash('session'); | ||||||
| 128 | my $auth = MojoX::Auth::Simple->new(); | ||||||
| 129 | $auth->load($session); | ||||||
| 130 | $page_title = 'Index - logged in' if $auth->is_logged_in(); | ||||||
| 131 | $self->stash(date => $date, | ||||||
| 132 | page_title => $page_title, | ||||||
| 133 | logged_in => $auth->session->data->{logged_in}, | ||||||
| 134 | template => $template, | ||||||
| 135 | layout => $layout, | ||||||
| 136 | ); | ||||||
| 137 | } => 'index'; | ||||||
| 138 | |||||||
| 139 | @@ index.html.ep | ||||||
| 140 | My appliction content goes here |
||||||
| 141 | ">Index |
||||||
| 142 | ">Edit |
||||||
| 143 | |||||||
| 144 | @@ layouts/default.html.ep | ||||||
| 145 | |||||||
| 146 | |||||||
| 147 | |
||||||
| 148 | |||||||
| 149 | |||||||
| 150 | |||||||
| 151 | <% if('true' eq $logged_in) { %> | ||||||
| 152 | |
||||||
| 153 | Logged in; |
||||||
| 154 | |||||||
| 155 | <% } else { %> | ||||||
| 156 | Not logged in; |
||||||
| 157 | |||||||
| 158 | <% } %> | ||||||
| 159 | |||||||
| 160 | |||||||
| 161 | <%= content %> | ||||||
| 162 | |||||||
| 163 | |||||||
| 164 | |||||||
| 165 | |||||||
| 166 | =head1 DESCRIPTION | ||||||
| 167 | |||||||
| 168 | The aim of this mobule is to provide a framework to allow a simple | ||||||
| 169 | authentication model for Mojolicious. | ||||||
| 170 | |||||||
| 171 | This module will change and become a plugin like MojoX::Session. | ||||||
| 172 | |||||||
| 173 | =head2 EXPORT | ||||||
| 174 | |||||||
| 175 | None by default. | ||||||
| 176 | |||||||
| 177 | =head1 SUBROUTINES/METHODS | ||||||
| 178 | |||||||
| 179 | L |
||||||
| 180 | |||||||
| 181 | =head2 C |
||||||
| 182 | |||||||
| 183 | my $auth = MojoX::Auth::Simple->new(); | ||||||
| 184 | |||||||
| 185 | Returns new L |
||||||
| 186 | |||||||
| 187 | =head2 C |
||||||
| 188 | |||||||
| 189 | my $auth = MojoX::Auth::Simple->new(); | ||||||
| 190 | $auth->log_in(); | ||||||
| 191 | |||||||
| 192 | Sets the logged_in key in the session store to 'true' and adds the uid key | ||||||
| 193 | in the session store to the uid of the logged in user. | ||||||
| 194 | |||||||
| 195 | =head2 C |
||||||
| 196 | |||||||
| 197 | $auth->load($session); | ||||||
| 198 | $auth->log_out(); | ||||||
| 199 | |||||||
| 200 | Sets the logged_in key in the session store to 'false'. | ||||||
| 201 | |||||||
| 202 | =head2 C |
||||||
| 203 | |||||||
| 204 | $auth->load($session); | ||||||
| 205 | $name = $auth->is_logged_in(); | ||||||
| 206 | |||||||
| 207 | Returns the name or uid of the user that is logged in, or an empty string if they are not. | ||||||
| 208 | |||||||
| 209 | =head2 C |
||||||
| 210 | |||||||
| 211 | $auth->load($session); | ||||||
| 212 | |||||||
| 213 | Adds the current session to the auth object to use it as a persistant store. | ||||||
| 214 | |||||||
| 215 | =head1 SEE ALSO | ||||||
| 216 | |||||||
| 217 | Please read the man pages for MojoX::Session to see how we are storing | ||||||
| 218 | the basic auth info in the session hash in the stash. | ||||||
| 219 | |||||||
| 220 | =head1 AUTHOR | ||||||
| 221 | |||||||
| 222 | Kim Hawtin | ||||||
| 223 | |||||||
| 224 | =head1 BUGS | ||||||
| 225 | |||||||
| 226 | Please report any bugs or feature requests to C |
||||||
| 227 | the web interface at L |
||||||
| 228 | automatically be notified of progress on your bug as I make changes. | ||||||
| 229 | |||||||
| 230 | =head1 SUPPORT | ||||||
| 231 | |||||||
| 232 | You can find documentation for this module with the perldoc command. | ||||||
| 233 | |||||||
| 234 | perldoc MojoX::Auth::Simple | ||||||
| 235 | |||||||
| 236 | =head1 ACKNOWLEDGEMENTS | ||||||
| 237 | |||||||
| 238 | Thanks to Justin Hawkins for help with the building module and | ||||||
| 239 | to Andy Kirkpatrick for debugging. | ||||||
| 240 | |||||||
| 241 | =head1 COPYRIGHT AND LICENSE | ||||||
| 242 | |||||||
| 243 | Copyright (C) 2010 by Kim Hawtin | ||||||
| 244 | |||||||
| 245 | This program is free software; you can redistribute it and/or modify it | ||||||
| 246 | under the terms of either: the GNU General Public License as published | ||||||
| 247 | by the Free Software Foundation; or the Artistic License. | ||||||
| 248 | |||||||
| 249 | =cut | ||||||
| 250 | |||||||
| 251 | 1; # End of MojoX::Auth::Simple |