Skip to content

Commit 7ba2d1d

Browse files
committed
feat(oauth): make Campsite API session cookie name configurable
Allow per-environment overrides of the Campsite session cookie used by the web UI and mono OAuth store, defaulting to _campsite_api_session.
1 parent 165f817 commit 7ba2d1d

14 files changed

Lines changed: 57 additions & 26 deletions

File tree

common/src/config/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,14 @@ pub struct OauthConfig {
660660
#[serde(default)]
661661
pub api_store_backend: OauthApiStoreBackend,
662662
pub allowed_cors_origins: Vec<String>,
663+
#[serde(default = "default_campsite_api_session_cookie")]
664+
pub campsite_api_session_cookie: String,
665+
}
666+
667+
pub const DEFAULT_CAMPSITE_API_SESSION_COOKIE: &str = "_campsite_api_session";
668+
669+
fn default_campsite_api_session_cookie() -> String {
670+
DEFAULT_CAMPSITE_API_SESSION_COOKIE.to_string()
663671
}
664672

665673
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
@@ -685,6 +693,7 @@ impl Default for OauthConfig {
685693
.into_iter()
686694
.map(|s| s.to_string())
687695
.collect(),
696+
campsite_api_session_cookie: default_campsite_api_session_cookie(),
688697
}
689698
}
690699
}

docker/demo/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ MEGA_BUILD__ENABLE_BUILD=true
9999
MEGA_BUILD__ORION_SERVER=http://orion_server:8004
100100

101101
MEGA_OAUTH__CAMPSITE_API_DOMAIN=http://api.gitmono.local:18080
102+
MEGA_OAUTH__CAMPSITE_API_SESSION_COOKIE=_campsite_api_session
102103
MEGA_OAUTH__UI_DOMAIN=http://app.gitmono.local
103104
MEGA_OAUTH__COOKIE_DOMAIN=gitmono.local
104105
MEGA_OAUTH__ALLOWED_CORS_ORIGINS="http://app.gitmono.local"

docker/demo/docker-compose.demo.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ services:
150150

151151
# Campsite integration (OAuth / SSO etc.)
152152
MEGA_OAUTH__CAMPSITE_API_DOMAIN: ${MEGA_OAUTH__CAMPSITE_API_DOMAIN:-http://campsite_api:8080}
153+
MEGA_OAUTH__CAMPSITE_API_SESSION_COOKIE: ${MEGA_OAUTH__CAMPSITE_API_SESSION_COOKIE:-_campsite_api_session}
153154
MEGA_OAUTH__UI_DOMAIN: ${MEGA_OAUTH__UI_DOMAIN:-http://app.gitmono.local}
154155
MEGA_OAUTH__COOKIE_DOMAIN: ${MEGA_OAUTH__COOKIE_DOMAIN:-localhost}
155156
# Note: allowed_cors_origins expects an array format in TOML

docker/deployment/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ MEGA_BUILD__ENABLE_BUILD=true
9292
MEGA_BUILD__ORION_SERVER=http://orion_server:8004
9393

9494
MEGA_OAUTH__CAMPSITE_API_DOMAIN=http://api.gitmono.local:18080
95+
MEGA_OAUTH__CAMPSITE_API_SESSION_COOKIE=_campsite_api_session
9596
MEGA_OAUTH__UI_DOMAIN=http://app.gitmono.local
9697
MEGA_OAUTH__COOKIE_DOMAIN=gitmono.local
9798
MEGA_OAUTH__ALLOWED_CORS_ORIGINS="http://app.gitmono.local"

mono/src/api/oauth/api_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub enum OAuthApiStore {
1616
}
1717

1818
impl OAuthApiStore {
19-
pub fn session_cookie_name(&self) -> &'static str {
19+
pub fn session_cookie_name(&self) -> &str {
2020
match self {
2121
OAuthApiStore::Campsite(store) => store.session_cookie_name(),
2222
OAuthApiStore::Tinyship(store) => store.session_cookie_name(),

mono/src/api/oauth/campsite_store.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ use tower_sessions::{
1212

1313
use crate::api::oauth::model::{CampsiteUserJson, LoginUser};
1414

15-
static CAMPSITE_API_COOKIE: &str = "_campsite_api_session";
16-
1715
#[derive(Debug, Clone)]
1816
pub struct CampsiteApiStore {
1917
client: Arc<Client>,
20-
// cookie_store: Arc<Jar>,
2118
api_base_url: String,
19+
session_cookie: String,
2220
}
2321

2422
#[async_trait]
@@ -41,18 +39,19 @@ impl SessionStore for CampsiteApiStore {
4139
}
4240

4341
impl CampsiteApiStore {
44-
pub fn session_cookie_name(&self) -> &'static str {
45-
CAMPSITE_API_COOKIE
42+
pub fn session_cookie_name(&self) -> &str {
43+
&self.session_cookie
4644
}
4745

48-
pub fn new(api_base_url: String) -> Self {
46+
pub fn new(api_base_url: String, session_cookie: String) -> Self {
4947
let client = Client::builder()
5048
.no_proxy()
5149
.build()
5250
.expect("Failed to build client");
5351
Self {
5452
client: Arc::new(client),
5553
api_base_url,
54+
session_cookie,
5655
}
5756
}
5857

@@ -68,7 +67,10 @@ impl CampsiteApiStore {
6867
let resp = self
6968
.client
7069
.get(url)
71-
.header(COOKIE, format!("{}={}", CAMPSITE_API_COOKIE, cookie_value))
70+
.header(
71+
COOKIE,
72+
format!("{}={}", self.session_cookie_name(), cookie_value),
73+
)
7274
.send()
7375
.await
7476
.context("failed to send request to campsite API")?;

mono/src/server/http_server.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,12 @@ pub async fn app(ctx: AppContext, host: String, port: u16) -> Router {
396396
git_object_cache,
397397
build_dispatch,
398398
Some(match oauth_config.api_store_backend {
399-
common::config::OauthApiStoreBackend::Campsite => {
400-
OAuthApiStore::Campsite(CampsiteApiStore::new(oauth_config.campsite_api_domain))
401-
}
399+
common::config::OauthApiStoreBackend::Campsite => OAuthApiStore::Campsite(
400+
CampsiteApiStore::new(
401+
oauth_config.campsite_api_domain.clone(),
402+
oauth_config.campsite_api_session_cookie.clone(),
403+
),
404+
),
402405
common::config::OauthApiStoreBackend::Tinyship => {
403406
OAuthApiStore::Tinyship(TinyshipApiStore::new(oauth_config.tinyship_api_domain))
404407
}

mono/tests/campsite_api_store_tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ use common::*;
4141
use qlean::{Distro, GuestArch, Image, ImageConfig, MachineConfig, with_machine};
4242
use serde_json::Value;
4343

44+
use common::config::DEFAULT_CAMPSITE_API_SESSION_COOKIE;
45+
4446
const TEST_COOKIE: &str = "test_session_cookie";
45-
const CAMPSITE_API_COOKIE_NAME: &str = "_campsite_api_session";
4647

4748
// ============================================================================
4849
// Test phases - directly calling Campsite API
@@ -54,7 +55,7 @@ async fn call_campsite_api(
5455
cookie: Option<&str>,
5556
) -> Result<(u16, Value)> {
5657
let cookie_arg = cookie
57-
.map(|c| format!("Cookie: {}={}", CAMPSITE_API_COOKIE_NAME, c))
58+
.map(|c| format!("Cookie: {}={}", DEFAULT_CAMPSITE_API_SESSION_COOKIE, c))
5859
.unwrap_or_default();
5960

6061
let cmd = if cookie.is_some() {

mono/tests/login_user_extractor_tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use std::time::Duration;
3636

3737
use anyhow::{Context, Result};
3838
use common::*;
39+
use common::config::DEFAULT_CAMPSITE_API_SESSION_COOKIE;
3940
use qlean::{Distro, GuestArch, Image, ImageConfig, MachineConfig, with_machine};
4041
use serde_json::Value;
4142

@@ -47,11 +48,11 @@ const CAMPSITE_API_PORT: u16 = 8080;
4748

4849
/// Call Campsite API /v1/users/me endpoint
4950
async fn call_users_me(vm: &mut qlean::Machine, cookie: &str) -> Result<(u16, Option<Value>)> {
50-
// Format cookie with prefix: _campsite_api_session=<value>
51+
// Format cookie with prefix: {session_cookie}=<value>
5152
let cookie_header = if cookie.is_empty() {
5253
"".to_string()
5354
} else {
54-
format!("_campsite_api_session={}", cookie)
55+
format!("{}={}", DEFAULT_CAMPSITE_API_SESSION_COOKIE, cookie)
5556
};
5657

5758
let cmd = if cookie.is_empty() {

moon/apps/web/.env.runtime

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# Provide the real values at runtime (ECS task definition / Cloud Run env):
1313
# NEXT_PUBLIC_API_URL, NEXT_PUBLIC_INTERNAL_API_URL, NEXT_PUBLIC_MONO_API_URL,
1414
# NEXT_PUBLIC_ORION_API_URL, NEXT_PUBLIC_AUTH_URL, NEXT_PUBLIC_WEB_URL,
15-
# NEXT_PUBLIC_SYNC_URL, NEXT_PUBLIC_CRATES_PRO_URL
15+
# NEXT_PUBLIC_SYNC_URL, NEXT_PUBLIC_CRATES_PRO_URL,
16+
# NEXT_PUBLIC_CAMPSITE_API_SESSION_COOKIE
1617
# =============================================================================
1718

1819
NEXT_PUBLIC_API_URL=https://rt-api.placeholder.local
@@ -23,3 +24,4 @@ NEXT_PUBLIC_AUTH_URL=https://rt-auth.placeholder.local
2324
NEXT_PUBLIC_WEB_URL=https://rt-web.placeholder.local
2425
NEXT_PUBLIC_SYNC_URL=wss://rt-sync.placeholder.local
2526
NEXT_PUBLIC_CRATES_PRO_URL=https://rt-crates-pro.placeholder.local
27+
NEXT_PUBLIC_CAMPSITE_API_SESSION_COOKIE=__rt_campsite_api_session_cookie__

0 commit comments

Comments
 (0)