Skip to content

Commit 4c2a864

Browse files
committed
Override config for Didomi integration
1 parent 4efe60b commit 4c2a864

File tree

4 files changed

+101
-5
lines changed

4 files changed

+101
-5
lines changed

crates/common/src/integrations/didomi.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::integrations::{IntegrationEndpoint, IntegrationProxy, IntegrationRegi
1414
use crate::settings::{IntegrationConfig, Settings};
1515

1616
const DIDOMI_INTEGRATION_ID: &str = "didomi";
17+
const DIDOMI_PREFIX: &str = "/didomi/consent";
1718

1819
/// Configuration for the Didomi consent notice reverse proxy.
1920
#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
@@ -179,8 +180,8 @@ pub fn register(settings: &Settings) -> Option<IntegrationRegistration> {
179180
impl IntegrationProxy for DidomiIntegration {
180181
fn routes(&self) -> Vec<IntegrationEndpoint> {
181182
vec![
182-
IntegrationEndpoint::get_prefix("/consent"),
183-
IntegrationEndpoint::post_prefix("/consent"),
183+
IntegrationEndpoint::get_prefix(DIDOMI_PREFIX),
184+
IntegrationEndpoint::post_prefix(DIDOMI_PREFIX),
184185
]
185186
}
186187

@@ -190,7 +191,7 @@ impl IntegrationProxy for DidomiIntegration {
190191
req: Request,
191192
) -> Result<Response, Report<TrustedServerError>> {
192193
let path = req.get_path();
193-
let consent_path = path.strip_prefix("/consent").unwrap_or(path);
194+
let consent_path = path.strip_prefix(DIDOMI_PREFIX).unwrap_or(path);
194195
let backend = self.backend_for_path(consent_path);
195196
let base_origin = match backend {
196197
DidomiBackend::Sdk => self.config.sdk_origin.as_str(),
@@ -271,8 +272,8 @@ mod tests {
271272
.expect("should insert config");
272273

273274
let registry = IntegrationRegistry::new(&settings);
274-
assert!(registry.has_route(&Method::GET, "/consent/loader.js"));
275-
assert!(registry.has_route(&Method::POST, "/consent/api/events"));
275+
assert!(registry.has_route(&Method::GET, "/didomi/consent/loader.js"));
276+
assert!(registry.has_route(&Method::POST, "/didomi/consent/api/events"));
276277
assert!(!registry.has_route(&Method::GET, "/other"));
277278
}
278279
}

crates/js/lib/package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { log } from '../../core/log';
2+
3+
const DEFAULT_SDK_PATH = 'https://sdk.privacy-center.org/';
4+
const CONSENT_PROXY_PATH = '/didomi/consent/';
5+
6+
type DidomiConfig = {
7+
sdkPath?: string;
8+
[key: string]: unknown;
9+
};
10+
11+
type DidomiWindow = Window & { didomiConfig?: DidomiConfig };
12+
13+
function buildProxySdkPath(win: DidomiWindow): string {
14+
const base = win.location?.origin ?? win.location?.href;
15+
if (!base) return CONSENT_PROXY_PATH;
16+
const url = new URL(CONSENT_PROXY_PATH, base);
17+
return `${url.origin}${url.pathname}`;
18+
}
19+
20+
export function installDidomiSdkProxy(): boolean {
21+
if (typeof window === 'undefined') return false;
22+
23+
const win = window as DidomiWindow;
24+
const config = (win.didomiConfig ??= {});
25+
const previousSdkPath =
26+
typeof config.sdkPath === 'string' && config.sdkPath.length > 0
27+
? config.sdkPath
28+
: DEFAULT_SDK_PATH;
29+
30+
const proxiedSdkPath = buildProxySdkPath(win);
31+
config.sdkPath = proxiedSdkPath;
32+
33+
log.info('didomi sdkPath overridden for trusted server proxy', {
34+
previousSdkPath,
35+
sdkPath: proxiedSdkPath,
36+
});
37+
38+
return true;
39+
}
40+
41+
if (typeof window !== 'undefined') {
42+
installDidomiSdkProxy();
43+
}
44+
45+
export default installDidomiSdkProxy;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2+
3+
import { installDidomiSdkProxy } from '../../../src/integrations/didomi';
4+
5+
const ORIGINAL_WINDOW = global.window;
6+
7+
function createWindow(url: string) {
8+
return {
9+
location: new URL(url) as unknown as Location,
10+
} as Window & { didomiConfig?: any };
11+
}
12+
13+
describe('integrations/didomi', () => {
14+
let testWindow: ReturnType<typeof createWindow>;
15+
16+
beforeEach(() => {
17+
testWindow = createWindow('https://example.com/page');
18+
Object.assign(globalThis as any, { window: testWindow });
19+
});
20+
21+
afterEach(() => {
22+
Object.assign(globalThis as any, { window: ORIGINAL_WINDOW });
23+
});
24+
25+
it('initializes didomiConfig and forces sdkPath through trusted server proxy', () => {
26+
installDidomiSdkProxy();
27+
28+
expect(testWindow.didomiConfig).toBeDefined();
29+
expect(testWindow.didomiConfig.sdkPath).toBe('https://example.com/didomi/consent/');
30+
});
31+
32+
it('preserves existing config fields while overriding sdkPath', () => {
33+
testWindow.didomiConfig = { apiKey: 'abc', sdkPath: 'https://sdk.privacy-center.org/' };
34+
35+
installDidomiSdkProxy();
36+
37+
expect(testWindow.didomiConfig.apiKey).toBe('abc');
38+
expect(testWindow.didomiConfig.sdkPath).toBe('https://example.com/didomi/consent/');
39+
});
40+
});

0 commit comments

Comments
 (0)