From ee7864da8c191a626e5b579aa6bf2ef86d9576cc Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 14 May 2024 11:30:45 -0500 Subject: [PATCH] Add a signerMiddleware --- src/app.ts | 17 ++++++++++++++--- src/middleware/signerMiddleware.ts | 13 +++++++++++++ src/signers/APISigner.ts | 1 + 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/middleware/signerMiddleware.ts diff --git a/src/app.ts b/src/app.ts index 05a1c81..4f93608 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,4 +1,4 @@ -import { NostrEvent, NStore } from '@nostrify/nostrify'; +import { NostrEvent, NostrSigner, NStore } from '@nostrify/nostrify'; import Debug from '@soapbox/stickynotes/debug'; import { type Context, Env as HonoEnv, type Handler, Hono, Input as HonoInput, type MiddlewareHandler } from 'hono'; import { cors, logger, serveStatic } from 'hono/middleware'; @@ -29,6 +29,7 @@ import { adminAccountAction, adminAccountsController } from '@/controllers/api/a import { appCredentialsController, createAppController } from '@/controllers/api/apps.ts'; import { blocksController } from '@/controllers/api/blocks.ts'; import { bookmarksController } from '@/controllers/api/bookmarks.ts'; +import { adminRelaysController, adminSetRelaysController } from '@/controllers/api/ditto.ts'; import { emptyArrayController, emptyObjectController, notImplementedController } from '@/controllers/api/fallback.ts'; import { instanceController } from '@/controllers/api/instance.ts'; import { markersController, updateMarkersController } from '@/controllers/api/markers.ts'; @@ -84,13 +85,15 @@ import { auth19, requirePubkey } from '@/middleware/auth19.ts'; import { auth98, requireProof, requireRole } from '@/middleware/auth98.ts'; import { cache } from '@/middleware/cache.ts'; import { csp } from '@/middleware/csp.ts'; -import { adminRelaysController, adminSetRelaysController } from '@/controllers/api/ditto.ts'; +import { signerMiddleware } from '@/middleware/signerMiddleware.ts'; import { storeMiddleware } from '@/middleware/store.ts'; import { blockController } from '@/controllers/api/accounts.ts'; import { unblockController } from '@/controllers/api/accounts.ts'; interface AppEnv extends HonoEnv { Variables: { + /** Signer to get the logged-in user's pubkey, relays, and to sign events, or `undefined` if the user isn't logged in. */ + signer?: NostrSigner; /** Hex pubkey for the current user. If provided, the user is considered "logged in." */ pubkey?: string; /** Hex secret key for the current user. Optional, but easiest way to use legacy Mastodon apps. */ @@ -123,7 +126,15 @@ app.get('/api/v1/streaming', streamingController); app.get('/api/v1/streaming/', streamingController); app.get('/relay', relayController); -app.use('*', csp(), cors({ origin: '*', exposeHeaders: ['link'] }), auth19, auth98(), storeMiddleware); +app.use( + '*', + csp(), + cors({ origin: '*', exposeHeaders: ['link'] }), + auth19, + auth98(), + storeMiddleware, + signerMiddleware, +); app.get('/.well-known/webfinger', webfingerController); app.get('/.well-known/host-meta', hostMetaController); diff --git a/src/middleware/signerMiddleware.ts b/src/middleware/signerMiddleware.ts new file mode 100644 index 0000000..8e7eb7a --- /dev/null +++ b/src/middleware/signerMiddleware.ts @@ -0,0 +1,13 @@ +import { AppMiddleware } from '@/app.ts'; +import { APISigner } from '@/signers/APISigner.ts'; + +/** Make a `signer` object available to all controllers, or unset if the user isn't logged in. */ +export const signerMiddleware: AppMiddleware = async (c, next) => { + try { + c.set('signer', new APISigner(c)); + } catch { + // do nothing + } + + await next(); +}; diff --git a/src/signers/APISigner.ts b/src/signers/APISigner.ts index e9914b1..0a9317b 100644 --- a/src/signers/APISigner.ts +++ b/src/signers/APISigner.ts @@ -2,6 +2,7 @@ import { NConnectSigner, NostrEvent, NostrSigner, NSecSigner } from '@nostrify/nostrify'; import { HTTPException } from 'hono'; + import { type AppContext } from '@/app.ts'; import { AdminSigner } from '@/signers/AdminSigner.ts'; import { Storages } from '@/storages.ts';