-
Notifications
You must be signed in to change notification settings - Fork 224
Core: EID Permissions extension #4349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a3153a7
9a711eb
e49d0ff
ab462db
86492b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package org.prebid.server.auction.model; | ||
|
|
||
| import com.iab.openrtb.request.Eid; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidDataEidPermissions; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public final class EidPermissionHolder { | ||
|
|
||
| private static final String WILDCARD_BIDDER = "*"; | ||
|
|
||
| private static final ExtRequestPrebidDataEidPermissions DEFAULT_RULE = ExtRequestPrebidDataEidPermissions.builder() | ||
| .bidders(Collections.singletonList(WILDCARD_BIDDER)) | ||
| .build(); | ||
|
|
||
| private static final EidPermissionHolder EMPTY = new EidPermissionHolder(Collections.emptyList()); | ||
|
|
||
| private final List<ExtRequestPrebidDataEidPermissions> eidPermissions; | ||
|
|
||
| private EidPermissionHolder(List<ExtRequestPrebidDataEidPermissions> eidPermissions) { | ||
| this.eidPermissions = new ArrayList<>(eidPermissions); | ||
| this.eidPermissions.add(DEFAULT_RULE); | ||
| } | ||
|
|
||
| public static EidPermissionHolder of(List<ExtRequestPrebidDataEidPermissions> eidPermissions) { | ||
| return new EidPermissionHolder(eidPermissions); | ||
| } | ||
|
|
||
| public static EidPermissionHolder empty() { | ||
| return EMPTY; | ||
| } | ||
|
|
||
| public boolean isAllowed(Eid eid, String bidder) { | ||
| final Map<Integer, List<ExtRequestPrebidDataEidPermissions>> matchingRulesBySpecificity = eidPermissions | ||
| .stream() | ||
| .filter(rule -> isRuleMatched(eid, rule)) | ||
| .collect(Collectors.groupingBy(this::getRuleSpecificity)); | ||
|
|
||
| final int highestSpecificityMatchingRules = Collections.max(matchingRulesBySpecificity.keySet()); | ||
| return matchingRulesBySpecificity.get(highestSpecificityMatchingRules).stream() | ||
| .anyMatch(eidPermission -> isBidderAllowed(bidder, eidPermission.getBidders())); | ||
| } | ||
|
|
||
| private int getRuleSpecificity(ExtRequestPrebidDataEidPermissions eidPermission) { | ||
| return (int) Stream.of(eidPermission.getInserter(), | ||
| eidPermission.getSource(), | ||
| eidPermission.getMatcher(), | ||
| eidPermission.getMm()) | ||
| .filter(Objects::nonNull) | ||
| .count(); | ||
| } | ||
|
|
||
| private boolean isRuleMatched(Eid eid, ExtRequestPrebidDataEidPermissions eidPermission) { | ||
| return (eidPermission.getInserter() == null || eidPermission.getInserter().equals(eid.getInserter())) | ||
| && (eidPermission.getSource() == null || eidPermission.getSource().equals(eid.getSource())) | ||
| && (eidPermission.getMatcher() == null || eidPermission.getMatcher().equals(eid.getMatcher())) | ||
| && (eidPermission.getMm() == null || eidPermission.getMm().equals(eid.getMm())); | ||
|
Comment on lines
+61
to
+64
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add method |
||
| } | ||
|
|
||
| private boolean isBidderAllowed(String bidder, List<String> ruleBidders) { | ||
| return ruleBidders == null || ruleBidders.stream() | ||
| .anyMatch(allowedBidder -> StringUtils.equalsIgnoreCase(allowedBidder, bidder) | ||
| || WILDCARD_BIDDER.equals(allowedBidder)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,43 @@ | ||
| package org.prebid.server.proto.openrtb.ext.request; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import lombok.Builder; | ||
| import lombok.Value; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Defines the contract for bidrequest.ext.prebid.data.eidPermissions | ||
| */ | ||
| @Value(staticConstructor = "of") | ||
| @Value | ||
| @Builder | ||
| public class ExtRequestPrebidDataEidPermissions { | ||
|
|
||
| /** | ||
| * Defines the contract for bidrequest.ext.prebid.data.eidPermissions.inserter | ||
| */ | ||
| String inserter; | ||
|
|
||
| /** | ||
| * Defines the contract for bidrequest.ext.prebid.data.eidPermissions.source | ||
| */ | ||
| String source; | ||
|
|
||
| /** | ||
| * Defines the contract for bidrequest.ext.prebid.data.eidPermissions.matcher | ||
| */ | ||
| String matcher; | ||
|
|
||
| /** | ||
| * Defines the contract for bidrequest.ext.prebid.data.eidPermissions.mm | ||
| */ | ||
| Integer mm; | ||
|
|
||
| /** | ||
| * Defines the contract for bidrequest.ext.prebid.data.eidPermissions.bidders | ||
| */ | ||
| @JsonFormat(without = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY) | ||
| List<String> bidders; | ||
|
|
||
| @Deprecated | ||
| public static ExtRequestPrebidDataEidPermissions of(String source, List<String> bidders) { | ||
| return new ExtRequestPrebidDataEidPermissions(null, source, null, null, bidders); | ||
| } | ||
|
Comment on lines
+39
to
+42
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the idea to keep it |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -376,7 +376,7 @@ private void validateEidPermissions(List<ExtRequestPrebidDataEidPermissions> eid | |
| boolean isDebugEnabled, | ||
| List<String> warnings) throws ValidationException { | ||
|
|
||
| if (eidPermissions == null) { | ||
| if (ObjectUtils.isEmpty(eidPermissions)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return; | ||
| } | ||
|
|
||
|
|
@@ -385,14 +385,24 @@ private void validateEidPermissions(List<ExtRequestPrebidDataEidPermissions> eid | |
| throw new ValidationException("request.ext.prebid.data.eidpermissions[i] can't be null"); | ||
| } | ||
|
|
||
| validateEidPermissionSource(eidPermission.getSource()); | ||
| validateEidPermissionCriteria(eidPermission.getInserter(), | ||
| eidPermission.getSource(), | ||
| eidPermission.getMatcher(), | ||
| eidPermission.getMm()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| validateEidPermissionBidders(eidPermission.getBidders(), aliases, isDebugEnabled, warnings); | ||
| } | ||
| } | ||
|
|
||
| private void validateEidPermissionSource(String source) throws ValidationException { | ||
| if (StringUtils.isEmpty(source)) { | ||
| throw new ValidationException("Missing required value request.ext.prebid.data.eidPermissions[].source"); | ||
| private void validateEidPermissionCriteria(String inserter, | ||
| String source, | ||
| String matcher, | ||
| Integer mm) throws ValidationException { | ||
| if (StringUtils.isEmpty(inserter) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add empty line after multi-line declaration |
||
| && StringUtils.isEmpty(source) | ||
| && StringUtils.isEmpty(matcher) | ||
| && mm == null) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about to use |
||
| throw new ValidationException("Missing required parameter(s) in request.ext.prebid.data.eidPermissions[]. " | ||
| + "Either one or a combination of inserter, source, matcher, or mm should be defined."); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can also be moved to EidPermissionHolder.