Add bunker login

This commit is contained in:
Jon Staab 2024-10-29 14:40:37 -07:00
parent 16c942c917
commit 12fab67961
10 changed files with 108 additions and 46 deletions

View file

@ -23,7 +23,8 @@ import {
import type {TrustedEvent, EventTemplate, List} from "@welshman/util" import type {TrustedEvent, EventTemplate, List} from "@welshman/util"
import type {SubscribeRequestWithHandlers} from "@welshman/net" import type {SubscribeRequestWithHandlers} from "@welshman/net"
import {PublishStatus, AuthStatus, ConnectionStatus} from "@welshman/net" import {PublishStatus, AuthStatus, ConnectionStatus} from "@welshman/net"
import {Nip59, stamp} from "@welshman/signer" import {Nip59, makeSecret, stamp, Nip46Broker} from "@welshman/signer"
import type {Nip46Handler} from "@welshman/signer"
import { import {
pubkey, pubkey,
signer, signer,
@ -44,6 +45,8 @@ import {
userInboxRelaySelections, userInboxRelaySelections,
nip44EncryptToSelf, nip44EncryptToSelf,
loadRelay, loadRelay,
addSession,
nip46Perms,
} from "@welshman/app" } from "@welshman/app"
import { import {
COMMENT, COMMENT,
@ -86,6 +89,24 @@ export const makeIMeta = (url: string, data: Record<string, string>) => [
...Object.entries(data).map(([k, v]) => [k, v].join(" ")), ...Object.entries(data).map(([k, v]) => [k, v].join(" ")),
] ]
// Log in
export const loginWithNip46 = async (token: string, handler: Nip46Handler) => {
const secret = makeSecret()
const broker = Nip46Broker.get({secret, handler})
const result = await broker.connect(token, nip46Perms)
if (!result) return false
const pubkey = await broker.getPublicKey()
if (!pubkey) return false
addSession({method: "nip46", pubkey, secret, handler})
return true
}
// Loaders // Loaders
export const loadUserData = ( export const loadUserData = (

View file

@ -0,0 +1,32 @@
<script lang="ts">
import Link from "@lib/components/Link.svelte"
import Button from "@lib/components/Button.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import {PLATFORM_NAME} from "@app/state"
</script>
<div class="column gap-4">
<ModalHeader>
<div slot="title">What is a bunker link?</div>
</ModalHeader>
<p>
<Link external class="link" href="https://nostr.com/">Nostr</Link> uses "keys" instead of
passwords to identify users. This allows users to own their social identity instead of
renting it from a tech company, and can bring it with them from app to app.
</p>
<p>
A good way to manage your keys is to use a remote signing application. These apps can hold
your keys and log you in remotely to as many applications as you like, without increasing
the risk of your keys being stolen.
</p>
<p>
One way to log in with a remote signer is using a "bunker link" which is more secure and
decentralized than other solutions. Check your signer for a link beginning with "bunker://",
copy it into {PLATFORM_NAME}, and you should be good to go!
</p>
<p>
If you don't have a signer yet, <Link external class="link" href="https://nsec.app/">nsec.app</Link>
is an easy way to get started.
</p>
<Button class="btn btn-primary" on:click={() => history.back()}>Got it</Button>
</div>

View file

@ -17,7 +17,7 @@
<div class="column gap-4"> <div class="column gap-4">
<div class="py-2"> <div class="py-2">
<h1 class="heading">Welcome to {PLATFORM_NAME}!</h1> <h1 class="heading">Welcome to {PLATFORM_NAME}!</h1>
<p class="text-center">The chat app built for sovereign communities.</p> <p class="text-center">The chat app built for self-hosted communities.</p>
</div> </div>
<Button on:click={logIn}> <Button on:click={logIn}>
<CardButton> <CardButton>

View file

@ -10,7 +10,7 @@
import SignUp from "@app/components/SignUp.svelte" import SignUp from "@app/components/SignUp.svelte"
import InfoNostr from "@app/components/InfoNostr.svelte" import InfoNostr from "@app/components/InfoNostr.svelte"
import LogInInfoRemoteSigner from "@app/components/LogInInfoRemoteSigner.svelte" import LogInInfoRemoteSigner from "@app/components/LogInInfoRemoteSigner.svelte"
import LogInKey from "@app/components/LogInKey.svelte" import LogInBunker from "@app/components/LogInBunker.svelte"
import {pushModal, clearModals} from "@app/modal" import {pushModal, clearModals} from "@app/modal"
import {PLATFORM_NAME} from "@app/state" import {PLATFORM_NAME} from "@app/state"
import {pushToast} from "@app/toast" import {pushToast} from "@app/toast"
@ -103,7 +103,7 @@
} }
} }
const loginWithKey = () => pushModal(LogInKey) const loginWithBunker = () => pushModal(LogInBunker)
let username = "" let username = ""
let domain = "nsec.app" let domain = "nsec.app"
@ -164,9 +164,9 @@
Log in with {app.name} Log in with {app.name}
</Button> </Button>
{/each} {/each}
<Button disabled={loading} on:click={loginWithKey} class="btn btn-neutral"> <Button disabled={loading} on:click={loginWithBunker} class="btn btn-neutral">
<Icon icon="key" /> <Icon icon="cpu" />
Log in with Key Log in with Bunker Link
</Button> </Button>
<div class="text-sm"> <div class="text-sm">
Need an account? Need an account?

View file

@ -1,6 +1,5 @@
<script lang="ts"> <script lang="ts">
import {nip19} from "nostr-tools" import {Nip46Broker} from "@welshman/signer"
import {getPubkey} from "@welshman/signer"
import {addSession} from "@welshman/app" import {addSession} from "@welshman/app"
import Spinner from "@lib/components/Spinner.svelte" import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
@ -8,68 +7,60 @@
import Icon from "@lib/components/Icon.svelte" import Icon from "@lib/components/Icon.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte" import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte" import ModalFooter from "@lib/components/ModalFooter.svelte"
import InfoNostr from "@app/components/InfoNostr.svelte" import InfoBunker from "@app/components/InfoBunker.svelte"
import {loadUserData} from "@app/commands" import {loginWithNip46, loadUserData} from "@app/commands"
import {pushModal, clearModals} from "@app/modal" import {pushModal, clearModals} from "@app/modal"
import {pushToast} from "@app/toast" import {pushToast} from "@app/toast"
import {PLATFORM_NAME} from "@app/state"
const back = () => history.back() const back = () => history.back()
const onSubmit = async () => { const onSubmit = async () => {
let secret = key const {pubkey, token, relays} = Nip46Broker.parseBunkerLink(bunker)
if (secret.startsWith("nsec")) { if (!pubkey || relays.length === 0) {
secret = nip19.decode(secret).data as string
}
if (!isKeyValid(secret)) {
return pushToast({ return pushToast({
theme: "error", theme: "error",
message: "Sorry, it looks like that's an invalid private key.", message: "Sorry, it looks like that's an invalid bunker link.",
}) })
} }
const pubkey = getPubkey(secret)
addSession({method: "nip01", pubkey, secret})
loading = true loading = true
await loadUserData(pubkey) try {
if (!await loginWithNip46(token, {pubkey, relays})) {
return pushToast({
theme: "error",
message: "Something went wrong, please try again!",
})
}
await loadUserData(pubkey)
} finally {
loading = false
}
clearModals() clearModals()
} }
const isKeyValid = (key: string) => { let bunker = ""
// Validate the key before setting it to state by encoding it using bech32.
// This will error if invalid (this works whether it's a public or a private key)
try {
getPubkey(key)
} catch (e) {
return false
}
return true
}
let key = ""
let loading = false let loading = false
</script> </script>
<form class="column gap-4" on:submit|preventDefault={onSubmit}> <form class="column gap-4" on:submit|preventDefault={onSubmit}>
<ModalHeader> <ModalHeader>
<div slot="title">Log In</div> <div slot="title">Log In</div>
<div slot="info">Already have a nostr key?</div> <div slot="info">Connect your signer app with {PLATFORM_NAME} using a bunker link.</div>
</ModalHeader> </ModalHeader>
<Field> <Field>
<p slot="label">Private Key*</p> <p slot="label">Bunker Link*</p>
<label class="input input-bordered flex w-full items-center gap-2" slot="input"> <label class="input input-bordered flex w-full items-center gap-2" slot="input">
<Icon icon="key" /> <Icon icon="cpu" />
<input bind:value={key} class="grow" type="password" /> <input bind:value={bunker} class="grow" placeholder="bunker://" />
</label> </label>
<p slot="info"> <p slot="info">
A nostr nsec or private key. Note that this log in method is not recommended. A login link provided by a nostr signing app.
<Button class="link" on:click={() => pushModal(InfoNostr)}>What is nostr?</Button> <Button class="link" on:click={() => pushModal(InfoBunker)}>What is a bunker link?</Button>
</p> </p>
</Field> </Field>
<ModalFooter> <ModalFooter>
@ -79,7 +70,7 @@
</Button> </Button>
<Button type="submit" class="btn btn-primary" disabled={loading}> <Button type="submit" class="btn btn-primary" disabled={loading}>
<Spinner {loading}>Next</Spinner> <Spinner {loading}>Next</Spinner>
<Icon icon="alt-arrow-right" class="!bg-base-300" /> <Icon icon="alt-arrow-right" />
</Button> </Button>
</ModalFooter> </ModalFooter>
</form> </form>

View file

@ -55,7 +55,7 @@
</Button> </Button>
<Button type="submit" class="btn btn-primary"> <Button type="submit" class="btn btn-primary">
Next Next
<Icon icon="alt-arrow-right" class="!bg-base-300" /> <Icon icon="alt-arrow-right" />
</Button> </Button>
</ModalFooter> </ModalFooter>
</form> </form>

View file

@ -51,7 +51,7 @@
</Button> </Button>
<Button type="submit" class="btn btn-primary"> <Button type="submit" class="btn btn-primary">
Next Next
<Icon icon="alt-arrow-right" class="!bg-base-300" /> <Icon icon="alt-arrow-right" />
</Button> </Button>
</ModalFooter> </ModalFooter>
</form> </form>

View file

@ -37,7 +37,7 @@
</Button> </Button>
<Button type="submit" class="btn btn-primary"> <Button type="submit" class="btn btn-primary">
Let's go Let's go
<Icon icon="alt-arrow-right" class="!bg-base-300" /> <Icon icon="alt-arrow-right" />
</Button> </Button>
</ModalFooter> </ModalFooter>
</form> </form>

16
src/assets/icons/CPU.svg Normal file
View file

@ -0,0 +1,16 @@
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7 10C7 8.58579 7 7.87868 7.43934 7.43934C7.87868 7 8.58579 7 10 7H14C15.4142 7 16.1213 7 16.5607 7.43934C17 7.87868 17 8.58579 17 10V14C17 15.4142 17 16.1213 16.5607 16.5607C16.1213 17 15.4142 17 14 17H10C8.58579 17 7.87868 17 7.43934 16.5607C7 16.1213 7 15.4142 7 14V10Z" stroke="#1C274C" stroke-width="1.5"/>
<path d="M4 12C4 8.22876 4 6.34315 5.17157 5.17157C6.34315 4 8.22876 4 12 4C15.7712 4 17.6569 4 18.8284 5.17157C20 6.34315 20 8.22876 20 12C20 15.7712 20 17.6569 18.8284 18.8284C17.6569 20 15.7712 20 12 20C8.22876 20 6.34315 20 5.17157 18.8284C4 17.6569 4 15.7712 4 12Z" stroke="#1C274C" stroke-width="1.5"/>
<path d="M4 12H2" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M22 12H20" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M4 9H2" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M22 9H20" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M4 15H2" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M22 15H20" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M12 20L12 22" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M12 2L12 4" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M9 20L9 22" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M9 2L9 4" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M15 20L15 22" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
<path d="M15 2L15 4" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
</svg>

After

Width:  |  Height:  |  Size: 1.7 KiB

View file

@ -34,6 +34,7 @@
import Copy from "@assets/icons/Copy.svg?dataurl" import Copy from "@assets/icons/Copy.svg?dataurl"
import Compass from "@assets/icons/Compass.svg?dataurl" import Compass from "@assets/icons/Compass.svg?dataurl"
import CompassBig from "@assets/icons/Compass Big.svg?dataurl" import CompassBig from "@assets/icons/Compass Big.svg?dataurl"
import CPU from "@assets/icons/CPU.svg?dataurl"
import Danger from "@assets/icons/Danger.svg?dataurl" import Danger from "@assets/icons/Danger.svg?dataurl"
import Exit from "@assets/icons/Exit.svg?dataurl" import Exit from "@assets/icons/Exit.svg?dataurl"
import File from "@assets/icons/File.svg?dataurl" import File from "@assets/icons/File.svg?dataurl"
@ -111,6 +112,7 @@
copy: Copy, copy: Copy,
compass: Compass, compass: Compass,
"compass-big": CompassBig, "compass-big": CompassBig,
cpu: CPU,
danger: Danger, danger: Danger,
exit: Exit, exit: Exit,
file: File, file: File,