Jifty/LetMe.pm

filter: Statementstmt 20.8% 20 / 96 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 28.5% 6 / 21 Podpod 100% 10 / 10 totaltotal 22.3% 36 / 161 CC 16 SCAR 26.7
CC
16
Cov
16%
_generate_digest
42.0
validate
30.0
from_token
12.0
CRAP 14.5
« Jifty/Plugin/ErrorTemplates/View.pm ↑ Summary Jifty/Logger.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::LetMe;
5 1 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Math::BigInt::Calc;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use String::Koremutake ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base qw/Jifty::Object Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 __PACKAGE__->mk_accessors ( qw/checksum_provided email path args until user/);
12  
13 =head1 NAME
14  
15 Jifty::LetMe - A way to expose single-link URLs to your applications
16  
17 =head2 new
18  
19 Create a new "LetMe" authentication object; it takes no parameters.
20 It calls L</_init> to do any initialization.
21  
22 A LetMe is a way to provide a one-time-use URL for a particular purpose.
23 All LetMe objects give you a way to validate a user's identity and to
24 allow them a very small set of possible actions or page-access permissions.
25  
26 For example, you can put a LetMe URL in an email to a new user,
27 so that when they click on the URL you know that their email address
28 is valid.
29  
30 =cut
31  
32 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
33 0 my $class = shift;
34 0 my $self = {};
35 0 bless $self, $class;
36 0 $self->args({});
37 0 $self->_init(@_);
38 0 return $self;
39  
40 }
41  
42 =head2 _init @_
43  
44 Called with whatever L</new> was called with. By default, does nothing.
45  
46 =cut
47  
48 0 ▶sub _init { return shift }
Subroutine _init: not called CC=1 SCAR=6.9
49  
50 =head2 user
51  
52 Contains an app-specific "user" object.
53  
54 =cut
55  
56 =head2 validated_current_user
57  
58 If the user has presented a valid token, returns an (app-specific
59 subclass of the) L<Jifty::CurrentUser> object for the user who has the
60 email address in $self->email. If no user has that email address,
61 returns undef.
62  
63 =cut
64  
65 0 ▶sub validated_current_user {
Subroutine validated_current_user: not called CC=2 SCAR=17.9
Pod documented
66 0 my $self = shift;
67 0 ▶ return undef unless ( $self->validate );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate 0 0 0
68 0 my $currentuser = Jifty->app_class("CurrentUser");
69 0 return Jifty->app_class('CurrentUser')->new( email => $self->email );
70  
71 }
72  
73  
74 =head2 _user_from_email ADDRESS
75  
76 Returns an (app-specific subclass of the) L<Jifty::CurrentUser> object
77 for the user who has the email address I<ADDRESS>.
78  
79 =cut
80  
81 0 ▶sub _user_from_email {
Subroutine _user_from_email: not called CC=1 SCAR=6.9
82 0 my $self = shift;
83 0 my $email = shift;
84 0 my $currentuser_object_class = Jifty->app_class("CurrentUser");
85 0 my $u = $currentuser_object_class->new( email => $email )->user_object;
86 # we want to be able to get at their auth token.
87 0 $u->current_user( $currentuser_object_class->superuser );
88 0 return $u;
89 }
90  
91 0 ▶sub _generate_digest {
Subroutine _generate_digest: not called CC=6 SCAR=37.4
92 0 my $self = shift;
93  
94 # get user's generic secret
95 0 my $user;
96 0 ▶ return '' unless ( $user = $self->user || $self->_user_from_email($self->email) );
Branch: 1 branch
BranchTrueFalseTotal
unless $user = $self->user || $self->_user_from_email($self->email) 0 0 0
Condition0%0/3
$self->user || $self->_user_from_email($self->email)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->user || $self->_user_from_email($self->email)
$self->user $self->_user_from_email($self->email)
Truth table0%0/3
A || B
A= $self->userB= $self->_user_from_email($self->email)
ABresult
1X1
011
000
97 0 ▶ return '' unless ($user->auth_token);
Branch: 1 branch
BranchTrueFalseTotal
unless $user->auth_token 0 0 0
98  
99  
100 # build an md5sum of the email token and until and our secret
101 0 my $digest = Digest::MD5->new();
102 0 $digest->add( $user->auth_token );
103 0 $digest->add( $self->path );
104 0 my %args = %{$self->args};
105 0 $digest->add( Encode::encode_utf8($_), Encode::encode_utf8($args{$_})) for sort keys %args;
106 0 ▶ $digest->add( $self->until ) if ($self->until);
Branch: 1 branch
BranchTrueFalseTotal
if $self->until 0 0 0
107 0 return $digest->hexdigest();
108 }
109  
110  
111  
112 =head2 generate_checksum
113  
114 Returns an authentication checksum for the current combination of:
115  
116 user
117 path
118 arguments
119 until
120  
121 =cut
122  
123 0 ▶sub generate_checksum {
Subroutine generate_checksum: not called CC=1 SCAR=6.9
Pod documented
124 0 my $self = shift;
125  
126 0 return substr( $self->_generate_digest, 0, 16 );
127 }
128  
129 =head2 generate_koremutake_checksum
130  
131 Generate a slightly more pronounceable version of the checksum using
132 L<String::Koremutake>. Due to hex -> integer limitations, this is
133 imprecise and may vary depending on the platform it is used on; as
134 such, it is deprecated.
135  
136 =cut
137  
138 0 ▶sub generate_koremutake_checksum {
Subroutine generate_koremutake_checksum: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140  
141 # Only take the first 16 characters. We're really just trying to
142 # get something reasonably short, memorable and unguessable. Also,
143 # don't use Math::BigInt->new directly for simple computation,
144 # because it insists exporting overload to us, which makes
145 # devel::cover and devel::dprof very sad. This is deprecated in
146 # favor of generate_checksum, which returns a straight hex digest.
147 0 my $integer_digest = Math::BigInt::Calc->_str(
148 Math::BigInt::Calc->_from_hex(
149 substr( $self->_generate_digest, 0, 16 )
150 )
151 );
152  
153 # koremutake it. This loses precision, since most perls can't
154 # deal with 64 bits with precision. Thus, $integer_digest ends up
155 # being rounded, possibly in unpredicatable ways.
156 0 my $k = String::Koremutake->new;
157 0 return( $k->integer_to_koremutake($integer_digest));
158  
159 }
160  
161 =head2 from_token PATH
162  
163 Parse a string of the form
164  
165 mylongusername@example.com/update_task/23/until/20050101/bekidrikufryvagygefuba
166  
167 into
168  
169 email => mylongusername@example.com,
170 path => 'update_task/23'
171 until => 20050101,
172 checksum_provided => bekidrikufryvagygefuba
173  
174 =cut
175  
176 0 ▶sub from_token {
Subroutine from_token: not called CC=3 SCAR=24.8
Pod documented
177 0 my $self = shift;
178 0 my $token = shift;
179  
180 0 my @atoms = split('/',$token);
181  
182 0 $self->email( Jifty::I18N->maybe_decode_utf8(URI::Escape::uri_unescape( shift @atoms )) );
183 0 $self->path( shift @atoms );
184 0 $self->checksum_provided( pop @atoms );
185  
186 # If they don't even have the right number of items in the path, then we know that it's not valid
187 0 ▶ return undef unless (scalar @atoms % 2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @atoms % 2 == 0 0 0 0
188  
189 0 my %args = map { Jifty::I18N->maybe_decode_utf8(URI::Escape::uri_unescape($_)) } @atoms;
190 0 ▶ $self->until( delete $args{until} ) if $args{until};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"until"} 0 0 0
191  
192 0 $self->args(\%args);
193 }
194  
195  
196 =head2 as_token
197  
198 Returns the "letme" token for this set of credentials. This should round
199 trip cleanly with from_token
200  
201 =cut
202  
203 0 ▶sub as_token {
Subroutine as_token: not called CC=1 SCAR=6.9
Pod documented
204 0 my $self = shift;
205 0 $self->_generate_token( email => $self->email );
206 }
207  
208 =head2 as_encoded_token
209  
210 A variant of as_token that encodes the user's email address suitably
211 for passing in a URL
212  
213 =cut
214  
215 0 ▶sub as_encoded_token {
Subroutine as_encoded_token: not called CC=1 SCAR=6.9
Pod documented
216 0 my $self = shift;
217 0 $self->_generate_token( email => URI::Escape::uri_escape_utf8($self->email) );
218 }
219  
220 0 ▶sub _generate_token {
Subroutine _generate_token: not called CC=2 SCAR=17.9
221 0 my $self = shift;
222 0 my %args = (email => undef, @_);
223 return join ('/',
224 $args{'email'},
225 $self->path,
226 0 ▶ (map {URI::Escape::uri_escape_utf8($_)} %{$self->args}),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->until ? : 0 0 0
227 (defined $self->until ? ( 'until', $self->until ) : () ), #?
228 $self->generate_checksum
229 );
230 }
231  
232  
233 =head2 as_url
234  
235 Returns the fully qualified URL for this LetMe. It's composed of
236 Jifty->web->url, L</base_path> and L</as_encoded_token>
237  
238 =cut
239  
240 0 ▶sub as_url {
Subroutine as_url: not called CC=1 SCAR=6.9
Pod documented
241 0 my $self = shift;
242 0 return Jifty->web->url(path => $self->base_path . $self->as_encoded_token);
243  
244 }
245  
246  
247 =head2 base_path
248  
249 By default, all "LetMe" actions live at URLs under '/let' inside your
250 application. Override this subroutine to change that.
251  
252 By default, it returns '/let/'
253  
254 =cut
255  
256 0 ▶sub base_path {
Subroutine base_path: not called CC=1 SCAR=6.9
Pod documented
257 0 return '/let/';
258  
259 }
260  
261  
262 =head2 validate
263  
264 Returns true if the credentials the user presented validate OK.
265 Returns false otherwise.
266  
267 =cut
268  
269 0 ▶sub validate {
Subroutine validate: not called CC=5 SCAR=34.0
Pod documented
270 0 my $self = shift;
271  
272 # email must exist
273  
274 0 ▶ unless ($self->_user_from_email($self->email)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_user_from_email($self->email)) 0 0 0
275 0 $self->log->debug("Token validation failed - Invalid user");
276 0 return undef;
277 }
278  
279 0 ▶ unless ($self->path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->path) 0 0 0
280 0 $self->log->debug("Token validation failed - Invalid path");
281 0 return undef;
282 }
283 0 ▶ unless ($self->checksum_provided) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->checksum_provided) 0 0 0
284 0 $self->log->debug("Token validation failed - Checksum not provided");
285 0 return undef;
286 }
287  
288  
289 0 ▶ unless ($self->_correct_checksum_provided) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_correct_checksum_provided) 0 0 0
290 0 $self->log->debug("Token validation failed - Checksum not correct");
291 0 return undef;
292 }
293  
294 0 return 1;
295 }
296  
297  
298 =head2 _correct_checksum_provided
299  
300 Returns true if the checksum the user provided is correct. Doesn't
301 actually do much input checking. You want to call "validate"
302  
303 =cut
304  
305 0 ▶sub _correct_checksum_provided {
Subroutine _correct_checksum_provided: not called CC=3 SCAR=24.8
306 0 my $self = shift;
307 0 $self->log->debug("LetMe checksum: ".$self->checksum_provided . " vs ". $self->generate_checksum );
308 return undef
309 0 ▶ unless ( $self->checksum_provided eq $self->generate_checksum )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checksum_provided eq $self->generate_checksum or $self->checksum_provided eq $self->generate_koremutake_checksum 0 0 0
Condition0%0/3
$self->checksum_provided eq $self->generate_checksum or $self->checksum_provided eq $self->generate_koremutake_checksum
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->checksum_provided eq $self->generate_checksum or $self->checksum_provided eq $self->generate_koremutake_checksum
$self->checksum_provided eq $self->generate_checksum $self->checksum_provided eq $self->generate_koremutake_checksum
Truth table0%0/3
A or B
A= $self->checksum_provided eq $self->generate_checksumB= $self->checksum_provided eq $self->generate_koremutake_checksum
ABresult
1X1
011
000
310 or
311 ( $self->checksum_provided eq $self->generate_koremutake_checksum );
312  
313 0 return 1;
314 }
315  
316 1 1;
« Jifty/Plugin/ErrorTemplates/View.pm ↑ Summary Jifty/Logger.pm »