Firebase/Auth.pm

filter: Statementstmt 100% 55 / 55 Branchbran 50.0% 4 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 90.8% 79 / 87 SCAR 18.0
CC
6
Cov
91%
create_claims
5.5
create_token
2.3
BEGIN
1.0
CRAP 6.0
« Firebase.pm ↑ Summary
1 package Firebase::Auth;
2 1 $Firebase::Auth::VERSION = '0.0400';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Digest::SHA qw(hmac_sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 has token_version => (
13 is => 'rw',
14 1 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 1 );
16  
17 1 has secret => (
18 is => 'rw',
19 required=> 1,
20 );
21  
22 1 has data => (
23 is => 'rw',
24 predicate => 'has_data',
25 );
26  
27 has token_seperator => (
28 is => 'rw',
29 1 ▶ default => sub { '.' },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 );
31  
32 1 has expires => (
33 is => 'rw',
34 predicate => 'has_expires',
35 );
36  
37 1 has not_before => (
38 is => 'rw',
39 predicate => 'has_not_before',
40 );
41  
42 1 has admin => (
43 is => 'rw',
44 predicate => 'has_admin',
45 );
46  
47 1 has debug => (
48 is => 'rw',
49 predicate => 'has_debug',
50 );
51  
52 sub create_token {
53 1 ▶ my ($self, $data) = @_;
Subroutine create_token: called CC=2 SCAR=8.1
Pod documented
54 1 ▶ return $self->encode_token($self->create_claims($data || $self->data));
Condition33%1/3
$data || $self->data
l!l&&r!l&&!r
100
MC/DC0%0/2
$data || $self->data
$data $self->data
Truth table33%1/3
A || B
A= $dataB= $self->data
ABresult
1X1
011
000
55 }
56  
57 sub create_claims {
58 1 ▶ my ($self, $data) = @_;
Subroutine create_claims: called CC=5 SCAR=17.0
Pod documented
59 1 my %claims = (
60 v => $self->token_version,
61 iat => mktime(localtime(time)),
62 d => $data,
63 );
64 1 ▶ $claims{admin} = $self->admin if $self->has_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_admin 1 0 1
65 1 ▶ $claims{exp} = $self->expires if $self->has_expires;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_expires 0 1 1
66 1 ▶ $claims{nbf} = $self->not_before if $self->has_not_before;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_not_before 0 1 1
67 1 ▶ $claims{debug} = $self->debug if $self->has_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_debug 0 1 1
68 1 return \%claims;
69 }
70  
71 sub encode_token {
72 1 ▶ my ($self, $claims) = @_;
Subroutine encode_token: called CC=1 SCAR=0.0
Pod documented
73 1 my $ejsn = JSON::XS->new->utf8->space_after->encode ({'typ'=> 'JWT', 'alg'=> 'HS256'}) ;
74 1 my $encoded_header = $self->urlbase64_encode( $ejsn);
75 1 my $eclm = JSON::XS->new->utf8->space_after->encode ($claims);
76 1 my $encoded_claims = $self->urlbase64_encode( $eclm );
77 1 my $secure_bits = $encoded_header . $self->token_seperator . $encoded_claims;
78 1 return $secure_bits . $self->token_seperator . $self->urlbase64_encode($self->sign($secure_bits));
79 }
80  
81 sub urlbase64_encode {
82 3 ▶ my ($self, $data) = @_;
Subroutine urlbase64_encode: called CC=1 SCAR=0.0
Pod documented
83 3 $data = encode_base64($data, '');
84 3 $data =~ tr|+/=|\-_|d;
85 3 return $data;
86 }
87  
88 sub sign {
89 1 ▶ my ($self, $bits) = @_;
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
90 1 return hmac_sha256($bits, $self->secret);
91 }
92  
93  
94  
95 =head1 NAME
96  
97 Firebase::Auth - Auth token generation for firebase.com.
98  
99 =head1 VERSION
100  
101 version 0.0400
102  
103 =head1 SYNOPSIS
104  
105 use Firebase::Auth;
106
107 my $token = Firebase::Auth->new(token => 'xxxxxxxxx', admin => 'true', data => \%user_data )->create_token();
108  
109  
110 =head1 DESCRIPTION
111  
112 This module provides a Perl class to generate auth tokens for L<http://www.firebase.com>. See L<https://www.firebase.com/docs/security/custom-login.html> for details on the spec.
113
114
115 =head1 METHODS
116  
117  
118 =head2 new
119  
120 Constructor.
121  
122 =over
123  
124 =item data
125  
126 Optional. If you don't specify this, then you need to specify it when you call create_token(). This should be a hash reference of all the data you want to pass for user data. This data will be available as the C<auth> object in Firebase's security rules.
127  
128 =item secret
129  
130 Required. The api secret token provided by firebase.com.
131  
132 =item admin
133  
134 Defaults to C<\0>. If set to C<\1> (a reference to zero or one) then full access will be granted for this token.
135  
136 =item debug
137  
138 Defaults to C<\0>. If set to C<\1> (a reference to zero or one) then verbose error messages will be returned from service calls.
139  
140 B<NOTE:> To access debug info, call C<debug> on the L<Firebase> object after making a request.
141  
142 =item expires
143  
144 An epoch date. Defaults to expiring 24 hours from the issued date.
145  
146 =item not_before
147  
148 An epoch date. The opposite of C<expires>. Defaults to now. The token will not be valid until after this date.
149  
150 =item token_version
151  
152 Defaults to C<0>.
153  
154 =item token_separator
155  
156 Defaults to C<.>
157  
158 =back
159  
160  
161 =head2 urlbase64_encode
162  
163 URL base-64 encodes a string, and then does some minor translation on it to make it compatible with Firebase.
164  
165 =over
166  
167 =item string
168  
169 The string to encode.
170  
171 =back
172  
173  
174  
175  
176 =head2 create_token
177  
178 Generates a signed token. This is probably the only method you'll ever need to call besides the constructor.
179  
180 =over
181  
182 =item data
183  
184 Required if not specified in constructor. Defaults to the C<data> element in the constructor. A hash reference of parameters you wish to pass to the service.
185  
186 =back
187  
188  
189  
190 =head2 create_claims
191  
192 Generates a list of claims based upon the options provided to the constructor.
193  
194 =over
195  
196 =item data
197  
198 Required. A hash reference of user data you wish to pass to the service.
199  
200 =back
201  
202  
203  
204 =head2 encode_token
205  
206 Encodes, signs, and formats the data into a token.
207  
208 =over
209  
210 =item claims
211  
212 Required. A list of claims as created by C<create_claims>
213  
214 =back
215  
216  
217 =head2 sign
218  
219 Generates a signature based upon a string of data.
220  
221 =over
222  
223 =item string
224  
225 A string to sign.
226  
227 =back
228  
229  
230  
231  
232 =head1 AUTHOR
233  
234 =over
235  
236 =item *
237  
238 Kiran Kumar, C<< <kiran at brainturk.com> >>
239  
240 =item *
241  
242 JT Smith, C<< <jt at plainblack.com> >>
243  
244 =back
245  
246  
247  
248 =head1 SUPPORT
249  
250 =over
251  
252 =item Source Code Repository
253  
254 L<https://github.com/rizen/Firebase>
255  
256 =item Issue Tracker
257  
258 L<https://github.com/rizen/Firebase/issues>
259  
260 =back
261  
262  
263  
264  
265 =head1 LICENSE AND COPYRIGHT
266  
267 Copyright 2013 Kiran Kumar.
268  
269 This program is free software; you can redistribute it and/or modify it
270 under the terms of the the Artistic License (2.0). You may obtain a
271 copy of the full license at:
272  
273 L<http://www.perlfoundation.org/artistic_license_2_0>
274  
275 Any use, modification, and distribution of the Standard or Modified
276 Versions is governed by this Artistic License. By using, modifying or
277 distributing the Package, you accept this license. Do not use, modify,
278 or distribute the Package, if you do not accept this license.
279  
280 If your Modified Version has been derived from a Modified Version made
281 by someone other than you, you are nevertheless required to ensure that
282 your Modified Version complies with the requirements of this license.
283  
284 This license does not grant you the right to use any trademark, service
285 mark, tradename, or logo of the Copyright Holder.
286  
287 This license includes the non-exclusive, worldwide, free-of-charge
288 patent license to make, have made, use, offer to sell, sell, import and
289 otherwise transfer the Package with respect to any patent claims
290 licensable by the Copyright Holder that are necessarily infringed by the
291 Package. If you institute patent litigation (including a cross-claim or
292 counterclaim) against any party alleging that the Package constitutes
293 direct or contributory patent infringement, then this Artistic License
294 to you shall terminate on the date that such litigation is filed.
295  
296 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
297 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
298 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
299 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
300 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
301 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
302 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
303 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
304  
305  
306 =cut
307  
308 1 1; # End of WWW::Firebase
« Firebase.pm ↑ Summary