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
//! Types to control access to HTTP API

use bui_backend_types::AccessToken;
use std::net::SocketAddr;

#[derive(Clone, Debug)]
struct JwtSecret(Vec<u8>);

/// Data required to specify all auth information when access is restricted
#[derive(Clone, Debug)]
pub struct AccessInfo {
    addr: SocketAddr,
    access_token: AccessToken,
    jwt_secret: JwtSecret,
}

impl AccessInfo {
    pub(crate) fn new(
        addr: SocketAddr,
        access_token: AccessToken,
        jwt_secret: Vec<u8>,
    ) -> Result<Self, crate::Error> {
        if let AccessToken::PreSharedToken(ref _token) = access_token {
            let jwt_secret = JwtSecret(jwt_secret);
            let access_token = access_token.clone();
            Ok(Self {
                addr,
                access_token,
                jwt_secret,
            })
        } else {
            Err(crate::Error::NonLocalhostRequiresPreSharedToken.into())
        }
    }
}

/// Access control method for the HTTP API
#[derive(Clone, Debug)]
pub enum AccessControl {
    /// Access is not restricted (for use with local IP addresses)
    Insecure(SocketAddr),
    /// Access is restricted
    WithToken(AccessInfo),
}

impl AccessControl {
    /// The address to bind the server to (e.g. `0.0.0.0`)
    pub(crate) fn bind_addr(&self) -> &SocketAddr {
        match self {
            &AccessControl::Insecure(ref addr) => addr,
            &AccessControl::WithToken(ref info) => &info.addr,
        }
    }

    pub(crate) fn token(&self) -> AccessToken {
        match self {
            AccessControl::Insecure(_) => AccessToken::NoToken,
            AccessControl::WithToken(ref info) => info.access_token.clone(),
        }
    }

    pub(crate) fn jwt_secret(&self) -> &[u8] {
        match self {
            &AccessControl::Insecure(ref _addr) => b"insecure",
            &AccessControl::WithToken(ref info) => &info.jwt_secret.0,
        }
    }
}