@@ -118,6 +118,16 @@ struct PushFramesResult {
118
118
baton : Option < String > ,
119
119
}
120
120
121
+ #[ derive( Debug , Clone ) ]
122
+ pub struct EncryptionContext {
123
+ /// The base64-encoded key for the encryption, sent on every request.
124
+ pub key_16_bytes_base64_encoded : String ,
125
+ /// Whether the pushed frames are already encrypted.
126
+ pub push_is_encrypted : bool ,
127
+ /// Whether to request the server to decrypt the pulled frames.
128
+ pub decrypt_pull : bool ,
129
+ }
130
+
121
131
pub struct SyncContext {
122
132
db_path : String ,
123
133
client : hyper:: Client < ConnectorService , Body > ,
@@ -133,6 +143,8 @@ pub struct SyncContext {
133
143
/// whenever sync is called very first time, we will call the remote server
134
144
/// to get the generation information and sync the db file if needed
135
145
initial_server_sync : bool ,
146
+ /// The encryption context for the sync.
147
+ remote_encryption : Option < EncryptionContext > ,
136
148
}
137
149
138
150
impl SyncContext {
@@ -141,6 +153,7 @@ impl SyncContext {
141
153
db_path : String ,
142
154
sync_url : String ,
143
155
auth_token : Option < String > ,
156
+ remote_encryption : Option < EncryptionContext > ,
144
157
) -> Result < Self > {
145
158
let client = hyper:: client:: Client :: builder ( ) . build :: < _ , hyper:: Body > ( connector) ;
146
159
@@ -163,6 +176,7 @@ impl SyncContext {
163
176
durable_generation : 0 ,
164
177
durable_frame_num : 0 ,
165
178
initial_server_sync : false ,
179
+ remote_encryption,
166
180
} ;
167
181
168
182
if let Err ( e) = me. read_metadata ( ) . await {
@@ -303,6 +317,16 @@ impl SyncContext {
303
317
None => { }
304
318
}
305
319
320
+ if let Some ( remote_encryption) = & self . remote_encryption {
321
+ if remote_encryption. decrypt_pull {
322
+ req = req. header ( "x-turso-decrypt-response" , "true" ) ;
323
+ }
324
+ if remote_encryption. push_is_encrypted {
325
+ req = req. header ( "x-turso-encrypted-request" , "true" ) ;
326
+ }
327
+ req = req. header ( "x-turso-encryption-key" , remote_encryption. key_16_bytes_base64_encoded . as_str ( ) ) ;
328
+ }
329
+
306
330
let req = req. body ( body. clone ( ) . into ( ) ) . expect ( "valid body" ) ;
307
331
308
332
let res = self
@@ -414,6 +438,16 @@ impl SyncContext {
414
438
None => { }
415
439
}
416
440
441
+ if let Some ( remote_encryption) = & self . remote_encryption {
442
+ if remote_encryption. decrypt_pull {
443
+ req = req. header ( "x-turso-decrypt-response" , "true" ) ;
444
+ }
445
+ if remote_encryption. push_is_encrypted {
446
+ req = req. header ( "x-turso-encrypted-request" , "true" ) ;
447
+ }
448
+ req = req. header ( "x-turso-encryption-key" , remote_encryption. key_16_bytes_base64_encoded . as_str ( ) ) ;
449
+ }
450
+
417
451
let req = req. body ( Body :: empty ( ) ) . expect ( "valid request" ) ;
418
452
419
453
let res = self
@@ -577,6 +611,16 @@ impl SyncContext {
577
611
req = req. header ( "Authorization" , auth_token) ;
578
612
}
579
613
614
+ if let Some ( remote_encryption) = & self . remote_encryption {
615
+ if remote_encryption. decrypt_pull {
616
+ req = req. header ( "x-turso-decrypt-response" , "true" ) ;
617
+ }
618
+ if remote_encryption. push_is_encrypted {
619
+ req = req. header ( "x-turso-encrypted-request" , "true" ) ;
620
+ }
621
+ req = req. header ( "x-turso-encryption-key" , remote_encryption. key_16_bytes_base64_encoded . as_str ( ) ) ;
622
+ }
623
+
580
624
let req = req. body ( Body :: empty ( ) ) . expect ( "valid request" ) ;
581
625
582
626
let res = self
@@ -673,6 +717,16 @@ impl SyncContext {
673
717
req = req. header ( "Authorization" , auth_token) ;
674
718
}
675
719
720
+ if let Some ( remote_encryption) = & self . remote_encryption {
721
+ if remote_encryption. decrypt_pull {
722
+ req = req. header ( "x-turso-decrypt-response" , "true" ) ;
723
+ }
724
+ if remote_encryption. push_is_encrypted {
725
+ req = req. header ( "x-turso-encrypted-request" , "true" ) ;
726
+ }
727
+ req = req. header ( "x-turso-encryption-key" , remote_encryption. key_16_bytes_base64_encoded . as_str ( ) ) ;
728
+ }
729
+
676
730
let req = req. body ( Body :: empty ( ) ) . expect ( "valid request" ) ;
677
731
678
732
let ( res, http_duration) =
0 commit comments