From 7bd6b0246afea78798076cdf29bef48b0ebfae4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andri=20=C3=93skarsson?= Date: Mon, 30 Dec 2024 12:20:42 +0100 Subject: [PATCH] trying hard to not implement session atm --- apps/classifier/index.ts | 2 +- apps/web/src/actions/index.ts | 8 +-- .../src/components/astro/page-layout.astro | 3 +- .../components/react/bluesky/post-list.tsx | 9 ---- apps/web/src/middleware.ts | 51 +++++++++---------- .../pages/{ => admin}/feed/[rkey]/index.astro | 6 +-- .../src/pages/{ => admin}/feed/index.astro | 0 packages/atproto/index.ts | 1 - 8 files changed, 36 insertions(+), 44 deletions(-) rename apps/web/src/pages/{ => admin}/feed/[rkey]/index.astro (87%) rename apps/web/src/pages/{ => admin}/feed/index.astro (100%) diff --git a/apps/classifier/index.ts b/apps/classifier/index.ts index 43e33f1..0cd001b 100644 --- a/apps/classifier/index.ts +++ b/apps/classifier/index.ts @@ -1,4 +1,4 @@ -import { classifier } from "@andrioid/atproto"; +import { classifier } from "@andrioid/atproto/scripts/classifier"; classifier().catch((err) => { console.log("[classifier] app level error"); diff --git a/apps/web/src/actions/index.ts b/apps/web/src/actions/index.ts index b30bcfc..6cd4273 100644 --- a/apps/web/src/actions/index.ts +++ b/apps/web/src/actions/index.ts @@ -44,9 +44,11 @@ export const server = { postUri: z.string(), }), handler: async (input, ctx) => { - if (!ctx.locals.mayClassify) { - throw new Error("Not authorized to classify"); - } + // TODO: Verify this later when we have proper session + // if (!ctx.locals.mayClassify) { + // console.log("mayClassify", ctx.locals.mayClassify); + // return []; + // } return getPostTags(ctx.locals.at, input.postUri); }, }), diff --git a/apps/web/src/components/astro/page-layout.astro b/apps/web/src/components/astro/page-layout.astro index 74bac27..ec6d336 100644 --- a/apps/web/src/components/astro/page-layout.astro +++ b/apps/web/src/components/astro/page-layout.astro @@ -4,9 +4,10 @@ interface Props { } const { title } = Astro.props; +const { mayClassify } = Astro.locals; ---
-

{title}

+

{title} {mayClassify ? "֍" : ""}

diff --git a/apps/web/src/components/react/bluesky/post-list.tsx b/apps/web/src/components/react/bluesky/post-list.tsx index 3575577..2ff36e8 100644 --- a/apps/web/src/components/react/bluesky/post-list.tsx +++ b/apps/web/src/components/react/bluesky/post-list.tsx @@ -16,12 +16,3 @@ export function BskyPostList({ ); } - -function mockPromise(): Promise { - console.log("promise called"); - return new Promise((resolve) => { - setTimeout(() => { - resolve(true); - }, 500); - }); -} diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts index fb9f90d..6949c39 100644 --- a/apps/web/src/middleware.ts +++ b/apps/web/src/middleware.ts @@ -23,41 +23,40 @@ export const onRequest = defineMiddleware((context, next) => { if (url.pathname.match(/(^\/admin)/)) { // Lock it down a bit - if (mayClassify) { + if (context.locals.mayClassify) { return next(); - } else { - return new Response("Auth required", { - status: 401, - headers: { - "WWW-authenticate": 'Basic realm="Secure Area"', - }, - }); } + return new Response("Auth required", { + status: 401, + headers: { + "WWW-authenticate": 'Basic realm="Secure Area"', + }, + }); } return next(); }); function isAuthorized(context: APIContext): boolean { - try { - const basicAuth = context.request.headers.get("authorization"); - - if (basicAuth) { - // Get the auth value from string "Basic authValue" - const authValue = basicAuth.split(" ")[1] ?? "username:password"; - - // Decode the Base64 encoded string via atob (https://developer.mozilla.org/en-US/docs/Web/API/atob) - // Get the username and password. NB: the decoded string is in the form "username:password" - const [username, pwd] = atob(authValue).split(":"); - - // check if the username and password are valid - if (username === USER && pwd === PASSWORD) { - // forward request - return true; - } + const basicAuth = context.request.headers.get("authorization"); + + if (basicAuth && basicAuth.match(/^basic\ (\w+)/i)) { + // Get the auth value from string "Basic authValue" + const authValue = basicAuth.split(" ")[1] ?? "username:password"; + const decoded = atob(authValue); + //if (!decoded.match(/:/)) return false; + + // Decode the Base64 encoded string via atob (https://developer.mozilla.org/en-US/docs/Web/API/atob) + // Get the username and password. NB: the decoded string is in the form "username:password" + const [username, pwd] = decoded.split(":"); + + // check if the username and password are valid + if (username === USER && pwd === PASSWORD) { + // forward request + return true; } - } catch (err) { - // ignore } + console.log("not authorized", basicAuth); + return false; } diff --git a/apps/web/src/pages/feed/[rkey]/index.astro b/apps/web/src/pages/admin/feed/[rkey]/index.astro similarity index 87% rename from apps/web/src/pages/feed/[rkey]/index.astro rename to apps/web/src/pages/admin/feed/[rkey]/index.astro index 1dbaa57..556b87c 100644 --- a/apps/web/src/pages/feed/[rkey]/index.astro +++ b/apps/web/src/pages/admin/feed/[rkey]/index.astro @@ -1,6 +1,7 @@ --- import { DEFAULT_FEED_ACTOR, feeds, getPublicPosts } from "@andrioid/atproto"; import Layout from "~astro/layout.astro"; +import PageLayout from "~astro/page-layout.astro"; import { BskyPostList } from "~react/bluesky/post-list"; const { rkey } = Astro.params; @@ -18,8 +19,7 @@ const mayClassify = Astro.locals.mayClassify; --- -
-

{feed.record.displayName}

+ { feed.record.description && (

{feed.record.description}

@@ -32,5 +32,5 @@ const mayClassify = Astro.locals.mayClassify; mayClassify={mayClassify} />
- +
diff --git a/apps/web/src/pages/feed/index.astro b/apps/web/src/pages/admin/feed/index.astro similarity index 100% rename from apps/web/src/pages/feed/index.astro rename to apps/web/src/pages/admin/feed/index.astro diff --git a/packages/atproto/index.ts b/packages/atproto/index.ts index 6fe5df3..6ed99c6 100644 --- a/packages/atproto/index.ts +++ b/packages/atproto/index.ts @@ -12,4 +12,3 @@ export * from "./domain/jetstream-subscription"; export * from "./domain/queue-for-classification"; export * from "./feeds"; export * from "./helpers/jwt"; -export * from "./scripts/classifier"; -- 2.51.2