Skip to content

Commit

Permalink
refactor sso provider selection
Browse files Browse the repository at this point in the history
avoid using boolean values in envvars as yaml, ansible, .env evaluate them differently
  • Loading branch information
henrinie-nc committed Sep 30, 2024
1 parent 8e616fe commit b63a90b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions src/auth/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { LoginProviderProps } from 'hds-react';

type OidcProviderName = 'tunnistamo' | 'tunnistus';

// Tunnistamo SSO (legacy)
const loginProviderTunnistamoProperties: LoginProviderProps = {
userManagerSettings: {
Expand Down Expand Up @@ -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;
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;
7 changes: 2 additions & 5 deletions src/auth/selectors.ts
Original file line number Diff line number Diff line change
@@ -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<ApiToken, void> = (state: Record<string, any>): 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<Record<string, any>, void> = (state: Record<string, any>): User | null => state.auth.user;

0 comments on commit b63a90b

Please sign in to comment.