forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bidders.go
155 lines (137 loc) · 5.17 KB
/
bidders.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
package openrtb_ext
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/mxmCherry/openrtb"
"github.com/xeipuuv/gojsonschema"
)
const schemaDirectory = "static/bidder-params"
// BidderName may refer to a bidder ID, or an Alias which is defined in the request.
type BidderName string
// These names _must_ coincide with the bidder code in Prebid.js, if an adapter also exists in that project.
// Please keep these (and the BidderMap) alphabetized to minimize merge conflicts among adapter submissions.
const (
BidderAdkernelAdn BidderName = "adkernelAdn"
BidderAdtelligent BidderName = "adtelligent"
BidderAdform BidderName = "adform"
BidderAppnexus BidderName = "appnexus"
BidderBeachfront BidderName = "beachfront"
BidderBrightroll BidderName = "brightroll"
BidderConversant BidderName = "conversant"
BidderEPlanning BidderName = "eplanning"
BidderFacebook BidderName = "audienceNetwork"
BidderIndex BidderName = "indexExchange"
BidderLifestreet BidderName = "lifestreet"
BidderOpenx BidderName = "openx"
BidderPubmatic BidderName = "pubmatic"
BidderPulsepoint BidderName = "pulsepoint"
BidderRubicon BidderName = "rubicon"
BidderSomoaudience BidderName = "somoaudience"
BidderSovrn BidderName = "sovrn"
)
// BidderMap stores all the valid OpenRTB 2.x Bidders in the project. This map *must not* be mutated.
var BidderMap = map[string]BidderName{
"adkernelAdn": BidderAdkernelAdn,
"adtelligent": BidderAdtelligent,
"adform": BidderAdform,
"appnexus": BidderAppnexus,
"beachfront": BidderBeachfront,
"audienceNetwork": BidderFacebook,
"brightroll": BidderBrightroll,
"conversant": BidderConversant,
"eplanning": BidderEPlanning,
"indexExchange": BidderIndex,
"lifestreet": BidderLifestreet,
"openx": BidderOpenx,
"pubmatic": BidderPubmatic,
"pulsepoint": BidderPulsepoint,
"rubicon": BidderRubicon,
"somoaudience": BidderSomoaudience,
"sovrn": BidderSovrn,
}
// BidderList returns the values of the BidderMap
func BidderList() []BidderName {
bidders := make([]BidderName, 0, len(BidderMap))
for _, value := range BidderMap {
bidders = append(bidders, value)
}
return bidders
}
func (name BidderName) MarshalJSON() ([]byte, error) {
return []byte(name), nil
}
func (name *BidderName) String() string {
if name == nil {
return ""
} else {
return string(*name)
}
}
// The BidderParamValidator is used to enforce bidrequest.imp[i].ext.{anyBidder} values.
//
// This is treated differently from the other types because we rely on JSON-schemas to validate bidder params.
type BidderParamValidator interface {
Validate(name BidderName, ext openrtb.RawJSON) error
// Schema returns the JSON schema used to perform validation.
Schema(name BidderName) string
}
// NewBidderParamsValidator makes a BidderParamValidator, assuming all the necessary files exist in the filesystem.
// This will error if, for example, a Bidder gets added but no JSON schema is written for them.
func NewBidderParamsValidator(schemaDirectory string) (BidderParamValidator, error) {
fileInfos, err := ioutil.ReadDir(schemaDirectory)
if err != nil {
return nil, fmt.Errorf("Failed to read JSON schemas from directory %s. %v", schemaDirectory, err)
}
schemaContents := make(map[BidderName]string, 50)
schemas := make(map[BidderName]*gojsonschema.Schema, 50)
for _, fileInfo := range fileInfos {
bidderName := strings.TrimSuffix(fileInfo.Name(), ".json")
if _, isValid := BidderMap[bidderName]; !isValid {
return nil, fmt.Errorf("File %s/%s does not match a valid BidderName.", schemaDirectory, fileInfo.Name())
}
toOpen, err := filepath.Abs(filepath.Join(schemaDirectory, fileInfo.Name()))
if err != nil {
return nil, fmt.Errorf("Failed to get an absolute representation of the path: %s, %v", toOpen, err)
}
schemaLoader := gojsonschema.NewReferenceLoader("file:///" + toOpen)
loadedSchema, err := gojsonschema.NewSchema(schemaLoader)
if err != nil {
return nil, fmt.Errorf("Failed to load json schema at %s: %v", toOpen, err)
}
fileBytes, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", schemaDirectory, fileInfo.Name()))
if err != nil {
return nil, fmt.Errorf("Failed to read file %s/%s: %v", schemaDirectory, fileInfo.Name(), err)
}
schemas[BidderName(bidderName)] = loadedSchema
schemaContents[BidderName(bidderName)] = string(fileBytes)
}
return &bidderParamValidator{
schemaContents: schemaContents,
parsedSchemas: schemas,
}, nil
}
type bidderParamValidator struct {
schemaContents map[BidderName]string
parsedSchemas map[BidderName]*gojsonschema.Schema
}
func (validator *bidderParamValidator) Validate(name BidderName, ext openrtb.RawJSON) error {
result, err := validator.parsedSchemas[name].Validate(gojsonschema.NewBytesLoader(ext))
if err != nil {
return err
}
if !result.Valid() {
errBuilder := bytes.NewBuffer(make([]byte, 0, 300))
for _, err := range result.Errors() {
errBuilder.WriteString(err.String())
}
return errors.New(errBuilder.String())
}
return nil
}
func (validator *bidderParamValidator) Schema(name BidderName) string {
return validator.schemaContents[name]
}