pipedash-core 0.8.0

Framework-agnostic core library for Pipedash
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
use std::collections::HashMap;
use std::sync::{
    Arc,
    Mutex,
    MutexGuard,
    PoisonError,
    Weak,
};
use std::time::Duration;

use chrono::Utc;
use pipedash_plugin_api::SignInClient;
use reqwest::Url;
use tokio::sync::watch;
use tokio::task::JoinHandle;
use tokio::time::Instant;

use crate::domain::{
    DomainError,
    DomainResult,
    SignInCredential,
    SignInStart,
    SignInStatus,
    SIGN_IN_EXPIRED,
};
use crate::infrastructure::oauth::loopback::{
    self,
    Loopback,
};
use crate::infrastructure::oauth::{
    self,
    Callback,
};

const PENDING_TTL: Duration = Duration::from_secs(10 * 60);
const COMPLETED_TTL: Duration = Duration::from_secs(30 * 60);
const FOREIGN_CALLBACK: &str =
    "This sign-in page belongs to another attempt. Start the sign-in again from Pipedash.";
const WRONG_ADDRESS: &str =
    "Paste the full address of the page your browser opened after you signed in.";
const MISSING_CODE: &str = "The sign-in server didn't send an authorization code.";
const TIMED_OUT: &str = "This sign-in timed out. Start it again from Pipedash.";
const UNFINISHED: &str = "Finish signing in before you continue.";

struct Pending {
    state: String,
    verifier: String,
    token_endpoint: Url,
    client: SignInClient,
}

struct Flow {
    status: watch::Sender<SignInStatus>,
    pending: Option<Pending>,
    credential: Option<SignInCredential>,
    http: Arc<reqwest::Client>,
    redirect_uri: Url,
    expires_at: Instant,
    listener: Option<JoinHandle<()>>,
}

impl Flow {
    fn close(&mut self) -> Option<JoinHandle<()>> {
        self.status.send_replace(SignInStatus::Expired);
        let listener = self.listener.take();
        if let Some(listener) = &listener {
            listener.abort();
        }
        listener
    }
}

#[derive(Default)]
pub(crate) struct SignInService {
    flows: Mutex<HashMap<String, Flow>>,
    renewal: tokio::sync::Mutex<()>,
}

impl SignInService {
    fn flows(&self) -> MutexGuard<'_, HashMap<String, Flow>> {
        self.flows.lock().unwrap_or_else(PoisonError::into_inner)
    }

    fn prune(&self) {
        let now = Instant::now();
        self.flows().retain(|_, flow| {
            let alive = flow.expires_at > now;
            if !alive {
                flow.close();
            }
            alive
        });
    }

    pub(crate) async fn start(
        self: &Arc<Self>, client: SignInClient, http: Arc<reqwest::Client>,
    ) -> DomainResult<SignInStart> {
        self.prune();
        let endpoints = oauth::discover(&http, &client.issuer).await?;
        let authorization = oauth::authorize(&client, &endpoints);
        let redirect_uri = Url::parse(&client.redirect_uri).map_err(|e| {
            DomainError::InvalidConfig(format!(
                "Invalid sign-in redirect {}: {e}",
                client.redirect_uri
            ))
        })?;
        let loopback = Loopback::bind(&redirect_uri).await;
        let id = oauth::random_id::<16>();
        let expires_at = Instant::now() + PENDING_TTL;
        let start = SignInStart {
            id: id.clone(),
            authorize_url: authorization.url.to_string(),
            label: client.label.clone(),
            manual: loopback.is_none(),
        };

        let mut flows = self.flows();
        let flow = flows.entry(id.clone()).insert_entry(Flow {
            status: watch::channel(SignInStatus::Pending).0,
            pending: Some(Pending {
                state: authorization.state,
                verifier: authorization.verifier,
                token_endpoint: endpoints.token,
                client,
            }),
            credential: None,
            http,
            redirect_uri,
            expires_at,
            listener: None,
        });
        flow.into_mut().listener = loopback
            .map(|loopback| tokio::spawn(listen(Arc::downgrade(self), id, loopback, expires_at)));
        Ok(start)
    }

    pub(crate) fn status(&self, id: &str) -> SignInStatus {
        self.prune();
        self.flows()
            .get(id)
            .map_or(SignInStatus::Expired, |flow| flow.status.borrow().clone())
    }

    pub(crate) async fn wait(&self, id: &str) -> SignInStatus {
        self.prune();
        let watched = self
            .flows()
            .get(id)
            .map(|flow| (flow.status.subscribe(), flow.expires_at));
        let Some((mut status, deadline)) = watched else {
            return SignInStatus::Expired;
        };
        let settled = status.wait_for(|s| *s != SignInStatus::Pending);
        let outcome = match tokio::time::timeout_at(deadline, settled).await {
            Ok(Ok(settled)) => settled.clone(),
            _ => SignInStatus::Expired,
        };
        outcome
    }

    pub(crate) async fn finish(&self, id: &str, callback: Callback) -> DomainResult<SignInStatus> {
        let (pending, http) = {
            let mut flows = self.flows();
            let Some(flow) = flows.get_mut(id) else {
                return Ok(SignInStatus::Expired);
            };
            let matching = flow
                .pending
                .take_if(|pending| callback.state.as_deref() == Some(pending.state.as_str()));
            match matching {
                Some(pending) => (pending, Arc::clone(&flow.http)),
                None if flow.pending.is_some() => {
                    return Err(DomainError::InvalidConfig(FOREIGN_CALLBACK.into()));
                }
                None => return Ok(flow.status.borrow().clone()),
            }
        };

        let Callback {
            code,
            error,
            error_description,
            ..
        } = callback;
        let result = match (error, code) {
            (Some(error), _) => Err(error_description.unwrap_or(error)),
            (None, None) => Err(MISSING_CODE.to_string()),
            (None, Some(code)) => oauth::exchange(
                &http,
                &pending.token_endpoint,
                &pending.client,
                &code,
                &pending.verifier,
            )
            .await
            .map_err(failure_message),
        };

        let mut flows = self.flows();
        let Some(flow) = flows.get_mut(id) else {
            return Ok(SignInStatus::Expired);
        };
        let status = match result {
            Ok(credential) => {
                let account = credential.account.clone();
                flow.credential = Some(credential);
                flow.expires_at = Instant::now() + COMPLETED_TTL;
                SignInStatus::Completed { account }
            }
            Err(message) => SignInStatus::Failed { message },
        };
        flow.status.send_replace(status.clone());
        Ok(status)
    }

    pub(crate) async fn complete(
        &self, id: &str, redirect_url: &str,
    ) -> DomainResult<SignInStatus> {
        self.prune();
        let redirect_uri = {
            let flows = self.flows();
            let Some(flow) = flows.get(id) else {
                return Ok(SignInStatus::Expired);
            };
            if flow.pending.is_none() {
                return Ok(flow.status.borrow().clone());
            }
            flow.redirect_uri.clone()
        };
        let pasted = Url::parse(redirect_url.trim())
            .ok()
            .filter(|url| {
                url.scheme() == redirect_uri.scheme()
                    && url.host_str() == redirect_uri.host_str()
                    && url.port_or_known_default() == redirect_uri.port_or_known_default()
                    && url.path() == redirect_uri.path()
            })
            .ok_or_else(|| DomainError::InvalidConfig(WRONG_ADDRESS.into()))?;

        let status = self.finish(id, Callback::from_url(&pasted)).await?;
        if status != SignInStatus::Pending {
            let listener = self
                .flows()
                .get_mut(id)
                .and_then(|flow| flow.listener.take());
            if let Some(listener) = listener {
                listener.abort();
                let _ = listener.await;
            }
        }
        Ok(status)
    }

    pub(crate) async fn cancel(&self, id: &str) {
        let listener = {
            let mut flows = self.flows();
            if flows.get(id).is_some_and(|flow| flow.credential.is_some()) {
                return;
            }
            flows.remove(id).and_then(|mut flow| flow.close())
        };
        if let Some(listener) = listener {
            let _ = listener.await;
        }
    }

    pub(crate) async fn credential(&self, id: &str) -> DomainResult<SignInCredential> {
        let _renewal = self.renewal.lock().await;
        let (current, http) = self.completed(id)?;
        if !current.renew_at().is_some_and(|at| at <= Utc::now()) {
            return Ok(current);
        }

        let renewed = oauth::refresh(&http, &current).await?;
        if let Some(flow) = self.flows().get_mut(id) {
            flow.credential = Some(renewed.clone());
        }
        Ok(renewed)
    }

    fn completed(&self, id: &str) -> DomainResult<(SignInCredential, Arc<reqwest::Client>)> {
        self.prune();
        let flows = self.flows();
        let Some(flow) = flows.get(id) else {
            return Err(DomainError::ProviderAuthenticationFailed(
                SIGN_IN_EXPIRED.into(),
            ));
        };
        if let Some(credential) = &flow.credential {
            return Ok((credential.clone(), Arc::clone(&flow.http)));
        }
        let status = flow.status.borrow().clone();
        Err(match status {
            SignInStatus::Pending => DomainError::InvalidConfig(UNFINISHED.into()),
            SignInStatus::Failed { message } => DomainError::ProviderAuthenticationFailed(message),
            SignInStatus::Completed { .. } | SignInStatus::Expired => {
                DomainError::ProviderAuthenticationFailed(SIGN_IN_EXPIRED.into())
            }
        })
    }

    pub(crate) fn forget(&self, id: &str) {
        self.flows().remove(id);
    }

    fn expire(&self, id: &str) {
        if let Some(mut flow) = self.flows().remove(id) {
            flow.close();
        }
    }
}

impl Drop for SignInService {
    fn drop(&mut self) {
        for flow in self.flows().values_mut() {
            flow.close();
        }
    }
}

fn failure_message(error: DomainError) -> String {
    match error {
        DomainError::ProviderAuthenticationFailed(message) => message,
        other => other.to_string(),
    }
}

async fn listen(service: Weak<SignInService>, id: String, loopback: Loopback, deadline: Instant) {
    loop {
        let (stream, url) = match tokio::time::timeout_at(deadline, loopback.accept()).await {
            Err(_) => {
                if let Some(service) = service.upgrade() {
                    service.expire(&id);
                }
                return;
            }
            Ok(Err(error)) => {
                tracing::warn!(%error, "Stopped listening for the sign-in redirect");
                return;
            }
            Ok(Ok(accepted)) => accepted,
        };
        let Some(service) = service.upgrade() else {
            return;
        };
        let outcome = service.finish(&id, Callback::from_url(&url)).await;
        let page = match &outcome {
            Ok(SignInStatus::Completed { .. }) => Ok(()),
            Ok(SignInStatus::Failed { message }) => Err(message.clone()),
            Ok(SignInStatus::Pending | SignInStatus::Expired) => Err(TIMED_OUT.to_string()),
            Err(error) => Err(failure_message(error.clone())),
        };
        loopback::respond(stream, &page).await;
        if outcome.is_ok() {
            return;
        }
    }
}

#[cfg(test)]
mod tests {
    use pipedash_plugin_api::SignInToken;
    use serde_json::json;

    use super::*;
    use crate::infrastructure::oauth::loopback::tests::{
        free_redirect_uri,
        get,
    };
    use crate::infrastructure::oauth::test_idp::{
        jwt,
        Idp,
    };

    async fn setup() -> (Arc<SignInService>, Idp, SignInClient) {
        let idp = Idp::start().await;
        idp.reply(
            200,
            json!({"access_token": "a", "id_token": jwt(json!({"email": "dev@example.com"})), "refresh_token": "r"}),
        );
        let client = SignInClient {
            label: "Dex".into(),
            issuer: idp.base_url.clone(),
            client_id: "cli".into(),
            scopes: vec!["openid".into()],
            redirect_uri: free_redirect_uri().await.to_string(),
            authorize_params: Vec::new(),
            token: SignInToken::IdToken,
        };
        (Arc::new(SignInService::default()), idp, client)
    }

    fn callback(client: &SignInClient, start: &SignInStart, query: &str) -> Url {
        let state = Url::parse(&start.authorize_url)
            .unwrap()
            .query_pairs()
            .find(|(k, _)| k == "state")
            .unwrap()
            .1
            .into_owned();
        let query = query.replace("{state}", &state);
        Url::parse(&format!("{}?{query}", client.redirect_uri)).unwrap()
    }

    #[tokio::test]
    async fn browser_redirect_completes_the_flow() {
        let (service, _idp, client) = setup().await;
        let start = service
            .start(client.clone(), Arc::new(reqwest::Client::new()))
            .await
            .unwrap();
        assert!(!start.manual);

        let (status, body) = get(&callback(&client, &start, "code=c&state={state}")).await;

        assert_eq!(status, 200);
        assert!(body.contains("Signed in"));
        let completed = SignInStatus::Completed {
            account: Some("dev@example.com".into()),
        };
        assert_eq!(service.wait(&start.id).await, completed);
        assert_eq!(
            service
                .credential(&start.id)
                .await
                .unwrap()
                .refresh_token
                .as_deref(),
            Some("r")
        );
    }

    #[tokio::test]
    async fn foreign_state_keeps_waiting_and_provider_errors_fail() {
        let (service, _idp, client) = setup().await;
        let start = service
            .start(client.clone(), Arc::new(reqwest::Client::new()))
            .await
            .unwrap();

        let (status, _) = get(&callback(&client, &start, "code=c&state=other")).await;
        assert_eq!(status, 400);
        assert_eq!(service.status(&start.id), SignInStatus::Pending);

        let (status, body) = get(&callback(
            &client,
            &start,
            "error=access_denied&error_description=Denied&state={state}",
        ))
        .await;
        assert_eq!(status, 400);
        assert!(body.contains("Denied"));
        assert_eq!(
            service.status(&start.id),
            SignInStatus::Failed {
                message: "Denied".into()
            }
        );
    }

    #[tokio::test]
    async fn pasted_address_completes_only_for_the_redirect() {
        let (service, _idp, client) = setup().await;
        let start = service
            .start(client.clone(), Arc::new(reqwest::Client::new()))
            .await
            .unwrap();

        let foreign = service
            .complete(&start.id, "https://example.com/auth/callback?code=c")
            .await;
        assert!(matches!(foreign, Err(DomainError::InvalidConfig(m)) if m == WRONG_ADDRESS));

        let pasted = callback(&client, &start, "code=c&state={state}");
        let status = service
            .complete(&start.id, &format!("  {pasted} "))
            .await
            .unwrap();
        assert!(matches!(status, SignInStatus::Completed { .. }));
    }

    #[tokio::test]
    async fn cancel_frees_the_redirect_port_and_expires_the_flow() {
        let (service, _idp, client) = setup().await;
        let http = Arc::new(reqwest::Client::new());
        let first = service.start(client.clone(), http.clone()).await.unwrap();

        service.cancel(&first.id).await;

        assert_eq!(service.status(&first.id), SignInStatus::Expired);
        assert!(service.credential(&first.id).await.is_err());
        let second = service.start(client, http).await.unwrap();
        assert!(!second.manual);
    }

    #[tokio::test]
    async fn cancel_keeps_a_completed_sign_in_for_saving() {
        let (service, _idp, client) = setup().await;
        let start = service
            .start(client.clone(), Arc::new(reqwest::Client::new()))
            .await
            .unwrap();
        let pasted = callback(&client, &start, "code=c&state={state}");
        service.complete(&start.id, pasted.as_str()).await.unwrap();

        service.cancel(&start.id).await;

        assert!(matches!(
            service.status(&start.id),
            SignInStatus::Completed { .. }
        ));
        assert!(service.credential(&start.id).await.is_ok());
    }

    #[tokio::test]
    async fn a_completed_sign_in_is_renewed_once_before_it_is_used() {
        let (service, idp, client) = setup().await;
        let expired = jwt(json!({"email": "dev@example.com", "exp": Utc::now().timestamp() - 10}));
        idp.reply(
            200,
            json!({"access_token": "a", "id_token": expired, "refresh_token": "r1"}),
        );
        let start = service
            .start(client.clone(), Arc::new(reqwest::Client::new()))
            .await
            .unwrap();
        let pasted = callback(&client, &start, "code=c&state={state}");
        service.complete(&start.id, pasted.as_str()).await.unwrap();
        let fresh = jwt(json!({"email": "dev@example.com", "exp": Utc::now().timestamp() + 3600}));
        idp.reply(
            200,
            json!({"access_token": "a", "id_token": fresh, "refresh_token": "r2"}),
        );

        let first = service.credential(&start.id).await.unwrap();
        let second = service.credential(&start.id).await.unwrap();

        assert_eq!(first.token, fresh);
        assert_eq!(first.refresh_token.as_deref(), Some("r2"));
        assert_eq!(second, first);
        assert_eq!(idp.forms.lock().unwrap().len(), 2);
    }
}