Skip to main content

isothrum_machine/cloud/
token.rs

1//! Clouds whose credential is an API token alone: whether a token still signs in, asked of the
2//! provider's own `account` endpoint over HTTPS (no CLI to ask). DigitalOcean's
3//! ([`digitalocean`]) and Linode's ([`linode`]). What a refusal means to a user is the caller's.
4
5use std::time::Duration;
6
7use isothrum_runner::tls::{Tls, Trust};
8
9use crate::place::proxmox::chain;
10
11/// How long the provider may take to answer.
12pub const TOKEN_TIMEOUT: Duration = Duration::from_secs(15);
13
14/// Why a token wasn't accepted.
15#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
16#[non_exhaustive]
17pub enum TokenError {
18    /// The provider refused the token (HTTP 401): wrong, revoked or expired.
19    #[error("the provider rejected the token")]
20    Rejected,
21    /// The provider answered with another status than a success (403 for a read-only scope, 5xx).
22    #[error("the provider's API answered HTTP {0}")]
23    Status(u16),
24    /// No answer: no network, the name not resolved, TLS refused, timed out. Why.
25    #[error("can't reach the provider: {0}")]
26    Unreachable(String),
27}
28
29/// DigitalOcean: `GET /v2/account` with the token.
30pub mod digitalocean {
31    use super::{TokenError, Trust, account};
32
33    /// The account endpoint a token is checked against.
34    pub const ACCOUNT_URL: &str = "https://api.digitalocean.com/v2/account";
35
36    /// Whether `token` signs in to DigitalOcean.
37    ///
38    /// # Errors
39    ///
40    /// The token was refused, or the API didn't answer as it does to a valid one.
41    pub async fn check_token(token: &str) -> Result<(), TokenError> {
42        account(ACCOUNT_URL, token, Trust::System).await
43    }
44}
45
46/// Linode (Akamai): `GET /v4/account` with the token.
47pub mod linode {
48    use super::{TokenError, Trust, account};
49
50    /// The account endpoint a token is checked against.
51    pub const ACCOUNT_URL: &str = "https://api.linode.com/v4/account";
52
53    /// Whether `token` signs in to Linode.
54    ///
55    /// # Errors
56    ///
57    /// The token was refused, or the API didn't answer as it does to a valid one.
58    pub async fn check_token(token: &str) -> Result<(), TokenError> {
59        account(ACCOUNT_URL, token, Trust::System).await
60    }
61}
62
63/// `GET url` with `token` as its bearer, the server trusted as `trust` says: a success is a token
64/// that signs in.
65async fn account(url: &str, token: &str, trust: Trust) -> Result<(), TokenError> {
66    let tls = Tls::new(trust).map_err(|e| TokenError::Unreachable(e.to_string()))?;
67    let http = reqwest::Client::builder()
68        .tls_backend_preconfigured(tls.config().clone())
69        .connect_timeout(TOKEN_TIMEOUT)
70        .timeout(TOKEN_TIMEOUT)
71        .build()
72        .map_err(|e| TokenError::Unreachable(chain(&e)))?;
73    let answer = http.get(url).bearer_auth(token).send().await.map_err(|e| TokenError::Unreachable(chain(&e)))?;
74    match answer.status().as_u16() {
75        200..=299 => Ok(()),
76        401 => Err(TokenError::Rejected),
77        other => Err(TokenError::Status(other)),
78    }
79}
80
81#[cfg(test)]
82mod tests;