|
1 | 1 | use crate::{local::Connection, util::ConnectorService, Error, Result};
|
2 | 2 |
|
3 |
| -use std::path::Path; |
4 |
| - |
| 3 | +use base64::engine::general_purpose; |
| 4 | +use base64::Engine; |
5 | 5 | use bytes::Bytes;
|
6 | 6 | use chrono::Utc;
|
7 | 7 | use http::{HeaderValue, StatusCode};
|
8 | 8 | use hyper::Body;
|
| 9 | +use std::path::Path; |
9 | 10 | use tokio::io::AsyncWriteExt as _;
|
10 | 11 | use uuid::Uuid;
|
11 | 12 |
|
@@ -118,10 +119,27 @@ struct PushFramesResult {
|
118 | 119 | baton: Option<String>,
|
119 | 120 | }
|
120 | 121 |
|
| 122 | +#[derive(Debug, Clone)] |
| 123 | +pub enum EncryptionKey { |
| 124 | + /// The key is a base64-encoded string. |
| 125 | + Base64Encoded(String), |
| 126 | + /// The key is a byte array. |
| 127 | + Bytes(Vec<u8>), |
| 128 | +} |
| 129 | + |
| 130 | +impl EncryptionKey { |
| 131 | + pub fn as_string(&self) -> String { |
| 132 | + match self { |
| 133 | + EncryptionKey::Base64Encoded(s) => s.clone(), |
| 134 | + EncryptionKey::Bytes(b) => general_purpose::STANDARD.encode(b), |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
121 | 139 | #[derive(Debug, Clone)]
|
122 | 140 | pub struct EncryptionContext {
|
123 | 141 | /// The base64-encoded key for the encryption, sent on every request.
|
124 |
| - pub key_32_bytes_base64_encoded: String, |
| 142 | + pub key: EncryptionKey, |
125 | 143 | }
|
126 | 144 |
|
127 | 145 | pub struct SyncContext {
|
@@ -314,10 +332,7 @@ impl SyncContext {
|
314 | 332 | }
|
315 | 333 |
|
316 | 334 | if let Some(remote_encryption) = &self.remote_encryption {
|
317 |
| - req = req.header( |
318 |
| - "x-turso-encryption-key", |
319 |
| - remote_encryption.key_32_bytes_base64_encoded.as_str(), |
320 |
| - ); |
| 335 | + req = req.header("x-turso-encryption-key", remote_encryption.key.as_string()); |
321 | 336 | }
|
322 | 337 |
|
323 | 338 | let req = req.body(body.clone().into()).expect("valid body");
|
@@ -432,10 +447,7 @@ impl SyncContext {
|
432 | 447 | }
|
433 | 448 |
|
434 | 449 | if let Some(remote_encryption) = &self.remote_encryption {
|
435 |
| - req = req.header( |
436 |
| - "x-turso-encryption-key", |
437 |
| - remote_encryption.key_32_bytes_base64_encoded.as_str(), |
438 |
| - ); |
| 450 | + req = req.header("x-turso-encryption-key", remote_encryption.key.as_string()); |
439 | 451 | }
|
440 | 452 |
|
441 | 453 | let req = req.body(Body::empty()).expect("valid request");
|
@@ -602,10 +614,7 @@ impl SyncContext {
|
602 | 614 | }
|
603 | 615 |
|
604 | 616 | if let Some(remote_encryption) = &self.remote_encryption {
|
605 |
| - req = req.header( |
606 |
| - "x-turso-encryption-key", |
607 |
| - remote_encryption.key_32_bytes_base64_encoded.as_str(), |
608 |
| - ); |
| 617 | + req = req.header("x-turso-encryption-key", remote_encryption.key.as_string()); |
609 | 618 | }
|
610 | 619 |
|
611 | 620 | let req = req.body(Body::empty()).expect("valid request");
|
@@ -705,10 +714,7 @@ impl SyncContext {
|
705 | 714 | }
|
706 | 715 |
|
707 | 716 | if let Some(remote_encryption) = &self.remote_encryption {
|
708 |
| - req = req.header( |
709 |
| - "x-turso-encryption-key", |
710 |
| - remote_encryption.key_32_bytes_base64_encoded.as_str(), |
711 |
| - ); |
| 717 | + req = req.header("x-turso-encryption-key", remote_encryption.key.as_string()); |
712 | 718 | }
|
713 | 719 |
|
714 | 720 | let req = req.body(Body::empty()).expect("valid request");
|
|
0 commit comments