Add oauth event handler everywhere, dedup auth_url handling, add requested perms, add since to reply filter

This commit is contained in:
artur 2024-03-08 08:49:36 +03:00
parent 2b98f0fc4a
commit 4f7b9f1b99
3 changed files with 15 additions and 6 deletions

View File

@ -85,6 +85,9 @@ export default function useLoginHandler() {
}
} else if (key.startsWith("bunker://")) {
const nip46 = new Nip46Signer(key);
nip46.on("oauth", url => {
window.open(url, CONFIG.appNameCapitalized, "width=600,height=800,popup=yes");
});
await nip46.init();
const loginPubkey = await nip46.getPubKey();

View File

@ -247,6 +247,9 @@ export function createPublisher(l: LoginSession) {
const relayArgs = (l.remoteSignerRelays ?? []).map(a => `relay=${encodeURIComponent(a)}`);
const inner = new PrivateKeySigner(unwrap(l.privateKeyData as KeyStorage).value);
const nip46 = new Nip46Signer(`bunker://${unwrap(l.publicKey)}?${[...relayArgs].join("&")}`, inner);
nip46.on("oauth", url => {
window.open(url, CONFIG.appNameCapitalized, "width=600,height=800,popup=yes");
});
return new EventPublisher(nip46, unwrap(l.publicKey));
}
case LoginSessionType.Nip7os: {

View File

@ -11,6 +11,8 @@ import EventKind from "../event-kind";
import { EventEmitter } from "eventemitter3";
const NIP46_KIND = 24_133;
// FIXME add all kinds that Snort signs
const PERMS = "nip04_encrypt,nip04_decrypt,sign_event:0,sign_event:1,sign_event:3,sign_event:4,sign_event:6,sign_event:7,sign_event:30078"
interface Nip46Metadata {
name: string;
@ -34,6 +36,7 @@ interface Nip46Response {
interface QueueObj {
resolve: (o: Nip46Response) => void;
reject: (e: Error) => void;
authed?: boolean;
}
interface Nip46Events {
@ -112,6 +115,8 @@ export class Nip46Signer extends EventEmitter<Nip46Events> implements EventSigne
{
kinds: [NIP46_KIND],
"#p": [this.#localPubkey],
// strfry doesn't always delete ephemeral events
since: Math.floor(Date.now() / 1000 - 10),
},
],
() => {},
@ -195,7 +200,7 @@ export class Nip46Signer extends EventEmitter<Nip46Events> implements EventSigne
*/
async createAccount(name: string, domain: string, email?: string) {
await this.init(false);
const rsp = await this.#rpc("create_account", [name, domain, email ?? ""]);
const rsp = await this.#rpc("create_account", [name, domain, email ?? "", PERMS]);
if (!rsp.error) {
this.#remotePubkey = rsp.result as string;
}
@ -206,10 +211,7 @@ export class Nip46Signer extends EventEmitter<Nip46Events> implements EventSigne
}
async #connect(pk: string) {
const connectParams = [pk];
if (this.#token) {
connectParams.push(this.#token);
}
const connectParams = [pk, this.#token ?? '', PERMS];
return await this.#rpc("connect", connectParams);
}
@ -241,7 +243,8 @@ export class Nip46Signer extends EventEmitter<Nip46Events> implements EventSigne
}
if ("result" in reply && reply.result === "auth_url") {
this.emit("oauth", reply.error);
if (!pending.authed) this.emit("oauth", reply.error);
pending.authed = true;
} else {
const rx = reply as Nip46Response;
if (rx.error) {