mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-27 09:14:20 +01:00
Add a UUID to auth tokens for private websocket event signing
This commit is contained in:
39
src/utils.ts
39
src/utils.ts
@@ -1,5 +1,5 @@
|
||||
import { getAuthor } from '@/client.ts';
|
||||
import { Context, getPublicKey, nip19, parseFormData } from '@/deps.ts';
|
||||
import { nip19, parseFormData } from '@/deps.ts';
|
||||
import { type Event } from '@/event.ts';
|
||||
import { lookupNip05Cached } from '@/nip05.ts';
|
||||
|
||||
@@ -9,30 +9,6 @@ const nostrNow = () => Math.floor(new Date().getTime() / 1000);
|
||||
/** Pass to sort() to sort events by date. */
|
||||
const eventDateComparator = (a: Event, b: Event) => b.created_at - a.created_at;
|
||||
|
||||
function getKeys(c: Context) {
|
||||
const auth = c.req.headers.get('Authorization') || '';
|
||||
|
||||
if (auth.startsWith('Bearer ')) {
|
||||
const privatekey = auth.split('Bearer ')[1];
|
||||
const pubkey = getPublicKey(privatekey);
|
||||
|
||||
return {
|
||||
privatekey,
|
||||
pubkey,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/** Return true if the value is a bech32 string, eg for use with NIP-19. */
|
||||
function isBech32(value: unknown): value is string {
|
||||
return typeof value === 'string' && nip19.BECH32_REGEX.test(value);
|
||||
}
|
||||
|
||||
/** Return true if the value is a Nostr pubkey, private key, or event ID. */
|
||||
function isNostrId(value: unknown): value is string {
|
||||
return typeof value === 'string' && /^[0-9a-f]{64}$/.test(value);
|
||||
}
|
||||
|
||||
/** Get pubkey from bech32 string, if applicable. */
|
||||
function bech32ToPubkey(bech32: string): string | undefined {
|
||||
try {
|
||||
@@ -99,15 +75,4 @@ async function parseBody(req: Request): Promise<unknown> {
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
bech32ToPubkey,
|
||||
eventDateComparator,
|
||||
getKeys,
|
||||
isBech32,
|
||||
isNostrId,
|
||||
lookupAccount,
|
||||
type Nip05,
|
||||
nostrNow,
|
||||
parseBody,
|
||||
parseNip05,
|
||||
};
|
||||
export { bech32ToPubkey, eventDateComparator, lookupAccount, type Nip05, nostrNow, parseBody, parseNip05 };
|
||||
|
||||
Reference in New Issue
Block a user