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
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0
//! A whole sign-in: the authorization request, the system's auth browser, the callback, and
//! the token exchange, as one future on [`Client`].
use crate::AuthError;
use crate::authorize::{Authorization, Client};
use crate::session;
use crate::tokens::{self, TokenSet};
impl Client {
/// Sign the person in: open the system's auth browser on the provider's authorization
/// page, take the callback, exchange the code, verify the ID token, and resolve with the
/// tokens. Call it on the UI thread (a button's action) and `.await` it under `day::task`;
/// the continuation runs on the UI thread again. The person dismissing the browser resolves
/// with [`AuthError::Cancelled`].
///
/// # Errors
///
/// The future resolves to the error [`Client::start`], the browser session or
/// [`tokens::exchange`] reports; [`AuthError::Cancelled`] when the person dismisses the
/// browser.
pub fn sign_in(&self) -> impl std::future::Future<Output = Result<TokenSet, AuthError>> + Send {
let client = self.clone();
let started = self.start();
async move {
let mut request = started?;
let callback = match request.take_loopback() {
Some(listener) => {
// The default browser, and the listener the redirect points at.
let waiting = listener.wait(&client.session);
day_core::open_url(request.url());
waiting.await?
}
None => {
session::open(request.url(), request.redirect_uri(), &client.session).await?
}
};
tokens::exchange(&client, request, &callback).await
}
}
/// Finish a sign-in whose browser the app drove itself: `request` from [`Client::start`],
/// `callback_url` from wherever the app received it.
///
/// # Errors
///
/// The future resolves to the error [`tokens::exchange`] reports.
pub fn complete(
&self,
request: Authorization,
callback_url: &str,
) -> impl std::future::Future<Output = Result<TokenSet, AuthError>> + Send {
let client = self.clone();
let callback = callback_url.to_string();
async move { tokens::exchange(&client, request, &callback).await }
}
/// Trade a refresh token for a new set; see [`tokens::refresh`].
///
/// # Errors
///
/// The future resolves to the error [`tokens::refresh`] reports.
pub fn refresh(
&self,
refresh_token: &str,
) -> impl std::future::Future<Output = Result<TokenSet, AuthError>> + Send {
let client = self.clone();
let token = refresh_token.to_string();
async move { tokens::refresh(&client, &token).await }
}
/// Tell the provider a token is no longer in use; see [`tokens::revoke`].
///
/// # Errors
///
/// The future resolves to the error [`tokens::revoke`] reports.
pub fn revoke(
&self,
token: &str,
hint: &str,
) -> impl std::future::Future<Output = Result<(), AuthError>> + Send {
let client = self.clone();
let token = token.to_string();
let hint = hint.to_string();
async move { tokens::revoke(&client, &token, &hint).await }
}
/// The userinfo endpoint's answer; see [`tokens::userinfo`].
///
/// # Errors
///
/// The future resolves to the error [`tokens::userinfo`] reports.
pub fn userinfo(
&self,
access_token: &str,
) -> impl std::future::Future<Output = Result<serde_json::Value, AuthError>> + Send {
let client = self.clone();
let token = access_token.to_string();
async move { tokens::userinfo(&client, &token).await }
}
/// The URL that signs the person out at the provider too; see [`tokens::end_session_url`].
pub fn end_session_url(
&self,
id_token_hint: Option<&str>,
post_logout_redirect_uri: Option<&str>,
) -> Option<String> {
tokens::end_session_url(self, id_token_hint, post_logout_redirect_uri)
}
}