From 7a6710e21981bff8c8f5590f91fd71c74c849faf Mon Sep 17 00:00:00 2001 From: Henri Nieminen Date: Wed, 25 Sep 2024 13:28:21 +0300 Subject: [PATCH] refactor sso provider selection avoid using boolean values in envvars as yaml, ansible, .env evaluate them differently --- .env.example | 3 ++- src/auth/constants.ts | 9 +++++++-- src/auth/selectors.ts | 7 ++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index 9da70a9b..eee3ee84 100644 --- a/.env.example +++ b/.env.example @@ -15,4 +15,5 @@ VITE_TUNNISTUS_OIDC_SCOPE=openid profile VITE_TUNNISTUS_OIDC_API_AUDIENCE=mvj-api-dev VITE_TUNNISTUS_OIDC_API_TOKEN_URL=https://tunnistus.test.hel.ninja/auth/realms/helsinki-tunnistus/protocol/openid-connect/token # Use legacy Tunnistamo SSO, or Tunnistus SSO? -VITE_USE_TUNNISTAMO_OPENID_CONNECT=true +# Options: "tunnistamo", "tunnistus" +VITE_OIDC_PROVIDER=tunnistamo diff --git a/src/auth/constants.ts b/src/auth/constants.ts index cd2c9b13..019e05d3 100644 --- a/src/auth/constants.ts +++ b/src/auth/constants.ts @@ -1,5 +1,7 @@ import type { LoginProviderProps } from 'hds-react'; +type OidcProviderName = 'tunnistamo' | 'tunnistus'; + // Tunnistamo SSO (legacy) const loginProviderTunnistamoProperties: LoginProviderProps = { userManagerSettings: { @@ -31,6 +33,9 @@ const loginProviderTunnistusProperties: LoginProviderProps = { sessionPollerSettings: { pollIntervalInMs: 300000 } // 300000ms = 5min }; -export const useTunnistamoOpenIdConnect = import.meta.env.VITE_USE_TUNNISTAMO_OPENID_CONNECT === 'true' || import.meta.env.VITE_USE_TUNNISTAMO_OPENID_CONNECT === true; +export const oidcProviderName: OidcProviderName = import.meta.env.VITE_OIDC_PROVIDER || 'tunnistus'; // By default use Tunnistus SSO -export const loginProviderProperties = useTunnistamoOpenIdConnect ? loginProviderTunnistamoProperties : loginProviderTunnistusProperties; \ No newline at end of file +export const loginProviderProperties = oidcProviderName === 'tunnistamo' ? loginProviderTunnistamoProperties : loginProviderTunnistusProperties; +const tunnistamoApiTokenKeyName: string = import.meta.env.VITE_OPENID_CONNECT_API_TOKEN_KEY || 'https://api.hel.fi/auth/mvj'; +const tunnistusApiTokenKeyName: string = import.meta.env.VITE_TUNNISTUS_OIDC_API_AUDIENCE || 'mvj-api'; +export const apiTokenKeyName = oidcProviderName === 'tunnistamo' ? tunnistamoApiTokenKeyName : tunnistusApiTokenKeyName; \ No newline at end of file diff --git a/src/auth/selectors.ts b/src/auth/selectors.ts index 37d53021..90c7aca6 100644 --- a/src/auth/selectors.ts +++ b/src/auth/selectors.ts @@ -1,12 +1,9 @@ import type { Selector } from "@/types"; import type { ApiToken, AuthState } from "./types"; import type { User } from 'hds-react'; -import { useTunnistamoOpenIdConnect } from "@/auth/constants"; +import { apiTokenKeyName } from "@/auth/constants"; // Helper functions to select state export const getApiToken: Selector = (state: Record): AuthState => { - if (useTunnistamoOpenIdConnect) { - return state.auth.apiToken[import.meta.env.VITE_OPENID_CONNECT_API_TOKEN_KEY || 'https://api.hel.fi/auth/mvj']; - } - return state.auth.apiToken[import.meta.env.VITE_TUNNISTUS_OIDC_API_AUDIENCE || 'mvj-api']; + return state.auth.apiToken[apiTokenKeyName]; }; export const getLoggedInUser: Selector, void> = (state: Record): User | null => state.auth.user;