use log::{info, trace, warn};
use std::collections::HashMap;
use std::process;
use tiny_http::{Response, Server};
use url::Url;
pub mod server {
use super::*;
#[allow(unused_assignments)]
pub(crate) fn run() -> HashMap<&'static str, String> {
let server = Server::http("0.0.0.0:8888").unwrap_or_else(|e| {
warn!("Could not bind to port 8888: {}", e);
panic!("Could not bind to port 8888: {}", e);
});
info!("Listening on port 8888 for redirect of OAuth2 code.");
let mut code = String::new();
let mut state = String::new();
let mut params = HashMap::new();
let req = server.incoming_requests().next();
if let Some(req) = req {
let url = format!("http://localhost{}", req.url());
let parsed_url = Url::parse(&url).unwrap();
code = parsed_url
.query_pairs()
.find(|(key, _)| key == "code")
.map(|(_, value)| value.into_owned())
.unwrap_or_default();
trace!("Code: {}", code);
state = parsed_url
.query_pairs()
.find(|(key, _)| key == "state")
.map(|(_, value)| value.into_owned())
.unwrap_or_default();
if state.is_empty() || { code.is_empty() } {
warn!("Could not get code or state from redirect URL. Exiting.");
process::exit(1);
}
trace!("State: {}", state);
params.insert("code", code.to_string());
params.insert("state", state.to_string());
let response = Response::from_string("Please return to the terminal.");
req.respond(response).unwrap_or_else(|e| {
warn!("Could not respond to request: {}", e);
panic!("Could not respond to request: {}", e);
});
drop(server);
}
params
}
}