2023-04-04 05:28:29 -07:00
|
|
|
import { computed, reactive, ref } from 'vue';
|
2023-03-17 13:07:08 -07:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
import { EnterpriseEditionFeature } from '@/constants';
|
|
|
|
import { useRootStore } from '@/stores/n8nRootStore';
|
|
|
|
import { useSettingsStore } from '@/stores/settings';
|
2023-04-04 05:28:29 -07:00
|
|
|
import * as ssoApi from '@/api/sso';
|
|
|
|
import { SamlPreferences } from '@/Interface';
|
2023-04-04 09:18:16 -07:00
|
|
|
import { updateCurrentUser } from '@/api/users';
|
|
|
|
import { useUsersStore } from '@/stores/users';
|
2023-03-17 13:07:08 -07:00
|
|
|
|
|
|
|
export const useSSOStore = defineStore('sso', () => {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
const settingsStore = useSettingsStore();
|
2023-04-04 09:18:16 -07:00
|
|
|
const usersStore = useUsersStore();
|
2023-03-17 13:07:08 -07:00
|
|
|
|
|
|
|
const state = reactive({
|
|
|
|
loading: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const isLoading = computed(() => state.loading);
|
|
|
|
|
|
|
|
const setLoading = (loading: boolean) => {
|
|
|
|
state.loading = loading;
|
|
|
|
};
|
|
|
|
|
2023-04-04 05:28:29 -07:00
|
|
|
const isSamlLoginEnabled = computed({
|
|
|
|
get: () => settingsStore.isSamlLoginEnabled,
|
|
|
|
set: (value: boolean) => {
|
|
|
|
settingsStore.setSettings({
|
|
|
|
...settingsStore.settings,
|
|
|
|
sso: {
|
|
|
|
...settingsStore.settings.sso,
|
|
|
|
saml: {
|
|
|
|
...settingsStore.settings.sso.saml,
|
|
|
|
loginEnabled: value,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
toggleLoginEnabled(value);
|
|
|
|
},
|
|
|
|
});
|
2023-03-17 13:07:08 -07:00
|
|
|
const isEnterpriseSamlEnabled = computed(() =>
|
|
|
|
settingsStore.isEnterpriseFeatureEnabled(EnterpriseEditionFeature.Saml),
|
|
|
|
);
|
|
|
|
const isDefaultAuthenticationSaml = computed(() => settingsStore.isDefaultAuthenticationSaml);
|
|
|
|
const showSsoLoginButton = computed(
|
|
|
|
() =>
|
|
|
|
isSamlLoginEnabled.value &&
|
|
|
|
isEnterpriseSamlEnabled.value &&
|
|
|
|
isDefaultAuthenticationSaml.value,
|
|
|
|
);
|
|
|
|
|
2023-04-04 05:28:29 -07:00
|
|
|
const getSSORedirectUrl = () => ssoApi.initSSO(rootStore.getRestApiContext);
|
|
|
|
|
|
|
|
const toggleLoginEnabled = (enabled: boolean) =>
|
|
|
|
ssoApi.toggleSamlConfig(rootStore.getRestApiContext, { loginEnabled: enabled });
|
|
|
|
|
|
|
|
const getSamlMetadata = () => ssoApi.getSamlMetadata(rootStore.getRestApiContext);
|
|
|
|
const getSamlConfig = () => ssoApi.getSamlConfig(rootStore.getRestApiContext);
|
|
|
|
const saveSamlConfig = (config: SamlPreferences) =>
|
|
|
|
ssoApi.saveSamlConfig(rootStore.getRestApiContext, config);
|
|
|
|
const testSamlConfig = () => ssoApi.testSamlConfig(rootStore.getRestApiContext);
|
2023-03-17 13:07:08 -07:00
|
|
|
|
2023-04-04 09:18:16 -07:00
|
|
|
const updateUser = async (params: { firstName: string; lastName: string }) =>
|
|
|
|
updateCurrentUser(rootStore.getRestApiContext, {
|
|
|
|
id: usersStore.currentUser!.id,
|
|
|
|
email: usersStore.currentUser!.email!,
|
|
|
|
...params,
|
|
|
|
});
|
|
|
|
|
2023-03-17 13:07:08 -07:00
|
|
|
return {
|
|
|
|
isLoading,
|
|
|
|
setLoading,
|
2023-04-04 05:28:29 -07:00
|
|
|
isSamlLoginEnabled,
|
|
|
|
isEnterpriseSamlEnabled,
|
2023-03-17 13:07:08 -07:00
|
|
|
showSsoLoginButton,
|
|
|
|
getSSORedirectUrl,
|
2023-04-04 05:28:29 -07:00
|
|
|
getSamlMetadata,
|
|
|
|
getSamlConfig,
|
|
|
|
saveSamlConfig,
|
|
|
|
testSamlConfig,
|
2023-04-04 09:18:16 -07:00
|
|
|
updateUser,
|
2023-03-17 13:07:08 -07:00
|
|
|
};
|
|
|
|
});
|