Skip to content

Commit ec7cc81

Browse files
committed
ui implementation of basic auth
1 parent a13478f commit ec7cc81

7 files changed

+93
-34
lines changed

ui/ui-app/package-lock.json

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/ui-app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
"vite-tsconfig-paths": "4.3.2"
3131
},
3232
"dependencies": {
33-
"@apicurio/data-models": "1.1.27",
3433
"@apicurio/common-ui-components": "2.0.0",
34+
"@apicurio/data-models": "1.1.27",
3535
"@patternfly/patternfly": "5.3.0",
3636
"@patternfly/react-code-editor": "5.3.1",
3737
"@patternfly/react-core": "5.3.1",
3838
"@patternfly/react-icons": "5.3.1",
3939
"@patternfly/react-table": "5.3.1",
4040
"axios": "1.6.8",
41+
"buffer": "^6.0.3",
4142
"luxon": "3.4.4",
4243
"oidc-client-ts": "3.0.1",
4344
"react": "18.3.1",

ui/ui-app/src/services/useAdminService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const exportAs = async (config: ConfigService, auth: AuthService, filename: stri
148148
options.headers = {
149149
...options.headers,
150150
"Accept": "application/zip"
151-
}
151+
};
152152

153153
const endpoint: string = createEndpoint(baseHref, "/admin/export", {}, {
154154
forBrowser: true
@@ -172,7 +172,7 @@ const importFrom = async (config: ConfigService, auth: AuthService, file: string
172172
options.headers = {
173173
...options.headers,
174174
"Accept": "application/zip"
175-
}
175+
};
176176
const endpoint: string = createEndpoint(baseHref, "/admin/import");
177177
return httpPost(endpoint, file, options,undefined, progressFunction);
178178
};

ui/ui-app/src/services/useGroupsService.ts

+14-24
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { ConfigService, useConfigService } from "@services/useConfigService.ts";
22
import {
33
createAuthOptions,
44
createEndpoint,
5-
createHeaders,
6-
createOptions,
75
httpDelete,
86
httpGet,
97
httpPostWithReturn,
@@ -71,42 +69,40 @@ export interface ClientGeneration {
7169

7270
const createArtifact = async (config: ConfigService, auth: AuthService, data: CreateArtifactData): Promise<ArtifactMetaData> => {
7371
const baseHref: string = config.artifactsUrl();
74-
const token: string | undefined = await auth.getToken();
7572
const endpoint: string = createEndpoint(baseHref, "/groups/:groupId/artifacts", { groupId: data.groupId });
7673
const options = await createAuthOptions(auth);
7774

78-
const headers: any = createHeaders(token);
7975
if (data.id) {
8076
options.headers = {
8177
...options.headers,
8278
"X-Registry-ArtifactId": data.id
83-
}
79+
};
8480
}
8581
if (data.type) {
8682
options.headers = {
8783
...options.headers,
8884
"X-Registry-ArtifactType": data.type
89-
}
85+
};
9086
}
9187
if (data.sha) {
9288
options.headers = {
9389
...options.headers,
9490
"X-Registry-Hash-Algorithm": "SHA256",
9591
"X-Registry-Content-Hash": data.sha
96-
}
92+
};
9793
}
9894

9995
if (data.fromURL) {
10096
options.headers = {
10197
...options.headers,
10298
"Content-Type": "application/create.extended+json"
103-
}
99+
};
104100
data.content = `{ "content": "${data.fromURL}" }`;
105101
} else {
106102
options.headers = {
107103
...options.headers,
108104
"Content-Type": contentType(data.type, data.content ? data.content : "")
109-
}
105+
};
110106
}
111107

112108
return httpPostWithReturn<any, ArtifactMetaData>(endpoint, data.content, options);
@@ -122,12 +118,12 @@ const createArtifactVersion = async (config: ConfigService, auth: AuthService, g
122118
options.headers = {
123119
...options.headers,
124120
"X-Registry-ArtifactType": data.type
125-
}
121+
};
126122
}
127123
options.headers = {
128124
...options.headers,
129125
"Content-Type": contentType(data.type, data.content)
130-
}
126+
};
131127
return httpPostWithReturn<any, VersionMetaData>(endpoint, data.content, options);
132128
};
133129

@@ -236,7 +232,7 @@ const getArtifactVersionContent = async (config: ConfigService, auth: AuthServic
236232
options.headers = {
237233
...options.headers,
238234
"Accept": "*"
239-
}
235+
};
240236
options.maxContentLength = 5242880; // TODO 5MB hard-coded, make this configurable?
241237
options.responseType = "text";
242238
options.transformResponse = (data: any) => data;
@@ -263,9 +259,8 @@ const getArtifactRules = async (config: ConfigService, auth: AuthService, groupI
263259

264260
console.info("[GroupsService] Getting the list of rules for artifact: ", groupId, artifactId);
265261
const baseHref: string = config.artifactsUrl();
266-
const token: string | undefined = await auth.getToken();
267262
const endpoint: string = createEndpoint(baseHref, "/groups/:groupId/artifacts/:artifactId/rules", { groupId, artifactId });
268-
const options = createOptions(createHeaders(token));
263+
const options = await createAuthOptions(auth);
269264
return httpGet<string[]>(endpoint, options).then( ruleTypes => {
270265
return Promise.all(ruleTypes.map(rt => getArtifactRule(config, auth, groupId, artifactId, rt)));
271266
});
@@ -275,13 +270,12 @@ const getArtifactRule = async (config: ConfigService, auth: AuthService, groupId
275270
groupId = normalizeGroupId(groupId);
276271

277272
const baseHref: string = config.artifactsUrl();
278-
const token: string | undefined = await auth.getToken();
279273
const endpoint: string = createEndpoint(baseHref, "/groups/:groupId/artifacts/:artifactId/rules/:rule", {
280274
groupId,
281275
artifactId,
282276
rule: type
283277
});
284-
const options = createOptions(createHeaders(token));
278+
const options = await createAuthOptions(auth);
285279
return httpGet<Rule>(endpoint, options);
286280
};
287281

@@ -291,13 +285,12 @@ const createArtifactRule = async (config: ConfigService, auth: AuthService, grou
291285
console.info("[GroupsService] Creating rule:", type);
292286

293287
const baseHref: string = config.artifactsUrl();
294-
const token: string | undefined = await auth.getToken();
295288
const endpoint: string = createEndpoint(baseHref, "/groups/:groupId/artifacts/:artifactId/rules", { groupId, artifactId });
296289
const body: Rule = {
297290
config: configValue,
298291
type
299292
};
300-
const options = createOptions(createHeaders(token));
293+
const options = await createAuthOptions(auth);
301294
return httpPostWithReturn(endpoint, body, options);
302295
};
303296

@@ -306,14 +299,13 @@ const updateArtifactRule = async (config: ConfigService, auth: AuthService, grou
306299

307300
console.info("[GroupsService] Updating rule:", type);
308301
const baseHref: string = config.artifactsUrl();
309-
const token: string | undefined = await auth.getToken();
310302
const endpoint: string = createEndpoint(baseHref, "/groups/:groupId/artifacts/:artifactId/rules/:rule", {
311303
groupId,
312304
artifactId,
313305
"rule": type
314306
});
315307
const body: Rule = { config: configValue, type };
316-
const options = createOptions(createHeaders(token));
308+
const options = await createAuthOptions(auth);
317309
return httpPutWithReturn<Rule, Rule>(endpoint, body, options);
318310
};
319311

@@ -322,13 +314,12 @@ const deleteArtifactRule = async (config: ConfigService, auth: AuthService, grou
322314

323315
console.info("[GroupsService] Deleting rule:", type);
324316
const baseHref: string = config.artifactsUrl();
325-
const token: string | undefined = await auth.getToken();
326317
const endpoint: string = createEndpoint(baseHref, "/groups/:groupId/artifacts/:artifactId/rules/:rule", {
327318
groupId,
328319
artifactId,
329320
"rule": type
330321
});
331-
const options = createOptions(createHeaders(token));
322+
const options = await createAuthOptions(auth);
332323
return httpDelete(endpoint, options);
333324
};
334325

@@ -337,9 +328,8 @@ const deleteArtifact = async (config: ConfigService, auth: AuthService, groupId:
337328

338329
console.info("[GroupsService] Deleting artifact:", groupId, artifactId);
339330
const baseHref: string = config.artifactsUrl();
340-
const token: string | undefined = await auth.getToken();
341331
const endpoint: string = createEndpoint(baseHref, "/groups/:groupId/artifacts/:artifactId", { groupId, artifactId });
342-
const options = createOptions(createHeaders(token));
332+
const options = await createAuthOptions(auth);
343333
return httpDelete(endpoint, options);
344334
};
345335

ui/ui-app/src/services/useUserService.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { UserInfo } from "@models/userInfo.model.ts";
22
import { AuthService, useAuth } from "@apicurio/common-ui-components";
3-
import { createEndpoint, createHeaders, createOptions, httpGet } from "@utils/rest.utils.ts";
3+
import { createAuthOptions, createEndpoint, httpGet } from "@utils/rest.utils.ts";
44
import { ConfigService, useConfigService } from "@services/useConfigService.ts";
55

66
let currentUserInfo: UserInfo = {
@@ -21,8 +21,7 @@ const updateCurrentUser = async (config: ConfigService, auth: AuthService): Prom
2121
if (isAuthenticated) {
2222
// TODO cache the response for a few minutes to limit the # of times this is called per minute??
2323
const endpoint: string = createEndpoint(config.artifactsUrl(), "/users/me");
24-
const token: string | undefined = await auth.getToken();
25-
const options = createOptions(createHeaders(token));
24+
const options = await createAuthOptions(auth);
2625
return httpGet<UserInfo>(endpoint, options).then(userInfo => {
2726
currentUserInfo = userInfo;
2827
return userInfo;
@@ -41,7 +40,7 @@ const isObacEnabled = (config: ConfigService): boolean => {
4140
};
4241

4342
const isUserAdmin = (config: ConfigService, auth: AuthService): boolean => {
44-
if (!auth.isAuthEnabled()) {
43+
if (!auth.isOidcAuthEnabled() && !auth.isBasicAuthEnabled()) {
4544
return true;
4645
}
4746
if (!isRbacEnabled(config) && !isObacEnabled(config)) {
@@ -51,7 +50,7 @@ const isUserAdmin = (config: ConfigService, auth: AuthService): boolean => {
5150
};
5251

5352
const isUserDeveloper = (config: ConfigService, auth: AuthService, resourceOwner?: string): boolean => {
54-
if (!auth.isAuthEnabled()) {
53+
if (!auth.isOidcAuthEnabled() && !auth.isBasicAuthEnabled()) {
5554
return true;
5655
}
5756
if (!isRbacEnabled(config) && !isObacEnabled(config)) {

ui/ui-app/src/utils/rest.utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios, { AxiosRequestConfig } from "axios";
22
import { ContentTypes } from "@models/contentTypes.model.ts";
33
import { AuthService } from "@apicurio/common-ui-components";
4+
import { Buffer } from "buffer";
45

56
const AXIOS = axios.create();
67

@@ -113,7 +114,8 @@ export async function createAuthOptions(auth: AuthService): Promise<AxiosRequest
113114
return createOptions(createHeaders(token));
114115
} else if (auth.isBasicAuthEnabled()) {
115116
const creds = auth.getUsernameAndPassword();
116-
const headers = { "Authorization": `Basic ${Buffer.from(`${creds?.username}:${creds?.password}`, 'base64')}`};
117+
const base64Credentials = Buffer.from(`${creds?.username}:${creds?.password}`, "ascii").toString("base64");
118+
const headers = { "Authorization": `Basic ${base64Credentials}` };
117119
return createOptions(headers);
118120
} else {
119121
return Promise.resolve({});

ui/ui-app/vite.config.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ export default defineConfig({
88
plugins: [react(), tsconfigPaths()],
99
server: {
1010
port: PORT
11-
}
11+
},
12+
// START: To use npm link
13+
// optimizeDeps: {
14+
// exclude: ['@apicurio/common-ui-components'],
15+
// },
16+
// END: To use npm link
1217
// define: {
1318
// "process.platform": {}
1419
// }

0 commit comments

Comments
 (0)