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
//! Methods for user authentication.

use rincon_core::api::auth::Credentials;
use rincon_core::api::method::{Method, Parameters, Operation, Prepare, RpcReturnType};
use rincon_core::arango::protocol::{FIELD_CODE, PATH_OPEN_AUTH};
use super::types::*;

/// Authenticates a user.
#[derive(Debug, Clone, PartialEq)]
pub struct Authenticate {
    request: AuthenticationRequest,
}

impl Authenticate {
    /// Constructs a new instance of the `Authenticate` initialized with the
    /// given credentials.
    pub fn with_credentials(credentials: &Credentials) -> Self {
        Authenticate {
            request: AuthenticationRequest::new(credentials.username(), credentials.password()),
        }
    }

    /// Constructs a new instance of the `Authenticate` initialized with the
    /// given username and password.
    pub fn with_user<N, P>(username: N, password: P) -> Self
        where N: Into<String>, P: Into<String>
    {
        Authenticate {
            request: AuthenticationRequest::new(username, password),
        }
    }
}

impl Method for Authenticate {
    type Result = AuthenticationResponse;
    const RETURN_TYPE: RpcReturnType = RpcReturnType {
        result_field: None,
        code_field: Some(FIELD_CODE),
    };
}

impl Prepare for Authenticate {
    type Content = AuthenticationRequest;

    fn operation(&self) -> Operation {
        Operation::Create
    }

    fn path(&self) -> String {
        String::from(PATH_OPEN_AUTH)
    }

    fn parameters(&self) -> Parameters {
        Parameters::empty()
    }

    fn header(&self) -> Parameters {
        Parameters::empty()
    }

    fn content(&self) -> Option<&Self::Content> {
        Some(&self.request)
    }
}