-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_v1.go
121 lines (105 loc) · 3.74 KB
/
parser_v1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package iab_tcf
import (
"slices"
"github.com/LiveRamp/iabconsent"
"github.com/hybridtheory/iab-tcf/cmp"
)
// ConsentV1 is an implementation of the Consent interface used to retrieve
// consent information given a TCF 1.0 format.
type ConsentV1 struct {
*cmp.Consent
ParsedConsent *iabconsent.ParsedConsent
}
// NewConsentV1 returns a consent interface from the reader received, with
// an error if something went wrong.
func NewConsentV1(reader *iabconsent.ConsentReader) (Consent, error) {
parsedConsent, err := ParseV1(reader)
if err != nil {
return nil, err
}
return &ConsentV1{
ParsedConsent: parsedConsent,
}, nil
}
// Version returns the version of this consent string.
func (c *ConsentV1) Version() int {
return int(iabconsent.V1)
}
// CMPID returns the CMP ID of this consent string.
func (c *ConsentV1) CMPID() int {
return c.ParsedConsent.CMPID
}
// IsCMPValid validates the consent string CMP ID agains the list of valid ones downloaded from IAB.
func (c *ConsentV1) IsCMPValid() bool {
return slices.Contains(c.ValidCMPs(), c.CMPID())
}
// HasConsentedPurpose returns always true because consent TFC 1.0 doesn't
// come with this information.
func (c *ConsentV1) HasConsentedPurpose(purposeID int) bool {
return c.ParsedConsent.PurposesAllowed[purposeID]
}
// GetConsentPurposeBitstring returns a string of 1 & 0 each of them representing the consent
// given for a specific purposeID.
func (c *ConsentV1) GetConsentPurposeBitstring() string {
bitString := ""
for i := 1; i <= 24; i++ {
bitString += booleanFormatter[c.HasConsentedPurpose(i)]
}
return bitString
}
// HasConsentedLegitimateInterestForPurpose returns always true because consent TFC 1.0 doesn't
// come with this information.
func (c *ConsentV1) HasConsentedLegitimateInterestForPurpose(purposeID int) bool {
return true
}
// HasUserConsented returns true if the user has given consent to the vendorID passed
// as parameter.
func (c *ConsentV1) HasUserConsented(vendorID int) bool {
return c.ParsedConsent.VendorAllowed(vendorID)
}
// HasUserLegitimateInterest returns always true because consent TFC 1.0 doesn't
// come with this information.
func (c *ConsentV1) HasUserLegitimateInterest(vendorID int) bool {
return true
}
// GetConsentBitstring returns a string of 1 & 0 each of them representing the consent
// given for a specific vendorID (the first number is for the vendorID 1, and so on).
func (c *ConsentV1) GetConsentBitstring() string {
bitString := ""
for i := 1; i <= c.ParsedConsent.MaxVendorID; i++ {
bitString += booleanFormatter[c.HasUserConsented(i)]
}
return bitString
}
// GetInterestsBitstring returns an empty string always because consent TFC 1.0 doesn't
// implement user legitimate interests.
func (c *ConsentV1) GetInterestsBitstring() string {
return ""
}
// GetPublisherRestrictions returns an empty list because consent TFC 1.0 doesn't
// implement user legitimate interests.
func (c *ConsentV1) GetPublisherRestrictions() []*iabconsent.PubRestrictionEntry {
return make([]*iabconsent.PubRestrictionEntry, 0, 0)
}
// ParseV1 uses a consent reader to extract information from a TCF 1.0 version
// consent string.
func ParseV1(r *iabconsent.ConsentReader) (*iabconsent.ParsedConsent, error) {
var p = &iabconsent.ParsedConsent{}
p.Version = int(iabconsent.V1)
r.ReadString(12)
p.CMPID, _ = r.ReadInt(12)
r.ReadString(3)
p.ConsentLanguage, _ = r.ReadString(2)
p.VendorListVersion, _ = r.ReadInt(12)
p.PurposesAllowed, _ = r.ReadBitField(24)
p.MaxVendorID, _ = r.ReadInt(16)
p.IsRangeEncoding, _ = r.ReadBool()
if p.IsRangeEncoding {
p.DefaultConsent, _ = r.ReadBool()
p.NumEntries, _ = r.ReadInt(12)
p.RangeEntries, _ = r.ReadRangeEntries(uint(p.NumEntries))
} else {
p.ConsentedVendors, _ = r.ReadBitField(uint(p.MaxVendorID))
}
return p, r.Err
}