Skip to content

Commit

Permalink
feat: add auth middleware to notes
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Jun 12, 2024
1 parent d2b3c8d commit be83b8b
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions pkg/notes/mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import { Result } from '@mikuroxina/mini-fn';
import { Cat, Ether, Promise, Result } from '@mikuroxina/mini-fn';

Check warning on line 2 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L2

Added line #L2 was not covered by tests

import type { AccountID } from '../accounts/model/account.js';
import { authenticateToken } from '../accounts/service/authenticationTokenService.js';
import { authenticateMiddleware } from '../adaptors/authenticateMiddleware.js';

Check warning on line 6 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L5-L6

Added lines #L5 - L6 were not covered by tests
import { prismaClient } from '../adaptors/prisma.js';
import { SnowflakeIDGenerator } from '../id/mod.js';
import type { ID } from '../id/type.js';
Expand Down Expand Up @@ -31,7 +33,22 @@ import { FetchBookmarkService } from './service/fetchBookmark.js';
import { RenoteService } from './service/renote.js';

const isProduction = process.env.NODE_ENV === 'production';
export const noteHandlers = new OpenAPIHono();
export const noteHandlers = new OpenAPIHono<{
Variables: {
/*
* @description authorization token (JWT, ES256)
*/
token: string;
/*
* @description whether the token is checked (if allowUnAuthorized set true, this will be false)
*/
isValidToken: boolean;
/*
* @description account name (if allowUnAuthorized set true, this will be undefined)
*/
accountName?: string;
};
}>();

Check warning on line 51 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L36-L51

Added lines #L36 - L51 were not covered by tests
const noteRepository = isProduction
? new PrismaNoteRepository(prismaClient)
: new InMemoryNoteRepository();
Expand All @@ -42,6 +59,19 @@ const idGenerator = new SnowflakeIDGenerator(0, {
now: () => BigInt(Date.now()),
});

const composer = Ether.composeT(Promise.monad);
const liftOverPromise = <const D extends Record<string, symbol>, T>(
ether: Ether.Ether<D, T>,
): Ether.EtherT<D, Promise.PromiseHkt, T> => ({
...ether,
handler: (resolved) => Promise.pure(ether.handler(resolved)),
});
const AuthMiddleware = await Ether.runEtherT(
Cat.cat(liftOverPromise(authenticateMiddleware)).feed(
composer(authenticateToken),
).value,
);

Check warning on line 74 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L62-L74

Added lines #L62 - L74 were not covered by tests
// Account
const accountModule = new AccountModule();

Expand Down Expand Up @@ -77,6 +107,10 @@ noteHandlers.doc('/notes/doc.json', {
},
});

noteHandlers[CreateNoteRoute.method](
CreateNoteRoute.path,
AuthMiddleware.handle({ allowUnAuthorized: false }),
);

Check warning on line 113 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L110-L113

Added lines #L110 - L113 were not covered by tests
noteHandlers.openapi(CreateNoteRoute, async (c) => {
const { content, visibility, contents_warning_comment, send_to } =
c.req.valid('json');
Expand All @@ -94,6 +128,10 @@ noteHandlers.openapi(CreateNoteRoute, async (c) => {
return c.json(res[1]);
});

noteHandlers[GetNoteRoute.method](
GetNoteRoute.path,
AuthMiddleware.handle({ allowUnAuthorized: true }),
);

Check warning on line 134 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L131-L134

Added lines #L131 - L134 were not covered by tests
noteHandlers.openapi(GetNoteRoute, async (c) => {
const { id } = c.req.param();
const res = await controller.getNoteByID(id);
Expand All @@ -104,6 +142,10 @@ noteHandlers.openapi(GetNoteRoute, async (c) => {
return c.json(res[1]);
});

noteHandlers[RenoteRoute.method](
RenoteRoute.path,
AuthMiddleware.handle({ allowUnAuthorized: false }),
);

Check warning on line 148 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L145-L148

Added lines #L145 - L148 were not covered by tests
noteHandlers.openapi(RenoteRoute, async (c) => {
const { id } = c.req.param();
const req = c.req.valid('json');
Expand All @@ -122,6 +164,10 @@ noteHandlers.openapi(RenoteRoute, async (c) => {
return c.json(res[1]);
});

noteHandlers[CreateBookmarkRoute.method](
CreateBookmarkRoute.path,
AuthMiddleware.handle({ allowUnAuthorized: false }),
);

Check warning on line 170 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L167-L170

Added lines #L167 - L170 were not covered by tests
noteHandlers.openapi(CreateBookmarkRoute, async (c) => {
const { id: noteID } = c.req.valid('param');
// ToDo: read AccountID from token
Expand All @@ -137,6 +183,10 @@ noteHandlers.openapi(CreateBookmarkRoute, async (c) => {
return c.json(res[1]);
});

noteHandlers[DeleteBookmarkRoute.method](
DeleteBookmarkRoute.path,
AuthMiddleware.handle({ allowUnAuthorized: false }),
);

Check warning on line 189 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L186-L189

Added lines #L186 - L189 were not covered by tests
noteHandlers.openapi(DeleteBookmarkRoute, async (c) => {
const { id: noteID } = c.req.valid('param');
// ToDo: read AccountID from token
Expand Down

0 comments on commit be83b8b

Please sign in to comment.