.ts
TypeScript
(application/typescript)
// 1st-party
import type { ServiceMethodFactory } from "@ethicdevs/react-monolith";
import { GitServer } from "@ethicdevs/fastify-git-server";
// app
import { GitServerServiceDeps } from "./types";
import { ORGS_REPOS } from "./temp_mock";

const makeAuthorizationResolver: ServiceMethodFactory<
  GitServerServiceDeps,
  [string, GitServer.AuthCredentials],
  PromiseLike<boolean>
> = (_) => {
  return async (repoSlug, { username, password }) => {
    const [org, repo] = repoSlug.split("/");
    if (org == null || org in ORGS_REPOS === false || ORGS_REPOS[org] == null) {
      throw new Error(`Unknown organization "${org}".`);
    }
    if (
      repo == null ||
      repo in ORGS_REPOS[org] === false ||
      ORGS_REPOS[org][repo] == null
    ) {
      throw new Error(
        `Unknown repository "${repo}" in (known) organization "${org}".`
      );
    }
    const orgRepo = ORGS_REPOS[org][repo];
    return username === orgRepo.username && password === orgRepo.password;
  };
};

export default makeAuthorizationResolver;