Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ futures = "0.3"
tokio = { version = "1.0", features = ["sync", "macros", "io-util", "rt", "time"] }
url = "2.4.1"
config = "0.15.11"
handlebars = "6.3.2"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ git clone [email protected]:IABTechLab/trusted-server.git
:information_source: Note that you’ll have to edit the following files for your setup:

- fastly.toml (service ID, author, description)
- Potsi.toml (KV store ID names)
- potsi.toml (KV store ID names)

### Build

Expand Down
8 changes: 8 additions & 0 deletions potsi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ server_url = "http://68.183.113.79:8000/openrtb2/auction"
counter_store = "jevans_synth_id_counter"
opid_store = "jevans_synth_id_opid"
secret_key = "potsi"
# Possible values
# - "client_ip"
# - "user_agent"
# - "first_party_id"
# - "auth_user_id"
# - "publisher_domain"
# - "accept_language"
template = "{{ client_ip }}:{{ user_agent }}:{{ first_party_id }}:{{ auth_user_id }}:{{ publisher_domain }}:{{ accept_language }}"
5 changes: 3 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub const SYNTH_HEADER_FRESH: &str = "X-Synthetic-Fresh";
pub const SYNTH_HEADER_POTSI: &str = "X-Synthetic-Potsi";
pub const SYNTHETIC_HEADER_FRESH: &str = "X-Synthetic-Fresh";
pub const SYNTHETIC_HEADER_POTSI: &str = "X-Synthetic-Potsi";
pub const SYNTHETIC_HEADER_PUB_USER_ID: &str = "X-Pub-User-ID";
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::env;

mod constants;
mod cookies;
use constants::{SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI};
use constants::{SYNTHETIC_HEADER_FRESH, SYNTHETIC_HEADER_POTSI};
mod models;
use models::AdResponse;
mod prebid;
Expand Down Expand Up @@ -60,7 +60,7 @@ fn handle_main_page(settings: &Settings, req: Request) -> Result<Response, Error

println!(
"Existing POTSI header: {:?}",
req.get_header(SYNTH_HEADER_POTSI)
req.get_header(SYNTHETIC_HEADER_POTSI)
);
println!("Generated Fresh ID: {}", fresh_id);
println!("Using POTSI ID: {}", synthetic_id);
Expand All @@ -69,8 +69,8 @@ fn handle_main_page(settings: &Settings, req: Request) -> Result<Response, Error
let mut response = Response::from_status(StatusCode::OK)
.with_body(HTML_TEMPLATE)
.with_header(header::CONTENT_TYPE, "text/html")
.with_header(SYNTH_HEADER_FRESH, &fresh_id) // Fresh ID always changes
.with_header(SYNTH_HEADER_POTSI, &synthetic_id); // POTSI ID remains stable
.with_header(SYNTHETIC_HEADER_FRESH, &fresh_id) // Fresh ID always changes
.with_header(SYNTHETIC_HEADER_POTSI, &synthetic_id); // POTSI ID remains stable

// Always set the cookie with the synthetic ID
response.set_header(
Expand Down Expand Up @@ -312,14 +312,14 @@ async fn handle_prebid_test(settings: &Settings, mut req: Request) -> Result<Res

println!(
"Existing POTSI header: {:?}",
req.get_header(SYNTH_HEADER_POTSI)
req.get_header(SYNTHETIC_HEADER_POTSI)
);
println!("Generated Fresh ID: {}", fresh_id);
println!("Using POTSI ID: {}", synthetic_id);

// Set both IDs as headers
req.set_header(SYNTH_HEADER_FRESH, &fresh_id);
req.set_header(SYNTH_HEADER_POTSI, &synthetic_id);
req.set_header(SYNTHETIC_HEADER_FRESH, &fresh_id);
req.set_header(SYNTHETIC_HEADER_POTSI, &synthetic_id);

println!("Using POTSI ID: {}, Fresh ID: {}", synthetic_id, fresh_id);

Expand All @@ -344,7 +344,7 @@ async fn handle_prebid_test(settings: &Settings, mut req: Request) -> Result<Res

println!("Attempting to send bid request to Prebid Server at prebid_backend");

match prebid_req.send_bid_request(&req).await {
match prebid_req.send_bid_request(settings, &req).await {
// Pass the original request
Ok(mut prebid_response) => {
println!("Received response from Prebid Server");
Expand Down
18 changes: 11 additions & 7 deletions src/prebid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use fastly::http::{header, Method};
use fastly::{Error, Request, Response};
use serde_json::json;

use crate::constants::{SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI};
use crate::constants::{SYNTHETIC_HEADER_FRESH, SYNTHETIC_HEADER_POTSI};
use crate::settings::Settings;
use crate::synthetic::generate_synthetic_id;

Expand Down Expand Up @@ -31,7 +31,7 @@ impl PrebidRequest {
pub fn new(settings: &Settings, req: &Request) -> Result<Self, Error> {
// Get the POTSI ID from header (which we just set in handle_prebid_test)
let synthetic_id = req
.get_header(SYNTH_HEADER_POTSI)
.get_header(SYNTHETIC_HEADER_POTSI)
.and_then(|h| h.to_str().ok())
.map(|s| s.to_string())
.unwrap_or_else(|| generate_synthetic_id(settings, req));
Expand Down Expand Up @@ -89,12 +89,16 @@ impl PrebidRequest {
///
/// # Returns
/// * `Result<Response, Error>` - Prebid Server response or error
pub async fn send_bid_request(&self, incoming_req: &Request) -> Result<Response, Error> {
let mut req = Request::new(Method::POST, "http://68.183.113.79:8000/openrtb2/auction");
pub async fn send_bid_request(
&self,
settings: &Settings,
incoming_req: &Request,
) -> Result<Response, Error> {
let mut req = Request::new(Method::POST, settings.prebid.server_url.to_owned());

// Get and store the POTSI ID value from the incoming request
let potsi_id = incoming_req
.get_header(SYNTH_HEADER_POTSI)
.get_header(SYNTHETIC_HEADER_POTSI)
.and_then(|h| h.to_str().ok())
.map(|s| s.to_string())
.unwrap_or_else(|| self.synthetic_id.clone());
Expand Down Expand Up @@ -165,8 +169,8 @@ impl PrebidRequest {
req.set_header(header::CONTENT_TYPE, "application/json");
req.set_header("X-Forwarded-For", &self.client_ip);
req.set_header(header::ORIGIN, &self.origin);
req.set_header(SYNTH_HEADER_FRESH, &self.synthetic_id);
req.set_header(SYNTH_HEADER_POTSI, &potsi_id);
req.set_header(SYNTHETIC_HEADER_FRESH, &self.synthetic_id);
req.set_header(SYNTHETIC_HEADER_POTSI, &potsi_id);

println!(
"Sending prebid request with Fresh ID: {} and POTSI ID: {}",
Expand Down
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Synthetic {
pub counter_store: String,
pub opid_store: String,
pub secret_key: String,
pub template: String,
}

#[derive(Debug, Deserialize)]
Expand Down
Loading