blib/lib/OAuthomatic/Types.pm

filter: Statementstmt 53.4% 47 / 88 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 56.2% 9 / 16 Podpod 0.0% 0 / 6 totaltotal 33.5% 56 / 167 CC 21 SCAR 34.6
CC
21
Cov
36%
__ANON__
90.0
of_my_type
20.0
equal
20.0
CRAP 31.8
« blib/lib/OAuthomatic/Internal/Util.pm ↑ Summary blib/lib/OAuthomatic/ServerDef.pm »
1  
2 ## no critic (ProhibitMultiplePackages, RequireFilenameMatchesPackage, RequireUseWArnings, RequireUseStrict, RequireExplicitPackage)
3  
4 =head1 NAME
5  
6 OAuthomatic::Types - few helper types to make code more readable and less error-prone
7  
8 =head1 DESCRIPTION
9  
10 Types below are defined to make code a bit more readable and less error prone.
11  
12 =cut
13  
14 =head1 OAuthomatic::Types::StructBase
15  
16 Role composed into types defined below. Handles a few common
17 conventions.
18  
19 =head2 METHODS
20  
21 =head3 new
22  
23 Adds two conventions to usual parameter handling:
24  
25 =over 4
26  
27 =item *
28  
29 Any empty or undef'ed values given to the constructor are dropped as
30 if they were not specified at all.
31  
32 =item *
33  
34 If args C<data> and C<remap> are given to the constructor, they are
35 used to translate field names, for example:
36  
37 Something->new(data=>{aaa=>'x', bbb=>'y'},
38 remap=>{aaa=>'token', 'bbb'=>'secret');
39  
40 is equivalent to:
41  
42 Something->new(token=>'x', secret=>'y');
43  
44 Partial replacements are possible too:
45  
46 Something->new(data=>{token=>'x', bbb=>'y'},
47 remap=>{'bbb'=>'secret');
48  
49 =back
50  
51 =head3 Class->of_my_type(obj)
52  
53 Checks whether given object is of given structure type. Returns 1 if so, 0 if it is undef,
54 throws if it is of another type.
55  
56 =head2 Class->equal(obj1, obj2)
57  
58 Compares two objects, allowing undef-s but raises on wrong type.
59  
60 =cut
61  
62 {
63 package OAuthomatic::Types::StructBase;
64 21 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 21 ▶ use OAuthomatic::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 21 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
67 21 ▶ use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 around BUILDARGS => sub {
70 0 ▶ my $orig = shift;
Subroutine __ANON__: not called CC=9 SCAR=45.0
71 0 my $class = shift;
72 0 my $ret = $class->$orig(@_);
73  
74 # Drop empty values (FIXME: this is close to MooseX::UndefTolerant)
75 0 foreach my $key (keys %$ret) {
76 0 my $val = $ret->{$key};
77 0 ▶ unless(defined($val) && $val =~ /./x) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val and $val =~ /./x) 0 0 0
Condition0%0/3
defined $val and $val =~ /./x
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val and $val =~ /./x
defined $val $val =~ /./x
Truth table0%0/3
A and B
A= defined $valB= $val =~ /./x
ABresult
0X0
100
111
78 0 delete $ret->{$key};
79 }
80 }
81  
82 # Remap names
83 0 ▶ if(exists $ret->{remap}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ret->{"remap"}) 0 0 0
84 0 my $remap = $ret->{remap};
85 my $data = $ret->{data} or
86 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
my $data = $ret->{"data"} or "OAuthomatic::Error::Generic"->throw("ident", "Bad call", "extra", "No data given in spite remap is specified") 0 0 0
87 ident => "Bad call",
88 extra => "No data given in spite remap is specified");
89 0 delete $ret->{remap};
90 0 delete $ret->{data};
91 0 my %data_unconsumed = %$data; # To delete consumed keys
92 0 foreach my $mapped (keys %$remap) {
93 0 my $mapped_to = $remap->{$mapped};
94 0 ▶ my $value = $data->{$mapped}
Branch: 1 branch
BranchTrueFalseTotal
my $value = $data->{$mapped} or "OAuthomatic::Error::Generic"->throw("ident", "Missing parameter", "extra", "Missing $mapped (while constructing $class). Known keys: ") . join(", ", keys %$data) . "\n" 0 0 0
95 or OAuthomatic::Error::Generic->throw(
96 ident => "Missing parameter",
97 extra => "Missing $mapped (while constructing $class). Known keys: ") . join(", ", keys %$data) . "\n";
98 0 delete $data_unconsumed{$mapped};
99 0 $ret->{$mapped_to} = $value;
100 }
101 # Copy unmapped data verbatim
102 0 ▶ while (my ($k, $v) = each %data_unconsumed) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %data_unconsumed) 0 0 0
103 0 $ret->{$k} = $v;
104 }
105 }
106 0 return $ret;
107 21 };
108  
109 sub of_my_type {
110 0 ▶ my ($class, $obj) = @_;
Subroutine of_my_type: not called CC=4 SCAR=30.0
Pod undocumented
111 0 ▶ return '' if ! defined($obj);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 0 0
112 0 my $pkg = blessed($obj);
113 0 ▶ unless( $pkg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg) 0 0 0
114 0 OAuthomatic::Error::Generic->throw(
115 ident => "Bad parameter", extra => "Wrong object type (expected $class, got non-blessed scalar)");
116 }
117 0 ▶ return 1 if $obj->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->isa($class) 0 0 0
118 0 return OAuthomatic::Error::Generic->throw(
119 ident => "Bad parameter", extra => "Wrong object type (expected $class, got $pkg)");
120 }
121 21 };
122  
123 =head1 OAuthomatic::Types::ClientCred
124  
125 Client (application) credentials. Fixed key and secret allocated manually
126 using server web interface (usually after filling some form with various
127 details) which identify the application.
128  
129 =head2 ATTRIBUTES
130  
131 =over 4
132  
133 =item key
134  
135 Client key - the application identifier.
136  
137 =item secret
138  
139 Client secret - confidential value used to sign requests, to prove key
140 is valid.
141  
142 =back
143  
144 =cut
145  
146 {
147 package OAuthomatic::Types::ClientCred;
148 21 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 21 with 'OAuthomatic::Types::StructBase';
150  
151 21 has 'key' => (is => 'ro', isa => 'Str', required => 1);
152 21 has 'secret' => (is => 'ro', isa => 'Str', required => 1);
153  
154 sub as_hash {
155 0 ▶ my ($self, $prefix) = @_;
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod undocumented
156 return (
157 0 $prefix . '_key' => $self->key,
158 $prefix . '_secret' => $self->secret,
159 );
160 }
161  
162 sub equal {
163 0 ▶ my ($class, $left, $right) = @_;
Subroutine equal: not called CC=4 SCAR=30.0
Pod undocumented
164 # Croaks of mismatches, false on undefs
165 0 ▶ return '' unless $class->of_my_type($left) && $class->of_my_type($right);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->of_my_type($left) and $class->of_my_type($right) 0 0 0
Condition0%0/3
$class->of_my_type($left) and $class->of_my_type($right)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->of_my_type($left) and $class->of_my_type($right)
$class->of_my_type($left) $class->of_my_type($right)
Truth table0%0/3
A and B
A= $class->of_my_type($left)B= $class->of_my_type($right)
ABresult
0X0
100
111
166 # Compare all fields
167 0 ▶ return ($left->key eq $right->key) && ($left->secret eq $right->secret);
Condition0%0/3
$left->key eq $right->key && $left->secret eq $right->secret
!ll&&!rl&&r
000
MC/DC0%0/2
$left->key eq $right->key && $left->secret eq $right->secret
$left->key eq $right->key $left->secret eq $right->secret
Truth table0%0/3
A && B
A= $left->key eq $right->keyB= $left->secret eq $right->secret
ABresult
0X0
100
111
168 }
169  
170 21 };
171  
172 # Common implementation for two classes below
173 {
174 package OAuthomatic::Types::GenericTokenCred;
175 21 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 21 with 'OAuthomatic::Types::StructBase';
177  
178 21 has 'token' => (is => 'ro', isa => 'Str', required => 1);
179 21 has 'secret' => (is => 'ro', isa => 'Str', required => 1);
180  
181 sub as_hash {
182 0 ▶ my $self = shift;
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod undocumented
183 return (
184 0 token => $self->token,
185 token_secret => $self->secret,
186 );
187 }
188  
189 sub equal {
190 0 ▶ my ($class, $left, $right) = @_;
Subroutine equal: not called CC=4 SCAR=30.0
Pod undocumented
191 # Croaks of mismatches, 0 on undefs
192 0 ▶ return '' unless $class->of_my_type($left) && $class->of_my_type($right);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->of_my_type($left) and $class->of_my_type($right) 0 0 0
Condition0%0/3
$class->of_my_type($left) and $class->of_my_type($right)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->of_my_type($left) and $class->of_my_type($right)
$class->of_my_type($left) $class->of_my_type($right)
Truth table0%0/3
A and B
A= $class->of_my_type($left)B= $class->of_my_type($right)
ABresult
0X0
100
111
193 # Compare all fields
194 0 ▶ return ($left->token eq $right->token) && ($left->secret eq $right->secret);
Condition0%0/3
$left->token eq $right->token && $left->secret eq $right->secret
!ll&&!rl&&r
000
MC/DC0%0/2
$left->token eq $right->token && $left->secret eq $right->secret
$left->token eq $right->token $left->secret eq $right->secret
Truth table0%0/3
A && B
A= $left->token eq $right->tokenB= $left->secret eq $right->secret
ABresult
0X0
100
111
195 }
196 21 };
197  
198 =head1 OAuthomatic::Types::TemporaryCred
199  
200 Temporary (request) credentials. Used during process of allocating
201 token credentials.
202  
203 =head2 ATTRIBUTES
204  
205 =over 4
206  
207 =item token
208  
209 Actual token - identifier quoted in requests.
210  
211 =item secret
212  
213 Associated secret - confidential value used to sign requests, to prove they
214 are valid.
215  
216 =item authorize_page
217  
218 Full URL of the page end user should use to spend this temporary credential
219 and generate access token. Already contains the token.
220  
221 =back
222  
223 =cut
224 {
225 21 package OAuthomatic::Types::TemporaryCred;
226 21 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 21 extends 'OAuthomatic::Types::GenericTokenCred';
228  
229 # This is rw and not required as we append it after initial object creation
230 21 has 'authorize_page' => (is => 'rw', isa => 'URI', required => 0);
231 };
232  
233  
234 =head1 OAuthomatic::Types::TokenCred
235  
236 Token (access) credentials. Those are used to sign actual API calls.
237  
238 =cut
239  
240 {
241 21 package OAuthomatic::Types::TokenCred;
242 21 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 21 extends 'OAuthomatic::Types::GenericTokenCred';
244 };
245  
246 =head1 OAuthomatic::Types::Verifier
247  
248 Verifier info, returned from authorization.
249  
250 =cut
251  
252 {
253 package OAuthomatic::Types::Verifier;
254 21 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
255 21 with 'OAuthomatic::Types::StructBase';
256  
257 21 has 'token' => (is => 'ro', isa => 'Str', required => 1);
258 21 has 'verifier' => (is => 'ro', isa => 'Str', required => 1);
259  
260 sub equal {
261 0 ▶ my ($class, $left, $right) = @_;
Subroutine equal: not called CC=4 SCAR=30.0
Pod undocumented
262 # Croaks of mismatches, 0 on undefs
263 0 ▶ return '' unless $class->of_my_type($left) && $class->of_my_type($right);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->of_my_type($left) and $class->of_my_type($right) 0 0 0
Condition0%0/3
$class->of_my_type($left) and $class->of_my_type($right)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->of_my_type($left) and $class->of_my_type($right)
$class->of_my_type($left) $class->of_my_type($right)
Truth table0%0/3
A and B
A= $class->of_my_type($left)B= $class->of_my_type($right)
ABresult
0X0
100
111
264 # Compare all fields
265 0 ▶ return ($left->token eq $right->token) && ($left->verifier eq $right->verifier);
Condition0%0/3
$left->token eq $right->token && $left->verifier eq $right->verifier
!ll&&!rl&&r
000
MC/DC0%0/2
$left->token eq $right->token && $left->verifier eq $right->verifier
$left->token eq $right->token $left->verifier eq $right->verifier
Truth table0%0/3
A && B
A= $left->token eq $right->tokenB= $left->verifier eq $right->verifier
ABresult
0X0
100
111
266 }
267 21 };
268  
269 21 1;
« blib/lib/OAuthomatic/Internal/Util.pm ↑ Summary blib/lib/OAuthomatic/ServerDef.pm »