mirror of
https://github.com/agersant/polaris
synced 2024-12-04 10:39:09 +00:00
autoformat
This commit is contained in:
parent
42fcf70f53
commit
942ac099c7
2 changed files with 18 additions and 14 deletions
26
src/api.rs
26
src/api.rs
|
@ -40,23 +40,26 @@ impl Version {
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
struct Session {
|
struct Session {
|
||||||
username: String,
|
username: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SessionKey {}
|
struct SessionKey {}
|
||||||
|
|
||||||
impl typemap::Key for SessionKey {
|
impl typemap::Key for SessionKey {
|
||||||
type Value = Session;
|
type Value = Session;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_handler(collection: Collection, secret: &str) -> Chain {
|
pub fn get_handler(collection: Collection, secret: &str) -> Chain {
|
||||||
let collection = Arc::new(collection);
|
let collection = Arc::new(collection);
|
||||||
let api_handler = get_endpoints(collection);
|
let api_handler = get_endpoints(collection);
|
||||||
let mut api_chain = Chain::new(api_handler);
|
let mut api_chain = Chain::new(api_handler);
|
||||||
|
|
||||||
let manager = ChaCha20Poly1305SessionManager::<Session>::from_password(secret.as_bytes());
|
let manager = ChaCha20Poly1305SessionManager::<Session>::from_password(secret.as_bytes());
|
||||||
let config = SessionConfig::default();
|
let config = SessionConfig::default();
|
||||||
let session_middleware = SessionMiddleware::<Session, SessionKey, ChaCha20Poly1305SessionManager<Session>>::new(manager, config);
|
let session_middleware =
|
||||||
|
SessionMiddleware::<Session,
|
||||||
|
SessionKey,
|
||||||
|
ChaCha20Poly1305SessionManager<Session>>::new(manager, config);
|
||||||
api_chain.link_around(session_middleware);
|
api_chain.link_around(session_middleware);
|
||||||
|
|
||||||
api_chain
|
api_chain
|
||||||
|
@ -142,11 +145,9 @@ struct AuthHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_cookie(username: &str, response: &mut Response) {
|
fn set_cookie(username: &str, response: &mut Response) {
|
||||||
response.headers.set(
|
response
|
||||||
SetCookie(vec![
|
.headers
|
||||||
format!("username={}; Path=/", username)
|
.set(SetCookie(vec![format!("username={}; Path=/", username)]));
|
||||||
])
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Handler for AuthHandler {
|
impl Handler for AuthHandler {
|
||||||
|
@ -161,7 +162,8 @@ impl Handler for AuthHandler {
|
||||||
auth_success = self.collection
|
auth_success = self.collection
|
||||||
.auth(auth.username.as_str(), password.as_str());
|
.auth(auth.username.as_str(), password.as_str());
|
||||||
username = Some(auth.username.clone());
|
username = Some(auth.username.clone());
|
||||||
req.extensions.insert::<SessionKey>(Session { username: auth.username.clone() });
|
req.extensions
|
||||||
|
.insert::<SessionKey>(Session { username: auth.username.clone() });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,7 +217,9 @@ fn auth(request: &mut Request, collection: &Collection) -> IronResult<Response>
|
||||||
if collection.auth(username.as_str(), password.as_str()) {
|
if collection.auth(username.as_str(), password.as_str()) {
|
||||||
let mut response = Response::with((status::Ok, ""));
|
let mut response = Response::with((status::Ok, ""));
|
||||||
set_cookie(&username, &mut response);
|
set_cookie(&username, &mut response);
|
||||||
request.extensions.insert::<SessionKey>(Session { username: username.clone() });
|
request
|
||||||
|
.extensions
|
||||||
|
.insert::<SessionKey>(Session { username: username.clone() });
|
||||||
Ok(response)
|
Ok(response)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::from(ErrorKind::IncorrectCredentials).into())
|
Err(Error::from(ErrorKind::IncorrectCredentials).into())
|
||||||
|
|
|
@ -84,7 +84,7 @@ fn daemonize() -> Result<()> {
|
||||||
log_file.push("polaris.log");
|
log_file.push("polaris.log");
|
||||||
match daemonize_redirect(Some(&log_file), Some(&log_file), ChdirMode::NoChdir) {
|
match daemonize_redirect(Some(&log_file), Some(&log_file), ChdirMode::NoChdir) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => bail!(ErrorKind::DaemonError)
|
Err(_) => bail!(ErrorKind::DaemonError),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue