Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/ui/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,31 @@ import { getApiV1BaseUrl } from './apiConfig';
const setAttestationConfigData = async (setData: (data: QuestionFormData[]) => void) => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/config/attestation`);
await axios(url.toString()).then((response) => {
setData(response.data.questions);
});
const response = await axios(url.toString());
setData(response.data.questions);
};

const setURLShortenerData = async (setData: (data: string) => void) => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/config/urlShortener`);
await axios(url.toString()).then((response) => {
setData(response.data);
});
const response = await axios(url.toString());
setData(response.data);
};

const setEmailContactData = async (setData: (data: string) => void) => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/config/contactEmail`);
await axios(url.toString()).then((response) => {
setData(response.data);
});
const response = await axios(url.toString());
setData(response.data);
};

const setUIRouteAuthData = async (setData: (data: UIRouteAuth) => void) => {
const apiV1Base = await getApiV1BaseUrl();
const urlString = `${apiV1Base}/config/uiRouteAuth`;
console.log(`URL: ${urlString}`);
const url = new URL(urlString);
await axios(url.toString()).then((response) => {
setData(response.data);
});
const response = await axios(url.toString());
setData(response.data);
};

export { setAttestationConfigData, setURLShortenerData, setEmailContactData, setUIRouteAuthData };
44 changes: 24 additions & 20 deletions src/ui/services/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ import { ServiceResult, getServiceError, errorResult, successResult } from './er
const canAddUser = async (repoId: string, user: string, action: string) => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo/${repoId}`);
return axios
.get<Repo>(url.toString(), getAxiosConfig())
.then((response) => {
const repo = response.data;
if (action === 'authorise') {
return !repo.users.canAuthorise.includes(user);
} else {
return !repo.users.canPush.includes(user);
}
})
.catch((error: any) => {
const { message } = getServiceError(error, 'Failed to validate repo permissions');
throw new Error(message);
});
try {
const response = await axios.get<Repo>(url.toString(), getAxiosConfig());
const repo = response.data;
if (action === 'authorise') {
return !repo.users.canAuthorise.includes(user);
} else {
return !repo.users.canPush.includes(user);
}
} catch (error: any) {
const { message } = getServiceError(error, 'Failed to validate repo permissions');
throw new Error(message);
}
};

class DupUserValidationError extends Error {
Expand Down Expand Up @@ -79,11 +77,13 @@ const addUser = async (repoId: string, user: string, action: string): Promise<vo
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo/${repoId}/user/${action}`);
const data = { username: user };
await axios.patch(url.toString(), data, getAxiosConfig()).catch((error: any) => {
try {
await axios.patch(url.toString(), data, getAxiosConfig());
} catch (error: any) {
const { message } = getServiceError(error, 'Failed to add user');
console.log(message);
throw new Error(message);
});
}
} else {
console.log('Duplicate user can not be added');
throw new DupUserValidationError('Duplicate user can not be added');
Expand All @@ -94,22 +94,26 @@ const deleteUser = async (user: string, repoId: string, action: string): Promise
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo/${repoId}/user/${action}/${user}`);

await axios.delete(url.toString(), getAxiosConfig()).catch((error: any) => {
try {
await axios.delete(url.toString(), getAxiosConfig());
} catch (error: any) {
const { message } = getServiceError(error, 'Failed to remove user');
console.log(message);
throw new Error(message);
});
}
};

const deleteRepo = async (repoId: string): Promise<void> => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo/${repoId}/delete`);

await axios.delete(url.toString(), getAxiosConfig()).catch((error: any) => {
try {
await axios.delete(url.toString(), getAxiosConfig());
} catch (error: any) {
const { message } = getServiceError(error, 'Failed to delete repository');
console.log(message);
throw new Error(message);
});
}
};

export { addUser, deleteUser, getRepos, getRepo, addRepo, deleteRepo };
79 changes: 40 additions & 39 deletions src/ui/views/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ const Login: React.FC = () => {
const [usernamePasswordMethod, setUsernamePasswordMethod] = useState<string>('');

useEffect(() => {
getBaseUrl().then((baseUrl) => {
axios.get(`${baseUrl}/api/auth/config`).then((response) => {
const usernamePasswordMethod = response.data.usernamePasswordMethod;
const otherMethods = response.data.otherMethods;
const fetchAuthConfig = async () => {
const baseUrl = await getBaseUrl();
const response = await axios.get(`${baseUrl}/api/auth/config`);
const usernamePasswordMethod = response.data.usernamePasswordMethod;
const otherMethods = response.data.otherMethods;

setUsernamePasswordMethod(usernamePasswordMethod);
setAuthMethods(otherMethods);
setUsernamePasswordMethod(usernamePasswordMethod);
setAuthMethods(otherMethods);

// Automatically login if only one non-username/password method is enabled
if (!usernamePasswordMethod && otherMethods.length === 1) {
handleAuthMethodLogin(otherMethods[0]);
}
});
});
// Automatically login if only one non-username/password method is enabled
if (!usernamePasswordMethod && otherMethods.length === 1) {
await handleAuthMethodLogin(otherMethods[0]);
}
};
fetchAuthConfig();
}, []);

function validateForm(): boolean {
Expand All @@ -58,40 +59,40 @@ const Login: React.FC = () => {
);
}

function handleAuthMethodLogin(authMethod: string): void {
getBaseUrl().then((baseUrl) => {
window.location.href = `${baseUrl}/api/auth/${authMethod}`;
});
async function handleAuthMethodLogin(authMethod: string): Promise<void> {
const baseUrl = await getBaseUrl();
window.location.href = `${baseUrl}/api/auth/${authMethod}`;
}

function handleSubmit(event: FormEvent): void {
async function handleSubmit(event: FormEvent): Promise<void> {
event.preventDefault();
setIsLoading(true);

getBaseUrl().then((baseUrl) => {
try {
const baseUrl = await getBaseUrl();
const loginUrl = `${baseUrl}/api/auth/login`;
axios
.post<LoginResponse>(loginUrl, { username, password }, getAxiosConfig())
.then(() => {
await axios.post<LoginResponse>(loginUrl, { username, password }, getAxiosConfig());
window.sessionStorage.setItem('git.proxy.login', 'success');
setMessage('Success!');
setSuccess(true);
await authContext.refreshUser();
navigate(0);
} catch (error: unknown) {
if (error instanceof AxiosError) {
if (error.response?.status === 307) {
window.sessionStorage.setItem('git.proxy.login', 'success');
setMessage('Success!');
setSuccess(true);
authContext.refreshUser().then(() => navigate(0));
})
.catch((error: AxiosError) => {
if (error.response?.status === 307) {
window.sessionStorage.setItem('git.proxy.login', 'success');
setGitAccountError(true);
} else if (error.response?.status === 403) {
setMessage(processAuthError(error, false));
} else {
setMessage('You entered an invalid username or password...');
}
})
.finally(() => {
setIsLoading(false);
});
});
setGitAccountError(true);
} else if (error.response?.status === 403) {
setMessage(processAuthError(error, false));
} else {
setMessage('You entered an invalid username or password...');
}
} else {
setMessage('You entered an invalid username or password...');
}
} finally {
setIsLoading(false);
}
}

if (gitAccountError) {
Expand Down
Loading