use hyper;
use std::fmt;
use std::io;
use std::error::Error;
use authenticator::Retry;
use types::RequestError;
use chrono::{DateTime, Local, UTC};
use std::time::Duration;
#[derive(Clone, Debug, PartialEq)]
pub struct PollInformation {
pub user_code: String,
pub verification_url: String,
pub expires_at: DateTime<UTC>,
pub interval: Duration,
}
impl fmt::Display for PollInformation {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
writeln!(f, "Proceed with polling until {}", self.expires_at)
}
}
#[derive(Debug)]
pub enum PollError {
HttpError(hyper::Error),
Expired(DateTime<UTC>),
AccessDenied,
}
impl fmt::Display for PollError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
PollError::HttpError(ref err) => err.fmt(f),
PollError::Expired(ref date) => writeln!(f, "Authentication expired at {}", date),
PollError::AccessDenied => "Access denied by user".fmt(f),
}
}
}
pub trait AuthenticatorDelegate {
fn connection_error(&mut self, &hyper::Error) -> Retry {
Retry::Abort
}
fn token_storage_failure(&mut self, is_set: bool, _: &Error) -> Retry {
let _ = is_set;
Retry::Abort
}
fn request_failure(&mut self, RequestError) {}
fn expired(&mut self, &DateTime<UTC>) {}
fn denied(&mut self) {}
fn token_refresh_failed(&mut self, error: &String, error_description: &Option<String>) {
{
let _ = error;
}
{
let _ = error_description;
}
}
fn pending(&mut self, &PollInformation) -> Retry {
Retry::After(Duration::from_secs(5))
}
fn present_user_code(&mut self, pi: &PollInformation) {
println!("Please enter {} at {} and grant access to this application",
pi.user_code,
pi.verification_url);
println!("Do not close this application until you either denied or granted access.");
println!("You have time until {}.",
pi.expires_at.with_timezone(&Local));
}
fn present_user_url(&mut self, url: &String, need_code: bool) -> Option<String> {
if need_code {
println!("Please direct your browser to {}, follow the instructions and enter the \
code displayed here: ",
url);
let mut code = String::new();
io::stdin().read_line(&mut code).ok().map(|_| code)
} else {
println!("Please direct your browser to {} and follow the instructions displayed \
there.",
url);
None
}
}
}
pub struct DefaultAuthenticatorDelegate;
impl AuthenticatorDelegate for DefaultAuthenticatorDelegate {}