n8n/packages/editor-ui/src/stores/sso.store.ts
Alex Grozav e3a53fd19d
feat: Add SSO SAML metadataUrl support and various improvements (#6139)
* feat: add various sso improvements

* fix: remove test button assertion

* fix: fix type imports

* test: attempt fixing unit tests

* fix: changed to using useToast for error toasts

* Minor copy tweaks and swapped buttons position.

* fix locale ref

* align error with UI wording

* simplify saving ux

* fix pretty

* fix: update saml sso setting saving

* fix: undo try/catch changes when saving saml config

* metadata url tab selected at first

* chore: fix linting issue

* test: fix activation checkbox test

---------

Co-authored-by: Giulio Andreini <g.andreini@gmail.com>
Co-authored-by: Michael Auerswald <michael.auerswald@gmail.com>
Co-authored-by: Romain Minaud <romain.minaud@gmail.com>
2023-05-23 16:25:28 +03:00

98 lines
2.9 KiB
TypeScript

import { computed, reactive } from 'vue';
import { defineStore } from 'pinia';
import { EnterpriseEditionFeature } from '@/constants';
import { useRootStore } from '@/stores/n8nRoot.store';
import { useSettingsStore } from '@/stores/settings.store';
import * as ssoApi from '@/api/sso';
import type { SamlPreferences } from '@/Interface';
import { updateCurrentUser } from '@/api/users';
import type { SamlPreferencesExtractedData } from '@/Interface';
import { useUsersStore } from '@/stores/users.store';
export const useSSOStore = defineStore('sso', () => {
const rootStore = useRootStore();
const settingsStore = useSettingsStore();
const usersStore = useUsersStore();
const state = reactive({
loading: false,
samlConfig: undefined as (SamlPreferences & SamlPreferencesExtractedData) | undefined,
});
const isLoading = computed(() => state.loading);
const samlConfig = computed(() => state.samlConfig);
const setLoading = (loading: boolean) => {
state.loading = loading;
};
const isSamlLoginEnabled = computed({
get: () => settingsStore.isSamlLoginEnabled,
set: (value: boolean) => {
settingsStore.setSettings({
...settingsStore.settings,
sso: {
...settingsStore.settings.sso,
saml: {
...settingsStore.settings.sso.saml,
loginEnabled: value,
},
},
});
void toggleLoginEnabled(value);
},
});
const isEnterpriseSamlEnabled = computed(() =>
settingsStore.isEnterpriseFeatureEnabled(EnterpriseEditionFeature.Saml),
);
const isDefaultAuthenticationSaml = computed(() => settingsStore.isDefaultAuthenticationSaml);
const showSsoLoginButton = computed(
() =>
isSamlLoginEnabled.value &&
isEnterpriseSamlEnabled.value &&
isDefaultAuthenticationSaml.value,
);
const getSSORedirectUrl = async () => ssoApi.initSSO(rootStore.getRestApiContext);
const toggleLoginEnabled = async (enabled: boolean) =>
ssoApi.toggleSamlConfig(rootStore.getRestApiContext, { loginEnabled: enabled });
const getSamlMetadata = async () => ssoApi.getSamlMetadata(rootStore.getRestApiContext);
const getSamlConfig = async () => {
const samlConfig = await ssoApi.getSamlConfig(rootStore.getRestApiContext);
state.samlConfig = samlConfig;
return samlConfig;
};
const saveSamlConfig = async (config: SamlPreferences) =>
ssoApi.saveSamlConfig(rootStore.getRestApiContext, config);
const testSamlConfig = async () => ssoApi.testSamlConfig(rootStore.getRestApiContext);
const updateUser = async (params: { firstName: string; lastName: string }) =>
updateCurrentUser(rootStore.getRestApiContext, {
id: usersStore.currentUser!.id,
email: usersStore.currentUser!.email!,
...params,
});
const userData = computed(() => usersStore.currentUser);
return {
isLoading,
setLoading,
isSamlLoginEnabled,
isEnterpriseSamlEnabled,
isDefaultAuthenticationSaml,
showSsoLoginButton,
samlConfig,
getSSORedirectUrl,
getSamlMetadata,
getSamlConfig,
saveSamlConfig,
testSamlConfig,
updateUser,
userData,
};
});