Skip to content

Commit

Permalink
Sublime Bid Adapter : refactoring notifyId, bid request validation, &…
Browse files Browse the repository at this point in the history
… device detection fix (prebid#7327)

* feat(sublimeBidAdapter): moving notifyId from an external to an internal ref

* fix(sublimeBidAdapter): fixing device detection regexps

* feat(sublimeBidAdapter): improve bid request validation

Co-authored-by: François-Georges Cloutier <fg.cloutier@sublime.xyz>
Co-authored-by: Mathieu Darse <code@mathieudarse.fr>
  • Loading branch information
3 people authored and Chris Pabst committed Jan 10, 2022
1 parent c04b1ac commit fd06f9c
Show file tree
Hide file tree
Showing 2 changed files with 322 additions and 33 deletions.
71 changes: 58 additions & 13 deletions modules/sublimeBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ const DEFAULT_CURRENCY = 'EUR';
const DEFAULT_PROTOCOL = 'https';
const DEFAULT_TTL = 600;
const SUBLIME_ANTENNA = 'antenna.ayads.co';
const SUBLIME_VERSION = '0.7.3';
const SUBLIME_VERSION = '0.8.0';

/**
* Identify the current device type
* @returns {string}
*/
function detectDevice() {
const isMobile = /(?:phone|windowss+phone|ipod|blackberry|Galaxy Nexus|SM-G892A|(?:android|bbd+|meego|silk|googlebot) .+?mobile|palm|windowss+ce|opera mini|avantgo|docomo)/i;
const isMobile = /(?:phone|windows\s+phone|ipod|blackberry|Galaxy Nexus|SM-G892A|(?:android|bbd+|meego|silk|googlebot) .+?mobile|palm|windows\s+ce|opera mini|avantgo|docomo)/i;

const isTablet = /(?:ipad|playbook|Tablet|(?:android|bb\\d+|meego|silk)(?! .+? mobile))/i;
const isTablet = /(?:ipad|playbook|Tablet|(?:android|bb\d+|meego|silk)(?! .+? mobile))/i;

return (
(isMobile.test(navigator.userAgent) && 'm') || // mobile
Expand All @@ -27,6 +27,16 @@ function detectDevice() {
);
}

const UUID_V4_RE = /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
/**
* Checks whether a notifyId is well-formed
* @param {*} value
* @returns {boolean}
*/
function isValidNotifyId(value) {
return UUID_V4_RE.test(value);
}

/**
* Debug log message
* @param {String} msg
Expand All @@ -53,6 +63,26 @@ export function setState(value) {
log('State has been updated :', state);
}

/**
* Get a notifyId from bid params or from sublime global
* @param {Object} params - The bid params
* @return {string}
*/
function getNotifyId(params) {
const sublime = window.sublime = window.sublime || {};

let notifyId = params.notifyId || sublime.notifyId;
if (!notifyId) {
notifyId = utils.generateUUID();
log('generating a notifyId', notifyId);
}
if (!sublime.notifyId) {
sublime.notifyId = notifyId;
}

return notifyId;
}

/**
* Send pixel to our debug endpoint
* @param {string} eventName - Event name that will be send in the e= query string
Expand Down Expand Up @@ -91,6 +121,16 @@ export function sendEvent(eventName, sspName) {
* @return {Boolean} True if this is a valid bid, and false otherwise.
*/
function isBidRequestValid(bid) {
const notifyId = getNotifyId(bid.params);
if (!isValidNotifyId(notifyId)) {
log(`invalid notifyId format, got "${notifyId}"`);
return false;
}
if (notifyId !== window.sublime.notifyId) {
log(`notifyId mismatch: params [${bid.params.notifyId}] / sublime [${window.sublime.notifyId}]`);
return false;
}

return !!Number(bid.params.zoneId);
}

Expand Down Expand Up @@ -127,9 +167,11 @@ function buildRequests(validBidRequests, bidderRequest) {
const bidHost = bid.params.bidHost || DEFAULT_BID_HOST;
const protocol = bid.params.protocol || DEFAULT_PROTOCOL;

const notifyId = getNotifyId(bid.params);

setState({
transactionId: bid.transactionId,
notifyId: bid.params.notifyId,
notifyId,
zoneId: bid.params.zoneId,
debug: bid.params.debug || false,
});
Expand All @@ -146,7 +188,7 @@ function buildRequests(validBidRequests, bidderRequest) {
h: size[1],
})),
transactionId: bid.transactionId,
notifyId: bid.params.notifyId,
notifyId,
zoneId: bid.params.zoneId,
};

Expand Down Expand Up @@ -250,15 +292,18 @@ export const spec = {
code: BIDDER_CODE,
gvlid: BIDDER_GVLID,
aliases: [],
isBidRequestValid: isBidRequestValid,
buildRequests: buildRequests,
interpretResponse: interpretResponse,
onBidWon: onBidWon,
onTimeout: onTimeout,
isBidRequestValid,
buildRequests,
interpretResponse,
onBidWon,
onTimeout,
// Exposed for test purpose
sendEvent: sendEvent,
setState: setState,
state: state,
sendEvent,
setState,
state,
detectDevice,
getNotifyId,
isValidNotifyId,
};

registerBidder(spec);
Loading

0 comments on commit fd06f9c

Please sign in to comment.