Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use reqwest::Method;
use url::Url;
use zeroize::Zeroizing;

use pubky_common::{
    auth::{
        AuthToken,
        grant::GrantClaims,
        jws::{ClientId, GRANT_JWS_TYP, GrantId},
    },
    crypto::{PublicKey, encrypt},
};

use crate::{
    Capabilities,
    actors::auth::{
        deep_links::{DeepLink, DeepLinkParseError, GrantApprovalFormat, GrantRelayChannel},
        grant::approval_encryption,
        grant::{approval_envelope::GrantApprovalEnvelope, constants::DEFAULT_GRANT_LIFETIME_SECS},
    },
    cross_log,
    errors::{AuthError, Result},
};

use super::PubkySigner;

impl PubkySigner {
    /// Approve all requested capabilities from another app (wallet / signer side).
    ///
    /// Use [`Self::approve_auth_with_capabilities`] to approve fewer capabilities.
    ///
    /// Signed approval links use a domain-separated hash of `epk` as the relay
    /// channel ID and encrypt the approval directly with HPKE. Legacy links
    /// keep shared-secret relay encryption.
    /// Grant requests with `af=v1` receive a signed envelope with the grant
    /// and keys only for approved `e` scopes. Requests without that opt-in
    /// retain the bare grant format. Only legacy links contain a relay secret.
    ///
    /// # Typical usage
    ///
    /// 1. The **requesting app** constructs a [`PubkyGrantAuthFlow`](crate::PubkyGrantAuthFlow)
    ///    and displays `authorization_url()` as a QR code or deep link.
    /// 2. The **signer app** (e.g. Pubky Ring) scans the QR and calls `approve_auth`
    ///    with the scanned URL.
    /// 3. The requesting app receives the approval and obtains a session.
    ///
    /// Use [`Self::handle_deeplink`] instead if the URL might be a `direct_signup` link.
    ///
    /// # Errors
    /// - Returns [`crate::errors::Error::Authentication`] if the `pubkyauth://`
    ///   URL is malformed or addresses an intent that `approve_auth` does not
    ///   handle (e.g. `secret_export`).
    /// - Propagates transport failures when posting to the relay or if the
    ///   relay responds with a non-success status.
    pub async fn approve_auth(&self, pubkyauth_url: impl AsRef<str>) -> Result<()> {
        self.approve_auth_deeplink(Self::parse_deeplink(pubkyauth_url)?, None)
            .await
    }

    /// Approve an auth request with capabilities selected by the user.
    ///
    /// Supports grant and legacy signin/signup links. Approved scopes and
    /// actions must be covered by the request; scopes can be narrowed and
    /// actions removed. An empty set approves no capabilities. Separate
    /// requested capabilities can cover different actions of one approval.
    /// Signed V1 approvals include keys only for approved `e` scopes.
    /// The request URL and its client identity, relay, and format are preserved.
    ///
    /// # Errors
    /// Returns [`crate::errors::Error::Authentication`] for malformed or
    /// unsupported links, or capabilities exceeding the request, before
    /// posting an approval. Propagates approval construction and relay errors.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn approve(signer: &pubky::PubkySigner, url: &str) -> Result<(), Box<dyn std::error::Error>> {
    /// // The app requested `/pub/example.app/:rwe`; approve only read access.
    /// let approved = pubky::Capabilities::builder()
    ///     .read("/pub/example.app/")?
    ///     .finish();
    /// signer.approve_auth_with_capabilities(url, &approved).await?;
    /// # Ok(()) }
    /// ```
    pub async fn approve_auth_with_capabilities(
        &self,
        pubkyauth_url: impl AsRef<str>,
        approved_capabilities: &Capabilities,
    ) -> Result<()> {
        self.approve_auth_deeplink(
            Self::parse_deeplink(pubkyauth_url)?,
            Some(approved_capabilities),
        )
        .await
    }

    /// Executes the action represented by a `pubkyauth://` deep link.
    ///
    /// Authentication links are approved through the relay, while a
    /// [`DeepLink::DirectSignup`] creates the signer's account directly on the
    /// target homeserver.
    ///
    /// # Errors
    /// - Returns [`crate::errors::Error::Authentication`] if the URL is malformed
    ///   or the signer does not handle its intent.
    /// - Propagates signup or authentication-delivery failures for the handled intent.
    pub async fn handle_deeplink(&self, pubkyauth_url: impl AsRef<str>) -> Result<()> {
        match Self::parse_deeplink(pubkyauth_url)? {
            DeepLink::DirectSignup(d) => {
                let params = d.params();
                self.signup(&params.homeserver, params.signup_token.as_deref())
                    .await
            }
            DeepLink::SeedExport(_) => Err(AuthError::Validation(
                "handle_deeplink does not handle seed_export deep links".into(),
            )
            .into()),
            deep_link => self.approve_auth_deeplink(deep_link, None).await,
        }
    }

    fn parse_deeplink(pubkyauth_url: impl AsRef<str>) -> Result<DeepLink> {
        pubkyauth_url
            .as_ref()
            .parse()
            .map_err(|e: DeepLinkParseError| {
                AuthError::Validation(format!("invalid pubkyauth URL: {e}")).into()
            })
    }

    async fn approve_auth_deeplink(
        &self,
        deep_link: DeepLink,
        approved: Option<&Capabilities>,
    ) -> Result<()> {
        let (relay, relay_channel, encrypted_payload) =
            match &deep_link {
                DeepLink::Signin(d) => {
                    let params = d.params();
                    let caps = select_capabilities(&params.capabilities, approved)?;
                    cross_log!(
                        info,
                        "Approving legacy signin via relay {} (caps={:?})",
                        params.relay,
                        caps
                    );
                    (
                        params.relay.clone(),
                        GrantRelayChannel::SharedSecret(params.secret),
                        self.build_encrypted_token(caps.clone(), &params.secret),
                    )
                }
                DeepLink::Signup(d) => {
                    let params = d.params();
                    let caps = select_capabilities(&params.capabilities, approved)?;
                    cross_log!(
                        info,
                        "Approving legacy signup via relay {} (caps={:?})",
                        params.relay,
                        caps
                    );
                    (
                        params.relay.clone(),
                        GrantRelayChannel::SharedSecret(params.secret),
                        self.build_encrypted_token(caps.clone(), &params.secret),
                    )
                }
                DeepLink::DirectSignup(_) => return Err(AuthError::Validation(
                    "direct_signup links create an account; use handle_deeplink or signup instead"
                        .into(),
                )
                .into()),
                DeepLink::SigninGrant(d) => {
                    let params = d.params();
                    let caps = select_capabilities(&params.capabilities, approved)?;
                    cross_log!(
                        info,
                        "Approving grant signin via relay {} (client_id={}, caps={:?})",
                        params.relay,
                        params.client_id,
                        caps
                    );
                    let payload = self.build_encrypted_grant(
                        caps,
                        params.client_id.clone(),
                        params.client_pk.clone(),
                        params.approval_format,
                        params.relay_channel,
                    )?;
                    (params.relay.clone(), params.relay_channel, payload)
                }
                DeepLink::SignupGrant(d) => {
                    let params = d.params();
                    let caps = select_capabilities(&params.capabilities, approved)?;
                    cross_log!(
                        info,
                        "Approving grant signup via relay {} (client_id={}, caps={:?})",
                        params.relay,
                        params.client_id,
                        caps
                    );
                    let payload = self.build_encrypted_grant(
                        caps,
                        params.client_id.clone(),
                        params.client_pk.clone(),
                        params.approval_format,
                        params.relay_channel,
                    )?;
                    (params.relay.clone(), params.relay_channel, payload)
                }
                DeepLink::SeedExport(_) => {
                    return Err(AuthError::Validation(
                        "approve_auth does not handle seed_export deep links".into(),
                    )
                    .into());
                }
            };

        let callback_url = Self::derive_callback_url(&relay, &relay_channel.http_channel_id())?;
        cross_log!(
            info,
            "Posting encrypted auth payload to relay channel {}",
            callback_url
        );

        let response = self
            .client
            .cross_request(Method::POST, callback_url)
            .await?
            .body(encrypted_payload)
            .send()
            .await?;

        self.client.check_http_status(response).await?;
        cross_log!(info, "Auth payload delivered successfully");
        Ok(())
    }

    fn build_encrypted_grant(
        &self,
        capabilities: &Capabilities,
        client_id: ClientId,
        client_pk: PublicKey,
        format: GrantApprovalFormat,
        relay_channel: GrantRelayChannel,
    ) -> Result<Vec<u8>> {
        format.validate_capabilities(capabilities)?;
        let now = web_time::SystemTime::now()
            .duration_since(web_time::UNIX_EPOCH)
            .map_or(0, |d| d.as_secs());
        let claims = GrantClaims {
            iss: self.keypair.public_key(),
            client_id,
            caps: capabilities.as_slice().to_vec(),
            cnf: client_pk,
            jti: GrantId::generate(),
            iat: now,
            exp: now + DEFAULT_GRANT_LIFETIME_SECS,
        };
        let payload =
            match format {
                GrantApprovalFormat::BareGrant => Zeroizing::new(
                    pubky_common::auth::jws::sign_jws(&self.keypair, GRANT_JWS_TYP, &claims),
                ),
                GrantApprovalFormat::SignedApprovalV1 => {
                    GrantApprovalEnvelope::sign(&self.keypair, &claims)
                }
            };
        match relay_channel {
            GrantRelayChannel::SharedSecret(secret) => Ok(encrypt(payload.as_bytes(), &secret)),
            GrantRelayChannel::Hpke {
                ephemeral_public_key,
            } => approval_encryption::seal(&ephemeral_public_key, payload.as_bytes()),
        }
    }

    fn build_encrypted_token(
        &self,
        capabilities: Capabilities,
        client_secret: &[u8; 32],
    ) -> Vec<u8> {
        let token = AuthToken::sign(&self.keypair, capabilities);
        encrypt(&token.serialize(), client_secret)
    }

    fn derive_callback_url(relay: &Url, channel_id: &str) -> Result<Url> {
        let mut callback_url = relay.clone();
        let mut path_segments = callback_url
            .path_segments_mut()
            .map_err(|()| url::ParseError::RelativeUrlWithCannotBeABaseBase)?;
        path_segments.pop_if_empty();
        path_segments.push(channel_id);
        drop(path_segments);
        Ok(callback_url)
    }
}

fn select_capabilities<'a>(
    requested: &'a Capabilities,
    approved: Option<&'a Capabilities>,
) -> Result<&'a Capabilities> {
    let Some(approved) = approved else {
        return Ok(requested);
    };
    // Check actions separately so split read/write requests can cover one approval.
    for capability in approved.iter() {
        for action in capability.actions() {
            if !requested.iter().any(|requested| {
                requested.scope_covers_path(capability.scope())
                    && requested.actions().contains(action)
            }) {
                return Err(AuthError::Validation(
                    "approved capabilities exceed the pending request".into(),
                )
                .into());
            }
        }
    }
    Ok(approved)
}

#[cfg(test)]
mod tests {
    use crate::actors::auth::deep_links::{
        DeepLinkScheme, SigninGrantDeepLink, SigninGrantParams, SignupGrantDeepLink,
        SignupGrantParams,
    };
    use crate::actors::auth::grant::approval::GrantApproval;
    use crate::actors::auth::grant::approval_encryption::ApprovalRecipientSecret;
    use crate::{Error, Keypair, Pubky, PubkyHttpClient};
    use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
    use httpmock::{Method::POST, MockServer};
    use pubky_common::crypto::decrypt;

    use super::*;

    #[tokio::test]
    #[allow(
        clippy::too_many_lines,
        reason = "The test covers both formats, relay paths, and signup modes."
    )]
    async fn grant_approvals_deliver_selected_capabilities_and_keys_in_the_negotiated_format() {
        let signer = Pubky::with_client(PubkyHttpClient::new().unwrap()).signer(Keypair::random());
        let client_pk = Keypair::random().public_key();
        let secret = [42; 32];
        for approval_format in [
            GrantApprovalFormat::BareGrant,
            GrantApprovalFormat::SignedApprovalV1,
        ] {
            let (approval_recipient, relay_channel) =
                if approval_format == GrantApprovalFormat::SignedApprovalV1 {
                    let (secret, public_key) = ApprovalRecipientSecret::generate();
                    (
                        Some(secret),
                        GrantRelayChannel::Hpke {
                            ephemeral_public_key: public_key,
                        },
                    )
                } else {
                    (None, GrantRelayChannel::SharedSecret(secret))
                };
            let requested = match approval_format {
                GrantApprovalFormat::BareGrant => "/priv/chat/:rw",
                GrantApprovalFormat::SignedApprovalV1 => {
                    "/priv/chat/:rw,/priv/chat/:e,/priv/backup/:r"
                }
            };
            let caps: Capabilities = requested.parse().unwrap();
            let approvals: &[Option<&str>] = match approval_format {
                GrantApprovalFormat::BareGrant => &[None, Some("/priv/chat/file:r"), Some("")],
                GrantApprovalFormat::SignedApprovalV1 => &[
                    None,
                    Some("/priv/chat/file:r"),
                    Some(""),
                    Some("/priv/chat/selected/:e"),
                ],
            };
            for relay_path in ["inbox", "link"] {
                for signup in [false, true] {
                    for &approved in approvals {
                        let approved = approved.map(|caps| caps.parse::<Capabilities>().unwrap());
                        let server = MockServer::start_async().await;
                        let relay = Url::parse(&server.url(format!("/{relay_path}/"))).unwrap();
                        let channel_id = relay_channel.http_channel_id();
                        let callback =
                            PubkySigner::derive_callback_url(&relay, &channel_id).unwrap();
                        let link = if signup {
                            SignupGrantDeepLink::new(
                                DeepLinkScheme::PubkyAuth,
                                SignupGrantParams {
                                    capabilities: caps.clone(),
                                    relay,
                                    relay_channel,
                                    homeserver: Keypair::random().public_key(),
                                    signup_token: None,
                                    client_id: ClientId::new("test.app").unwrap(),
                                    client_pk: client_pk.clone(),
                                    approval_format,
                                },
                            )
                            .to_string()
                        } else {
                            SigninGrantDeepLink::new(
                                DeepLinkScheme::PubkyAuth,
                                SigninGrantParams {
                                    capabilities: caps.clone(),
                                    relay,
                                    relay_channel,
                                    client_id: ClientId::new("test.app").unwrap(),
                                    client_pk: client_pk.clone(),
                                    approval_format,
                                },
                            )
                            .to_string()
                        };
                        let issuer = signer.public_key();
                        let expected_client = client_pk.clone();
                        let expected_caps = approved.as_ref().unwrap_or(&caps).clone();
                        let approval_recipient = approval_recipient.clone();
                        let delivery = server
                            .mock_async(move |when, then| {
                                when.method(POST)
                                    .path(callback.path())
                                    .is_true(move |request| {
                                        let plaintext = match &approval_recipient {
                                            Some(recipient) => {
                                                recipient.open(request.body_ref()).ok()
                                            }
                                            None => decrypt(request.body_ref(), &secret)
                                                .ok()
                                                .map(Zeroizing::new),
                                        };
                                        let Some(plaintext) = plaintext else {
                                            return false;
                                        };
                                        let Ok(text) = std::str::from_utf8(&plaintext) else {
                                            return false;
                                        };
                                        GrantApproval::decode_text(text, approval_format).is_ok_and(
                                            |approval| {
                                                let claims = approval.claims;
                                                claims.iss == issuer
                                                    && claims.cnf == expected_client
                                                    && claims.client_id.as_str() == "test.app"
                                                    && claims.caps == expected_caps.to_vec()
                                            },
                                        )
                                    });
                                then.status(200);
                            })
                            .await;
                        if let Some(approved) = &approved {
                            signer
                                .approve_auth_with_capabilities(link, approved)
                                .await
                                .unwrap();
                        } else {
                            signer.approve_auth(link).await.unwrap();
                        }
                        delivery.assert_async().await;
                    }
                }
            }
        }
    }

    #[tokio::test]
    async fn selective_approval_validates_all_auth_intents_before_delivery() {
        let server = MockServer::start_async().await;
        let secret = [42; 32];
        let signer = Pubky::with_client(PubkyHttpClient::new().unwrap()).signer(Keypair::random());
        for intent in ["signin", "signup", "signin_grant", "signup_grant"] {
            let mut url = Url::parse(&format!("pubkyauth://{intent}")).unwrap();
            url.query_pairs_mut()
                .append_pair("caps", "/pub/app/:r,/pub/app/:w")
                .append_pair("relay", &server.url("/inbox"))
                .append_pair("secret", &URL_SAFE_NO_PAD.encode(secret))
                .append_pair("cid", "test.app")
                .append_pair("cpk", &Keypair::random().public_key().to_z32())
                .append_pair("hs", &Keypair::random().public_key().to_z32());
            let issuer = signer.public_key();
            let expected: Capabilities = "/pub/app/file:rw".parse().unwrap();
            let delivery = server
                .mock_async(move |when, then| {
                    when.method(POST).is_true(move |request| {
                        let Ok(plaintext) = decrypt(request.body_ref(), &secret) else {
                            return false;
                        };
                        if intent.ends_with("_grant") {
                            let Ok(text) = std::str::from_utf8(&plaintext) else {
                                return false;
                            };
                            GrantClaims::decode(text).is_ok_and(|claims| {
                                claims.iss == issuer && claims.caps == expected.to_vec()
                            })
                        } else {
                            AuthToken::verify(&plaintext).is_ok_and(|token| {
                                token.public_key() == &issuer && token.capabilities() == &expected
                            })
                        }
                    });
                    then.status(200);
                })
                .await;
            for caps in ["/:r", "/pub/other/:r", "/pub/application/:r", "/pub/app/:e"] {
                let error = signer
                    .approve_auth_with_capabilities(url.as_str(), &caps.parse().unwrap())
                    .await
                    .unwrap_err();
                assert!(
                    matches!(error, Error::Authentication(AuthError::Validation(message))
                    if message == "approved capabilities exceed the pending request")
                );
            }
            delivery.assert_calls_async(0).await;
            // Separate requested read and write actions can cover one approval.
            signer
                .approve_auth_with_capabilities(url.as_str(), &"/pub/app/file:rw".parse().unwrap())
                .await
                .unwrap();
            delivery.assert_calls_async(1).await;
            delivery.delete_async().await;
        }
    }

    #[tokio::test]
    async fn selective_approval_rejects_malformed_and_non_auth_links() {
        let signer = Pubky::with_client(PubkyHttpClient::new().unwrap()).signer(Keypair::random());
        for url in [
            "not a URL",
            "pubkyauth://direct_signup?hs=5jsjx1o6fzu6aeeo697r3i5rx15zq41kikcye8wtwdqm4nb4tryo",
            "pubkyauth://secret_export?secret=kqnceEMgrNQM_xi06oQXjA3cJHX_RQmw1BY6JE1bse8",
        ] {
            let error = signer
                .approve_auth_with_capabilities(url, &Capabilities::default())
                .await
                .unwrap_err();
            assert!(matches!(
                error,
                Error::Authentication(AuthError::Validation(_))
            ));
        }
    }

    #[test]
    fn unversioned_grant_clients_keep_the_bare_jws_payload() {
        let signer = Pubky::with_client(PubkyHttpClient::new().unwrap()).signer(Keypair::random());
        let payload = signer
            .build_encrypted_grant(
                &Capabilities::default(),
                ClientId::new("test.app").unwrap(),
                Keypair::random().public_key(),
                GrantApprovalFormat::BareGrant,
                GrantRelayChannel::SharedSecret([42; 32]),
            )
            .unwrap();
        let plaintext = decrypt(&payload, &[42; 32]).unwrap();
        let claims = GrantClaims::decode(std::str::from_utf8(&plaintext).unwrap()).unwrap();
        assert_eq!(claims.iss, signer.public_key());
    }

    #[tokio::test]
    async fn approve_auth_rejects_direct_signup_links() {
        let signer = Pubky::with_client(PubkyHttpClient::new().unwrap()).signer(Keypair::random());

        let error = signer
            .approve_auth(
                "pubkyauth://direct_signup?hs=5jsjx1o6fzu6aeeo697r3i5rx15zq41kikcye8wtwdqm4nb4tryo",
            )
            .await
            .unwrap_err();

        assert!(matches!(
            error,
            Error::Authentication(AuthError::Validation(message)) if message.contains("handle_deeplink")
        ));
    }
}