Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
Isolate cookie management (prebid#506)
Browse files Browse the repository at this point in the history
* Moved the usersyncers into their own package. Pulled the cookie parsing code into the usersync package.

* Fixed the tests, moving them to the appropriate area.

* Updated markdown docs.

* Fixed newer code so that it points to the new file locations too.
  • Loading branch information
dbemiller authored May 15, 2018
1 parent a2cf705 commit d380980
Show file tree
Hide file tree
Showing 53 changed files with 486 additions and 417 deletions.
3 changes: 2 additions & 1 deletion adapters/adform/adform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/prebid/prebid-server/adapters/adapterstest"
"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"

"fmt"

Expand Down Expand Up @@ -193,7 +194,7 @@ func preparePrebidRequest(serverUrl string, t *testing.T) *pbs.PBSRequest {
prebidHttpRequest.Header.Add("Referer", adformTestData.referrer)
prebidHttpRequest.Header.Add("X-Real-IP", adformTestData.deviceIP)

pbsCookie := pbs.ParsePBSCookieFromRequest(prebidHttpRequest, &config.Cookie{})
pbsCookie := usersync.ParsePBSCookieFromRequest(prebidHttpRequest, &config.Cookie{})
pbsCookie.TrySync("adform", adformTestData.buyerUID)
fakeWriter := httptest.NewRecorder()
pbsCookie.SetCookieOnResponse(fakeWriter, "", time.Minute)
Expand Down
3 changes: 2 additions & 1 deletion adapters/appnexus/appnexus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"

"fmt"

Expand Down Expand Up @@ -359,7 +360,7 @@ func TestAppNexusBasicResponse(t *testing.T) {
req.Header.Add("User-Agent", andata.deviceUA)
req.Header.Add("X-Real-IP", andata.deviceIP)

pc := pbs.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc.TrySync("adnxs", andata.buyerUID)
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
Expand Down
3 changes: 2 additions & 1 deletion adapters/audienceNetwork/facebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"

"fmt"

Expand Down Expand Up @@ -205,7 +206,7 @@ func GenerateBidRequestForTestData(fbdata bidInfo, url string) (*pbs.PBSRequest,
req.Header.Add("User-Agent", fbdata.deviceUA)
req.Header.Add("X-Real-IP", fbdata.deviceIP)

pc := pbs.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc.TrySync("audienceNetwork", fbdata.buyerUID)
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
Expand Down
3 changes: 2 additions & 1 deletion adapters/conversant/conversant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"
)

// Constants
Expand Down Expand Up @@ -606,7 +607,7 @@ func ParseRequest(req *pbs.PBSRequest) (*pbs.PBSRequest, error) {
// Need to pass the conversant user id thru uid cookie

httpReq := httptest.NewRequest("POST", "/foo", body)
cookie := pbs.NewPBSCookie()
cookie := usersync.NewPBSCookie()
cookie.TrySync("conversant", ExpectedBuyerUID)
httpReq.Header.Set("Cookie", cookie.ToHTTPCookie(90*24*time.Hour).String())
httpReq.Header.Add("Referer", "http://example.com")
Expand Down
3 changes: 2 additions & 1 deletion adapters/lifestreet/lifestreet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"

"fmt"

Expand Down Expand Up @@ -224,7 +225,7 @@ func TestLifestreetBasicResponse(t *testing.T) {
req.Header.Add("Referer", lsdata.referrer)
req.Header.Add("X-Real-IP", lsdata.deviceIP)

pc := pbs.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(req, &config.Cookie{})
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
req.Header.Add("Cookie", fakewriter.Header().Get("Set-Cookie"))
Expand Down
7 changes: 5 additions & 2 deletions adapters/openrtb_util_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package adapters

import (
"testing"

"encoding/json"

"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCommonMediaTypes(t *testing.T) {
Expand Down Expand Up @@ -374,7 +377,7 @@ func TestOpenRTBEmptyUser(t *testing.T) {
}

func TestOpenRTBUserWithCookie(t *testing.T) {
pbsCookie := pbs.NewPBSCookie()
pbsCookie := usersync.NewPBSCookie()
pbsCookie.TrySync("test", "abcde")
pbReq := pbs.PBSRequest{
User: &openrtb.User{},
Expand Down
3 changes: 2 additions & 1 deletion adapters/pubmatic/pubmatic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"
)

func CompareStringValue(val1 string, val2 string, t *testing.T) {
Expand Down Expand Up @@ -625,7 +626,7 @@ func TestPubmaticSampleRequest(t *testing.T) {

httpReq := httptest.NewRequest("POST", server.URL, body)
httpReq.Header.Add("Referer", "http://test.com/sports")
pc := pbs.ParsePBSCookieFromRequest(httpReq, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(httpReq, &config.Cookie{})
pc.TrySync("pubmatic", "12345")
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
Expand Down
3 changes: 2 additions & 1 deletion adapters/pulsepoint/pulsepoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"
)

/**
Expand Down Expand Up @@ -220,7 +221,7 @@ func SampleRequest(numberOfImpressions int, t *testing.T) *pbs.PBSRequest {
// setup a http request
httpReq := httptest.NewRequest("POST", CreateService(adapterstest.BidOnTags("")).Server.URL, body)
httpReq.Header.Add("Referer", "http://news.pub/topnews")
pc := pbs.ParsePBSCookieFromRequest(httpReq, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(httpReq, &config.Cookie{})
pc.TrySync("pulsepoint", "pulsepointUser123")
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
Expand Down
3 changes: 2 additions & 1 deletion adapters/rubicon/rubicon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/prebid/prebid-server/adapters/adapterstest"
"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"

"fmt"

Expand Down Expand Up @@ -945,7 +946,7 @@ func CreatePrebidRequest(server *httptest.Server, t *testing.T) (an *RubiconAdap
req.Header.Add("User-Agent", rubidata.deviceUA)
req.Header.Add("X-Real-IP", rubidata.deviceIP)

pc := pbs.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc.TrySync("rubicon", rubidata.buyerUID)
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
Expand Down
8 changes: 5 additions & 3 deletions adapters/sovrn/sovrn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (

"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"

"context"
"net/http"

"strconv"
"time"

"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/adapters/adapterstest"
"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/config"
"strconv"
"time"
)

func TestJsonSamples(t *testing.T) {
Expand Down Expand Up @@ -181,7 +183,7 @@ func SampleSovrnRequest(numberOfImpressions int, t *testing.T) *pbs.PBSRequest {
httpReq.Header.Add("Referer", testUrl)
httpReq.Header.Add("User-Agent", testUserAgent)
httpReq.Header.Add("X-Forwarded-For", testIp)
pc := pbs.ParsePBSCookieFromRequest(httpReq, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(httpReq, &config.Cookie{})
pc.TrySync("sovrn", testSovrnUserId)
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
Expand Down
4 changes: 2 additions & 2 deletions docs/developers/add-new-bidder.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Bidder implementations are scattered throughout several files.

- `adapters/{bidder}/{bidder}.go`: contains an implementation of [the Bidder interface](../../adapters/bidder.go).
- `openrtb_ext/imp_{bidder}.go`: contract classes for your Bidder's params.
- `usersync/{bidder}.go`: A [Usersyncer](../../usersync/usersync.go) which returns cookie sync info for your bidder.
- `usersync/{bidder}_test.go`: Unit tests for your Usersyncer
- `usersync/usersyncers/{bidder}.go`: A [Usersyncer](../../usersync/usersync.go) which returns cookie sync info for your bidder.
- `usersync/usersyncers/{bidder}_test.go`: Unit tests for your Usersyncer
- `static/bidder-params/{bidder}.json`: A [draft-4 json-schema](https://spacetelescope.github.io/understanding-json-schema/) which [validates your Bidder's params](https://www.jsonschemavalidator.net/).
- `static/bidder-info/{bidder}.yaml`: contains metadata (e.g. contact email, platform & media type support) about the adapter

Expand Down
3 changes: 1 addition & 2 deletions endpoints/cookie_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/prebid/prebid-server/analytics"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/pbsmetrics"
"github.com/prebid/prebid-server/usersync"
)
Expand All @@ -27,7 +26,7 @@ func NewCookieSyncEndpoint(syncers map[openrtb_ext.BidderName]usersync.Usersynce
defer pbsAnalytics.LogCookieSyncObject(&co)

metrics.RecordCookieSync(pbsmetrics.Labels{})
userSyncCookie := pbs.ParsePBSCookieFromRequest(r, optOutCookie)
userSyncCookie := usersync.ParsePBSCookieFromRequest(r, optOutCookie)
if !userSyncCookie.AllowSyncs() {
http.Error(w, "User has opted out", http.StatusUnauthorized)
co.Status = http.StatusUnauthorized
Expand Down
12 changes: 6 additions & 6 deletions endpoints/cookie_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
analyticsConf "github.com/prebid/prebid-server/analytics/config"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/pbsmetrics"
"github.com/prebid/prebid-server/usersync"
"github.com/prebid/prebid-server/usersync/usersyncers"
)

func TestCookieSyncNoCookies(t *testing.T) {
Expand Down Expand Up @@ -65,7 +65,7 @@ func doPost(body string, existingSyncs map[string]string) *httptest.ResponseReco
router.POST("/cookie_sync", endpoint)
req, _ := http.NewRequest("POST", "/cookie_sync", strings.NewReader(body))
if len(existingSyncs) > 0 {
pcs := pbs.NewPBSCookie()
pcs := usersync.NewPBSCookie()
for bidder, uid := range existingSyncs {
pcs.TrySync(bidder, uid)
}
Expand All @@ -79,10 +79,10 @@ func doPost(body string, existingSyncs map[string]string) *httptest.ResponseReco

func testableEndpoint() httprouter.Handle {
knownSyncers := map[openrtb_ext.BidderName]usersync.Usersyncer{
openrtb_ext.BidderAppnexus: usersync.NewAppnexusSyncer("someurl.com"),
openrtb_ext.BidderFacebook: usersync.NewFacebookSyncer("facebookurl.com"),
openrtb_ext.BidderLifestreet: usersync.NewLifestreetSyncer("anotherurl.com"),
openrtb_ext.BidderPubmatic: usersync.NewPubmaticSyncer("thaturl.com"),
openrtb_ext.BidderAppnexus: usersyncers.NewAppnexusSyncer("someurl.com"),
openrtb_ext.BidderFacebook: usersyncers.NewFacebookSyncer("facebookurl.com"),
openrtb_ext.BidderLifestreet: usersyncers.NewLifestreetSyncer("anotherurl.com"),
openrtb_ext.BidderPubmatic: usersyncers.NewPubmaticSyncer("thaturl.com"),
}
return NewCookieSyncEndpoint(knownSyncers, &config.Cookie{}, &pbsmetrics.DummyMetricsEngine{}, analyticsConf.NewPBSAnalytics(&config.Analytics{}))
}
Expand Down
4 changes: 2 additions & 2 deletions endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/exchange"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/pbsmetrics"
"github.com/prebid/prebid-server/stored_requests"
"github.com/prebid/prebid-server/usersync"
)

const defaultAmpRequestTimeoutMillis = 900
Expand Down Expand Up @@ -111,7 +111,7 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
}
defer cancel()

usersyncs := pbs.ParsePBSCookieFromRequest(r, &(deps.cfg.HostCookie.OptOutCookie))
usersyncs := usersync.ParsePBSCookieFromRequest(r, &(deps.cfg.HostCookie.OptOutCookie))
if req.App != nil {
labels.Source = pbsmetrics.DemandApp
} else {
Expand Down
4 changes: 2 additions & 2 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/exchange"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/pbsmetrics"
"github.com/prebid/prebid-server/prebid"
"github.com/prebid/prebid-server/stored_requests"
"github.com/prebid/prebid-server/usersync"
"golang.org/x/net/publicsuffix"
)

Expand Down Expand Up @@ -108,7 +108,7 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
}
defer cancel()

usersyncs := pbs.ParsePBSCookieFromRequest(r, &(deps.cfg.HostCookie.OptOutCookie))
usersyncs := usersync.ParsePBSCookieFromRequest(r, &(deps.cfg.HostCookie.OptOutCookie))
if req.App != nil {
labels.Source = pbsmetrics.DemandApp
} else {
Expand Down
3 changes: 2 additions & 1 deletion exchange/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/usersync"
)

// AdaptLegacyAdapter turns a bidder.Adapter into an adaptedBidder.
Expand Down Expand Up @@ -98,7 +99,7 @@ func (bidder *adaptedAdapter) toLegacyRequest(req *openrtb.BidRequest) (*pbs.PBS
domain = req.Site.Domain
}

cookie := pbs.NewPBSCookie()
cookie := usersync.NewPBSCookie()
if req.User != nil {
if req.User.BuyerUID != "" {
cookie.TrySync(bidder.adapter.Name(), req.User.BuyerUID)
Expand Down
14 changes: 7 additions & 7 deletions pbs/pbsrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ type PBSRequest struct {
SDK *SDK `json:"sdk"`

// internal
Bidders []*PBSBidder `json:"-"`
User *openrtb.User `json:"-"`
Cookie *PBSCookie `json:"-"`
Url string `json:"-"`
Domain string `json:"-"`
Regs *openrtb.Regs `json:"-"`
Bidders []*PBSBidder `json:"-"`
User *openrtb.User `json:"-"`
Cookie *usersync.PBSCookie `json:"-"`
Url string `json:"-"`
Domain string `json:"-"`
Regs *openrtb.Regs `json:"-"`
Start time.Time
}

Expand Down Expand Up @@ -262,7 +262,7 @@ func ParsePBSRequest(r *http.Request, cache cache.Cache, hostCookieSettings *Hos

// use client-side data for web requests
if pbsReq.App == nil {
pbsReq.Cookie = ParsePBSCookieFromRequest(r, &(hostCookieSettings.OptOutCookie))
pbsReq.Cookie = usersync.ParsePBSCookieFromRequest(r, &(hostCookieSettings.OptOutCookie))

// Host has right to leverage private cookie store for user ID
if uid, _, _ := pbsReq.Cookie.GetUID(hostCookieSettings.Family); uid == "" && hostCookieSettings.CookieName != "" {
Expand Down
Loading

0 comments on commit d380980

Please sign in to comment.