ソースを参照

Added: adding ssh keys

clement 1 年間 前
コミット
20a9ba8a3e
4 ファイル変更55 行追加8 行削除
  1. 36 4
      src/handler.rs
  2. 16 1
      src/ldap/lib.rs
  3. 1 1
      src/settings.toml.example
  4. 2 2
      templates/sshhomepage.html

+ 36 - 4
src/handler.rs

@@ -19,6 +19,11 @@ pub struct FormChangePasswd {
     new_password_conf: String,
 }
 
+#[derive(Deserialize)]
+pub struct FormSSHKey {
+    new_ssh_key: String,
+}
+
 async fn get_template(template_name: String, session: Session) -> String {
     let error_message: Option<String> = session.get("error_message")
     .unwrap_or(None);
@@ -201,10 +206,37 @@ pub async fn form_ssh(ldap_wrapper: web::Data<LdapWrapper>, session: Session) ->
     .body(body)
 }
 
-pub async fn add_ssh_key(session: Session) -> impl Responder {
-    let body = get_template("sshhomepage.html".to_string(), session).await;
-    HttpResponse::Ok().content_type("text/html")
-    .body(body)
+pub async fn add_ssh_key(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormSSHKey>,session: Session) -> impl Responder {
+    if !validate_session(&session) {
+        return HttpResponse::Ok()
+        .status(StatusCode::FOUND)
+        .append_header((header::LOCATION, "/"))
+        .finish();
+    }
+
+    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();
+    }
+
+    let uid = session.get("user_id").unwrap().unwrap();
+
+    match ldap_wrapper.add_ssh_key(uid, form.new_ssh_key.clone()).await {
+        Ok(_) => return HttpResponse::Ok()
+                .status(StatusCode::FOUND)
+                .append_header((header::LOCATION, "/ssh"))
+                .finish(),
+        Err(e) => {
+            session.insert("error_message", e.to_string()).unwrap();
+            return HttpResponse::Ok()
+            .status(StatusCode::FOUND)
+            .append_header((header::LOCATION, "/ssh"))
+            .finish()
+        },
+    }
 }
 
 pub async fn del_ssh_key(session: Session) -> impl Responder {

+ 16 - 1
src/ldap/lib.rs

@@ -2,7 +2,7 @@ use core::fmt;
 use std::{collections::{HashMap, HashSet}, vec};
 use base64::{engine::general_purpose::STANDARD, Engine as _};
 use deadpool::managed::Pool;
-use ldap3::SearchEntry;
+use ldap3::{Mod, SearchEntry};
 use regex::{Captures, Regex};
 use ring::rand::{self, SecureRandom};
 use serde::Serialize;
@@ -193,4 +193,19 @@ impl LdapWrapper {
 
         Ok(ssh_keys)
     }
+
+    pub async fn add_ssh_key(&self, username: String, ssh_key: String) -> Result<(), Error> {
+        let mut ldap = self.ldap_pool.get().await.unwrap();
+        let add = ldap
+        .modify(format!("uid={},{}", username, self.config.basedn).as_str(), vec![Mod::Add("sshPublicKey", HashSet::from([ssh_key.as_str()]))]).await;
+
+        if let Err(e) = add {
+            return Err(Error::LdapServerError { message: format!("An error occured, contact admins: {}", e)});
+        }
+
+        match add.unwrap().success() {
+            Ok(_) => Ok(()),
+            Err(e) => Err(Error::LdapServerError { message: format!("An error occured, contact admins: {}", e)}),
+        }
+    }
 }

+ 1 - 1
src/settings.toml.example

@@ -4,4 +4,4 @@ basedn = "ou=people,dc=example,dc=com"
 groupsdn = "ou=Groups,dc=example,dc=com"
 binddn = ""
 bindpw = ""
-starttls = false    
+starttls = false

+ 2 - 2
templates/sshhomepage.html

@@ -35,9 +35,9 @@
                 <button class="button is-primary">Add</button>
             </form>
         </div>
-        {% if session.message is defined %}
+        {% if error_message %}
             <div class="container">
-                <div class="box notification is-warning">{{ session.message }}</div>
+                <div class="box notification is-warning">{{ error_message }}</div>
             </div>
         {% endif %}
     </body>