From 0ec66b1de026f19a9cba2be9ed7a80092a967e6e Mon Sep 17 00:00:00 2001 From: braginini Date: Fri, 28 Jul 2023 17:46:48 +0200 Subject: [PATCH] Drag custom query params to auth layer (e.g., utm_source) --- docker/init_react_envs.sh | 3 ++- src/config.json | 3 ++- src/config.ts | 2 ++ src/index.tsx | 21 +++++++++++++++++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/docker/init_react_envs.sh b/docker/init_react_envs.sh index 7271d2db..a0b2f25f 100644 --- a/docker/init_react_envs.sh +++ b/docker/init_react_envs.sh @@ -58,11 +58,12 @@ export NETBIRD_MGMT_API_ENDPOINT=$(echo $NETBIRD_MGMT_API_ENDPOINT | sed -E 's/( export NETBIRD_MGMT_GRPC_API_ENDPOINT=${NETBIRD_MGMT_GRPC_API_ENDPOINT} export NETBIRD_HOTJAR_TRACK_ID=${NETBIRD_HOTJAR_TRACK_ID} export NETBIRD_TOKEN_SOURCE=${NETBIRD_TOKEN_SOURCE:-accessToken} +export NETBIRD_DRAG_QUERY_PARAMS=${NETBIRD_DRAG_QUERY_PARAMS:-false} echo "NetBird latest version: ${NETBIRD_LATEST_VERSION}" # replace ENVs in the config -ENV_STR="\$\$USE_AUTH0 \$\$AUTH_AUDIENCE \$\$AUTH_AUTHORITY \$\$AUTH_CLIENT_ID \$\$AUTH_CLIENT_SECRET \$\$AUTH_SUPPORTED_SCOPES \$\$NETBIRD_MGMT_API_ENDPOINT \$\$NETBIRD_MGMT_GRPC_API_ENDPOINT \$\$NETBIRD_HOTJAR_TRACK_ID \$\$AUTH_REDIRECT_URI \$\$AUTH_SILENT_REDIRECT_URI \$\$NETBIRD_TOKEN_SOURCE" +ENV_STR="\$\$USE_AUTH0 \$\$AUTH_AUDIENCE \$\$AUTH_AUTHORITY \$\$AUTH_CLIENT_ID \$\$AUTH_CLIENT_SECRET \$\$AUTH_SUPPORTED_SCOPES \$\$NETBIRD_MGMT_API_ENDPOINT \$\$NETBIRD_MGMT_GRPC_API_ENDPOINT \$\$NETBIRD_HOTJAR_TRACK_ID \$\$AUTH_REDIRECT_URI \$\$AUTH_SILENT_REDIRECT_URI \$\$NETBIRD_TOKEN_SOURCE \$\$NETBIRD_DRAG_QUERY_PARAMS" MAIN_JS=$(find /usr/share/nginx/html/static/js/main.*js) OIDC_TRUSTED_DOMAINS="/usr/share/nginx/html/OidcTrustedDomains.js" diff --git a/src/config.json b/src/config.json index 6a43b603..e803a083 100644 --- a/src/config.json +++ b/src/config.json @@ -11,5 +11,6 @@ "hotjarTrackID": "$NETBIRD_HOTJAR_TRACK_ID", "redirectURI": "$AUTH_REDIRECT_URI", "silentRedirectURI": "$AUTH_SILENT_REDIRECT_URI", - "tokenSource": "$NETBIRD_TOKEN_SOURCE" + "tokenSource": "$NETBIRD_TOKEN_SOURCE", + "dragQueryParams": "$NETBIRD_DRAG_QUERY_PARAMS" } \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index 40267c7c..29c6cfe7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -39,5 +39,7 @@ export function getConfig() { redirectURI: redirectURI, silentRedirectURI: silentRedirectURI, tokenSource: tokenSource, + // drags all the query params to the auth layer specified in the URL when accessing dashboard. + dragQueryParams: configJson.dragQueryParams == "true" }; } diff --git a/src/index.tsx b/src/index.tsx index 1beb687a..9908e542 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -17,12 +17,29 @@ const config = getConfig(); // is required for doing logout. Therefore, we need to hardcode the config for auth const auth0AuthorityConfig: AuthorityConfiguration = { authorization_endpoint: new URL("authorize", config.authority).href, - token_endpoint: new URL("oauth/token", config.authority).href, + token_endpoint: new URL("oauth/token", config.authority).href, revocation_endpoint: new URL("oauth/revoke", config.authority).href, end_session_endpoint: new URL("v2/logout", config.authority).href, userinfo_endpoint: new URL("userinfo", config.authority).href, } as AuthorityConfiguration +const buildExtras = (config: any) => { + type Extras = { [key: string]: string } + let extras: Extras = {}; + + if (config.dragQueryParams) { + const searchParams = new URLSearchParams(window.location.search); + searchParams.forEach((value, key) => { + extras[key] = value + }); + } + + if (config.audience) { + extras.audience = config.audience + } + return extras +} + const providerConfig = { authority: config.authority, client_id: config.clientId, @@ -34,7 +51,7 @@ const providerConfig = { // service_worker_relative_url:'/OidcServiceWorker.js', service_worker_only: false, authority_configuration: config.auth0Auth ? auth0AuthorityConfig : undefined, - ...(config.audience ? {extras: {audience: config.audience}} : null), + extras: buildExtras(config), ...(config.clientSecret ? {token_request_extras: {client_secret: config.clientSecret}} : null) };