-
Notifications
You must be signed in to change notification settings - Fork 236
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a3153a7
EID Permissions extension
Net-burst 9a711eb
Minor refactoring
Net-burst e49d0ff
Simplification refactoring
Net-burst ab462db
Merge branch 'refs/heads/master' into feature/extend-eid-permissions
Net-burst 86492b5
Refactor
Lightwood13 5372661
Refactoring.
And1sS 6823ce6
Refactoring.
And1sS 5018a0a
Refactored unit tests.
And1sS 47844e0
Tests: EID Permissions extension (#4357)
osulzhenko ae8cace
minor test fix
osulzhenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/main/java/org/prebid/server/auction/model/EidPermissionIndex.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package org.prebid.server.auction.model; | ||
|
|
||
| import com.iab.openrtb.request.Eid; | ||
| import org.apache.commons.collections4.CollectionUtils; | ||
| import org.apache.commons.lang3.ObjectUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidDataEidPermissions; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public final class EidPermissionIndex { | ||
|
|
||
| // bitmask for which fields are present in a permission | ||
| private static final int INSERTER = 1; // 0001 | ||
|
Net-burst marked this conversation as resolved.
Outdated
|
||
| private static final int SOURCE = 2; // 0010 | ||
| private static final int MATCHER = 4; // 0100 | ||
| private static final int MM = 8; // 1000 | ||
|
Net-burst marked this conversation as resolved.
Outdated
|
||
| private static final String WILDCARD_BIDDER = "*"; | ||
|
|
||
| private final Map<Integer, Map<Key, Set<String>>> ruleIndexByMask; | ||
|
|
||
| private record Key(String inserter, String source, String matcher, Integer mm) { | ||
| } | ||
|
|
||
| private EidPermissionIndex(Map<Integer, Map<Key, Set<String>>> ruleIndexByMask) { | ||
| this.ruleIndexByMask = ruleIndexByMask; | ||
| } | ||
|
|
||
| public static EidPermissionIndex build(List<ExtRequestPrebidDataEidPermissions> permissions) { | ||
| if (ObjectUtils.isEmpty(permissions)) { | ||
| return null; | ||
| } | ||
|
|
||
| final Map<Integer, Map<Key, Set<String>>> idx = new HashMap<>(); | ||
|
|
||
| for (ExtRequestPrebidDataEidPermissions permission : permissions) { | ||
| final List<String> bidders = CollectionUtils.emptyIfNull(permission.getBidders()) | ||
| .stream() | ||
| .filter(StringUtils::isNotBlank) | ||
| .map(String::toLowerCase) | ||
| .toList(); | ||
|
|
||
| if (bidders.isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| final int ruleMask = maskOf(permission.getInserter(), | ||
| permission.getSource(), | ||
| permission.getMatcher(), | ||
| permission.getMm()); | ||
| final Key ruleKey = new Key(permission.getInserter(), | ||
| permission.getSource(), | ||
| permission.getMatcher(), | ||
| permission.getMm()); | ||
|
|
||
| idx.computeIfAbsent(ruleMask, ignored -> new HashMap<>()) | ||
| .computeIfAbsent(ruleKey, ignored -> new HashSet<>()) | ||
| .addAll(bidders); | ||
| } | ||
|
|
||
| return new EidPermissionIndex(idx); | ||
| } | ||
|
|
||
| private static int maskOf(String inserter, String source, String matcher, Integer mm) { | ||
| int mask = 0; | ||
|
|
||
| if (inserter != null) { | ||
| mask |= INSERTER; | ||
| } | ||
| if (source != null) { | ||
| mask |= SOURCE; | ||
| } | ||
| if (matcher != null) { | ||
| mask |= MATCHER; | ||
| } | ||
| if (mm != null) { | ||
| mask |= MM; | ||
| } | ||
|
|
||
| return mask; | ||
|
Net-burst marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public boolean isAllowed(Eid eid, String bidder) { | ||
| final int eidMask = maskOf(eid.getInserter(), eid.getSource(), eid.getMatcher(), eid.getMm()); | ||
|
|
||
| boolean ruleMatched = false; | ||
|
|
||
| // Check every permission bucket whose criteria fields are a subset of the Eid’s populated fields | ||
| for (Map.Entry<Integer, Map<Key, Set<String>>> ruleBucket : ruleIndexByMask.entrySet()) { | ||
| final int ruleMask = ruleBucket.getKey(); | ||
|
|
||
| // rule can only match if all its required fields exist on the Eid | ||
| if ((ruleMask & eidMask) != ruleMask) { | ||
| continue; | ||
| } | ||
|
|
||
| final Key normalizedEidKey = new Key((ruleMask & INSERTER) != 0 ? eid.getInserter() : null, | ||
| (ruleMask & SOURCE) != 0 ? eid.getSource() : null, | ||
| (ruleMask & MATCHER) != 0 ? eid.getMatcher() : null, | ||
| (ruleMask & MM) != 0 ? eid.getMm() : null); | ||
|
|
||
| final Set<String> allowedBidders = ruleBucket.getValue().get(normalizedEidKey); | ||
| if (allowedBidders != null) { | ||
| ruleMatched = true; | ||
| if (allowedBidders.contains(WILDCARD_BIDDER) || allowedBidders.contains(bidder.toLowerCase())) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // allow-by-default: if no rule matched at all, allow | ||
| return !ruleMatched; | ||
| } | ||
| } | ||
27 changes: 23 additions & 4 deletions
27
.../java/org/prebid/server/proto/openrtb/ext/request/ExtRequestPrebidDataEidPermissions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
And1sS marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.