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
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 an auth request from another app (wallet / signer side).
    ///
    /// 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)?)
            .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).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) -> Result<()> {
        let (relay, relay_channel, encrypted_payload) =
            match &deep_link {
                DeepLink::Signin(d) => {
                    let params = d.params();
                    cross_log!(
                        info,
                        "Approving legacy signin via relay {} (caps={:?})",
                        params.relay,
                        params.capabilities
                    );
                    let payload =
                        self.build_encrypted_token(params.capabilities.clone(), &params.secret);
                    (
                        params.relay.clone(),
                        GrantRelayChannel::SharedSecret(params.secret),
                        payload,
                    )
                }
                DeepLink::Signup(d) => {
                    let params = d.params();
                    cross_log!(
                        info,
                        "Approving legacy signup via relay {} (caps={:?})",
                        params.relay,
                        params.capabilities
                    );
                    let payload =
                        self.build_encrypted_token(params.capabilities.clone(), &params.secret);
                    (
                        params.relay.clone(),
                        GrantRelayChannel::SharedSecret(params.secret),
                        payload,
                    )
                }
                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();
                    cross_log!(
                        info,
                        "Approving grant signin via relay {} (client_id={}, caps={:?})",
                        params.relay,
                        params.client_id,
                        params.capabilities
                    );
                    let payload = self.build_encrypted_grant(
                        &params.capabilities,
                        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();
                    cross_log!(
                        info,
                        "Approving grant signup via relay {} (client_id={}, caps={:?})",
                        params.relay,
                        params.client_id,
                        params.capabilities
                    );
                    let payload = self.build_encrypted_grant(
                        &params.capabilities,
                        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)
    }
}

#[cfg(test)]
mod tests {
    use crate::actors::auth::deep_links::{
        DeepLinkScheme, SigninGrantDeepLink, SigninGrantParams, SignupGrantDeepLink,
        SignupGrantParams,
    };
    use crate::actors::auth::grant::approval_encryption::ApprovalRecipientSecret;
    use crate::{Capability, Error, Keypair, Pubky, PubkyHttpClient};
    use httpmock::{Method::POST, MockServer};
    use pubky_common::auth::jws::decode_jws_payload;
    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_signin_and_signup_deliver_the_negotiated_format_to_the_relay() {
        let signer = Pubky::with_client(PubkyHttpClient::new().unwrap()).signer(Keypair::random());
        let client_pk = Keypair::random().public_key();
        let caps = Capabilities::builder()
            .read_write("/priv/chat/")
            .unwrap()
            .finish();
        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 caps = if approval_format == GrantApprovalFormat::SignedApprovalV1 {
                Capabilities::builder()
                    .extend(caps.to_vec())
                    .encryption_keys("/priv/chat/")
                    .unwrap()
                    .read("/priv/backup/")
                    .unwrap()
                    .finish()
            } else {
                caps.clone()
            };
            for relay_path in ["inbox", "link"] {
                for signup in [false, true] {
                    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 = 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 = if let Some(recipient) = &approval_recipient {
                                        let Ok(plaintext) = recipient.open(request.body_ref())
                                        else {
                                            return false;
                                        };
                                        plaintext
                                    } else {
                                        let Ok(plaintext) = decrypt(request.body_ref(), &secret)
                                        else {
                                            return false;
                                        };
                                        zeroize::Zeroizing::new(plaintext)
                                    };
                                    let Ok(text) = std::str::from_utf8(&plaintext) else {
                                        return false;
                                    };
                                    let claims = match approval_format {
                                        GrantApprovalFormat::BareGrant => GrantClaims::decode(text),
                                        GrantApprovalFormat::SignedApprovalV1 => {
                                            let Ok(envelope) =
                                                decode_jws_payload::<GrantApprovalEnvelope>(text)
                                            else {
                                                return false;
                                            };
                                            if envelope.encryption_keys.scopes().collect::<Vec<_>>()
                                                != expected_caps
                                                    .iter()
                                                    .filter(|cap| cap.grants_encryption_keys())
                                                    .map(Capability::scope)
                                                    .collect::<Vec<_>>()
                                            {
                                                return false;
                                            }
                                            GrantClaims::decode(&envelope.grant)
                                        }
                                    };
                                    claims.is_ok_and(|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;
                    signer.approve_auth(link).await.unwrap();
                    delivery.assert_async().await;
                }
            }
        }
    }

    #[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")
        ));
    }
}