Add a getClientConnectUri function, add "Nostr Connect" link in the OAuth form

This commit is contained in:
Alex Gleason 2024-05-12 13:12:46 -05:00
parent bdfa6f8826
commit 3b0739f187
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 25 additions and 1 deletions

View File

@ -2,10 +2,11 @@ import { encodeBase64 } from '@std/encoding/base64';
import { nip19 } from 'nostr-tools';
import { z } from 'zod';
import { lodash } from '@/deps.ts';
import { AppController } from '@/app.ts';
import { lodash } from '@/deps.ts';
import { nostrNow } from '@/utils.ts';
import { parseBody } from '@/utils/api.ts';
import { getClientConnectUri } from '@/utils/connect.ts';
const passwordGrantSchema = z.object({
grant_type: z.literal('password'),
@ -68,6 +69,7 @@ const oauthController: AppController = async (c) => {
}
const redirectUri = maybeDecodeUri(encodedUri);
const connectUri = await getClientConnectUri(c.req.raw.signal);
const script = `
window.addEventListener('load', function() {
@ -101,6 +103,8 @@ const oauthController: AppController = async (c) => {
<input type="hidden" name="redirect_uri" id="redirect_uri" value="${lodash.escape(redirectUri)}">
<button type="submit">Authorize</button>
</form>
<br>
<a href="${lodash.escape(connectUri)}">Nostr Connect</a>
</body>
</html>
`);

20
src/utils/connect.ts Normal file
View File

@ -0,0 +1,20 @@
import { Conf } from '@/config.ts';
import { getInstanceMetadata } from '@/utils/instance.ts';
/** Get NIP-46 `nostrconnect://` URI for the Ditto server. */
export async function getClientConnectUri(signal?: AbortSignal): Promise<string> {
const uri = new URL('nostrconnect://');
const { name, description } = await getInstanceMetadata(signal);
const metadata = {
name,
description,
url: Conf.localDomain,
};
uri.host = Conf.pubkey;
uri.searchParams.set('relay', Conf.relay);
uri.searchParams.set('metadata', JSON.stringify(metadata));
return uri.toString();
}