-
Notifications
You must be signed in to change notification settings - Fork 73
/
hub.rs
112 lines (95 loc) · 3.11 KB
/
hub.rs
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
use crate::app_config;
use google_drive3::hyper;
use google_drive3::hyper::client::HttpConnector;
use google_drive3::hyper_rustls::HttpsConnector;
use google_drive3::hyper_rustls::HttpsConnectorBuilder;
use google_drive3::oauth2;
use google_drive3::oauth2::authenticator::Authenticator;
use google_drive3::oauth2::authenticator_delegate::InstalledFlowDelegate;
use google_drive3::DriveHub;
use std::future::Future;
use std::io;
use std::ops::Deref;
use std::path::PathBuf;
use std::pin::Pin;
pub struct HubConfig {
pub secret: oauth2::ApplicationSecret,
pub tokens_path: PathBuf,
}
pub struct Hub(DriveHub<HttpsConnector<HttpConnector>>);
impl Deref for Hub {
type Target = DriveHub<HttpsConnector<HttpConnector>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Hub {
pub async fn new(auth: Auth) -> Hub {
let connector = HttpsConnectorBuilder::new()
.with_native_roots()
.https_or_http()
.enable_http1()
.enable_http2()
.build();
let http_client = hyper::Client::builder().build(connector);
Hub(google_drive3::DriveHub::new(http_client, auth.0))
}
}
pub struct Auth(pub Authenticator<HttpsConnector<HttpConnector>>);
impl Deref for Auth {
type Target = Authenticator<HttpsConnector<HttpConnector>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Auth {
pub async fn new(
config: &app_config::Secret,
tokens_path: &PathBuf,
) -> Result<Auth, io::Error> {
let secret = oauth2_secret(config);
let delegate = Box::new(AuthDelegate);
let auth = oauth2::InstalledFlowAuthenticator::builder(
secret,
oauth2::InstalledFlowReturnMethod::HTTPPortRedirect(8085),
)
.persist_tokens_to_disk(tokens_path)
.flow_delegate(delegate)
.build()
.await?;
Ok(Auth(auth))
}
}
fn oauth2_secret(config: &app_config::Secret) -> oauth2::ApplicationSecret {
oauth2::ApplicationSecret {
client_id: config.client_id.clone(),
client_secret: config.client_secret.clone(),
token_uri: String::from("https://oauth2.googleapis.com/token"),
auth_uri: String::from("https://accounts.google.com/o/oauth2/auth"),
redirect_uris: vec![String::from("urn:ietf:wg:oauth:2.0:oob")],
project_id: None,
client_email: None,
auth_provider_x509_cert_url: Some(String::from(
"https://www.googleapis.com/oauth2/v1/certs",
)),
client_x509_cert_url: None,
}
}
struct AuthDelegate;
impl InstalledFlowDelegate for AuthDelegate {
fn present_user_url<'a>(
&'a self,
url: &'a str,
_need_code: bool,
) -> Pin<Box<dyn Future<Output = Result<String, String>> + Send + 'a>> {
Box::pin(present_user_url(url))
}
}
async fn present_user_url(url: &str) -> Result<String, String> {
println!();
println!();
println!("Gdrive requires permissions to manage your files on Google Drive.");
println!("Open the url in your browser and follow the instructions:");
println!("{}", url);
Ok(String::new())
}