Skip to content

Commit

Permalink
wip - admin routes
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetso committed Jan 20, 2025
1 parent 44c50fb commit a6b8c7b
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 66 deletions.
39 changes: 16 additions & 23 deletions packages/api-express-router/src/apiExpressRouterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,24 @@ class ApiExpressRouterFactory {
apiEndpointAdapter: ApiExpressEndpointAdapter,
): void {
for (const route of routes) {
this.registerRoute(router, route, apiEndpointAdapter);
console.log("registering route", route.path);
router.post(
route.path,
async (
request: Request,
response: Response,
next: NextFunction,
): Promise<void> => {
await apiEndpointAdapter.handleRequest(
route.endpoint,
request,
response,
next,
);
},
);
}
}

protected registerRoute(
router: Router,
route: ApiRoute,
apiEndpointAdapter: ApiExpressEndpointAdapter,
): void {
router.post(
route.path,
async (
request: Request,
response: Response,
next: NextFunction,
): Promise<void> => {
await apiEndpointAdapter.handleRequest(
route.endpoint,
request,
response,
next,
);
},
);
}
}

export { ApiExpressRouterFactory };
10 changes: 6 additions & 4 deletions packages/cli/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ProviderEnvironment } from "@prosopo/env";
import { getPairAsync } from "@prosopo/keyring";
import { i18nMiddleware } from "@prosopo/locale";
import {
api,
createApiAdminRoutesProvider,
domainMiddleware,
getClientList,
headerCheckMiddleware,
Expand All @@ -37,6 +37,7 @@ import {
createApiRuleRoutesProvider,
getExpressApiRuleRateLimits,
} from "@prosopo/user-access-policy";
import { apiRulePaths } from "@prosopo/user-access-policy";
import cors from "cors";
import express from "express";
import rateLimit from "express-rate-limit";
Expand All @@ -57,7 +58,7 @@ function startApi(
const apiRuleRoutesProvider = createApiRuleRoutesProvider(
env.getDb().getUserAccessRulesStorage(),
);
const apiAdminRoutesProvider = api.admin.createApiAdminRoutesProvider(env);
const apiAdminRoutesProvider = createApiAdminRoutesProvider(env);

// https://express-rate-limit.mintlify.app/guides/troubleshooting-proxy-issues
apiApp.set(
Expand All @@ -77,21 +78,22 @@ function startApi(
apiApp.use(publicRouter(env));

// Admin routes
env.logger.info("Enabling admin auth middleware");
apiApp.use("/v1/prosopo/provider/admin", authMiddleware(env));
apiApp.use(apiRulePaths.INSERT_MANY, authMiddleware(env));
apiApp.use(apiRulePaths.DELETE_MANY, authMiddleware(env));
apiApp.use(
apiExpressRouterFactory.createRouter(
apiRuleRoutesProvider,
apiEndpointAdapter,
),
authMiddleware(env),
);
apiApp.use(
apiExpressRouterFactory.createRouter(
apiAdminRoutesProvider,
// unlike the default one, it should have errorStatusCode as 400
createApiExpressDefaultEndpointAdapter(env.logger, 400),
),
authMiddleware(env),
);

// Rate limiting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import type { ProviderEnvironment } from "@prosopo/types-env";
import { Tasks } from "../../tasks/index.js";
import { ApiAdminRoutesProvider } from "./apiAdminRoutesProvider.js";

const createApiAdminRoutesProvider = (
export const createApiAdminRoutesProvider = (
providerEnvironment: ProviderEnvironment,
): ApiRoutesProvider => {
const tasks = new Tasks(providerEnvironment);

return new ApiAdminRoutesProvider(tasks);
};

export { createApiAdminRoutesProvider };
17 changes: 0 additions & 17 deletions packages/provider/src/api/admin/export.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/provider/src/api/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const authMiddleware = (env: ProviderEnvironment) => {
return;
}

console.log("In admin auth middleware");
const { signature, timestamp } = extractHeaders(req);

if (env.authAccount) {
Expand Down
14 changes: 0 additions & 14 deletions packages/provider/src/api/export.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/provider/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export * from "./api/domainMiddleware.js";
export * from "./schedulers/captchaScheduler.js";
export * from "./schedulers/getClientList.js";
export * from "./api/headerCheckMiddleware.js";
export * as api from "./api/export.js";
export * from "./api/admin/createApiAdminRoutesProvider.js";
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("Image Captcha Integration Tests", () => {
await registerSiteKey(dappAccount, CaptchaType.image);
});

it("should supply an image captcha challenge to a Dapp User", async () => {
it.only("should supply an image captcha challenge to a Dapp User", async () => {
const origin = "http://localhost";
const getImageCaptchaURL = `${baseUrl}${ApiPaths.GetImageCaptchaChallenge}`;
const getImgCaptchaBody: CaptchaRequestBodyType = {
Expand Down
2 changes: 2 additions & 0 deletions packages/user-access-policy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { ApiRoutesProvider } from "@prosopo/api-route";
import type { Logger } from "@prosopo/common";
import type { Model } from "mongoose";
import type { BlacklistInspector } from "./blacklistInspector.js";
import { apiRulePaths } from "./rules/api/apiRulePaths.js";
import { ApiRuleRoutesProvider } from "./rules/api/apiRuleRoutesProvider.js";
import { getExpressApiRuleRateLimits } from "./rules/api/getExpressApiRuleRateLimits.js";
import { BlacklistRulesInspector } from "./rules/blacklistRulesInspector.js";
Expand Down Expand Up @@ -63,4 +64,5 @@ export {
createApiRuleRoutesProvider,
getRuleMongooseSchema,
getExpressApiRuleRateLimits,
apiRulePaths,
};
4 changes: 1 addition & 3 deletions packages/user-access-policy/src/rules/api/apiRulePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const apiRulePaths = {
export const apiRulePaths = {
INSERT_MANY: "/v1/prosopo/user-access-policy/rules/insert-many",
DELETE_MANY: "/v1/prosopo/user-access-policy/rules/delete-many",
};

export { apiRulePaths };

0 comments on commit a6b8c7b

Please sign in to comment.