diff --git a/examples/authz/3rd-party-app/public/pubky.svg b/examples/authz/3rd-party-app/public/pubky.svg index 6802915..e24e21d 100644 --- a/examples/authz/3rd-party-app/public/pubky.svg +++ b/examples/authz/3rd-party-app/public/pubky.svg @@ -1 +1,9 @@ - + + + + diff --git a/examples/authz/3rd-party-app/src/pubky-auth-widget.js b/examples/authz/3rd-party-app/src/pubky-auth-widget.js index 2070f82..56b4f63 100644 --- a/examples/authz/3rd-party-app/src/pubky-auth-widget.js +++ b/examples/authz/3rd-party-app/src/pubky-auth-widget.js @@ -48,6 +48,8 @@ export class PubkyAuthWidget extends LitElement { // TODO: allow using mainnet /** @type {import("@synonymdev/pubky").PubkyClient} */ this.pubkyClient = window.pubky.PubkyClient.testnet(); + + this.caps = this.caps || "" } connectedCallback() { @@ -55,9 +57,12 @@ export class PubkyAuthWidget extends LitElement { let [url, promise] = this.pubkyClient.authRequest(this.relay || DEFAULT_HTTP_RELAY, this.caps); - promise.then(session => { - console.log({ id: session.pubky().z32(), capabilities: session.capabilities() }) - alert(`Successfully signed in to ${session.pubky().z32()} with capabilities: ${session.capabilities().join(",")}`) + promise.then(pubky => { + if (this.caps?.length > 0) { + alert(`Successfully signed in to ${pubky.z32()} with capabilities: ${this.caps}`) + } else { + alert(`Successfully authenticated ${pubky.z32()} without signing in to their homeserver`) + } }).catch(e => { console.error(e) }) @@ -134,10 +139,20 @@ export class PubkyAuthWidget extends LitElement { class=${this.open ? "open" : ""} >
@@ -219,16 +234,25 @@ export class PubkyAuthWidget extends LitElement { } .header { + width: 100%; height: var(--header-height); display: flex; justify-content: center; - align-items: center; + align-items:center; + } + + .header-content { + display: flex; + justify-content: center; + align-items: baseline; + column-gap: .5rem; } #widget .header .text { display: none; font-weight: bold; + font-size: 1.5rem; } #widget.open .header .text { @@ -242,14 +266,13 @@ export class PubkyAuthWidget extends LitElement { } #pubky-icon { - height: 100%; + height: 1.5rem; width: 100%; } #widget.open #pubky-icon { - width: var(--header-height); - height: 74%; + width: auto; } #widget-content{ diff --git a/pubky/pkg/README.md b/pubky/pkg/README.md index 18dce88..81b2cf4 100644 --- a/pubky/pkg/README.md +++ b/pubky/pkg/README.md @@ -98,7 +98,7 @@ let [pubkyauthUrl, sessionPromise] = client.authRequest(relay, capabilities); showQr(pubkyauthUrl); -let session = await sessionPromise; +let pubky = await sessionPromise; ``` Sign in to a user's Homeserver, without access to their [Keypair](#keypair), nor even [PublicKey](#publickey), @@ -109,7 +109,7 @@ instead request permissions (showing the user pubkyauthUrl), and await a Session Returns: - pubkyauthUrl: A url to show to the user to scan or paste into an Authenticator app holding the user [Keypair](#keypair) -- sessionPromise: A promise that resolves into a [Session](#session) on success. +- sessionPromise: A promise that resolves into a [PublicKey](#publickey) on success, which you can use in `client.session(pubky)` to resolve more information about the Session. #### sendAuthToken ```js diff --git a/pubky/pkg/test/auth.js b/pubky/pkg/test/auth.js index 2207946..fe7e559 100644 --- a/pubky/pkg/test/auth.js +++ b/pubky/pkg/test/auth.js @@ -56,8 +56,10 @@ test("3rd party signin", async (t) => { await client.sendAuthToken(keypair, pubkyauth_url) } - let session = await pubkyauthResponse; + let authedPubky = await pubkyauthResponse; - t.is(session.pubky().z32(), pubky) + t.is(authedPubky.z32(), pubky); + + let session = await client.session(authedPubky); t.deepEqual(session.capabilities(), capabilities.split(',')) }) diff --git a/pubky/src/native.rs b/pubky/src/native.rs index ba0f086..2a77039 100644 --- a/pubky/src/native.rs +++ b/pubky/src/native.rs @@ -184,14 +184,14 @@ impl PubkyClient { &self, relay: impl TryInto, capabilities: &Capabilities, - ) -> Result<(Url, tokio::sync::oneshot::Receiver>)> { + ) -> Result<(Url, tokio::sync::oneshot::Receiver)> { let mut relay: Url = relay .try_into() .map_err(|_| Error::Generic("Invalid relay Url".into()))?; let (pubkyauth_url, client_secret) = self.create_auth_request(&mut relay, capabilities)?; - let (tx, rx) = oneshot::channel::>(); + let (tx, rx) = oneshot::channel::(); let this = self.clone(); diff --git a/pubky/src/shared/auth.rs b/pubky/src/shared/auth.rs index 88c4259..60ae1ae 100644 --- a/pubky/src/shared/auth.rs +++ b/pubky/src/shared/auth.rs @@ -212,18 +212,17 @@ impl PubkyClient { &self, relay: Url, client_secret: &[u8; 32], - ) -> Result> { + ) -> Result { let response = self.http.request(Method::GET, relay).send().await?; let encrypted_token = response.bytes().await?; let token_bytes = decrypt(&encrypted_token, client_secret)?; let token = AuthToken::verify(&token_bytes)?; - if token.capabilities().is_empty() { - Ok(None) - } else { - let session = self.signin_with_authtoken(&token).await?; - Ok(Some(session)) + if !token.capabilities().is_empty() { + self.signin_with_authtoken(&token).await?; } + + Ok(token.pubky().clone()) } } diff --git a/pubky/src/wasm.rs b/pubky/src/wasm.rs index 09dc045..cbbf71b 100644 --- a/pubky/src/wasm.rs +++ b/pubky/src/wasm.rs @@ -81,7 +81,7 @@ impl PubkyClient { Ok(Session( self.inner_signup(keypair.as_inner(), homeserver.as_inner()) .await - .map_err(|e| JsValue::from(e))?, + .map_err(JsValue::from)?, )) } @@ -135,12 +135,7 @@ impl PubkyClient { let future = async move { this.subscribe_to_auth_response(relay, &client_secret) .await - .map(|opt| { - opt.map_or_else( - || JsValue::NULL, // Convert `None` to `JsValue::NULL` - |session| JsValue::from(Session(session)), - ) - }) + .map(|pubky| JsValue::from(PublicKey(pubky))) .map_err(|err| JsValue::from_str(&format!("{:?}", err))) };