use std::str::Utf8Error;
use url::percent_encoding::{QUERY_ENCODE_SET, percent_encode, percent_decode};
use hyper::header::{Authorization, Basic};
use ClientType;
#[derive(Clone, Debug)]
pub struct ClientData {
pub client_id: String,
pub client_type: ClientType,
pub redirect_uri: Vec<String>,
pub credentials: String,
pub authn_scheme: Option<String>,
}
impl ClientData {
pub fn http_basic_authentication_generate(&self) -> Authorization<Basic> {
let username: String = percent_encode(
self.client_id.as_bytes(), QUERY_ENCODE_SET).collect::<String>();
let password: String = percent_encode(
self.credentials.as_bytes(), QUERY_ENCODE_SET).collect::<String>();
Authorization(
Basic {
username: username,
password: Some(password),
}
)
}
pub fn http_basic_authentication_deconstruct(basic: Basic)
-> Result<(String, String), Utf8Error>
{
let client_id =
try!(percent_decode(basic.username.as_bytes()).decode_utf8()).into_owned();
let authz_credentials = if basic.password.is_none() {
String::new()
} else {
try!(percent_decode(basic.password.unwrap().as_bytes()).decode_utf8()).into_owned()
};
Ok((client_id, authz_credentials))
}
}