summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2018-11-26 21:15:23 +0100
committerYves Fischer <yvesf-git@xapek.org>2018-11-26 21:15:23 +0100
commitcf40041946626b105102e3dab2515d2ef2fb0506 (patch)
tree54566468adc3c3dd9fdc30d9c0c19988f8ad8a5a
parent16055300c760c636399f555ce30c07deff2a6820 (diff)
downloadnginx-auth-totp-cf40041946626b105102e3dab2515d2ef2fb0506.tar.gz
nginx-auth-totp-cf40041946626b105102e3dab2515d2ef2fb0506.zip
Implement logout
-rw-r--r--src/request_handler/mod.rs16
-rw-r--r--src/request_handler/views.rs15
2 files changed, 27 insertions, 4 deletions
diff --git a/src/request_handler/mod.rs b/src/request_handler/mod.rs
index 72e9142..6812bb4 100644
--- a/src/request_handler/mod.rs
+++ b/src/request_handler/mod.rs
@@ -13,6 +13,7 @@ use std::cell::RefCell;
use time;
use http::{Request, Response, StatusCode, Method};
use http::response::Builder;
+use http::header::SET_COOKIE;
use tokio::prelude::*;
use horrorshow;
use cookie::{Cookie, CookieBuilder};
@@ -141,18 +142,24 @@ fn login<'a>(state: &super::ApplicationState, req: &Request<Bytes>, path_rest: &
}
}
-// unimplemented
fn logout<'a>(state: &super::ApplicationState, req: &Request<Bytes>, path_rest: &'a str,
) -> Response<String> {
let header_infos = match parse_header_infos(req) {
Ok(infos) => infos,
Err(message) => return error_handler_internal(message),
};
+
+ let cookie_delete = CookieBuilder::new(COOKIE_NAME, "")
+ .http_only(true)
+ .path("/")
+ .expires(time::at_utc(time::Timespec::new(0, 0)))
+ .finish();
+
Response::builder().set_defaults()
- .body(format!("Rest: {}", path_rest)).unwrap()
+ .header(SET_COOKIE, cookie_delete.to_string())
+ .body(views::logout()).unwrap()
}
-
fn check<'a>(state: &super::ApplicationState, req: &Request<Bytes>, path_rest: &'a str) -> Response<String> {
let header_infos = match parse_header_infos(req) {
Ok(infos) => infos,
@@ -180,6 +187,9 @@ fn parse_header_infos(req: &Request<Bytes>) -> Result<HeaderExtract, String> {
for header_value in req.headers().get_all(::http::header::COOKIE) {
let value = header_value.to_str().or(Err("Failed to read cookie value"))?;
for cookie_part in value.split("; ") {
+ if cookie_part.is_empty() {
+ continue;
+ }
let cookie = Cookie::parse(cookie_part).or(Err("Failed to parse cookie value"))?;
cookies.push(cookie);
}
diff --git a/src/request_handler/views.rs b/src/request_handler/views.rs
index 1a239a4..bdd7999 100644
--- a/src/request_handler/views.rs
+++ b/src/request_handler/views.rs
@@ -61,6 +61,9 @@ pub(in super) fn login_is_logged_in() -> String {
h1(id = "heading") {
: "Currently logged in"
}
+ a(href="logout") {
+ : "Go to logout";
+ }
})
}
@@ -102,7 +105,6 @@ pub(in super) fn login_auth_success(redirect: &String) -> String {
})
}
-
pub(in super) fn login_auth_fail() -> String {
render_base_template("Login failed", box_html! {
h1(id = "heading") {
@@ -112,4 +114,15 @@ pub(in super) fn login_auth_fail() -> String {
: "Try again... "
}
})
+}
+
+pub(in super) fn logout() -> String {
+ render_base_template("Logout", box_html! {
+ h1(id = "heading") {
+ : "Logout applied"
+ }
+ a(href="login") {
+ : "go to login again..."
+ }
+ })
} \ No newline at end of file