forked from LiveRamp/iabconsent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ccpa_parsed_consent.go
57 lines (45 loc) · 1.39 KB
/
ccpa_parsed_consent.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
package iabconsent
import "github.com/pkg/errors"
const (
CCPAYes = 'Y'
CCPANo = 'N'
CCPANotApplicable = '-'
CCPAVersion = 1
)
// CcpaParsedConsent represents data extract from a California Consumer Privacy Act (CCPA) consent string.
// Format can be found here: https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/master/CCPA/US%20Privacy%20String.md
type CcpaParsedConsent struct {
// The version of this string specification used to encode the string
Version int
// N = No, Y = Yes, - = Not Applicable
Notice uint8
// N = No, Y = Yes, - = Not Applicable; For use ONLY when CCPA does not apply.
OptOutSale uint8
// 0 = No, 1 = Yes, - = Not Applicable
LSPACoveredTransaction uint8
}
func IsValidCCPAString(ccpaString string) (bool, error) {
if len(ccpaString) != 4 {
return false, errors.New("invalid uspv consent string length")
}
if ccpaString[0]-'0' != CCPAVersion {
return false, errors.New("invalid uspv consent string version")
}
for i := 1; i < 4; i++ {
if ccpaString[i] != CCPAYes && ccpaString[i] != CCPANo && ccpaString[i] != CCPANotApplicable {
return false, errors.New("invalid uspv consent string")
}
}
return true, nil
}
func ParseCCPA(s string) (*CcpaParsedConsent, error) {
if valid, err := IsValidCCPAString(s); !valid {
return nil, err
}
return &CcpaParsedConsent{
int(s[0] - '0'),
s[1],
s[2],
s[3],
}, nil
}