Skip to content

Commit

Permalink
refactor: move getAuthConfig to related module
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Feb 7, 2025
1 parent c06510a commit 8553ee0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
3 changes: 2 additions & 1 deletion src/frontend/src/lib/components/auth/AuthConfig.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import SkeletonText from '$lib/components/ui/SkeletonText.svelte';
import Value from '$lib/components/ui/Value.svelte';
import { getRuleUser } from '$lib/services/collection.services';
import { getAuthConfig, listCustomDomains } from '$lib/services/hosting.services';
import { listCustomDomains } from '$lib/services/hosting.services';
import { authStore } from '$lib/stores/auth.store';
import { busy } from '$lib/stores/busy.store';
import { i18n } from '$lib/stores/i18n.store';
import { emit } from '$lib/utils/events.utils';
import {getAuthConfig} from "$lib/services/auth.config.services";
interface Props {
satellite: Satellite;
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/lib/components/hosting/Hosting.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
import CustomDomainInfo from '$lib/components/hosting/CustomDomainInfo.svelte';
import HostingCount from '$lib/components/hosting/HostingCount.svelte';
import { sortedSatelliteCustomDomains } from '$lib/derived/satellite-custom-domains.derived';
import { listCustomDomains, getAuthConfig } from '$lib/services/hosting.services';
import { listCustomDomains } from '$lib/services/hosting.services';
import { authStore } from '$lib/stores/auth.store';
import { i18n } from '$lib/stores/i18n.store';
import type { CustomDomainRegistrationState } from '$lib/types/custom-domain';
import type { SatelliteIdText } from '$lib/types/satellite';
import type { Option } from '$lib/types/utils';
import { satelliteUrl } from '$lib/utils/satellite.utils';
import {getAuthConfig} from "$lib/services/auth.config.services";
interface Props {
satellite: Satellite;
Expand Down
58 changes: 56 additions & 2 deletions src/frontend/src/lib/services/auth.config.services.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { Satellite } from '$declarations/mission_control/mission_control.did';
import type { AuthenticationConfig, Rule } from '$declarations/satellite/satellite.did';
import { setAuthConfig, setRule } from '$lib/api/satellites.api';
import {
getAuthConfig as getAuthConfigApi,
satelliteVersion,
setAuthConfig,
setRule
} from '$lib/api/satellites.api';
import { DEFAULT_RATE_CONFIG_TIME_PER_TOKEN_NS } from '$lib/constants/data.constants';
import { SATELLITE_v0_0_17 } from '$lib/constants/version.constants';
import { i18n } from '$lib/stores/i18n.store';
import { toasts } from '$lib/stores/toasts.store';
import type { OptionIdentity } from '$lib/types/itentity';
Expand All @@ -10,7 +16,15 @@ import {
buildDeleteAuthenticationConfig,
buildSetAuthenticationConfig
} from '$lib/utils/auth.config.utils';
import { fromNullishNullable, isNullish, nonNullish, toNullable } from '@dfinity/utils';
import type { Principal } from '@dfinity/principal';
import {
fromNullable,
fromNullishNullable,
isNullish,
nonNullish,
toNullable
} from '@dfinity/utils';
import { compare } from 'semver';
import { get } from 'svelte/store';

interface UpdateAuthConfigParams {
Expand Down Expand Up @@ -161,3 +175,43 @@ const updateRule = async ({

return { result: 'success' };
};

export const getAuthConfig = async ({
satelliteId,
identity
}: {
satelliteId: Principal;
identity: OptionIdentity;
}): Promise<{
result: 'success' | 'error' | 'skip';
config?: AuthenticationConfig | undefined;
}> => {
try {
// TODO: load versions globally and use store value instead of fetching version again
const version = await satelliteVersion({ satelliteId, identity });

// TODO: keep a list of those version checks and remove them incrementally
// Also would be cleaner than to have 0.0.17 hardcoded there and there...
const authConfigSupported = compare(version, SATELLITE_v0_0_17) >= 0;

if (!authConfigSupported) {
return { result: 'skip' };
}

const config = await getAuthConfigApi({
satelliteId,
identity
});

return { result: 'success', config: fromNullable(config) };
} catch (err: unknown) {
const labels = get(i18n);

toasts.error({
text: labels.errors.authentication_config_loading,
detail: err
});

return { result: 'error' };
}
};
47 changes: 1 addition & 46 deletions src/frontend/src/lib/services/hosting.services.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import type { AuthenticationConfig, CustomDomain } from '$declarations/satellite/satellite.did';
import type { CustomDomain } from '$declarations/satellite/satellite.did';
import {
deleteCustomDomain as deleteCustomDomainApi,
getAuthConfig as getAuthConfigApi,
listCustomDomains as listCustomDomainsApi,
satelliteVersion,
setCustomDomain as setCustomDomainApi
} from '$lib/api/satellites.api';
import { SATELLITE_v0_0_17 } from '$lib/constants/version.constants';
import { deleteDomain, registerDomain } from '$lib/rest/bn.rest';
import { authStore } from '$lib/stores/auth.store';
import { customDomainsStore } from '$lib/stores/custom-domains.store';
import { i18n } from '$lib/stores/i18n.store';
import { toasts } from '$lib/stores/toasts.store';
import type { OptionIdentity } from '$lib/types/itentity';
import type { Principal } from '@dfinity/principal';
import { fromNullable, nonNullish } from '@dfinity/utils';
import { compare } from 'semver';
import { get } from 'svelte/store';

/**
Expand Down Expand Up @@ -110,43 +105,3 @@ export const listCustomDomains = async ({
return { success: false };
}
};

export const getAuthConfig = async ({
satelliteId,
identity
}: {
satelliteId: Principal;
identity: OptionIdentity;
}): Promise<{
result: 'success' | 'error' | 'skip';
config?: AuthenticationConfig | undefined;
}> => {
try {
// TODO: load versions globally and use store value instead of fetching version again
const version = await satelliteVersion({ satelliteId, identity });

// TODO: keep a list of those version checks and remove them incrementally
// Also would be cleaner than to have 0.0.17 hardcoded there and there...
const authConfigSupported = compare(version, SATELLITE_v0_0_17) >= 0;

if (!authConfigSupported) {
return { result: 'skip' };
}

const config = await getAuthConfigApi({
satelliteId,
identity
});

return { result: 'success', config: fromNullable(config) };
} catch (err: unknown) {
const labels = get(i18n);

toasts.error({
text: labels.errors.authentication_config_loading,
detail: err
});

return { result: 'error' };
}
};

0 comments on commit 8553ee0

Please sign in to comment.