Explorar el Código

Simplified cli.rs.

Kestrel hace 1 año
padre
commit
06c4865053
Se han modificado 7 ficheros con 115 adiciones y 107 borrados
  1. 18 103
      src/cli.rs
  2. 3 1
      src/client_management.rs
  3. 5 1
      src/error.rs
  4. 64 0
      src/group_management.rs
  5. 0 1
      src/login.rs
  6. 2 1
      src/main.rs
  7. 23 0
      src/token_management.rs

+ 18 - 103
src/cli.rs

@@ -1,5 +1,5 @@
 use crate::{
-    key, client_management, config, schema::{self, RealmID}, server, token, user_management, UIDCError,
+    key, client_management, config, schema::{self, RealmID}, server, token, user_management, UIDCError, group_management, token_management,
 };
 use clap::{Parser, Subcommand};
 use microrm::prelude::*;
@@ -113,13 +113,12 @@ impl KeyArgs {
     async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
         match &self.command {
             KeyCommand::Inspect => {
-                key::inspect(&args.db, args.realm_id);
+                key::inspect(&args.db, args.realm_id)
             }
             KeyCommand::Generate => {
-                key::generate(&args.db, args.realm_id);
+                key::generate(&args.db, args.realm_id)
             }
         }
-        Ok(())
     }
 }
 
@@ -140,14 +139,13 @@ impl ClientArgs {
     async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
         match &self.command {
             ClientCommand::Create { name } => {
-                client_management::create(&args.db, args.realm_id, name);
+                client_management::create(&args.db, args.realm_id, name)
             }
-            ClientCommand::List => {}
+            ClientCommand::List => { todo!() }
             ClientCommand::Inspect { name } => {
-                client_management::inspect(&args.db, args.realm_id, name);
+                client_management::inspect(&args.db, args.realm_id, name)
             }
         }
-        Ok(())
     }
 }
 
@@ -206,79 +204,22 @@ impl GroupArgs {
         let qi = args.db.query_interface();
         match &self.command {
             GroupCommand::Create { group_name } => {
-                match qi.add(&schema::Group {
-                    realm: args.realm_id,
-                    shortname: group_name.clone(),
-                }) {
-                    Ok(_) => {
-                        println!("Added group {}", group_name);
-                    },
-                    Err(e) => {
-                        println!("Failed to add group: {:?}", e);
-                    },
-                }
+                group_management::create_group(&qi, args.realm_id, group_name)?;
             },
             GroupCommand::Members { group_name } => {
-                todo!()
+                group_management::list_members(&qi, args.realm_id, group_name.as_str())?;
             },
             GroupCommand::AttachRole { group_name, role_name } => {
-                let group = qi.get().by(schema::Group::Realm, &args.realm_id).by(schema::Group::Shortname, group_name).one().unwrap();
-                let role = qi.get().by(schema::Role::Realm, &args.realm_id).by(schema::Role::Shortname, role_name).one().unwrap();
-
-                match (group, role) {
-                    (None, _) => {
-                        println!("No such group!");
-                    },
-                    (_, None) => {
-                        println!("No such role!");
-                    },
-                    (Some(group), Some(role)) => {
-                        match qi.add(&schema::GroupRole {
-                            group: group.id(),
-                            role: role.id(),
-                        }) {
-                            Ok(_) => {
-                                println!("Attached role {} to {}", role_name, group_name);
-                            },
-                            Err(e) => {
-                                println!("Failed to attach role: {:?}", e);
-                            }
-                        }
-                    },
-                }
-
+                group_management::attach_role(&qi, args.realm_id, group_name.as_str(), role_name.as_str())?;
             },
             GroupCommand::DetachRole { group_name, role_name } => {
-                
+                group_management::detach_role(&qi, args.realm_id, group_name.as_str(), role_name.as_str())?;
             },
             GroupCommand::AttachUser { group_name, username } => {
-                let group = qi.get().by(schema::Group::Realm, &args.realm_id).by(schema::Group::Shortname, group_name).one().unwrap();
-                let user = qi.get().by(schema::User::Realm, &args.realm_id).by(schema::User::Username, username).one().unwrap();
-
-                match (group, user) {
-                    (None, _) => {
-                        println!("No such group!");
-                    },
-                    (_, None) => {
-                        println!("No such user!");
-                    },
-                    (Some(group), Some(user)) => {
-                        match qi.add(&schema::GroupMembership {
-                            group: group.id(),
-                            user: user.id(),
-                        }) {
-                            Ok(_) => {
-                                println!("Attached user {} to {}", username, group_name);
-                            },
-                            Err(e) => {
-                                println!("Failed to attach user: {:?}", e);
-                            }
-                        }
-                    },
-                }
+                group_management::attach_user(&qi, args.realm_id, group_name.as_str(), username.as_str())?;
             },
             GroupCommand::DetachUser { group_name, username } => {
-                
+                group_management::detach_user(&qi, args.realm_id, group_name.as_str(), username.as_str())?;
             }
         }
         Ok(())
@@ -330,50 +271,24 @@ struct TokenArgs {
 impl TokenArgs {
     async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
         let config = config::Config::build_from(&args.db.query_interface(), None);
+        let qi = args.db.query_interface();
         match &self.command {
             TokenCommand::GenerateAuth {
                 client,
                 username,
                 scopes,
             } => {
-                let qi = args.db.query_interface();
-                let token = token::generate_auth_token(
-                    &config,
-                    &qi,
-                    args.realm_id,
-                    qi.get()
-                        .by(schema::Client::Realm, &args.realm_id)
-                        .by(schema::Client::Shortname, client.as_str())
-                        .one()
-                        .unwrap()
-                        .expect("no such client")
-                        .id(),
-                    qi.get()
-                        .by(schema::User::Realm, &args.realm_id)
-                        .by(schema::User::Username, username.as_str())
-                        .one()
-                        .unwrap()
-                        .expect("no such user")
-                        .id(),
-                    scopes.split_whitespace(),
-                );
-                match token {
-                    Ok(t) => {
-                        println!("token: {t}");
-                    }
-                    Err(e) => {
-                        println!("could not generate token: {:?}", e);
-                    }
-                }
+                let token = token_management::create_auth_token(&qi, &config, args.realm_id, client.as_str(), username.as_str(), scopes.as_str())?;
+                println!("{}", token);
+                Ok(())
             }
             TokenCommand::GenerateRefresh {
                 client,
                 username,
                 scopes,
-            } => {}
-            TokenCommand::Inspect { token } => {}
+            } => { todo!() }
+            TokenCommand::Inspect { token } => { todo!() }
         }
-        Ok(())
     }
 }
 

+ 3 - 1
src/client_management.rs

@@ -12,4 +12,6 @@ pub fn create(db: &microrm::DB, realm_id: schema::RealmID, name: &str) -> Result
     Ok(())
 }
 
-pub fn inspect(db: &microrm::DB, realm_id: schema::RealmID, name: &str) {}
+pub fn inspect(db: &microrm::DB, realm_id: schema::RealmID, name: &str) -> Result<(), UIDCError> {
+    todo!()
+}

+ 5 - 1
src/error.rs

@@ -1,4 +1,4 @@
-use crate::{key::KeyError, user::UserError};
+use crate::{key::KeyError, user::UserError, token::TokenError};
 
 #[derive(Debug)]
 pub enum UIDCError {
@@ -10,6 +10,9 @@ pub enum UIDCError {
     /// error with key generation or message signing
     KeyError(KeyError),
 
+    /// error with token generation or verification
+    TokenError(TokenError),
+
     /// error with user operation
     UserError(UserError),
 }
@@ -40,3 +43,4 @@ macro_rules! error_converter {
 
 error_converter!(KeyError);
 error_converter!(UserError);
+error_converter!(TokenError);

+ 64 - 0
src/group_management.rs

@@ -0,0 +1,64 @@
+use crate::{schema, UIDCError};
+use microrm::prelude::*;
+
+pub fn create_group(qi: &microrm::QueryInterface, realm_id: schema::RealmID, name: &str) -> Result<(), UIDCError> {
+    qi.add(&schema::Group {
+        realm: realm_id,
+        shortname: name.into(),
+    })?;
+    Ok(())
+}
+
+pub fn list_members(qi: &microrm::QueryInterface, realm_id: schema::RealmID, name: &str) -> Result<(), UIDCError> {
+    todo!()
+}
+
+pub fn attach_user(qi: &microrm::QueryInterface, realm_id: schema::RealmID, group_name: &str, username: &str) -> Result<(), UIDCError> {
+    let group = qi.get().by(schema::Group::Realm, &realm_id).by(schema::Group::Shortname, group_name).one()?;
+    let user = qi.get().by(schema::User::Realm, &realm_id).by(schema::User::Username, username).one()?;
+
+    match (group, user) {
+        (None, _) => {
+            Err(UIDCError::Abort("no such group"))
+        },
+        (_, None) => {
+            Err(UIDCError::Abort("no such user"))
+        },
+        (Some(group), Some(user)) => {
+            qi.add(&schema::GroupMembership {
+                group: group.id(),
+                user: user.id(),
+            })?;
+            Ok(())
+        },
+    }
+}
+
+pub fn detach_user(qi: &microrm::QueryInterface, realm_id: schema::RealmID, group_name: &str, username: &str) -> Result<(), UIDCError> {
+    todo!()
+}
+
+pub fn attach_role(qi: &microrm::QueryInterface, realm_id: schema::RealmID, group_name: &str, role_name: &str) -> Result<(), UIDCError> {
+    let group = qi.get().by(schema::Group::Realm, &realm_id).by(schema::Group::Shortname, group_name).one().unwrap();
+    let role = qi.get().by(schema::Role::Realm, &realm_id).by(schema::Role::Shortname, role_name).one().unwrap();
+
+    match (group, role) {
+        (None, _) => {
+            Err(UIDCError::Abort("no such group"))
+        },
+        (_, None) => {
+            Err(UIDCError::Abort("no such role"))
+        },
+        (Some(group), Some(role)) => {
+            qi.add(&schema::GroupRole {
+                group: group.id(),
+                role: role.id(),
+            })?;
+            Ok(())
+        },
+    }
+}
+
+pub fn detach_role(qi: &microrm::QueryInterface, realm_id: schema::RealmID, group_name: &str, role: &str) -> Result<(), UIDCError> {
+    todo!()
+}

+ 0 - 1
src/login.rs

@@ -1 +0,0 @@
-

+ 2 - 1
src/main.rs

@@ -2,13 +2,14 @@ mod cli;
 mod client_management;
 mod config;
 mod error;
+mod group_management;
 mod jwt;
 mod key;
-mod login;
 mod role_management;
 mod schema;
 mod server;
 mod token;
+mod token_management;
 mod user;
 mod user_management;
 

+ 23 - 0
src/token_management.rs

@@ -0,0 +1,23 @@
+use crate::{schema, UIDCError, config::Config, token};
+use microrm::prelude::*;
+
+pub fn create_auth_token(qi: &microrm::QueryInterface, config: &Config, realm_id: schema::RealmID, client: &str, username: &str, scopes: &str) -> Result<String, UIDCError> {
+    Ok(token::generate_auth_token(
+        &config,
+        &qi,
+        realm_id,
+        qi.get()
+            .by(schema::Client::Realm, &realm_id)
+            .by(schema::Client::Shortname, client)
+            .one()?
+            .expect("no such client")
+            .id(),
+        qi.get()
+            .by(schema::User::Realm, &realm_id)
+            .by(schema::User::Username, username)
+            .one()?
+            .expect("no such user")
+            .id(),
+        scopes.split_whitespace(),
+    )?)
+}