-
Notifications
You must be signed in to change notification settings - Fork 1
/
consentv1.go
337 lines (291 loc) · 7.25 KB
/
consentv1.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package consent
import (
"encoding/base64"
"errors"
"strings"
"time"
)
type encodingType byte
const (
bitFieldType encodingType = 0
rangeType encodingType = 1
)
var (
// ErrUnexpectedEnd is returned when a consent string is too short
ErrUnexpectedEnd = errors.New("consent: unexpected end")
// ErrUnsupported is returned when a string version is not 1
ErrUnsupported = errors.New("consent: version is not supported")
)
// Consent is a golang representation of an IAB consent string
//
// Implementation is done as per IAB Consent String format v1.1:
// https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/68f5e0012a7bdb00867ce9fee57fb67cfe9153e3/Consent%20string%20and%20vendor%20list%20formats%20v1.1%20Final.md
type ConsentV1 struct {
Created time.Time
LastUpdated time.Time
CmpID int
CmpVersion int
ConsentScreen byte
ConsentLanguage string
VendorListVersion int
PurposesAllowed [24]bool
MaxVendorID int
encodingType encodingType
Vendors map[int]bool
validateOnlyMode bool
}
func (c *ConsentV1) Version() byte {
return 1
}
// New creates a new instance of a Consent struct
func NewConsentV1(cmpID, cmpVersion int, consentScreen byte, lang string, vendorListVerson int, purposesAllowed [24]bool, allowedVendors map[int]bool) ConsentV1 {
vendors := make(map[int]bool)
for k, v := range allowedVendors {
vendors[k] = v
}
return ConsentV1{
Created: time.Now(),
LastUpdated: time.Now(),
CmpID: cmpID,
CmpVersion: cmpVersion,
ConsentScreen: consentScreen,
ConsentLanguage: lang,
VendorListVersion: vendorListVerson,
PurposesAllowed: purposesAllowed,
Vendors: vendors,
}
}
// Bytes returns raw, i.e. not base64 encoded, consent string bytes
func (c *ConsentV1) Bytes() []byte {
var b bitWriter
b.AppendByte(c.Version(), 6)
b.AppendInt(c.Created.UnixNano()/int64(time.Second/10), 36)
b.AppendInt(c.LastUpdated.UnixNano()/int64(time.Second/10), 36)
b.AppendInt(int64(c.CmpID), 12)
b.AppendInt(int64(c.CmpVersion), 12)
b.AppendByte(c.ConsentScreen, 6)
lang := []byte(strings.ToUpper(c.ConsentLanguage))
if len(lang) == 2 {
b.AppendByte(lang[0]-byte('A'), 6)
b.AppendByte(lang[1]-byte('A'), 6)
} else {
b.AppendByte(byte('X')-byte('A'), 6)
b.AppendByte(byte('X')-byte('A'), 6)
}
b.AppendInt(int64(c.VendorListVersion), 12)
b.AppendBools(c.PurposesAllowed[:])
b.AppendInt(int64(c.MaxVendorID), 16)
switch ecType, defConsent, rngCount := c.findSmallest(); ecType {
case bitFieldType:
b.AppendByte(byte(bitFieldType), 1) // encoding type
for i := 1; i <= c.MaxVendorID; i++ {
var v byte
if c.Vendors[i] {
v = 1
}
b.AppendByte(v, 1)
}
case rangeType:
b.AppendByte(byte(rangeType), 1) // encoding type
if defConsent {
b.AppendByte(1, 1)
} else {
b.AppendByte(0, 1)
}
b.AppendInt(rngCount, 12)
for i := 1; i <= c.MaxVendorID; i++ {
start := i
for ; start <= c.MaxVendorID && c.Vendors[start] == defConsent; start++ {
}
end := start
for ; end <= c.MaxVendorID && c.Vendors[end] != defConsent; end++ {
}
if end == start {
break
} else if end-start == 1 {
b.AppendByte(0, 1)
b.AppendInt(int64(start), 16)
} else {
b.AppendByte(1, 1)
b.AppendInt(int64(start), 16)
b.AppendInt(int64(end-1), 16)
}
i = end
}
}
return b.Bytes()
}
func (c *ConsentV1) findSmallest() (encodingType, bool, int64) {
var bfScore, rtScore, rfScore int
// bitfield, range with default consent == true, and range with false
var rtRecords, rfRecords int64
bfScore = c.MaxVendorID
rtScore = 1 + 12
rfScore = 1 + 12
for i := 1; i <= c.MaxVendorID; i++ {
cur := c.Vendors[i]
start := i
for ; i <= c.MaxVendorID && c.Vendors[i] == cur; i++ {
}
end := i
i--
size := 1
if end-start == 1 {
size += 16
} else {
size += 16 + 16
}
if cur {
rfScore += size
rfRecords++
} else {
rtScore += size
rtRecords++
}
}
min := bfScore
if min > rtScore {
min = rtScore
}
if min > rfScore {
min = rfScore
}
if min == rfScore {
return rangeType, false, rfRecords
} else if min == rtScore {
return rangeType, true, rtRecords
} else {
return bitFieldType, false, 0
}
}
func (c *ConsentV1) outputRange(defConsent bool) (int64, bitWriter) {
var b bitWriter
var count int64
return count, b
}
// String return a base64-encoded consent string
func (c *ConsentV1) String() string {
return base64.RawURLEncoding.EncodeToString(c.Bytes())
}
// ParseRaw converts a raw, i.e. non base64-encoded, consent string into the
// Consent struct
func (c *ConsentV1) ParseRaw(binary []byte) error {
if len(binary) < 21 {
return ErrUnexpectedEnd
}
b := newBitReader(binary)
version, _ := b.ReadByte(6)
if version != 1 {
return ErrUnsupported
}
dt, _ := b.ReadInt(36)
c.Created = time.Unix(dt/10, dt%10*100*1000*1000)
dt, _ = b.ReadInt(36)
c.LastUpdated = time.Unix(dt/10, dt%10*100*1000*1000)
dt, _ = b.ReadInt(12)
c.CmpID = int(dt)
dt, _ = b.ReadInt(12)
c.CmpVersion = int(dt)
c.ConsentScreen, _ = b.ReadByte(6)
l1, _ := b.ReadByte(6)
l2, _ := b.ReadByte(6)
c.ConsentLanguage = string([]byte{l1 + byte('A'), l2 + byte('A')})
dt, _ = b.ReadInt(12)
c.VendorListVersion = int(dt)
for i := range c.PurposesAllowed {
by, _ := b.ReadByte(1)
v := false
if by == 1 {
v = true
}
c.PurposesAllowed[i] = v
}
dt, _ = b.ReadInt(16)
c.MaxVendorID = int(dt)
by, _ := b.ReadByte(1)
c.encodingType = encodingType(by)
c.Vendors = make(map[int]bool)
switch c.encodingType {
case bitFieldType:
for i := 1; i <= c.MaxVendorID; i++ {
if by, ok := b.ReadByte(1); !ok {
return ErrUnexpectedEnd
} else if by == 1 {
if !c.validateOnlyMode {
c.Vendors[i] = true
}
}
}
case rangeType:
by, ok := b.ReadByte(1)
if !ok {
return ErrUnexpectedEnd
}
defCons := false
if by == 1 {
defCons = true
for i := 1; i <= int(c.MaxVendorID); i++ {
if !c.validateOnlyMode {
c.Vendors[i] = true
}
}
}
numEntries, ok := b.ReadInt(12)
if !ok {
return ErrUnexpectedEnd
}
for i := 0; i < int(numEntries); i++ {
singleOrRange, ok := b.ReadByte(1)
if !ok {
return ErrUnexpectedEnd
}
if singleOrRange == 0 { // Single
dt, ok = b.ReadInt(16)
if !ok {
return ErrUnexpectedEnd
}
c.Vendors[int(dt)] = !defCons
} else { // Range
start, ok := b.ReadInt(16)
if !ok {
return ErrUnexpectedEnd
}
end, ok := b.ReadInt(16)
if !ok {
return ErrUnexpectedEnd
}
for j := start; j <= end; j++ {
if defCons {
delete(c.Vendors, int(j))
} else {
if !c.validateOnlyMode {
c.Vendors[int(j)] = true
}
}
}
}
}
}
return nil
}
// Parse parses base64 encoded consent
func (c *ConsentV1) Parse(data string) error {
bytes := []byte(data)
bin := make([]byte, base64.RawStdEncoding.DecodedLen(len(bytes)))
_, err := base64.RawURLEncoding.Decode(bin, bytes)
if err != nil {
return err
}
return c.ParseRaw(bin)
}
// Parse parses base64 encoded consent
func ParseV1(data string) (*ConsentV1, error) {
c := new(ConsentV1)
return c, c.Parse(data)
}
// Validate validates base64 encoded consent string
func ValidateV1(data string) error {
c := new(ConsentV1)
c.validateOnlyMode = true
return c.Parse(data)
}