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(Authentication): Refactor of unused bearer Token #43

Merged
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function IdSettingsCard() {
setIsLoading(true);
for (const setting of data.idSettings) {
if (setting.prefix.value && setting.dynamicPart.value) {
await putSingleIdGenerationSetting(setting.name, bearerToken, {
await putSingleIdGenerationSetting(setting.name, {
prefix: setting.prefix.value,
dynamicPart: setting.dynamicPart.value,
});
Expand Down
6 changes: 3 additions & 3 deletions src/app/[locale]/templates/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export default function Page() {
const env = useEnv();
const fetchCustom = async () => {
if (!id) return;
const custom = await getCustomTemplateById(bearerToken, id);
const custom = await getCustomTemplateById(id);
setLocalFrontendTemplate(generateSubmodelViewObject(custom));
};

const fetchDefaultTemplates = async () => {
const defaultTemplates = await getDefaultTemplates(bearerToken);
const defaultTemplates = await getDefaultTemplates();
setDefaultTemplates(defaultTemplates);
};

Expand Down Expand Up @@ -134,7 +134,7 @@ export default function Page() {
const deleteTemplate = async () => {
if (!id) return;
try {
await deleteCustomTemplateById(bearerToken, id);
await deleteCustomTemplateById(id);
notificationSpawner.spawn({
message: intl.formatMessage(messages.mnestix.templateDeletedSuccessfully),
severity: 'success',
Expand Down
6 changes: 3 additions & 3 deletions src/app/[locale]/templates/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function Page() {
const bearerToken = auth.getBearerToken();
const fetchAll = async () => {
// fetching defaults first
const _defaults = await getDefaultTemplates(bearerToken);
const _defaults = await getDefaultTemplates();
_defaults.sort((a: Submodel, b: Submodel) => sortWithNullableValues(a.idShort, b.idShort));
setDefaults(_defaults);
const _defaultItems: TabSelectorItem[] = [
Expand Down Expand Up @@ -80,7 +80,7 @@ export default function Page() {

const fetchCustoms = async (_defaultItems: Array<TabSelectorItem>) => {
const _customTemplateItems: CustomTemplateItemType[] = [];
const customs = (await getCustomTemplates(bearerToken)) as Submodel[];
const customs = (await getCustomTemplates()) as Submodel[];
customs?.forEach((customSubmodel: Submodel) => {
// get displayName out of Qualifiers or use idShort of Submodel
const displayName =
Expand Down Expand Up @@ -185,7 +185,7 @@ export default function Page() {
const deleteTemplate = async (item: CustomTemplateItemType) => {
if (!item.id) return;
try {
await deleteCustomTemplateById(bearerToken, item.id);
await deleteCustomTemplateById(item.id);
notificationSpawner.spawn({
message: intl.formatMessage(messages.mnestix.templateDeletedSuccessfully),
severity: 'success',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,17 @@ export class ConfigurationShellApi implements IConfigurationShellApi {

async putSingleIdGenerationSetting(
idShort: string,
bearerToken: string,
values: {
prefix: string;
dynamicPart: string;
},
) {
await this.putSingleSettingValue(`${idShort}.Prefix`, bearerToken, values.prefix, 'idGeneration');
await this.putSingleSettingValue(`${idShort}.DynamicPart`, bearerToken, values.dynamicPart, 'idGeneration');
await this.putSingleSettingValue(`${idShort}.Prefix`, values.prefix, 'idGeneration');
await this.putSingleSettingValue(`${idShort}.DynamicPart`, values.dynamicPart, 'idGeneration');
}

async putSingleSettingValue(
path: string,
bearerToken: string,
value: string,
settingsType: string,
): Promise<Response> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ export interface IConfigurationShellApi {

putSingleIdGenerationSetting(
idShort: string,
bearerToken: string,
values: {
prefix: string;
dynamicPart: string;
},
): Promise<void>;

putSingleSettingValue(path: string, bearerToken: string, value: string, settingsType: string): Promise<Response>;
putSingleSettingValue(path: string, value: string, settingsType: string): Promise<Response>;

processPutSingleSettingValue(response: Response): Promise<Response>;
}
38 changes: 16 additions & 22 deletions src/lib/api/template-shell-api/templateShellApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ export class TemplateShellApi {
return new TemplateShellApi(backendApiUrl, enable_authentication, http);
}

public async getDefaults(token: string): Promise<Submodel[]> {
const headers = this.prepareHeader(token);
public async getDefaults(): Promise<Submodel[]> {

const response = await this.http.fetch(`${this.basePathOwnApi}/allDefaultSubmodels`, {
method: 'GET',
headers,
headers: {
'Accept': 'application/json',
},
});

if (response.status >= 200 && response.status < 300) {
Expand All @@ -41,12 +42,13 @@ export class TemplateShellApi {
}
}

public async getCustoms(token: string): Promise<Submodel[]> {
const headers = this.prepareHeader(token);
public async getCustoms(): Promise<Submodel[]> {

const response = await this.http.fetch(`${this.basePathOwnApi}/allCustomSubmodels`, {
method: 'GET',
headers,
headers: {
'Accept': 'application/json',
},
});

if (response.status >= 200 && response.status < 300) {
Expand All @@ -56,14 +58,15 @@ export class TemplateShellApi {
}
}

public async getCustom(token: string, submodelIdShort: string): Promise<Submodel> {
const headers = this.prepareHeader(token);
public async getCustom(submodelIdShort: string): Promise<Submodel> {

const response = await this.http.fetch(
`${this.basePathOwnApi}/CustomSubmodel/${encodeBase64(submodelIdShort)}`,
{
method: 'GET',
headers,
headers: {
'Accept': 'application/json',
},
},
);

Expand All @@ -74,14 +77,15 @@ export class TemplateShellApi {
}
}

public async deleteCustomById(token: string, id: string): Promise<string | number> {
const headers = this.prepareHeader(token);
public async deleteCustomById(id: string): Promise<string | number> {

// We use the regular delete endpoint, which expects an idShort, but because of our backend interception, we saved the actual id in the idShort field earlier.
// That's why this works.
const response = await this.http.fetch(`${this.basePathCustoms}/${encodeBase64(id)}`, {
method: 'DELETE',
headers,
headers: {
'Accept': 'application/json',
},
});

if (response.status >= 200 && response.status < 300) {
Expand All @@ -90,14 +94,4 @@ export class TemplateShellApi {
throw response;
}
}

private prepareHeader(token: string) {
const headers = {
Accept: 'application/json',
};
if (this.enable_authentication) {
headers['Authorization'] = token;
}
return headers;
}
}
3 changes: 1 addition & 2 deletions src/lib/services/configurationApiActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ export async function getIdGenerationSettings(): Promise<Submodel> {

export async function putSingleIdGenerationSetting(
idShort: string,
bearerToken: string,
values: {
prefix: string;
dynamicPart: string;
},
): Promise<void> {
return configurationShellApi.putSingleIdGenerationSetting(idShort, bearerToken, values);
return configurationShellApi.putSingleIdGenerationSetting(idShort, values);
}
16 changes: 8 additions & 8 deletions src/lib/services/templatesApiActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ const templateApiClient = TemplateShellApi.create(
mnestixFetchLegacy(),
);

export async function getDefaultTemplates(bearerToken: string): Promise<Submodel[]> {
return templateApiClient.getDefaults(bearerToken);
export async function getDefaultTemplates(): Promise<Submodel[]> {
return templateApiClient.getDefaults();
}

export async function getCustomTemplates(bearerToken: string): Promise<Submodel[]> {
return templateApiClient.getCustoms(bearerToken);
export async function getCustomTemplates(): Promise<Submodel[]> {
return templateApiClient.getCustoms();
}

export async function getCustomTemplateById(bearerToken: string, id: string): Promise<Submodel> {
return templateApiClient.getCustom(bearerToken, id);
export async function getCustomTemplateById(id: string): Promise<Submodel> {
return templateApiClient.getCustom(id);
}

export async function deleteCustomTemplateById(bearerToken: string, id: string): Promise<string | number> {
return templateApiClient.deleteCustomById(bearerToken, id);
export async function deleteCustomTemplateById(id: string): Promise<string | number> {
return templateApiClient.deleteCustomById(id);
}
Loading