blib/lib/MojoX/Auth/Simple.pm

filter: Statementstmt 30.9% 17 / 55 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 36.3% 4 / 11 Podpod 100% 5 / 5 totaltotal 26.5% 26 / 98 CC 13 SCAR 28.5
CC
13
Cov
21%
load
42.0
log_in
20.0
_authenticate
12.0
CRAP 17.4
↑ Summary
1 package MojoX::Auth::Simple;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base 'Mojo::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MojoX::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.04.02';
9  
10 2 __PACKAGE__->attr(logged_in => 0);
11 0 ▶__PACKAGE__->attr(loader => sub { Mojo::Loader->new });
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 2 __PACKAGE__->attr(qw/session/);
13  
14 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
15 0 my $class = shift;
16 0 my %args = @_;
17 0 my $self = $class->SUPER::new(%args);
18  
19 0 return $self;
20 }
21  
22 0 ▶sub log_in {
Subroutine log_in: not called CC=4 SCAR=30.0
Pod documented
23 0 my $self = shift;
24 0 my $uid = shift;
25 0 my $token = shift;
26  
27 0 ▶ die "self undefined" unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
28 0 ▶ die "session undefined" unless defined $self->session;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->session 0 0 0
29 0 ▶ _authenticate($self, $uid, $token) unless 'true' eq $self->session->data->{logged_in};
Branch: 1 branch
BranchTrueFalseTotal
unless "true" eq $self->session->data->{"logged_in"} 0 0 0
30 }
31  
32 0 ▶sub log_out {
Subroutine log_out: not called CC=2 SCAR=17.9
Pod documented
33 0 my $self = shift;
34
35 0 ▶ if (is_logged_in($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_logged_in($self)) 0 0 0
36 0 $self->session->data('logged_in' => 'false', 'uid' => '');
37 }
38 }
39  
40 0 ▶sub is_logged_in {
Subroutine is_logged_in: not called CC=2 SCAR=17.9
Pod documented
41 0 my $self = shift;
42  
43 0 ▶ if ($self->session->data->{logged_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->session->data->{"logged_in"}) 0 0 0
44 0 return $self->session->data->{uid};
45 }
46 0 return "";
47 }
48  
49 0 ▶sub load {
Subroutine load: not called CC=6 SCAR=37.4
Pod documented
50 0 my $self = shift;
51 0 ▶ my $session = shift || $self->session;
Condition0%0/3
shift() || $self->session
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->session
shift() $self->session
Truth table0%0/3
A || B
A= shift()B= $self->session
ABresult
1X1
011
000
52  
53 0 ▶ die "session undefined" unless defined $session;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session 0 0 0
54 0 ▶ die "session data undefined" unless defined $session->data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session->data 0 0 0
55
56 0 ▶ if ($session->load) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->load) { } 0 0 0
57 0 ▶ if ($session->is_expired) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->is_expired) { } 0 0 0
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 0 ▶sub _authenticate {
Subroutine _authenticate: not called CC=3 SCAR=24.8
75 0 my ($self, $uid, $token) = @_;
76  
77 0 ▶ if ('guest' eq $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('guest' eq $uid) { } 0 0 0
78 #warn "uid match";
79 0 ▶ if ('guest' eq $token){
Branch: 1 branch
BranchTrueFalseTotal
if ('guest' eq $token) { } 0 0 0
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 <h2>My appliction content goes here</h2><br>
141 <h3><a href="<%= url_for 'index' %><%="?from_url=$this_url" %>">Index</a></h3>
142 <h3><a href="<%= url_for 'edit' %><%="?from_url=$this_url" %>">Edit</a></h3>
143  
144 @@ layouts/default.html.ep
145 <html>
146 <head>
147 <title>My Application - <%= $page_title %> - Default layout</title>
148 </head>
149 <body>
150 <!-- Header region begin -->
151 <% if('true' eq $logged_in) { %>
152 <div>
153 <div>Logged in; <form action="<%= url_for 'logout' %>" method="POST">
154 <input type="submit" value="Logout"></form></div>
155 <% } else { %>
156 <div>Not logged in; <form action="<%= url_for 'login' %>" method="POST">
157 <input type="submit" value="Login"></form></div>
158 <% } %>
159 </div>
160 <!-- Content region begin -->
161 <%= content %>
162 <!-- Content region end -->
163 </body>
164 </html>
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<MojoX::Auth::Simple> inherits all methods from L<Mojo::Base> and implements the following new ones.
180  
181 =head2 C<new>
182  
183 my $auth = MojoX::Auth::Simple->new();
184  
185 Returns new L<MojoX::Auth> object.
186  
187 =head2 C<log_in>
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<log_out>
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<is_logged_in>
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<load>
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<bug-mojox-auth-simple at rt.cpan.org>, or through
227 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MojoX-Auth-Simple>. I will be notified, and then you'll
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 2 1; # End of MojoX::Auth::Simple
↑ Summary