This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 9247f89471 refactor(services/d1): Build bearer headers with 
reqsign-cloudflare (#9447)
9247f89471 is described below

commit 9247f894714b59fd0c6760bd6bc306da3780e28c
Author: Xuanwo <[email protected]>
AuthorDate: Tue Dec 1 02:18:35 2026 +0800

    refactor(services/d1): Build bearer headers with reqsign-cloudflare (#9447)
    
    refactor(services/d1): use Reqsign for bearer authorization
---
 core/Cargo.lock                 |  1 +
 core/services/d1/Cargo.toml     |  1 +
 core/services/d1/src/backend.rs | 12 +++++++-----
 core/services/d1/src/core.rs    | 15 +++++++++++----
 4 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/core/Cargo.lock b/core/Cargo.lock
index c0c0de3dae..6ba4814212 100644
--- a/core/Cargo.lock
+++ b/core/Cargo.lock
@@ -7817,6 +7817,7 @@ dependencies = [
  "futures",
  "http 1.5.0",
  "opendal-core",
+ "reqsign-cloudflare",
  "serde",
  "serde_json",
  "tokio",
diff --git a/core/services/d1/Cargo.toml b/core/services/d1/Cargo.toml
index 0950addbbf..ad54e78f2b 100644
--- a/core/services/d1/Cargo.toml
+++ b/core/services/d1/Cargo.toml
@@ -35,6 +35,7 @@ all-features = true
 bytes = { workspace = true }
 http = { workspace = true }
 opendal-core = { path = "../../core", version = "0.59.5", default-features = 
false }
+reqsign-cloudflare = { version = "1.0.0", default-features = false }
 serde = { workspace = true, features = ["derive"] }
 serde_json = { workspace = true }
 
diff --git a/core/services/d1/src/backend.rs b/core/services/d1/src/backend.rs
index 77b669b946..e6983fed39 100644
--- a/core/services/d1/src/backend.rs
+++ b/core/services/d1/src/backend.rs
@@ -122,11 +122,13 @@ impl Builder for D1Builder {
     type Config = D1Config;
 
     fn build(self) -> Result<impl Service> {
-        let mut authorization = None;
         let config = self.config;
-
-        if let Some(token) = config.token {
-            authorization = Some(format_authorization_by_bearer(&token)?)
+        // Preserve the build-time error for an explicitly empty token.
+        if config.token.as_deref() == Some("") {
+            return Err(Error::new(
+                ErrorKind::Unexpected,
+                "can't build authorization header with empty token",
+            ));
         }
 
         let Some(account_id) = config.account_id else {
@@ -168,7 +170,7 @@ impl Builder for D1Builder {
                 .as_str(),
         );
         Ok(D1Backend::new(D1Core {
-            authorization,
+            token: config.token,
             account_id,
             database_id,
             table,
diff --git a/core/services/d1/src/core.rs b/core/services/d1/src/core.rs
index e46ff16f00..253e59082a 100644
--- a/core/services/d1/src/core.rs
+++ b/core/services/d1/src/core.rs
@@ -20,6 +20,7 @@ use std::fmt::Debug;
 use http::Request;
 use http::StatusCode;
 use http::header;
+use reqsign_cloudflare::Credential;
 use serde_json::Value;
 
 use super::model::*;
@@ -28,7 +29,7 @@ use opendal_core::*;
 
 #[derive(Clone)]
 pub struct D1Core {
-    pub authorization: Option<String>,
+    pub(super) token: Option<String>,
     pub account_id: String,
     pub database_id: String,
 
@@ -74,8 +75,14 @@ impl D1Core {
         let url: String = format!("{}{}", CLOUDFLARE_API_BASE_URL, 
percent_encode_path(&p));
 
         let mut req = Request::post(&url);
-        if let Some(auth) = &self.authorization {
-            req = req.header(header::AUTHORIZATION, auth);
+        if let Some(token) = &self.token {
+            let authorization = Credential::new(token.as_str())
+                .and_then(|credential| credential.bearer_header_value())
+                .map_err(|err| {
+                    Error::new(ErrorKind::Unexpected, "building D1 
authorization header")
+                        .set_source(err)
+                })?;
+            req = req.header(header::AUTHORIZATION, authorization);
         }
         req = req.header(header::CONTENT_TYPE, "application/json");
 
@@ -328,7 +335,7 @@ mod tests {
 
     fn test_core(table: &str, key_field: &str, value_field: &str) -> D1Core {
         D1Core {
-            authorization: Some("Bearer d1-sentinel-secret".to_string()),
+            token: Some("d1-sentinel-secret".to_string()),
             account_id: "acc123".to_string(),
             database_id: "db456".to_string(),
             table: table.to_string(),

Reply via email to