Skip to content

Commit 8d18867

Browse files
authored
Include ecosystem headers in auth flow (#105)
Add support for propagating ecosystem identifiers through the auth flow. Introduce constants and a helper (extract_ecosystem_headers) to read x-ecosystem-id and x-ecosystem-partner-id from request headers in server extractors, and pass them into ThirdwebClientIdAndServiceKey. Extend ThirdwebClientIdAndServiceKey with optional ecosystem_id and ecosystem_partner_id (with serde defaults), and emit these headers when building request headers in ThirdwebAuth::ClientIdServiceKey. This preserves backward compatibility while allowing ecosystem metadata to be forwarded with auth credentials.
1 parent 1f4db8d commit 8d18867

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

server/src/http/extractors.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ const HEADER_AWS_KMS_ARN: &str = "x-aws-kms-arn";
2525
const HEADER_AWS_ACCESS_KEY_ID: &str = "x-aws-access-key-id";
2626
const HEADER_AWS_SECRET_ACCESS_KEY: &str = "x-aws-secret-access-key";
2727
const HEADER_DIAGNOSTIC_ACCESS_PASSWORD: &str = "x-diagnostic-access-password";
28+
const HEADER_ECOSYSTEM_ID: &str = "x-ecosystem-id";
29+
const HEADER_ECOSYSTEM_PARTNER_ID: &str = "x-ecosystem-partner-id";
30+
31+
fn extract_ecosystem_headers(parts: &Parts) -> (Option<String>, Option<String>) {
32+
let ecosystem_id = parts
33+
.headers
34+
.get(HEADER_ECOSYSTEM_ID)
35+
.and_then(|v| v.to_str().ok())
36+
.map(str::to_string);
37+
let ecosystem_partner_id = parts
38+
.headers
39+
.get(HEADER_ECOSYSTEM_PARTNER_ID)
40+
.and_then(|v| v.to_str().ok())
41+
.map(str::to_string);
42+
(ecosystem_id, ecosystem_partner_id)
43+
}
2844

2945
/// Extractor for RPC credentials from headers
3046
#[derive(OperationIo)]
@@ -70,10 +86,14 @@ where
7086
})
7187
})?;
7288

89+
let (ecosystem_id, ecosystem_partner_id) = extract_ecosystem_headers(parts);
90+
7391
Ok(RpcCredentialsExtractor(RpcCredentials::Thirdweb(
7492
ThirdwebAuth::ClientIdServiceKey(thirdweb_core::auth::ThirdwebClientIdAndServiceKey {
7593
client_id: client_id.to_string(),
7694
service_key: service_key.to_string(),
95+
ecosystem_id,
96+
ecosystem_partner_id,
7797
}),
7898
)))
7999
}
@@ -231,10 +251,14 @@ impl SigningCredentialsExtractor {
231251
})
232252
})?;
233253

254+
let (ecosystem_id, ecosystem_partner_id) = extract_ecosystem_headers(parts);
255+
234256
let thirdweb_auth = ThirdwebAuth::ClientIdServiceKey(
235257
thirdweb_core::auth::ThirdwebClientIdAndServiceKey {
236258
client_id: client_id.to_string(),
237259
service_key: service_key.to_string(),
260+
ecosystem_id,
261+
ecosystem_partner_id,
238262
},
239263
);
240264

thirdweb-core/src/auth.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ use crate::error::ThirdwebError;
77
pub struct ThirdwebClientIdAndServiceKey {
88
pub client_id: String,
99
pub service_key: String,
10+
#[serde(default, skip_serializing_if = "Option::is_none")]
11+
pub ecosystem_id: Option<String>,
12+
#[serde(default, skip_serializing_if = "Option::is_none")]
13+
pub ecosystem_partner_id: Option<String>,
1014
}
1115

1216
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -30,6 +34,21 @@ impl ThirdwebAuth {
3034
HeaderValue::from_str(&creds.service_key)
3135
.map_err(|_| ThirdwebError::header_value(creds.client_id.clone()))?,
3236
);
37+
if let Some(ecosystem_id) = &creds.ecosystem_id {
38+
headers.insert(
39+
"x-ecosystem-id",
40+
HeaderValue::from_str(ecosystem_id)
41+
.map_err(|_| ThirdwebError::header_value(ecosystem_id.clone()))?,
42+
);
43+
}
44+
if let Some(ecosystem_partner_id) = &creds.ecosystem_partner_id {
45+
headers.insert(
46+
"x-ecosystem-partner-id",
47+
HeaderValue::from_str(ecosystem_partner_id).map_err(|_| {
48+
ThirdwebError::header_value(ecosystem_partner_id.clone())
49+
})?,
50+
);
51+
}
3352
Ok(headers)
3453
}
3554
ThirdwebAuth::SecretKey(secret_key) => {

0 commit comments

Comments
 (0)