Add a ConnectSigner to wrap our default opts to NConnectSigner, add c.set('signer') calls to nip98 middleware
This commit is contained in:
parent
c715827c81
commit
1accae2222
|
@ -126,9 +126,9 @@ app.use(
|
||||||
'*',
|
'*',
|
||||||
csp(),
|
csp(),
|
||||||
cors({ origin: '*', exposeHeaders: ['link'] }),
|
cors({ origin: '*', exposeHeaders: ['link'] }),
|
||||||
|
signerMiddleware,
|
||||||
auth98(),
|
auth98(),
|
||||||
storeMiddleware,
|
storeMiddleware,
|
||||||
signerMiddleware,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
app.get('/.well-known/webfinger', webfingerController);
|
app.get('/.well-known/webfinger', webfingerController);
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
import { NostrEvent } from '@nostrify/nostrify';
|
import { NostrEvent } from '@nostrify/nostrify';
|
||||||
import { HTTPException } from 'hono';
|
import { HTTPException } from 'hono';
|
||||||
|
|
||||||
import { type AppContext, type AppMiddleware } from '@/app.ts';
|
import { type AppContext, type AppMiddleware } from '@/app.ts';
|
||||||
|
import { findUser, User } from '@/db/users.ts';
|
||||||
|
import { ConnectSigner } from '@/signers/ConnectSigner.ts';
|
||||||
|
import { localRequest } from '@/utils/api.ts';
|
||||||
import {
|
import {
|
||||||
buildAuthEventTemplate,
|
buildAuthEventTemplate,
|
||||||
parseAuthRequest,
|
parseAuthRequest,
|
||||||
type ParseAuthRequestOpts,
|
type ParseAuthRequestOpts,
|
||||||
validateAuthEvent,
|
validateAuthEvent,
|
||||||
} from '@/utils/nip98.ts';
|
} from '@/utils/nip98.ts';
|
||||||
import { localRequest } from '@/utils/api.ts';
|
|
||||||
import { findUser, User } from '@/db/users.ts';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NIP-98 auth.
|
* NIP-98 auth.
|
||||||
|
@ -20,7 +22,7 @@ function auth98(opts: ParseAuthRequestOpts = {}): AppMiddleware {
|
||||||
const result = await parseAuthRequest(req, opts);
|
const result = await parseAuthRequest(req, opts);
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
c.set('pubkey', result.data.pubkey);
|
c.set('signer', new ConnectSigner(result.data.pubkey));
|
||||||
c.set('proof', result.data);
|
c.set('proof', result.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +80,7 @@ function withProof(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (proof) {
|
if (proof) {
|
||||||
c.set('pubkey', proof.pubkey);
|
c.set('signer', new ConnectSigner(proof.pubkey));
|
||||||
c.set('proof', proof);
|
c.set('proof', proof);
|
||||||
await handler(c, proof, next);
|
await handler(c, proof, next);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import { NConnectSigner, NSecSigner } from '@nostrify/nostrify';
|
import { NSecSigner } from '@nostrify/nostrify';
|
||||||
import { nip19 } from 'nostr-tools';
|
import { nip19 } from 'nostr-tools';
|
||||||
|
|
||||||
import { AppMiddleware } from '@/app.ts';
|
import { AppMiddleware } from '@/app.ts';
|
||||||
import { AdminSigner } from '@/signers/AdminSigner.ts';
|
import { ConnectSigner } from '@/signers/ConnectSigner.ts';
|
||||||
import { Storages } from '@/storages.ts';
|
|
||||||
|
|
||||||
/** We only accept "Bearer" type. */
|
/** We only accept "Bearer" type. */
|
||||||
const BEARER_REGEX = new RegExp(`^Bearer (${nip19.BECH32_REGEX.source})$`);
|
const BEARER_REGEX = new RegExp(`^Bearer (${nip19.BECH32_REGEX.source})$`);
|
||||||
|
@ -21,15 +20,7 @@ export const signerMiddleware: AppMiddleware = async (c, next) => {
|
||||||
|
|
||||||
switch (decoded.type) {
|
switch (decoded.type) {
|
||||||
case 'npub':
|
case 'npub':
|
||||||
c.set(
|
c.set('signer', new ConnectSigner(decoded.data));
|
||||||
'signer',
|
|
||||||
new NConnectSigner({
|
|
||||||
pubkey: decoded.data,
|
|
||||||
relay: Storages.pubsub,
|
|
||||||
signer: new AdminSigner(),
|
|
||||||
timeout: 60000,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
case 'nsec':
|
case 'nsec':
|
||||||
c.set('signer', new NSecSigner(decoded.data));
|
c.set('signer', new NSecSigner(decoded.data));
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
import { NConnectSigner } from '@nostrify/nostrify';
|
||||||
|
|
||||||
|
import { AdminSigner } from '@/signers/AdminSigner.ts';
|
||||||
|
import { Storages } from '@/storages.ts';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NIP-46 signer.
|
||||||
|
*
|
||||||
|
* Simple extension of nostrify's `NConnectSigner`, with our options to keep it DRY.
|
||||||
|
*/
|
||||||
|
export class ConnectSigner extends NConnectSigner {
|
||||||
|
constructor(pubkey: string) {
|
||||||
|
super({
|
||||||
|
pubkey,
|
||||||
|
relay: Storages.pubsub,
|
||||||
|
signer: new AdminSigner(),
|
||||||
|
timeout: 60000,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue