| 1 | package GDPR::IAB::TCFv2::PublisherRestrictions 0.512; | ||||||||||||||||||||
| 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< | |||||||||||||||||||
| 8 | get_uint2 | ||||||||||||||||||||
| 9 | get_uint6 | ||||||||||||||||||||
| 10 | get_uint12 | ||||||||||||||||||||
| 11 | 72 | ▶ | >; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 72 | ▶ | use constant ASSUMED_MAX_VENDOR_ID => 0x7FFF; # 32767 or (1 << 15) -1 | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | sub Parse { | ||||||||||||||||||||
| 17 | 11417 | ▶ | my ($klass, %args) = @_; | ||||||||||||||||||
|
Subroutine
Parse: called CC=9 SCAR=22.3
Pod
undocumented
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 11417 | ▶ | croak "missing 'data'" unless defined $args{data}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 20 | 11417 | ▶ | croak "missing 'data_size'" unless defined $args{data_size}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 11417 | ▶ | croak "missing 'offset'" unless defined $args{offset}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 11417 | ▶ | croak "missing 'options'" unless defined $args{options}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | 11417 | ▶ | croak "missing 'options.json'" unless defined $args{options}->{json}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 11417 | my $data = $args{data}; | |||||||||||||||||||
| 27 | 11417 | my $data_size = $args{data_size}; | |||||||||||||||||||
| 28 | 11417 | my $offset = $args{offset}; | |||||||||||||||||||
| 29 | 11417 | my $max_id = ASSUMED_MAX_VENDOR_ID; | |||||||||||||||||||
| 30 | 11417 | my $options = $args{options}; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 11417 | ▶ | return if $offset + 12 > $data_size; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 11417 | my ($num_restrictions, $next_offset) = get_uint12($data, $offset); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 11417 | my %restrictions; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 11417 | for (1 .. $num_restrictions) { | |||||||||||||||||||
| 39 | 23996 | my ($purpose_id, $restriction_type, $vendor_restrictions); | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 23996 | ($purpose_id, $next_offset) = get_uint6($data, $next_offset); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 23996 | ($restriction_type, $next_offset) = get_uint2($data, $next_offset); | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 23996 | ($vendor_restrictions, $next_offset) = GDPR::IAB::TCFv2::RangeSection->Parse( | |||||||||||||||||||
| 46 | data => $data, | ||||||||||||||||||||
| 47 | data_size => $data_size, | ||||||||||||||||||||
| 48 | offset => $next_offset, | ||||||||||||||||||||
| 49 | max_id => ASSUMED_MAX_VENDOR_ID, | ||||||||||||||||||||
| 50 | options => $options, | ||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 23996 | ▶ | $restrictions{$purpose_id} ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$restrictions{$purpose_id} ||= {}
MC/DC100%1/1
$restrictions{$purpose_id} ||= {}
$restrictions{$purpose_id}
Truth table100%2/2
A
A= $restrictions{$purpose_id}
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 23996 | $restrictions{$purpose_id}->{$restriction_type} = $vendor_restrictions; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 11417 | my $self = {restrictions => \%restrictions,}; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 11417 | bless $self, $klass; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 11417 | return $self; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | sub has_restrictions { | ||||||||||||||||||||
| 66 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
has_restrictions: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 67 | 2 | ▶ | return scalar keys %{$self->{restrictions}} ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | sub restrictions { | ||||||||||||||||||||
| 71 | 8 | ▶ | my ($self, $vendor_id) = @_; | ||||||||||||||||||
|
Subroutine
restrictions: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 8 | my %restrictions; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 8 | foreach my $purpose_id (keys %{$self->{restrictions}}) { | |||||||||||||||||||
| 76 | 18 | foreach my $restriction_type (keys %{$self->{restrictions}->{$purpose_id}}) { | |||||||||||||||||||
| 77 | 16 | ▶ | if ($self->{restrictions}->{$purpose_id}->{$restriction_type}->contains($vendor_id)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 8 | ▶ | $restrictions{$purpose_id} ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$restrictions{$purpose_id} ||= {}
MC/DC100%1/1
$restrictions{$purpose_id} ||= {}
$restrictions{$purpose_id}
Truth table100%2/2
A
A= $restrictions{$purpose_id}
| |||||||||||||||||||||
| 79 | 8 | $restrictions{$purpose_id}->{$restriction_type} = 1; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 8 | return \%restrictions; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | sub check_restriction { | ||||||||||||||||||||
| 88 | 21522 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
check_restriction: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 21522 | my $nargs = scalar(@_); | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 21522 | ▶ | croak "missing arguments: purpose id, restriction type and vendor id" if $nargs == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 21522 | ▶ | croak "missing arguments: restriction type and vendor id" if $nargs == 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 21522 | ▶ | croak "missing argument: vendor id" if $nargs == 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 21522 | my ($purpose_id, $restriction_type, $vendor_id) = @_; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 21522 | ▶ | return 0 unless exists $self->{restrictions}->{$purpose_id}->{$restriction_type}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 1244 | return $self->{restrictions}->{$purpose_id}->{$restriction_type}->contains($vendor_id); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | sub TO_JSON { | ||||||||||||||||||||
| 104 | 1064 | ▶ | my ($self, $filter_id) = @_; | ||||||||||||||||||
|
Subroutine
TO_JSON: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 1064 | my %publisher_restrictions; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 1064 | foreach my $purpose_id (sort { $a <=> $b } keys %{$self->{restrictions}}) { | |||||||||||||||||||
| 109 | 2209 | my $restriction_map = $self->{restrictions}->{$purpose_id}; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 2209 | my %purpose_restrictions; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 2209 | foreach my $restriction_type (keys %{$restriction_map}) { | |||||||||||||||||||
| 114 | 2182 | ▶ | if (defined $filter_id) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 4 | ▶ | if ($restriction_map->{$restriction_type}->contains($filter_id)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 1 | $purpose_restrictions{$filter_id} = int($restriction_type); | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||
| 120 | 2178 | my $vendors = $restriction_map->{$restriction_type}->all; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 2178 | foreach my $vendor (@{$vendors}) { | |||||||||||||||||||
| 123 | 2239882 | $purpose_restrictions{$vendor} = int($restriction_type); | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # If not filtering, or if we found the filtered vendor, add the purpose | ||||||||||||||||||||
| 129 | 2209 | ▶ | if (!defined $filter_id || scalar keys %purpose_restrictions) { | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
not defined $filter_id or scalar keys %purpose_restrictions
MC/DC100%1/1
not defined $filter_id or scalar keys %purpose_restrictions
not defined $filter_id
Truth table100%2/2
not(A)
A= not defined $filter_id
| |||||||||||||||||||||
| 130 | 2022 | $publisher_restrictions{$purpose_id} = \%purpose_restrictions; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 1064 | return \%publisher_restrictions; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 72 | 1; | |||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =head1 NAME | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | GDPR::IAB::TCFv2::PublisherRestrictions - TCF v2.3 publisher restrictions parser | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head1 SYNOPSIS | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | use GDPR::IAB::TCFv2::PublisherRestrictions; | ||||||||||||||||||||
| 148 | my $data = '...'; # raw binary data | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | my $publisher_restrictions = GDPR::IAB::TCFv2::PublisherRestrictions->Parse( | ||||||||||||||||||||
| 151 | data => $data, | ||||||||||||||||||||
| 152 | data_size => length($data), | ||||||||||||||||||||
| 153 | offset => 213, | ||||||||||||||||||||
| 154 | options => { json => {} }, | ||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | if ($publisher_restrictions->check_restriction(1, 0, 284)) { | ||||||||||||||||||||
| 158 | # ... | ||||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head1 CONSTRUCTOR | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | Constructor C<Parse> receives a hash of 4 parameters: | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =over | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =item * | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | Key C<data> is the binary core data | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =item * | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Key C<data_size> is the core data size in bits | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =item * | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | Key C<offset> is the bit offset | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =item * | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | Key C<options> is the L<GDPR::IAB::TCFv2> options (includes the C<json> field to modify the L</TO_JSON> method output. | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | =back | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head1 METHODS | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head2 check_restriction | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | Return true for a given combination of purpose id, restriction type and vendor | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | my $purpose_id = 1; | ||||||||||||||||||||
| 193 | my $restriction_type = 0; | ||||||||||||||||||||
| 194 | my $vendor_id = 284; | ||||||||||||||||||||
| 195 | my $ok = $object->check_restriction($purpose_id, $restriction_type, $vendor_id); | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =head2 restrictions | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | Return a hashref of purpose => { restriction type => bool } for a given vendor id. | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | Example, by parsing the consent C<COwAdDhOwAdDhN4ABAENAPCgAAQAAv___wAAAFP_AAp_4AI6ACACAA> we can generate this. | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | my $restrictions = $object->restrictions(32); | ||||||||||||||||||||
| 204 | # returns { 7 => { 1 => 1 } } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | =head2 TO_JSON | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | Returns a hashref keyed by purpose id; each inner hashref maps vendor id | ||||||||||||||||||||
| 209 | to the integer C<restriction_type> that vendor was given for that purpose: | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | { | ||||||||||||||||||||
| 212 | '[purpose id]' => { | ||||||||||||||||||||
| 213 | # value is the restriction type: | ||||||||||||||||||||
| 214 | # 0 - Not Allowed | ||||||||||||||||||||
| 215 | # 1 - Require Consent | ||||||||||||||||||||
| 216 | # 2 - Require Legitimate Interest | ||||||||||||||||||||
| 217 | '[vendor id]' => '[restriction type]', | ||||||||||||||||||||
| 218 | }, | ||||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | Example, by parsing the consent C<COwAdDhOwAdDhN4ABAENAPCgAAQAAv___wAAAFP_AAp_4AI6ACACAA> we can generate this hashref (vendor 32 has restriction type 1 -- Require Consent -- for purpose 7): | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | { | ||||||||||||||||||||
| 224 | "7" => { | ||||||||||||||||||||
| 225 | "32" => 1 | ||||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | } | ||||||||||||||||||||