@@ -3,8 +3,10 @@ use fastly::Request;
33use hmac:: { Hmac , Mac } ;
44use log;
55use sha2:: Sha256 ;
6+ use handlebars:: Handlebars ;
7+ use serde_json:: json;
68
7- use crate :: constants:: SYNTH_HEADER_POTSI ;
9+ use crate :: constants:: { SYNTHETIC_HEADER_POTSI , SYNTHETIC_HEADER_PUB_USER_ID } ;
810use crate :: cookies:: handle_request_cookies;
911use crate :: settings:: Settings ;
1012
@@ -14,34 +16,36 @@ type HmacSha256 = Hmac<Sha256>;
1416pub fn generate_synthetic_id ( settings : & Settings , req : & Request ) -> String {
1517 let user_agent = req
1618 . get_header ( header:: USER_AGENT )
17- . map ( |h| h. to_str ( ) . unwrap_or ( "Unknown " ) ) ;
19+ . map ( |h| h. to_str ( ) . unwrap_or ( "unknown " ) ) ;
1820 let first_party_id = handle_request_cookies ( req) . and_then ( |jar| {
1921 jar. get ( "pub_userid" )
2022 . map ( |cookie| cookie. value ( ) . to_string ( ) )
2123 } ) ;
2224 let auth_user_id = req
23- . get_header ( "X-Pub-User-ID" )
25+ . get_header ( SYNTHETIC_HEADER_PUB_USER_ID )
2426 . map ( |h| h. to_str ( ) . unwrap_or ( "anonymous" ) ) ;
2527 let publisher_domain = req
2628 . get_header ( header:: HOST )
27- . map ( |h| h. to_str ( ) . unwrap_or ( "unknown.com " ) ) ;
29+ . map ( |h| h. to_str ( ) . unwrap_or ( "unknown" ) ) ;
2830 let client_ip = req. get_client_ip_addr ( ) . map ( |ip| ip. to_string ( ) ) ;
2931 let accept_language = req
3032 . get_header ( header:: ACCEPT_LANGUAGE )
3133 . and_then ( |h| h. to_str ( ) . ok ( ) )
3234 . map ( |lang| lang. split ( ',' ) . next ( ) . unwrap_or ( "unknown" ) ) ;
3335
34- let input_string = format ! (
35- "{}:{}:{}:{}:{}:{}" ,
36- client_ip. unwrap_or( "unknown" . to_string( ) ) ,
37- user_agent. unwrap_or( "unknown" ) ,
38- first_party_id. unwrap_or( "anonymous" . to_string( ) ) ,
39- auth_user_id. unwrap_or( "anonymous" ) ,
40- publisher_domain. unwrap_or( "unknown.com" ) ,
41- accept_language. unwrap_or( "unknown" )
42- ) ;
36+ let handlebars = Handlebars :: new ( ) ;
37+ let data = & json ! ( {
38+ "client_ip" : client_ip. unwrap_or( "unknown" . to_string( ) ) ,
39+ "user_agent" : user_agent. unwrap_or( "unknown" ) ,
40+ "first_party_id" : first_party_id. unwrap_or( "anonymous" . to_string( ) ) ,
41+ "auth_user_id" : auth_user_id. unwrap_or( "anonymous" ) ,
42+ "publisher_domain" : publisher_domain. unwrap_or( "unknown.com" ) ,
43+ "accept_language" : accept_language. unwrap_or( "unknown" )
44+ } ) ;
45+
46+ let input_string = handlebars. render_template ( & settings. synthetic . template , data) . unwrap ( ) ;
47+ println ! ( "Input string for fresh ID: {} {}" , input_string, data) ;
4348
44- log:: info!( "Input string for fresh ID: {}" , input_string) ;
4549
4650 let mut mac = HmacSha256 :: new_from_slice ( settings. synthetic . secret_key . as_bytes ( ) )
4751 . expect ( "HMAC can take key of any size" ) ;
@@ -57,7 +61,7 @@ pub fn generate_synthetic_id(settings: &Settings, req: &Request) -> String {
5761pub fn get_or_generate_synthetic_id ( settings : & Settings , req : & Request ) -> String {
5862 // First try to get existing POTSI ID from header
5963 if let Some ( potsi) = req
60- . get_header ( SYNTH_HEADER_POTSI )
64+ . get_header ( SYNTHETIC_HEADER_POTSI )
6165 . and_then ( |h| h. to_str ( ) . ok ( ) )
6266 . map ( |s| s. to_string ( ) )
6367 {
@@ -113,6 +117,7 @@ mod tests {
113117 counter_store : "https://example.com" . to_string ( ) ,
114118 opid_store : "https://example.com" . to_string ( ) ,
115119 secret_key : "secret_key" . to_string ( ) ,
120+ template : "{{ client_ip }}:{{ user_agent }}:{{ first_party_id }}:{{ auth_user_id }}:{{ publisher_domain }}:{{ accept_language }}" . to_string ( ) ,
116121 } ,
117122 }
118123 }
@@ -129,6 +134,7 @@ mod tests {
129134 ] ) ;
130135
131136 let synthetic_id = generate_synthetic_id ( & settings, & req) ;
137+ print ! ( "Generated synthetic ID: {}" , synthetic_id) ;
132138 assert_eq ! (
133139 synthetic_id,
134140 "07cd73bb8c7db39753ab6b10198b10c3237a3f5a6d2232c6ce578f2c2a623e56"
@@ -138,7 +144,7 @@ mod tests {
138144 #[ test]
139145 fn test_get_or_generate_synthetic_id_with_header ( ) {
140146 let settings = create_settings ( ) ;
141- let req = create_test_request ( vec ! [ ( SYNTH_HEADER_POTSI , "existing_potsi_id" ) ] ) ;
147+ let req = create_test_request ( vec ! [ ( SYNTHETIC_HEADER_POTSI , "existing_potsi_id" ) ] ) ;
142148
143149 let synthetic_id = get_or_generate_synthetic_id ( & settings, & req) ;
144150 assert_eq ! ( synthetic_id, "existing_potsi_id" ) ;
0 commit comments