client_management.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use crate::{
  2. key::{HMacType, KeyType},
  3. schema, UIDCError,
  4. };
  5. use microrm::prelude::*;
  6. pub fn create(
  7. realm: &microrm::Stored<schema::Realm>,
  8. name: &String,
  9. key_type: KeyType,
  10. ) -> Result<(), UIDCError> {
  11. let rng = ring::rand::SystemRandom::new();
  12. let client_secret: [u8; 32] = ring::rand::generate(&rng).unwrap().expose();
  13. realm.clients.insert(schema::Client {
  14. realm: realm.id(),
  15. shortname: name.into(),
  16. secret: base64::encode(client_secret),
  17. access_key_type: key_type.into(),
  18. refresh_key_type: KeyType::HMac(HMacType::Sha256).into_serialized(),
  19. direct_grant_enabled: false,
  20. redirects: Default::default(),
  21. scopes: Default::default(),
  22. })?;
  23. Ok(())
  24. }
  25. pub fn rotate_secret(realm: &microrm::Stored<schema::Realm>, name: &str) -> Result<(), UIDCError> {
  26. let rng = ring::rand::SystemRandom::new();
  27. let client_secret: [u8; 32] = ring::rand::generate(&rng).unwrap().expose();
  28. let mut client = realm
  29. .clients
  30. .with(schema::Client::Shortname, name)
  31. .first()
  32. .get()?
  33. .ok_or(UIDCError::Abort("no such client"))?;
  34. client.secret = base64::encode(client_secret);
  35. client.sync()?;
  36. Ok(())
  37. }
  38. pub fn add_redirect(
  39. realm: &microrm::Stored<schema::Realm>,
  40. name: &str,
  41. pattern: &str,
  42. ) -> Result<(), UIDCError> {
  43. let Some(client) = realm.clients.keyed((realm.id(), name)).get()? else {
  44. return Err(UIDCError::Abort("no such client"));
  45. };
  46. client.redirects.insert(schema::ClientRedirect {
  47. redirect_pattern: pattern.into(),
  48. })?;
  49. Ok(())
  50. }