Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor sso provider selection #525

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'tunnistamo' string is assigned to an env var, but 'tunnistus' is just a plain string without a reference to a variable. Looks a bit odd, but is there a reason for it? Could it be a bit cleaner if 'tunnistus' could also be defined in variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is defined in VITE_OIDC_PROVIDER, || 'tunnistus' is a fallback default value.
How do you suggest to change it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be defined as DEFAULT_PROVIDER_NAME. But I'm not sure, doesn't feel necessary, because when I think about it more, the string 'tunnistus' is used only here. Maybe defining it in a variable becomes necessary only if the exact 'tunnistus' string is used in several places, to avoid the pitfall of mistyping. So maybe there is no need to define it in a variable in these changes.

// 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;