|
|
@@ -83,17 +83,25 @@ fn validate_session(session: &Session) -> bool {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+fn redirect_to(path: &str) -> HttpResponse {
|
|
|
+ return HttpResponse::Ok()
|
|
|
+ .status(StatusCode::FOUND)
|
|
|
+ .append_header((header::LOCATION, path))
|
|
|
+ .finish();
|
|
|
+}
|
|
|
+
|
|
|
+async fn go_to(template_name: &str, session: Session) -> HttpResponse {
|
|
|
+ let body = get_template(template_name.to_string(), session).await;
|
|
|
+ HttpResponse::Ok().content_type("text/html")
|
|
|
+ .body(body)
|
|
|
+}
|
|
|
+
|
|
|
pub async fn index(session: Session) -> impl Responder {
|
|
|
if validate_session(&session) {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/home"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/home");
|
|
|
}
|
|
|
|
|
|
- let body = get_template("signin.html".to_string(), session).await;
|
|
|
- HttpResponse::Ok().content_type("text/html")
|
|
|
- .body(body)
|
|
|
+ return go_to("signin.html", session).await;
|
|
|
}
|
|
|
|
|
|
pub async fn auth(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormLoginData>, session: Session) -> impl Responder {
|
|
|
@@ -104,96 +112,65 @@ pub async fn auth(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormLogi
|
|
|
session.insert("user_id", user.uid).unwrap();
|
|
|
session.insert("user_is_ssh", user.is_ssh).unwrap();
|
|
|
session.insert("user_groups", user.groups).unwrap();
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/home"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/home");
|
|
|
},
|
|
|
Err(e) => {
|
|
|
session.insert("error_message", e.to_string()).unwrap();
|
|
|
- return HttpResponse::Ok().status(StatusCode::FOUND).append_header((header::LOCATION, "/")).finish();
|
|
|
+ return redirect_to("/");
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pub async fn home(session: Session) -> impl Responder {
|
|
|
if !validate_session(&session) {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/");
|
|
|
}
|
|
|
- let body = get_template("home.html".to_string(), session).await;
|
|
|
- HttpResponse::Ok().content_type("text/html")
|
|
|
- .body(body)
|
|
|
+ return go_to("home.html", session).await;
|
|
|
}
|
|
|
|
|
|
pub async fn form_password(session: Session) -> impl Responder {
|
|
|
if !validate_session(&session) {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/");
|
|
|
}
|
|
|
- let body = get_template("formpassword.html".to_string(), session).await;
|
|
|
- HttpResponse::Ok().content_type("text/html")
|
|
|
- .body(body)
|
|
|
+ return go_to("formpassword.html", session).await;
|
|
|
}
|
|
|
|
|
|
pub async fn change_password(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormChangePasswd>, session: Session) -> impl Responder {
|
|
|
if !validate_session(&session) {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/");
|
|
|
}
|
|
|
|
|
|
if form.new_password != form.new_password_conf {
|
|
|
session.insert("error_message", "Passwords do not match").unwrap();
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/changepassword"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/changepassword");
|
|
|
}
|
|
|
|
|
|
if form.new_password.len() < 12 {
|
|
|
session.insert("error_message", "Password too short (min 12 chars)").unwrap();
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/changepassword"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/changepassword");
|
|
|
}
|
|
|
|
|
|
let uid = session.get("user_id").unwrap().unwrap();
|
|
|
|
|
|
match ldap_wrapper.change_password(uid, form.current_password.clone(), form.new_password.clone()).await {
|
|
|
Ok(()) => {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/home"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/home");
|
|
|
},
|
|
|
Err(e) => {
|
|
|
session.insert("error_message", e.to_string()).unwrap();
|
|
|
- return HttpResponse::Ok().status(StatusCode::FOUND).append_header((header::LOCATION, "/changepassword")).finish();
|
|
|
+ return redirect_to("/changepassword");
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pub async fn form_ssh(ldap_wrapper: web::Data<LdapWrapper>, session: Session) -> impl Responder {
|
|
|
if !validate_session(&session) {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/");
|
|
|
}
|
|
|
|
|
|
let is_ssh: bool = session.get("user_is_ssh").unwrap_or(None).unwrap();
|
|
|
if !is_ssh {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ redirect_to("/home");
|
|
|
}
|
|
|
|
|
|
let uid = session.get("user_id").unwrap().unwrap();
|
|
|
@@ -206,25 +183,17 @@ pub async fn form_ssh(ldap_wrapper: web::Data<LdapWrapper>, session: Session) ->
|
|
|
session.insert("error_message", e.to_string()).unwrap();
|
|
|
},
|
|
|
}
|
|
|
- let body = get_template("sshhomepage.html".to_string(), session).await;
|
|
|
- HttpResponse::Ok().content_type("text/html")
|
|
|
- .body(body)
|
|
|
+ return go_to("sshhomepage.html", session).await;
|
|
|
}
|
|
|
|
|
|
pub async fn add_ssh_key(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormAddSSHKey>,session: Session) -> impl Responder {
|
|
|
if !validate_session(&session) {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/");
|
|
|
}
|
|
|
|
|
|
let is_ssh: bool = session.get("user_is_ssh").unwrap_or(None).unwrap();
|
|
|
if !is_ssh {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ redirect_to("/home");
|
|
|
}
|
|
|
|
|
|
let uid = session.get("user_id").unwrap().unwrap();
|
|
|
@@ -232,64 +201,40 @@ pub async fn add_ssh_key(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<F
|
|
|
//todo next time: regex check if a sshkey was provided
|
|
|
if form.new_ssh_key.is_empty() {
|
|
|
session.insert("error_message", "SSH key can't be empty".to_string()).unwrap();
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/ssh"))
|
|
|
- .finish()
|
|
|
+ return redirect_to("/ssh");
|
|
|
}
|
|
|
|
|
|
match ldap_wrapper.manage_ssh_key(uid, form.new_ssh_key.clone(), ldap::ManageSSHOps::ADD).await {
|
|
|
- Ok(_) => return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/ssh"))
|
|
|
- .finish(),
|
|
|
+ Ok(_) => return redirect_to("/ssh"),
|
|
|
Err(e) => {
|
|
|
session.insert("error_message", e.to_string()).unwrap();
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/ssh"))
|
|
|
- .finish()
|
|
|
+ redirect_to("/ssh")
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pub async fn del_ssh_key(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormDelSSHKey>,session: Session) -> impl Responder {
|
|
|
if !validate_session(&session) {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ return redirect_to("/");
|
|
|
}
|
|
|
|
|
|
let is_ssh: bool = session.get("user_is_ssh").unwrap_or(None).unwrap();
|
|
|
if !is_ssh {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ redirect_to("/home");
|
|
|
}
|
|
|
|
|
|
let uid = session.get("user_id").unwrap().unwrap();
|
|
|
|
|
|
match ldap_wrapper.manage_ssh_key(uid, form.key_to_delete.clone(), ldap::ManageSSHOps::DEL).await {
|
|
|
- Ok(_) => return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/ssh"))
|
|
|
- .finish(),
|
|
|
+ Ok(_) => return redirect_to("/ssh"),
|
|
|
Err(e) => {
|
|
|
session.insert("error_message", e.to_string()).unwrap();
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/ssh"))
|
|
|
- .finish()
|
|
|
+ return redirect_to("/ssh")
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pub async fn signout(session: Session) -> impl Responder {
|
|
|
session.purge();
|
|
|
- HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish()
|
|
|
+ return redirect_to("/");
|
|
|
}
|