aboutsummaryrefslogtreecommitdiffstats
path: root/teleterm/src/web/login.rs
blob: 2873887b1ae008d70e5461ecf879f35661afc6c0 (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
use crate::prelude::*;

use gotham::state::FromState as _;

#[derive(
    serde::Deserialize,
    gotham_derive::StateData,
    gotham_derive::StaticResponseExtender,
)]
pub struct QueryParams {
    username: String,
}

#[derive(serde::Serialize)]
struct Response {
    username: String,
}

pub fn run(
    mut state: gotham::state::State,
) -> (gotham::state::State, hyper::Response<hyper::Body>) {
    let username = {
        let query_params = QueryParams::borrow_from(&state);
        query_params.username.clone()
    };

    let config = crate::web::Config::borrow_from(&state);

    let (_, address) = config.server_address;
    let connector: crate::client::Connector<_> = Box::new(move || {
        Box::new(
            tokio::net::tcp::TcpStream::connect(&address)
                .context(crate::error::Connect { address }),
        )
    });
    let auth = crate::protocol::Auth::plain(&username);
    let client = crate::client::Client::raw("teleterm-web", connector, &auth);

    let (w_login, r_login) = tokio::sync::oneshot::channel();

    tokio::spawn(
        Client::new(client, auth, w_login)
            // XXX if this happens, we might not have sent anything on the
            // channel, and so the wait might block forever
            .map_err(|e| log::error!("error logging in: {}", e)),
    );

    let session = gotham::middleware::session::SessionData::<
        crate::web::SessionData,
    >::borrow_mut_from(&mut state);

    match r_login.wait().unwrap() {
        Ok(login) => {
            session.login = Some(login);
        }
        Err(e) => {
            session.login = None;
            log::error!("error logging in: {}", e);
            return (
                state,
                hyper::Response::builder()
                    .status(hyper::StatusCode::INTERNAL_SERVER_ERROR)
                    .body(hyper::Body::empty())
                    .unwrap(),
            );
        }
    }

    (
        state,
        hyper::Response::new(hyper::Body::from(
            serde_json::to_string(&Response { username }).unwrap(),
        )),
    )
}

pub(crate) struct Client<
    S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static,
> {
    client: crate::client::Client<S>,
    auth: crate::protocol::Auth,
    w_login: Option<tokio::sync::oneshot::Sender<Result<super::LoginState>>>,
}

impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static>
    Client<S>
{
    pub(crate) fn new(
        client: crate::client::Client<S>,
        auth: crate::protocol::Auth,
        w_login: tokio::sync::oneshot::Sender<Result<super::LoginState>>,
    ) -> Self {
        Self {
            client,
            auth,
            w_login: Some(w_login),
        }
    }
}

impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static>
    Client<S>
{
    const POLL_FNS:
        &'static [&'static dyn for<'a> Fn(
            &'a mut Self,
        )
            -> component_future::Poll<
            (),
            Error,
        >] = &[&Self::poll_client];

    fn poll_client(&mut self) -> component_future::Poll<(), Error> {
        let res =
            match component_future::try_ready!(self.client.poll()).unwrap() {
                crate::client::Event::ServerMessage(msg) => match msg {
                    crate::protocol::Message::Disconnected => {
                        Err(Error::ServerDisconnected)
                    }
                    crate::protocol::Message::Error { msg } => {
                        Err(Error::Server { message: msg })
                    }
                    crate::protocol::Message::LoggedIn { username } => {
                        Ok(super::LoginState {
                            auth: self.auth.clone(),
                            username,
                        })
                    }
                    _ => {
                        return Ok(component_future::Async::DidWork);
                    }
                },
                _ => unreachable!(),
            };
        self.w_login.take().unwrap().send(res).unwrap();
        Ok(component_future::Async::Ready(()))
    }
}

impl<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + 'static>
    futures::Future for Client<S>
{
    type Item = ();
    type Error = Error;

    fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
        component_future::poll_future(self, Self::POLL_FNS)
    }
}