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
use crate::parse::slice_to_string;
use serde::{Deserialize, Serialize};

use nom::{
    bytes::complete::take_while,
    character::{complete::char, is_alphanumeric},
    combinator::{map_res, opt},
    error::ParseError,
    IResult,
};

use std::fmt;

/// URI Credentials
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct UriAuth {
    pub username: String,
    pub password: Option<String>,
}

impl UriAuth {
    /// Create new UriAuth from `username`.
    pub fn new<S: Into<String>>(username: S) -> UriAuth {
        UriAuth {
            username: username.into(),
            password: None,
        }
    }

    /// Set the uri password.
    pub fn password<S: Into<String>>(mut self, p: S) -> UriAuth {
        self.password = Some(p.into());
        self
    }
}

impl fmt::Display for UriAuth {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(pass) = &self.password {
            write!(f, "{}:{}", &self.username, pass)?;
        } else {
            write!(f, "{}", &self.username)?;
        }
        Ok(())
    }
}

/// Parse the username/password of a uri.
pub fn parse_uriauth<'a, E: ParseError<&'a [u8]>>(
    input: &'a [u8],
) -> IResult<&'a [u8], UriAuth, E> {
    let (input, username) = map_res(take_while(is_alphanumeric), slice_to_string::<E>)(input)?;
    let (input, password) = opt(parse_password::<E>)(input)?;
    let (input, _) = char('@')(input)?;
    Ok((input, UriAuth { username, password }))
}

/// Currently this will only accept alphanumeric characters.
pub fn parse_password<'a, E: ParseError<&'a [u8]>>(
    input: &'a [u8],
) -> IResult<&'a [u8], String, E> {
    let (input, _) = char(':')(input)?;
    Ok(map_res(take_while(is_alphanumeric), slice_to_string::<E>)(
        input,
    )?)
}