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
mod authenticate;

pub use authenticate::{AuthenticateRequest, AuthenticateResponse};

use tonic::transport::Channel;

use crate::proto::etcdserverpb::auth_client::AuthClient;
use crate::Result;

/// Auth client.
#[derive(Clone)]
pub struct Auth {
    client: AuthClient<Channel>,
}

impl Auth {
    pub(crate) fn new(client: AuthClient<Channel>) -> Self {
        Self { client }
    }

    /// Performs an authenticating operation.
    /// It generates an authentication token based on a given user name and password.
    /// # Errors
    /// Will returns `Err` if the status of `response` is not `ok`
    pub async fn authenticate(&mut self, req: AuthenticateRequest) -> Result<AuthenticateResponse> {
        let resp = self
            .client
            .authenticate(tonic::Request::new(req.into()))
            .await?;

        Ok(resp.into_inner().into())
    }
}