aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/api/util.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/server/src/api/util.ts')
-rw-r--r--packages/server/src/api/util.ts60
1 files changed, 29 insertions, 31 deletions
diff --git a/packages/server/src/api/util.ts b/packages/server/src/api/util.ts
index 72c8992..49144d4 100644
--- a/packages/server/src/api/util.ts
+++ b/packages/server/src/api/util.ts
@@ -1,28 +1,26 @@
import { Git, GitAPI } from "./git";
import { readdir } from "fs";
-type VerificationResultErrorType = "REPO_NOT_FOUND" | "REPO_INVALID" | "COMMIT_NOT_FOUND" | "COMMIT_INVALID" | "ACCESS_DENIED";
-
-const verification_error_types = {
- REPO_NOT_FOUND: { code: 404, message: "Repository not found!" },
- REPO_INVALID: { code: 403, message: "Invalid repository!" },
- COMMIT_NOT_FOUND: { code: 404, message: "Commit not found!" },
- COMMIT_INVALID: { code: 403, message: "Invalid commit!" },
- ACCESS_DENIED: { code: 403, message: "Access denied!" }
-};
+type VerificationResultType = "SUCCESS" | "NOT_FOUND" | "INVALID" | "ACCESS_DENIED";
export class VerificationResult {
- constructor(success: boolean, error_type?: VerificationResultErrorType) {
- this.success = success;
-
- if(error_type) {
- this.message = verification_error_types[error_type].message;
- this.code = verification_error_types[error_type].code;
+ constructor(result: VerificationResultType, subject?: string) {
+ this.success = result === "SUCCESS" ? true : false;
+
+ if(result !== "SUCCESS") {
+ const verification_error_types = {
+ NOT_FOUND: { code: 404, message: `${ String(subject?.substr(0, 1).toUpperCase()) + subject?.substr(1)} not found!` },
+ INVALID: { code: 403, message: `Invalid ${subject}` },
+ ACCESS_DENIED: { code: 403, message: "Access denied!" }
+ };
+
+ this.message = verification_error_types[result].message;
+ this.code = verification_error_types[result].code;
}
}
success: boolean;
- code: number = 0;
+ code: number | null = null;
message: string | null = null;
}
@@ -31,49 +29,49 @@ export function verifyRepoName(base_dir: string, repo_name: string) {
console.log(repo_name);
const is_valid_repo_name = (/^[a-zA-Z0-9.\-_]+$/u).test(repo_name);
if(!is_valid_repo_name) {
- resolve(new VerificationResult(false, "REPO_INVALID"));
+ resolve(new VerificationResult("INVALID", "repository"));
return;
}
readdir(base_dir, (err, dir_content) => {
if(err) {
- resolve(new VerificationResult(false, "REPO_NOT_FOUND"));
+ resolve(new VerificationResult("NOT_FOUND", "repository"));
return;
}
const dir_content_repos = dir_content.filter(repo => repo.endsWith(".git"));
if(!dir_content_repos.includes(repo_name + ".git")) {
- resolve(new VerificationResult(false, "REPO_NOT_FOUND"));
+ resolve(new VerificationResult("NOT_FOUND", "repository"));
return;
}
- resolve(new VerificationResult(true));
+ resolve(new VerificationResult("SUCCESS"));
});
});
}
-export async function verifyCommitID(git: GitAPI, repo: string, commit_id: string) {
- if(!(/^[a-fA-F0-9]+$/u).test(commit_id)) {
- return new VerificationResult(false, "COMMIT_INVALID");
+export async function verifySHA(git: GitAPI, repo_name: string, sha: string) {
+ if(!(/^[a-fA-F0-9]+$/u).test(sha)) {
+ return new VerificationResult("INVALID", "sha");
}
- const commit_exists = await git.doesCommitExist(repo, commit_id);
+ const object_exists = await git.doesObjectExist(repo_name, sha);
- if(!commit_exists) {
- return new VerificationResult(false, "COMMIT_NOT_FOUND");
+ if(!object_exists) {
+ return new VerificationResult("NOT_FOUND", "object");
}
- return new VerificationResult(true);
+ return new VerificationResult("SUCCESS");
}
-export function verifyGitRequest(request_info: Git.RequestInfo): VerificationResult {
+export function verifyGitRequest(request_info: Git.RequestInfo) {
if((/\.\/|\.\./u).test(request_info.parsed_url.pathname)) {
- return new VerificationResult(false, "REPO_NOT_FOUND");
+ return new VerificationResult("INVALID", "path");
}
if(request_info.service !== "git-upload-pack") {
- return new VerificationResult(false, "ACCESS_DENIED");
+ return new VerificationResult("ACCESS_DENIED");
}
- return new VerificationResult(true);
+ return new VerificationResult("SUCCESS");
} \ No newline at end of file