github.com/prebid/prebid-server/v2@v2.18.0/privacy/activitycontrol.go (about)

     1  package privacy
     2  
     3  import (
     4  	"github.com/prebid/prebid-server/v2/config"
     5  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     6  )
     7  
     8  type ActivityResult int
     9  
    10  const (
    11  	ActivityAbstain ActivityResult = iota
    12  	ActivityAllow
    13  	ActivityDeny
    14  )
    15  
    16  const defaultActivityResult = true
    17  
    18  func NewRequestFromPolicies(p Policies) ActivityRequest {
    19  	return ActivityRequest{policies: &p}
    20  }
    21  
    22  func NewRequestFromBidRequest(r openrtb_ext.RequestWrapper) ActivityRequest {
    23  	return ActivityRequest{bidRequest: &r}
    24  }
    25  
    26  type ActivityRequest struct {
    27  	policies   *Policies
    28  	bidRequest *openrtb_ext.RequestWrapper
    29  }
    30  
    31  func (r ActivityRequest) IsPolicies() bool {
    32  	return r.policies != nil
    33  }
    34  
    35  func (r ActivityRequest) IsBidRequest() bool {
    36  	return r.bidRequest != nil
    37  }
    38  
    39  type ActivityControl struct {
    40  	plans      map[Activity]ActivityPlan
    41  	IPv6Config config.IPv6
    42  	IPv4Config config.IPv4
    43  }
    44  
    45  func NewActivityControl(cfg *config.AccountPrivacy) ActivityControl {
    46  	ac := ActivityControl{}
    47  
    48  	if cfg == nil || cfg.AllowActivities == nil {
    49  		return ac
    50  	}
    51  
    52  	plans := make(map[Activity]ActivityPlan, 8)
    53  	plans[ActivitySyncUser] = buildPlan(cfg.AllowActivities.SyncUser)
    54  	plans[ActivityFetchBids] = buildPlan(cfg.AllowActivities.FetchBids)
    55  	plans[ActivityEnrichUserFPD] = buildPlan(cfg.AllowActivities.EnrichUserFPD)
    56  	plans[ActivityReportAnalytics] = buildPlan(cfg.AllowActivities.ReportAnalytics)
    57  	plans[ActivityTransmitUserFPD] = buildPlan(cfg.AllowActivities.TransmitUserFPD)
    58  	plans[ActivityTransmitPreciseGeo] = buildPlan(cfg.AllowActivities.TransmitPreciseGeo)
    59  	plans[ActivityTransmitUniqueRequestIDs] = buildPlan(cfg.AllowActivities.TransmitUniqueRequestIds)
    60  	plans[ActivityTransmitTIDs] = buildPlan(cfg.AllowActivities.TransmitTids)
    61  	ac.plans = plans
    62  
    63  	ac.IPv4Config = cfg.IPv4Config
    64  	ac.IPv6Config = cfg.IPv6Config
    65  
    66  	return ac
    67  }
    68  
    69  func buildPlan(activity config.Activity) ActivityPlan {
    70  	return ActivityPlan{
    71  		rules:         cfgToRules(activity.Rules),
    72  		defaultResult: cfgToDefaultResult(activity.Default),
    73  	}
    74  }
    75  
    76  func cfgToRules(rules []config.ActivityRule) []Rule {
    77  	var enfRules []Rule
    78  
    79  	for _, r := range rules {
    80  		result := ActivityDeny
    81  		if r.Allow {
    82  			result = ActivityAllow
    83  		}
    84  
    85  		er := ConditionRule{
    86  			result:        result,
    87  			componentName: r.Condition.ComponentName,
    88  			componentType: r.Condition.ComponentType,
    89  		}
    90  		enfRules = append(enfRules, er)
    91  	}
    92  	return enfRules
    93  }
    94  
    95  func cfgToDefaultResult(activityDefault *bool) bool {
    96  	if activityDefault == nil {
    97  		return defaultActivityResult
    98  	}
    99  	return *activityDefault
   100  }
   101  
   102  func (e ActivityControl) Allow(activity Activity, target Component, request ActivityRequest) bool {
   103  	plan, planDefined := e.plans[activity]
   104  
   105  	if !planDefined {
   106  		return defaultActivityResult
   107  	}
   108  
   109  	return plan.Evaluate(target, request)
   110  }
   111  
   112  type ActivityPlan struct {
   113  	defaultResult bool
   114  	rules         []Rule
   115  }
   116  
   117  func (p ActivityPlan) Evaluate(target Component, request ActivityRequest) bool {
   118  	for _, rule := range p.rules {
   119  		result := rule.Evaluate(target, request)
   120  		if result == ActivityDeny || result == ActivityAllow {
   121  			return result == ActivityAllow
   122  		}
   123  	}
   124  	return p.defaultResult
   125  }