Skip to content

Commit

Permalink
Fixes for PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhjort committed Mar 25, 2020
1 parent 421203b commit 84e1d03
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
11 changes: 8 additions & 3 deletions gdpr/gdpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type Permissions interface {
PersonalInfoAllowed(ctx context.Context, bidder openrtb_ext.BidderName, PublisherID string, consent string) (bool, error)
}

const (
tCF1 uint8 = 1
tCF2 uint8 = 2
)

// NewPermissions gets an instance of the Permissions for use elsewhere in the project.
func NewPermissions(ctx context.Context, cfg config.GDPR, vendorIDs map[openrtb_ext.BidderName]uint16, client *http.Client) Permissions {
// If the host doesn't buy into the IAB GDPR consent framework, then save some cycles and let all syncs happen.
Expand All @@ -36,9 +41,9 @@ func NewPermissions(ctx context.Context, cfg config.GDPR, vendorIDs map[openrtb_
return &permissionsImpl{
cfg: cfg,
vendorIDs: vendorIDs,
fetchVendorList: []func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
newVendorListFetcher(ctx, cfg, client, vendorListURLMaker, 1),
newVendorListFetcher(ctx, cfg, client, vendorListURLMaker, 2)},
fetchVendorList: map[uint8]func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
tCF1: newVendorListFetcher(ctx, cfg, client, vendorListURLMaker, tCF1),
tCF2: newVendorListFetcher(ctx, cfg, client, vendorListURLMaker, tCF2)},
}
}

Expand Down
4 changes: 2 additions & 2 deletions gdpr/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
type permissionsImpl struct {
cfg config.GDPR
vendorIDs map[openrtb_ext.BidderName]uint16
fetchVendorList []func(ctx context.Context, id uint16) (vendorlist.VendorList, error)
fetchVendorList map[uint8]func(ctx context.Context, id uint16) (vendorlist.VendorList, error)
}

func (p *permissionsImpl) HostCookiesAllowed(ctx context.Context, consent string) (bool, error) {
Expand Down Expand Up @@ -122,7 +122,7 @@ func (p *permissionsImpl) parseVendor(ctx context.Context, vendorID uint16, cons
if version < 1 || version > 2 {
return
}
vendorList, err := p.fetchVendorList[version-1](ctx, parsedConsent.VendorListVersion())
vendorList, err := p.fetchVendorList[version](ctx, parsedConsent.VendorListVersion())
if err != nil {
return
}
Expand Down
42 changes: 21 additions & 21 deletions gdpr/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func TestNoConsentButAllowByDefault(t *testing.T) {
UsersyncIfAmbiguous: true,
},
vendorIDs: nil,
fetchVendorList: []func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
failedListFetcher,
failedListFetcher,
fetchVendorList: map[uint8]func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
tCF1: failedListFetcher,
tCF2: failedListFetcher,
},
}
allowSync, err := perms.BidderSyncAllowed(context.Background(), openrtb_ext.BidderAppnexus, "")
Expand All @@ -39,9 +39,9 @@ func TestNoConsentAndRejectByDefault(t *testing.T) {
UsersyncIfAmbiguous: false,
},
vendorIDs: nil,
fetchVendorList: []func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
failedListFetcher,
failedListFetcher,
fetchVendorList: map[uint8]func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
tCF1: failedListFetcher,
tCF2: failedListFetcher,
},
}
allowSync, err := perms.BidderSyncAllowed(context.Background(), openrtb_ext.BidderAppnexus, "")
Expand Down Expand Up @@ -69,11 +69,11 @@ func TestAllowedSyncs(t *testing.T) {
openrtb_ext.BidderAppnexus: 2,
openrtb_ext.BidderPubmatic: 3,
},
fetchVendorList: []func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
listFetcher(map[uint16]vendorlist.VendorList{
fetchVendorList: map[uint8]func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
tCF1: listFetcher(map[uint16]vendorlist.VendorList{
1: parseVendorListData(t, vendorListData),
}),
listFetcher(map[uint16]vendorlist.VendorList{
tCF2: listFetcher(map[uint16]vendorlist.VendorList{
1: parseVendorListData(t, vendorListData),
}),
},
Expand Down Expand Up @@ -105,11 +105,11 @@ func TestProhibitedPurposes(t *testing.T) {
openrtb_ext.BidderAppnexus: 2,
openrtb_ext.BidderPubmatic: 3,
},
fetchVendorList: []func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
listFetcher(map[uint16]vendorlist.VendorList{
fetchVendorList: map[uint8]func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
tCF1: listFetcher(map[uint16]vendorlist.VendorList{
1: parseVendorListData(t, vendorListData),
}),
listFetcher(map[uint16]vendorlist.VendorList{
tCF2: listFetcher(map[uint16]vendorlist.VendorList{
1: parseVendorListData(t, vendorListData),
}),
},
Expand Down Expand Up @@ -141,11 +141,11 @@ func TestProhibitedVendors(t *testing.T) {
openrtb_ext.BidderAppnexus: 2,
openrtb_ext.BidderPubmatic: 3,
},
fetchVendorList: []func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
listFetcher(map[uint16]vendorlist.VendorList{
fetchVendorList: map[uint8]func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
tCF1: listFetcher(map[uint16]vendorlist.VendorList{
1: parseVendorListData(t, vendorListData),
}),
listFetcher(map[uint16]vendorlist.VendorList{
tCF2: listFetcher(map[uint16]vendorlist.VendorList{
1: parseVendorListData(t, vendorListData),
}),
},
Expand All @@ -165,9 +165,9 @@ func TestMalformedConsent(t *testing.T) {
cfg: config.GDPR{
HostVendorID: 2,
},
fetchVendorList: []func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
listFetcher(nil),
listFetcher(nil),
fetchVendorList: map[uint8]func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
tCF1: listFetcher(nil),
tCF2: listFetcher(nil),
},
}

Expand All @@ -193,11 +193,11 @@ func TestAllowPersonalInfo(t *testing.T) {
openrtb_ext.BidderAppnexus: 2,
openrtb_ext.BidderPubmatic: 3,
},
fetchVendorList: []func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
listFetcher(map[uint16]vendorlist.VendorList{
fetchVendorList: map[uint8]func(ctx context.Context, id uint16) (vendorlist.VendorList, error){
tCF1: listFetcher(map[uint16]vendorlist.VendorList{
1: parseVendorListData(t, vendorListData),
}),
listFetcher(map[uint16]vendorlist.VendorList{
tCF2: listFetcher(map[uint16]vendorlist.VendorList{
1: parseVendorListData(t, vendorListData),
}),
},
Expand Down
16 changes: 8 additions & 8 deletions gdpr/vendorlist-fetching.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type saveVendors func(uint16, api.VendorList)
//
// Nothing in this file is exported. Public APIs can be found in gdpr.go

func newVendorListFetcher(initCtx context.Context, cfg config.GDPR, client *http.Client, urlMaker func(uint16, int) string, TCFVer int) func(ctx context.Context, id uint16) (vendorlist.VendorList, error) {
func newVendorListFetcher(initCtx context.Context, cfg config.GDPR, client *http.Client, urlMaker func(uint16, uint8) string, TCFVer uint8) func(ctx context.Context, id uint16) (vendorlist.VendorList, error) {
// These save and load functions can be used to store & retrieve lists from our cache.
save, load := newVendorListCache()

Expand All @@ -51,18 +51,18 @@ func newVendorListFetcher(initCtx context.Context, cfg config.GDPR, client *http
}

// populateCache saves all the known versions of the vendor list for future use.
func populateCache(ctx context.Context, client *http.Client, urlMaker func(uint16, int) string, saver saveVendors, TCFVer int) {
latestVersion := saveOne(ctx, client, urlMaker(0, 1), saver, TCFVer)
func populateCache(ctx context.Context, client *http.Client, urlMaker func(uint16, uint8) string, saver saveVendors, TCFVer uint8) {
latestVersion := saveOne(ctx, client, urlMaker(0, TCFVer), saver, TCFVer)

for i := uint16(1); i < latestVersion; i++ {
saveOne(ctx, client, urlMaker(i, 1), saver, TCFVer)
saveOne(ctx, client, urlMaker(i, TCFVer), saver, TCFVer)
}
}

// Make a URL which can be used to fetch a given version of the Global Vendor List. If the version is 0,
// this will fetch the latest version.
func vendorListURLMaker(version uint16, tCFVersion int) string {
if tCFVersion == 2 {
func vendorListURLMaker(version uint16, TCFVer uint8) string {
if TCFVer == 2 {
if version == 0 {
return "https://vendorlist.consensu.org/v2/vendor-list.json"
}
Expand All @@ -79,7 +79,7 @@ func vendorListURLMaker(version uint16, tCFVersion int) string {
// The goal here is to update quickly when new versions of the VendorList are released, but not wreck
// server performance if a bad CMP starts sending us malformed consent strings that advertize a version
// that doesn't exist yet.
func newOccasionalSaver(timeout time.Duration, TCFVer int) func(ctx context.Context, client *http.Client, url string, saver saveVendors) {
func newOccasionalSaver(timeout time.Duration, TCFVer uint8) func(ctx context.Context, client *http.Client, url string, saver saveVendors) {
lastSaved := &atomic.Value{}
lastSaved.Store(time.Time{})

Expand All @@ -94,7 +94,7 @@ func newOccasionalSaver(timeout time.Duration, TCFVer int) func(ctx context.Cont
}
}

func saveOne(ctx context.Context, client *http.Client, url string, saver saveVendors, cTFVer int) uint16 {
func saveOne(ctx context.Context, client *http.Client, url string, saver saveVendors, cTFVer uint8) uint16 {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
glog.Errorf("Failed to build GET %s request. Cookie syncs may be affected: %v", url, err)
Expand Down
4 changes: 2 additions & 2 deletions gdpr/vendorlist-fetching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ func mockVendorListData(t *testing.T, version uint16, vendors map[uint16]*purpos
return string(data)
}

func testURLMaker(server *httptest.Server) func(uint16, int) string {
func testURLMaker(server *httptest.Server) func(uint16, uint8) string {
url := server.URL
return func(version uint16, TCFVer int) string {
return func(version uint16, TCFVer uint8) string {
return url + "?version=" + strconv.Itoa(int(version))
}
}
Expand Down

0 comments on commit 84e1d03

Please sign in to comment.