From 48f04e48dede2640c86ec9e8a816f7168e74ef83 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 4 Mar 2023 22:49:08 -0600 Subject: [PATCH] Enable CORS, fix nsec1 keys --- .gitignore | 1 + src/api/oauth.ts | 7 ++++++- src/app.ts | 4 +++- src/deps.ts | 1 + 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/src/api/oauth.ts b/src/api/oauth.ts index fa3dbac..e6db5b8 100644 --- a/src/api/oauth.ts +++ b/src/api/oauth.ts @@ -1,3 +1,5 @@ +import converter from 'npm:bech32-converting'; + import { validator, z } from '@/deps.ts'; const createTokenSchema = z.object({ @@ -8,8 +10,11 @@ const createTokenController = validator('json', (value, c) => { const result = createTokenSchema.safeParse(value); if (result.success) { + const password = result.data.password; + const token = password.startsWith('nsec1') ? converter('nsec').toHex(password).slice(2) : password; + return c.json({ - access_token: result.data.password, + access_token: token, token_type: 'Bearer', scope: 'read write follow push', created_at: Math.floor(new Date().getTime() / 1000), diff --git a/src/app.ts b/src/app.ts index 5278497..95470fe 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,4 +1,4 @@ -import { Hono } from '@/deps.ts'; +import { cors, Hono } from '@/deps.ts'; import { credentialsController } from './api/accounts.ts'; import { appCredentialsController, createAppController } from './api/apps.ts'; @@ -8,6 +8,8 @@ import { createTokenController } from './api/oauth.ts'; const app = new Hono(); +app.use('/*', cors()); + app.get('/api/v1/instance', instanceController); app.get('/api/v1/apps/verify_credentials', appCredentialsController); diff --git a/src/deps.ts b/src/deps.ts index 12042ca..104db22 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -1,5 +1,6 @@ import { Context, Hono, validator } from 'https://deno.land/x/hono@v3.0.2/mod.ts'; export { Hono, validator }; +export { cors } from 'https://deno.land/x/hono@v3.0.2/middleware.ts'; export { z } from 'https://deno.land/x/zod@v3.20.5/mod.ts'; export { Author, RelayPool } from 'https://dev.jspm.io/nostr-relaypool@0.5.3'; export { getPublicKey } from 'https://dev.jspm.io/nostr-tools@1.6.0';