aboutsummaryrefslogtreecommitdiffstats
path: root/teleterm/src/oauth.rs
blob: 894ba1d8076d4351894d0b4497d03cb8a34fc0a4 (plain) (blame)
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
use crate::prelude::*;
use oauth2::TokenResponse as _;
use std::io::Read as _;

mod recurse_center;
pub use recurse_center::RecurseCenter;

// this needs to be fixed because we listen for it in a hardcoded place
pub const CLI_REDIRECT_URL: &str = "http://localhost:44141/oauth";

pub trait Oauth {
    fn client(&self) -> &oauth2::basic::BasicClient;
    fn user_id(&self) -> &str;
    fn name(&self) -> &str;

    fn server_token_file(
        &self,
        must_exist: bool,
    ) -> Option<std::path::PathBuf> {
        let name = format!("server-oauth-{}-{}", self.name(), self.user_id());
        crate::dirs::Dirs::new().data_file(&name, must_exist)
    }

    fn generate_authorize_url(&self) -> String {
        let (auth_url, _) = self
            .client()
            .authorize_url(oauth2::CsrfToken::new_random)
            .url();
        auth_url.to_string()
    }

    fn get_access_token_from_auth_code(
        &self,
        code: &str,
    ) -> Box<dyn futures::Future<Item = String, Error = Error> + Send> {
        let token_cache_file = self.server_token_file(false).unwrap();
        let fut = self
            .client()
            .exchange_code(oauth2::AuthorizationCode::new(code.to_string()))
            .request_async(oauth2::reqwest::async_http_client)
            .map_err(|e| {
                let msg = stringify_oauth2_http_error(&e);
                Error::ExchangeCode { msg }
            })
            .and_then(|token| {
                cache_refresh_token(token_cache_file, &token)
                    .map(move |_| token.access_token().secret().to_string())
            });
        Box::new(fut)
    }

    fn get_access_token_from_refresh_token(
        &self,
        token: &str,
    ) -> Box<dyn futures::Future<Item = String, Error = Error> + Send> {
        let token_cache_file = self.server_token_file(false).unwrap();
        let fut = self
            .client()
            .exchange_refresh_token(&oauth2::RefreshToken::new(
                token.to_string(),
            ))
            .request_async(oauth2::reqwest::async_http_client)
            .map_err(|e| {
                let msg = stringify_oauth2_http_error(&e);
                Error::ExchangeRefreshToken { msg }
            })
            .and_then(|token| {
                cache_refresh_token(token_cache_file, &token)
                    .map(move |_| token.access_token().secret().to_string())
            });
        Box::new(fut)
    }

    fn get_username_from_access_token(
        &self,
        token: &str,
    ) -> Box<dyn futures::Future<Item = String, Error = Error> + Send>;
}

pub fn save_client_auth_id(
    auth: crate::protocol::AuthType,
    id: &str,
) -> impl futures::Future<Item = (), Error = Error> {
    let id_file = client_id_file(auth, false).unwrap();
    let id = id.to_string();
    tokio::fs::File::create(id_file.clone())
        .with_context(move || crate::error::CreateFile {
            filename: id_file.to_string_lossy().to_string(),
        })
        .and_then(|file| {
            tokio::io::write_all(file, id).context(crate::error::WriteFile)
        })
        .map(|_| ())
}

pub fn load_client_auth_id(
    auth: crate::protocol::AuthType,
) -> Option<String> {
    client_id_file(auth, true).and_then(|id_file| {
        std::fs::File::open(id_file).ok().and_then(|mut file| {
            let mut id = vec![];
            file.read_to_end(&mut id).ok().map(|_| {
                std::string::String::from_utf8_lossy(&id).to_string()
            })
        })
    })
}

fn client_id_file(
    auth: crate::protocol::AuthType,
    must_exist: bool,
) -> Option<std::path::PathBuf> {
    let filename = format!("client-oauth-{}", auth.name());
    crate::dirs::Dirs::new().data_file(&filename, must_exist)
}

fn cache_refresh_token(
    token_cache_file: std::path::PathBuf,
    token: &oauth2::basic::BasicTokenResponse,
) -> Box<dyn futures::Future<Item = (), Error = Error> + Send> {
    let token_data = format!(
        "{}\n{}\n",
        token.refresh_token().unwrap().secret(),
        token.access_token().secret(),
    );
    let fut = tokio::fs::File::create(token_cache_file.clone())
        .with_context(move || crate::error::CreateFile {
            filename: token_cache_file.to_string_lossy().to_string(),
        })
        .and_then(|file| {
            tokio::io::write_all(file, token_data)
                .context(crate::error::WriteFile)
        })
        .map(|_| ());
    Box::new(fut)
}

#[derive(Debug, Clone)]
pub struct Config {
    client_id: String,
    client_secret: String,
    auth_url: url::Url,
    token_url: url::Url,
    redirect_url: url::Url,
}

impl Config {
    pub fn set_redirect_url(&mut self, url: url::Url) {
        self.redirect_url = url;
    }

    fn into_basic_client(self) -> oauth2::basic::BasicClient {
        oauth2::basic::BasicClient::new(
            oauth2::ClientId::new(self.client_id),
            Some(oauth2::ClientSecret::new(self.client_secret)),
            oauth2::AuthUrl::new(self.auth_url),
            Some(oauth2::TokenUrl::new(self.token_url)),
        )
        .set_redirect_url(oauth2::RedirectUrl::new(self.redirect_url))
    }
}

// make this actually give useful information, because the default
// stringification is pretty useless
fn stringify_oauth2_http_error(
    e: &oauth2::RequestTokenError<
        oauth2::reqwest::Error,
        oauth2::StandardErrorResponse<oauth2::basic::BasicErrorResponseType>,
    >,
) -> String {
    match e {
        oauth2::RequestTokenError::ServerResponse(t) => {
            format!("ServerResponse({})", t)
        }
        oauth2::RequestTokenError::Request(re) => format!("Request({})", re),
        oauth2::RequestTokenError::Parse(se, b) => format!(
            "Parse({}, {})",
            se,
            std::string::String::from_utf8_lossy(b)
        ),
        oauth2::RequestTokenError::Other(s) => format!("Other({})", s),
    }
}