blib/lib/GDPR/IAB/TCFv2/PublisherTC.pm

filter: Statementstmt 93.8% 76 / 81 Branchbran 53.8% 14 / 26 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 17 / 17 Podpod 85.7% 6 / 7 totaltotal 77.4% 117 / 151 CC 18 SCAR 14.0
CC
18
Cov
79%
Parse
9.0
is_purpose_consent_allowed
3.8
is_purpose_legitimate_interest_allowed
3.8
CRAP 4.0
« lib/GDPR/IAB/TCFv2/CMPValidator.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/Publisher.pm »
1 package GDPR::IAB::TCFv2::PublisherTC 0.520;
2 72 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 72 ▶use Carp qw<croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 72 use GDPR::IAB::TCFv2::BitUtils qw<is_set
8 get_uint3
9 get_uint6
10 72 ▶>;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use constant {
13 72 SEGMENT_TYPE_PUBLISHER_TC => 3,
14 MAX_PURPOSE_ID => 24,
15 OFFSETS => {
16 SEGMENT_TYPE => 0,
17 PURPOSE_CONSENT_ALLOWED => 3,
18 PURPOSE_LIT_ALLOWED => 27,
19 NUM_CUSTOM_PURPOSES => 51,
20 CUSTOM_PURPOSES_CONSENT => 57,
21 },
22 72 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub Parse {
25 9679 ▶ my ($klass, %args) = @_;
Subroutine Parse: called CC=8 SCAR=22.0
Pod undocumented
26  
27 9679 ▶ croak "missing 'data'" unless defined $args{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"data"} 0 9679 9679
28 9679 ▶ croak "missing 'data_size'" unless defined $args{data_size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"data_size"} 0 9679 9679
29  
30 9679 ▶ croak "missing 'options'" unless defined $args{options};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"} 0 9679 9679
31 9679 ▶ croak "missing 'options.json'" unless defined $args{options}->{json};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"}{"json"} 0 9679 9679
32  
33 9679 my $data = $args{data};
34 9679 my $data_size = $args{data_size}; # size in bits
35 9679 my $options = $args{options};
36  
37 9679 ▶ croak "invalid min size" if $data_size < 57;
Branch: 1 branch
BranchTrueFalseTotal
if $data_size < 57 0 9679 9679
38  
39 9679 my $segment_type = get_uint3($data, OFFSETS->{SEGMENT_TYPE});
40  
41 9679 ▶ croak "invalid segment type ${segment_type}: expected @{[ SEGMENT_TYPE_PUBLISHER_TC ]}"
Branch: 1 branch
BranchTrueFalseTotal
if $segment_type != 3 0 9679 9679
42 if $segment_type != SEGMENT_TYPE_PUBLISHER_TC;
43  
44 9679 my $num_custom_purposes = get_uint6($data, OFFSETS->{NUM_CUSTOM_PURPOSES});
45  
46 9679 my $total_expected_size = 2 * $num_custom_purposes + 57;
47  
48 9679 ▶ croak "invalid size" if $data_size < $total_expected_size;
Branch: 1 branch
BranchTrueFalseTotal
if $data_size < $total_expected_size 0 9679 9679
49  
50 my $self = {
51 data => $data,
52 options => $options,
53 num_custom_purposes => $num_custom_purposes,
54 9679 custom_purpose_lit_offset => OFFSETS->{CUSTOM_PURPOSES_CONSENT} + $num_custom_purposes,
55 };
56  
57 9679 bless $self, $klass;
58  
59 9679 return $self;
60 }
61  
62 sub num_custom_purposes {
63 2 ▶ my $self = shift;
Subroutine num_custom_purposes: called CC=1 SCAR=0.0
Pod documented
64  
65 2 return $self->{num_custom_purposes};
66 }
67  
68 sub is_purpose_consent_allowed {
69 48 ▶ my ($self, $id) = @_;
Subroutine is_purpose_consent_allowed: called CC=3 SCAR=13.3
Pod documented
70  
71 48 ▶ croak "invalid purpose id $id: must be between 1 and @{[ MAX_PURPOSE_ID ]}" if $id < 1 || $id > MAX_PURPOSE_ID;
Branch: 1 branch
BranchTrueFalseTotal
if $id < 1 or $id > 24 0 48 48
Condition33%1/3
$id < 1 or $id > 24
l!l&&r!l&&!r
0048
MC/DC0%0/2
$id < 1 or $id > 24
$id < 1 $id > 24
Truth table33%1/3
A or B
A= $id < 1B= $id > 24
ABresult
1X1
011
000
72  
73 48 return $self->_safe_is_purpose_consent_allowed($id);
74 }
75  
76 sub is_purpose_legitimate_interest_allowed {
77 48 ▶ my ($self, $id) = @_;
Subroutine is_purpose_legitimate_interest_allowed: called CC=3 SCAR=13.3
Pod documented
78  
79 48 ▶ croak "invalid purpose id $id: must be between 1 and @{[ MAX_PURPOSE_ID ]}" if $id < 1 || $id > MAX_PURPOSE_ID;
Branch: 1 branch
BranchTrueFalseTotal
if $id < 1 or $id > 24 0 48 48
Condition33%1/3
$id < 1 or $id > 24
l!l&&r!l&&!r
0048
MC/DC0%0/2
$id < 1 or $id > 24
$id < 1 $id > 24
Truth table33%1/3
A or B
A= $id < 1B= $id > 24
ABresult
1X1
011
000
80  
81 48 return $self->_safe_is_purpose_legitimate_interest_allowed($id);
82 }
83  
84 sub is_custom_purpose_consent_allowed {
85 2 ▶ my ($self, $id) = @_;
Subroutine is_custom_purpose_consent_allowed: called CC=3 SCAR=13.3
Pod documented
86  
87 0 croak "invalid custom purpose id $id: must be between 1 and @{[ $self->{num_custom_purposes} ]}"
88 2 ▶ if $id < 1 || $id > $self->{num_custom_purposes};
Branch: 1 branch
BranchTrueFalseTotal
if $id < 1 or $id > $self->{"num_custom_purposes"} 0 2 2
Condition33%1/3
$id < 1 or $id > $self->{"num_custom_purposes"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$id < 1 or $id > $self->{"num_custom_purposes"}
$id < 1 $id > $self->{"num_custom_purposes"}
Truth table33%1/3
A or B
A= $id < 1B= $id > $self->{"num_custom_purposes"}
ABresult
1X1
011
000
89  
90 2 return $self->_safe_is_custom_purpose_consent_allowed($id);
91 }
92  
93 sub is_custom_purpose_legitimate_interest_allowed {
94 2 ▶ my ($self, $id) = @_;
Subroutine is_custom_purpose_legitimate_interest_allowed: called CC=3 SCAR=13.3
Pod documented
95  
96 0 croak "invalid custom purpose id $id: must be between 1 and @{[ $self->{num_custom_purposes} ]}"
97 2 ▶ if $id < 1 || $id > $self->{num_custom_purposes};
Branch: 1 branch
BranchTrueFalseTotal
if $id < 1 or $id > $self->{"num_custom_purposes"} 0 2 2
Condition33%1/3
$id < 1 or $id > $self->{"num_custom_purposes"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$id < 1 or $id > $self->{"num_custom_purposes"}
$id < 1 $id > $self->{"num_custom_purposes"}
Truth table33%1/3
A or B
A= $id < 1B= $id > $self->{"num_custom_purposes"}
ABresult
1X1
011
000
98  
99 2 return $self->_safe_is_custom_purpose_legitimate_interest_allowed($id);
100 }
101  
102 sub TO_JSON {
103 882 ▶ my $self = shift;
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
104  
105 882 my %consents = map { $_ => $self->_safe_is_purpose_consent_allowed($_) } 1 .. MAX_PURPOSE_ID;
106 882 my %legitimate_interests = map { $_ => $self->_safe_is_purpose_legitimate_interest_allowed($_) } 1 .. MAX_PURPOSE_ID;
107 my %custom_purpose_consents
108 882 = map { $_ => $self->_safe_is_custom_purpose_consent_allowed($_) } 1 .. $self->{num_custom_purposes};
109 my %custom_purpose_legitimate_interests
110 882 = map { $_ => $self->_safe_is_custom_purpose_legitimate_interest_allowed($_) } 1 .. $self->{num_custom_purposes};
111  
112 return {
113 consents => $self->_format_json_subsection(\%consents, MAX_PURPOSE_ID),
114 legitimate_interests => $self->_format_json_subsection(\%legitimate_interests, MAX_PURPOSE_ID),
115 custom_purposes => {
116 consents => $self->_format_json_subsection(\%custom_purpose_consents, $self->{num_custom_purposes}),
117 legitimate_interests =>
118 882 $self->_format_json_subsection(\%custom_purpose_legitimate_interests, $self->{num_custom_purposes}),
119 },
120 };
121 }
122  
123 sub _format_json_subsection {
124 3528 ▶ my ($self, $data, $max) = @_;
Subroutine _format_json_subsection: called CC=3 SCAR=11.0
125  
126 3528 my ($false, $true) = @{$self->{options}->{json}->{boolean_values}};
127  
128 3528 ▶ if (!!$self->{options}->{json}->{compact}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"compact"}) 16 3512 3528
129 16 return [grep { $data->{$_} } 1 .. $max,];
130 }
131  
132 3512 my $verbose = !!$self->{options}->{json}->{verbose};
133  
134 3512 ▶ return $data if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 3512 3512
135  
136 3512 return {map { $_ => $true } grep { $data->{$_} } keys %{$data}};
137 }
138  
139 sub _safe_is_purpose_consent_allowed {
140 21216 ▶ my ($self, $id) = @_;
Subroutine _safe_is_purpose_consent_allowed: called CC=1 SCAR=0.0
141 21216 return scalar(is_set($self->{data}, OFFSETS->{PURPOSE_CONSENT_ALLOWED} + $id - 1));
142 }
143  
144 sub _safe_is_purpose_legitimate_interest_allowed {
145 21216 ▶ my ($self, $id) = @_;
Subroutine _safe_is_purpose_legitimate_interest_allowed: called CC=1 SCAR=0.0
146  
147 21216 return scalar(is_set($self->{data}, OFFSETS->{PURPOSE_LIT_ALLOWED} + $id - 1));
148 }
149  
150 sub _safe_is_custom_purpose_consent_allowed {
151 46 ▶ my ($self, $id) = @_;
Subroutine _safe_is_custom_purpose_consent_allowed: called CC=1 SCAR=0.0
152 46 return scalar(is_set($self->{data}, OFFSETS->{CUSTOM_PURPOSES_CONSENT} + $id - 1));
153 }
154  
155 sub _safe_is_custom_purpose_legitimate_interest_allowed {
156 46 ▶ my ($self, $id) = @_;
Subroutine _safe_is_custom_purpose_legitimate_interest_allowed: called CC=1 SCAR=0.0
157  
158 46 return scalar(is_set($self->{data}, $self->{custom_purpose_lit_offset} + $id - 1));
159 }
160  
161 72 1;
162 __END__
163  
164 =head1 NAME
165  
166 GDPR::IAB::TCFv2::PublisherTC - TCF v2.3 publisher TC section parser
167  
168 =head1 SYNOPSIS
169  
170 use GDPR::IAB::TCFv2::PublisherTC;
171 my $publisher_tc_data = '...';
172  
173 my $publisher_tc = GDPR::IAB::TCFv2::PublisherTC->Parse(
174 data => $publisher_tc_data,
175 data_size => length($publisher_tc_data) * 8,
176 options => { json => {} },
177 );
178  
179 my $num = $publisher_tc->num_custom_purposes;
180  
181 if ($publisher_tc->is_purpose_consent_allowed(1)) {
182 # ...
183 }
184  
185 =head1 CONSTRUCTOR
186  
187 Constructor C<Parse> receives a hash of 3 parameters:
188  
189 =over
190  
191 =item *
192  
193 Key C<data> is the binary data
194  
195 =item *
196  
197 Key C<data_size> is the original binary data size
198  
199 =item *
200  
201 Key C<options> is the L<GDPR::IAB::TCFv2> options (includes the C<json> field to modify the L</TO_JSON> method output.
202  
203 =back
204  
205 =head1 METHODS
206  
207 =head2 num_custom_purposes
208  
209 Custom purpose IDs are numbered 1 to NumberCustomPurposes. Custom purposes will be defined by the publisher and displayed to a user in a CMP user interface.
210  
211 If the publisher does not use any Custom Purposes, this method returns 0.
212  
213 =head2 is_purpose_consent_allowed
214  
215 The user's consent value for each Purpose established on the legal basis of consent, for the publisher.
216  
217 =head2 is_purpose_legitimate_interest_allowed
218  
219 The Purposes transparency requirements are met for each Purpose established on the legal basis of legitimate interest and the user has not exercised their "Right to Object" to that Purpose.
220  
221 By default or if the user has exercised their "Right to Object to a Purpose", the corresponding bit for that purpose is set to 0
222  
223 =head2 is_custom_purpose_consent_allowed
224  
225 The consent value for each custom purpose id
226  
227 =head2 is_custom_purpose_legitimate_interest_allowed
228  
229 The legitimate Interest disclosure establishment value for each custom purpose id
230  
231 =head2 TO_JSON
232  
233 Returns a hashref describing the publisher's purpose decisions:
234  
235 {
236 consents => ...,
237 legitimate_interests => ...,
238 custom_purposes => {
239 consents => ...,
240 legitimate_interests => ...,
241 },
242 }
243  
244 Publisher restrictions are emitted by L<GDPR::IAB::TCFv2::PublisherRestrictions/TO_JSON>;
245 the L<GDPR::IAB::TCFv2::Publisher/TO_JSON> wrapper combines the two views into a
246 single hashref.
247  
248 Example, by parsing the consent C<COwAdDhOwAdDhN4ABAENAPCgAAQAAv___wAAAFP_AAp_4AI6ACACAA.argAC0gAAAAAAAAAAAA> we can generate this compact hashref.
249  
250 {
251 "consents" : [
252 2,
253 4,
254 6,
255 8,
256 9,
257 10
258 ],
259 "legitimate_interests" : [
260 2,
261 4,
262 5,
263 7,
264 10
265 ],
266 "custom_purposes" : {
267 "consents" : [],
268 "legitimate_interests" : []
269 }
270 }
« lib/GDPR/IAB/TCFv2/CMPValidator.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/Publisher.pm »