day-part-auth 0.1.2

Sign-in for Day apps: OAuth 2.0 and OpenID Connect through the system's auth browser, Sign in with Apple, Google, and passkeys
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
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! A client's identity and redirect, the authorization request it sends the provider's
//! authorization endpoint, and the callback it gets back: RFC 6749 §4.1.1 and §4.1.2 with
//! PKCE (RFC 7636) and the OpenID Connect `nonce`.
//!
//! [`Client`] holds what never changes between sign-ins (provider, client id, redirect, scopes).
//! [`Client::start`] draws the per-request secrets, resolves the redirect for this platform
//! (binding a loopback listener where that is the redirect) and returns an [`Authorization`]:
//! the URL to open, and the secrets to check the callback against. The crate's `flow` module
//! runs the browser and the token exchange; an app with its own browser uses these two halves
//! directly.

use crate::AuthError;
use crate::crypto;
use crate::http::{query_of, with_query};
use crate::provider::Provider;
use crate::session::{Loopback, SessionOptions};

/// Where the provider sends the person back.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Redirect {
    /// What fits this platform: the app's own scheme (`DAY_APP_SCHEME`, which `day build` sets
    /// from `Day.toml`) with the path `/auth/callback` on iOS, Android and HarmonyOS; a loopback
    /// listener on the desktops; the page's own URL (without its fragment) on the web.
    Auto,
    /// A custom-scheme URL the app registered (`myapp://auth/callback`). The platform's auth
    /// browser returns it on Apple; Android, the desktops and HarmonyOS receive it as a deep
    /// link.
    Scheme(String),
    /// A listener this crate opens on `127.0.0.1` for the flow (RFC 8252 §7.3). Port 0 lets
    /// the OS choose, which is what Google requires of desktop apps; a provider that registers
    /// one exact URL takes a fixed port.
    Loopback { port: u16, path: String },
    /// An `https://` URL the app owns: a universal link (iOS), an app link (Android), the page
    /// itself on the web.
    Https(String),
}

impl Redirect {
    /// A loopback redirect on any free port at `/callback`.
    pub fn loopback() -> Redirect {
        Redirect::Loopback {
            port: 0,
            path: "/callback".into(),
        }
    }
}

/// An OAuth 2.0 / OpenID Connect client: what never changes between sign-ins.
#[derive(Clone, Debug, PartialEq, Eq)]
#[must_use]
#[expect(
    clippy::struct_field_names,
    reason = "`client_id` and `client_secret` are the OAuth parameter names"
)]
pub struct Client {
    pub(crate) provider: Provider,
    pub(crate) client_id: String,
    pub(crate) client_secret: Option<String>,
    pub(crate) redirect: Redirect,
    pub(crate) scopes: Vec<String>,
    pub(crate) extra: Vec<(String, String)>,
    pub(crate) session: SessionOptions,
    pub(crate) pkce: bool,
}

impl Client {
    /// A public client (no secret) asking for `openid`, with the platform's default redirect.
    pub fn new(provider: Provider, client_id: impl Into<String>) -> Client {
        Client {
            provider,
            client_id: client_id.into(),
            client_secret: None,
            redirect: Redirect::Auto,
            scopes: vec!["openid".into()],
            extra: Vec::new(),
            session: SessionOptions::default(),
            pkce: true,
        }
    }

    /// The provider this client talks to.
    pub fn provider(&self) -> &Provider {
        &self.provider
    }

    /// This client's id at the provider.
    pub fn client_id(&self) -> &str {
        &self.client_id
    }

    /// Where the provider sends the person back.
    pub fn redirect(mut self, redirect: Redirect) -> Self {
        self.redirect = redirect;
        self
    }

    /// The scopes to ask for, replacing the default `openid`.
    pub fn scopes<S: Into<String>>(mut self, scopes: impl IntoIterator<Item = S>) -> Self {
        self.scopes = scopes.into_iter().map(Into::into).collect();
        self
    }

    /// A confidential client's secret, sent with every token request. A native app has none:
    /// a secret shipped inside an app is not one.
    pub fn client_secret(mut self, secret: impl Into<String>) -> Self {
        self.client_secret = Some(secret.into());
        self
    }

    /// An extra parameter on the authorization request (`prompt`, `login_hint`, `access_type`,
    /// `hd`, a provider's own).
    pub fn param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra.push((name.into(), value.into()));
        self
    }

    /// How the browser session is shown.
    pub fn session(mut self, options: SessionOptions) -> Self {
        self.session = options;
        self
    }

    /// Whether to send a PKCE challenge. On by default; off only for a provider that rejects
    /// unknown parameters, which is rare and unsafe for a public client.
    pub fn pkce(mut self, on: bool) -> Self {
        self.pkce = on;
        self
    }

    /// Whether `openid` is among the scopes, which is what makes the answer an ID token.
    pub fn is_openid(&self) -> bool {
        self.scopes.iter().any(|s| s == "openid")
    }

    /// Draw this request's secrets, resolve the redirect, and build the authorization URL.
    ///
    /// # Errors
    ///
    /// Returns [`AuthError::InvalidRequest`] when the client id is empty, an endpoint or the
    /// redirect is unusable, or an extra parameter repeats a reserved one, and
    /// [`AuthError::Platform`] when the random source or the loopback listener fails.
    pub fn start(&self) -> Result<Authorization, AuthError> {
        if self.client_id.is_empty() {
            return Err(AuthError::InvalidRequest("the client id is empty".into()));
        }
        self.provider.validate()?;
        let reserved = [
            "response_type",
            "client_id",
            "redirect_uri",
            "scope",
            "state",
            "nonce",
            "code_challenge",
            "code_challenge_method",
        ];
        let endpoint = crate::urls::endpoint(&self.provider.authorization_endpoint)?;
        if self
            .extra
            .iter()
            .any(|(key, _)| reserved.contains(&key.as_str()))
            || endpoint
                .query_pairs()
                .any(|(key, _)| reserved.contains(&key.as_ref()))
        {
            return Err(AuthError::InvalidRequest(
                "authorization parameters cannot override protocol fields".into(),
            ));
        }
        let (redirect_uri, loopback) = resolve_redirect(&self.redirect)?;
        let state = crypto::random_token(24)?;
        let nonce = if self.is_openid() {
            Some(crypto::random_token(24)?)
        } else {
            None
        };
        let verifier = if self.pkce {
            Some(crypto::random_token(48)?)
        } else {
            None
        };
        let scope = self.scopes.join(" ");
        let mut fields: Vec<(&str, &str)> = vec![
            ("response_type", "code"),
            ("client_id", &self.client_id),
            ("redirect_uri", &redirect_uri),
            ("scope", &scope),
            ("state", &state),
        ];
        if let Some(nonce) = &nonce {
            fields.push(("nonce", nonce));
        }
        let challenge = verifier
            .as_deref()
            .map(|v| crypto::base64url(&crypto::sha256(v.as_bytes())));
        if let Some(challenge) = &challenge {
            fields.push(("code_challenge", challenge));
            fields.push(("code_challenge_method", "S256"));
        }
        for (k, v) in &self.extra {
            fields.push((k, v));
        }
        let url = with_query(&self.provider.authorization_endpoint, &fields);
        Ok(Authorization {
            client: self.clone(),
            url,
            redirect_uri,
            state,
            nonce,
            verifier,
            loopback,
        })
    }
}

/// One authorization request in flight: the URL to open, and what the callback is checked
/// against. Consumed by `complete`.
pub struct Authorization {
    client: Client,
    url: String,
    redirect_uri: String,
    state: String,
    nonce: Option<String>,
    verifier: Option<String>,
    loopback: Option<Loopback>,
}

impl Authorization {
    pub(crate) fn validate_client(&self, client: &Client) -> Result<(), AuthError> {
        if &self.client != client {
            return Err(AuthError::InvalidRequest(
                "authorization belongs to a different client configuration".into(),
            ));
        }
        Ok(())
    }

    /// The URL to open in the auth browser.
    pub fn url(&self) -> &str {
        &self.url
    }

    /// The redirect URI this request named, resolved for this platform.
    pub fn redirect_uri(&self) -> &str {
        &self.redirect_uri
    }

    /// The `state` the callback must echo.
    pub fn state(&self) -> &str {
        &self.state
    }

    /// The `nonce` the ID token must echo, when `openid` was asked for.
    pub fn nonce(&self) -> Option<&str> {
        self.nonce.as_deref()
    }

    /// The PKCE verifier the token request sends.
    pub fn verifier(&self) -> Option<&str> {
        self.verifier.as_deref()
    }

    /// The loopback listener, when the redirect is one. [`Client::sign_in`] awaits it itself;
    /// an app that opens the browser its own way (or a test that fetches the URL with an HTTP
    /// client, which follows the redirect to the listener) takes it here and awaits
    /// [`Loopback::wait`] before completing the request.
    pub fn take_loopback(&mut self) -> Option<Loopback> {
        self.loopback.take()
    }

    /// Check the callback URL's `state` and read its `code`, or the error the provider sent
    /// (RFC 6749 §4.1.2.1). The state comparison does not stop at the first difference.
    ///
    /// # Errors
    ///
    /// Returns [`AuthError::InvalidRequest`] when the callback is for another redirect or carries
    /// no code, [`AuthError::Provider`] with the provider's error, and
    /// [`AuthError::StateMismatch`] when the `state` differs.
    pub fn code_from(&self, callback_url: &str) -> Result<String, AuthError> {
        if !crate::urls::callback_matches(&self.redirect_uri, callback_url) {
            return Err(AuthError::InvalidRequest(
                "the callback is not for this request's redirect".into(),
            ));
        }
        let query = query_of(callback_url);
        let mut names = std::collections::HashSet::new();
        if query.iter().any(|(key, _)| !names.insert(key)) {
            return Err(AuthError::InvalidRequest(
                "duplicate authorization response parameter".into(),
            ));
        }
        let get = |name: &str| {
            query
                .iter()
                .find(|(k, _)| k == name)
                .map(|(_, v)| v.as_str())
        };
        match get("state") {
            Some(state) if crypto::constant_time_eq(state.as_bytes(), self.state.as_bytes()) => {}
            _ => return Err(AuthError::StateMismatch),
        }
        if let Some(issuer) = get("iss")
            && self.client.provider.issuer.as_deref() != Some(issuer)
        {
            return Err(AuthError::InvalidRequest(
                "authorization response issuer mismatch".into(),
            ));
        }
        if get("code").is_some() && get("error").is_some() {
            return Err(AuthError::InvalidRequest(
                "authorization response contains both code and error".into(),
            ));
        }
        if let Some(error) = get("error") {
            return Err(AuthError::Provider {
                error: error.to_string(),
                description: get("error_description").map(str::to_string),
            });
        }
        match get("code") {
            Some(code) if !code.is_empty() => Ok(code.to_string()),
            _ => Err(AuthError::InvalidRequest(
                "the callback carries no code".into(),
            )),
        }
    }
}

/// The redirect URI for this platform, and the loopback listener when the redirect is one.
fn resolve_redirect(redirect: &Redirect) -> Result<(String, Option<Loopback>), AuthError> {
    match redirect {
        Redirect::Scheme(url) => {
            if !url.contains("://")
                || matches!(
                    crate::urls::parse(url)?.scheme(),
                    "http" | "https" | "javascript" | "data" | "file"
                )
            {
                return Err(AuthError::InvalidRequest(
                    "a scheme redirect is `<scheme>://<path>` with the app's own scheme".into(),
                ));
            }
            Ok((url.clone(), None))
        }
        Redirect::Https(url) => {
            if crate::urls::endpoint(url)?.scheme() != "https" {
                return Err(AuthError::InvalidRequest(
                    "an https redirect must start with https://".into(),
                ));
            }
            Ok((url.clone(), None))
        }
        Redirect::Loopback { port, path } => {
            let listener = Loopback::bind(*port, path)?;
            Ok((listener.redirect_uri().to_string(), Some(listener)))
        }
        Redirect::Auto => {
            if cfg!(target_arch = "wasm32") {
                // The page itself: the popup comes back to it and posts the URL to the opener.
                // A page served over plain http (local development) is accepted as the redirect
                // the provider sees; whether the provider takes it is the provider's rule.
                let page = crate::session::page_url().ok_or_else(|| {
                    AuthError::InvalidRequest(
                        "the page's URL is unknown; name a Redirect::Https".into(),
                    )
                })?;
                return Ok((page, None));
            }
            if cfg!(any(
                target_os = "ios",
                target_os = "android",
                target_env = "ohos"
            )) {
                let scheme = app_scheme().ok_or_else(|| {
                    AuthError::InvalidRequest(
                        "no app scheme is known (DAY_APP_SCHEME); name a Redirect::Scheme".into(),
                    )
                })?;
                return Ok((format!("{scheme}://auth/callback"), None));
            }
            resolve_redirect(&Redirect::loopback())
        }
    }
}

/// The app's URL scheme, which `day build` and `day launch` export as `DAY_APP_SCHEME`.
pub fn app_scheme() -> Option<String> {
    std::env::var("DAY_APP_SCHEME")
        .ok()
        .filter(|s| !s.is_empty())
        .or_else(|| option_env!("DAY_APP_SCHEME").map(str::to_owned))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::assert_matches;

    #[test]
    fn callback_spoofing_and_ambiguous_parameters_are_rejected() {
        let client = Client::new(Provider::google(), "fixture")
            .redirect(Redirect::Scheme("fixture://auth/callback".into()));
        let request = client.start().expect("request");
        for suffix in [
            "/extra?code=x&state=",
            ".evil?code=x&state=",
            "?code=x&code=y&state=",
            "?code=x&error=access_denied&state=",
            "?code=x&iss=https%3A%2F%2Fevil.test&state=",
        ] {
            let callback = format!("fixture://auth/callback{suffix}{}", request.state());
            assert!(request.code_from(&callback).is_err(), "{suffix}");
        }
        assert!(request.validate_client(&client.clone()).is_ok());
        assert!(
            request
                .validate_client(&client.clone().param("prompt", "login"))
                .is_err()
        );
    }

    #[test]
    fn custom_parameters_cannot_replace_protocol_fields() {
        let client = Client::new(Provider::google(), "fixture")
            .redirect(Redirect::Scheme("fixture://callback".into()));
        assert!(client.clone().param("state", "fixed").start().is_err());
        assert!(
            client
                .clone()
                .param("code_challenge", "fixed")
                .start()
                .is_err()
        );
        assert!(client.param("prompt", "login").start().is_ok());
    }

    fn client() -> Client {
        Client::new(Provider::new("https://p/auth", "https://p/token"), "app")
            .redirect(Redirect::Scheme("myapp://auth/callback".into()))
            .scopes(["openid", "email"])
            .param("prompt", "consent")
    }

    #[test]
    fn the_request_carries_pkce_state_nonce_and_the_extras() {
        let a = client().start().expect("start");
        let query = query_of(a.url());
        let get = |k: &str| query.iter().find(|(n, _)| n == k).map(|(_, v)| v.clone());
        assert!(a.url().starts_with("https://p/auth?"));
        assert_eq!(get("response_type").as_deref(), Some("code"));
        assert_eq!(get("client_id").as_deref(), Some("app"));
        assert_eq!(
            get("redirect_uri").as_deref(),
            Some("myapp://auth/callback")
        );
        assert_eq!(get("scope").as_deref(), Some("openid email"));
        assert_eq!(get("state").as_deref(), Some(a.state()));
        assert_eq!(get("nonce").as_deref(), a.nonce());
        assert_eq!(get("code_challenge_method").as_deref(), Some("S256"));
        assert_eq!(get("prompt").as_deref(), Some("consent"));
        let expected =
            crypto::base64url(&crypto::sha256(a.verifier().expect("verifier").as_bytes()));
        assert_eq!(get("code_challenge").as_deref(), Some(expected.as_str()));
        // Two requests never share secrets.
        let b = client().start().expect("start");
        assert_ne!(a.state(), b.state());
        assert_ne!(a.verifier(), b.verifier());
    }

    #[test]
    fn plain_oauth_sends_no_nonce_and_pkce_can_be_turned_off() {
        let a = client()
            .scopes(["read"])
            .pkce(false)
            .start()
            .expect("start");
        assert!(a.nonce().is_none() && a.verifier().is_none());
        assert!(!a.url().contains("code_challenge"));
        assert!(!a.url().contains("nonce="));
    }

    #[test]
    fn callbacks_are_checked_for_redirect_state_error_and_code() {
        let a = client().start().expect("start");
        let good = format!("myapp://auth/callback?state={}&code=abc", a.state());
        assert_eq!(a.code_from(&good).as_deref(), Ok("abc"));
        assert_eq!(
            a.code_from("myapp://other?code=abc"),
            Err(AuthError::InvalidRequest(
                "the callback is not for this request's redirect".into()
            ))
        );
        assert_eq!(
            a.code_from("myapp://auth/callback?state=wrong&code=abc"),
            Err(AuthError::StateMismatch)
        );
        assert_eq!(
            a.code_from("myapp://auth/callback?code=abc"),
            Err(AuthError::StateMismatch)
        );
        let denied = format!(
            "myapp://auth/callback?state={}&error=access_denied&error_description=no",
            a.state()
        );
        assert_eq!(
            a.code_from(&denied),
            Err(AuthError::Provider {
                error: "access_denied".into(),
                description: Some("no".into())
            })
        );
        let empty = format!("myapp://auth/callback?state={}", a.state());
        assert_matches!(a.code_from(&empty), Err(AuthError::InvalidRequest(_)));
    }

    #[test]
    fn redirects_are_validated_and_loopback_binds() {
        assert!(
            Client::new(Provider::new("https://p/a", "https://p/t"), "app")
                .redirect(Redirect::Scheme("https://x".into()))
                .start()
                .is_err()
        );
        assert!(
            Client::new(Provider::new("https://p/a", "https://p/t"), "app")
                .redirect(Redirect::Https("http://x".into()))
                .start()
                .is_err()
        );
        assert!(
            Client::new(Provider::new("http://p/a", "https://p/t"), "app")
                .start()
                .is_err()
        );
        assert!(
            Client::new(Provider::new("https://p/a", "https://p/t"), "")
                .start()
                .is_err()
        );
        let mut a = Client::new(Provider::new("https://p/a", "https://p/t"), "app")
            .redirect(Redirect::loopback())
            .start()
            .expect("start");
        assert!(a.redirect_uri().starts_with("http://127.0.0.1:"));
        assert!(a.take_loopback().is_some());
        assert!(a.take_loopback().is_none());
    }
}