v1.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. pub use microrm::prelude::*;
  2. use serde::{Deserialize, Serialize};
  3. use strum::EnumString;
  4. use crate::key::KeyType;
  5. // ----------------------------------------------------------------------
  6. // Session types
  7. // ----------------------------------------------------------------------
  8. #[derive(Entity)]
  9. pub struct Session {
  10. #[key]
  11. pub session_id: String,
  12. pub auth: microrm::RelationMap<SessionAuth>,
  13. pub expiry: time::OffsetDateTime,
  14. }
  15. #[derive(Entity)]
  16. pub struct SessionAuth {
  17. pub realm: RealmID,
  18. pub user: Option<UserID>,
  19. pub pending_user: Option<UserID>,
  20. pub pending_challenges: microrm::Serialized<Vec<AuthChallengeType>>,
  21. }
  22. #[derive(Clone, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
  23. pub enum AuthChallengeType {
  24. Username,
  25. Password,
  26. Totp,
  27. Grid,
  28. WebAuthn,
  29. }
  30. #[derive(Entity)]
  31. pub struct AuthChallenge {
  32. #[key]
  33. pub user_id: UserID,
  34. #[key]
  35. pub challenge_type: microrm::Serialized<AuthChallengeType>,
  36. #[elide]
  37. pub public: Vec<u8>,
  38. #[elide]
  39. pub secret: Vec<u8>,
  40. pub enabled: bool,
  41. }
  42. #[derive(Entity)]
  43. pub struct SingleUseAuth {
  44. #[key]
  45. pub code: String,
  46. pub user: UserID,
  47. pub expiry: time::OffsetDateTime,
  48. }
  49. // ----------------------------------------------------------------------
  50. // OIDC types
  51. // ----------------------------------------------------------------------
  52. pub struct UserGroupRelation;
  53. impl microrm::Relation for UserGroupRelation {
  54. type Domain = User;
  55. type Range = Group;
  56. const NAME: &'static str = "UserGroup";
  57. }
  58. pub struct GroupRoleRelation;
  59. impl microrm::Relation for GroupRoleRelation {
  60. type Domain = Group;
  61. type Range = Role;
  62. const NAME: &'static str = "GroupRole";
  63. }
  64. #[derive(serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Eq, Debug)]
  65. pub enum KeyState {
  66. /// Key can be used without restrictions for signing and verification.
  67. Active,
  68. /// Key will be used for signing only if no other key of the same type is found, but is still
  69. /// good for verification of existing tokens.
  70. Retiring,
  71. /// Key is now fully retired and will not be used for signing or for verification.
  72. Retired,
  73. }
  74. #[derive(Entity)]
  75. pub struct Key {
  76. #[key]
  77. pub key_id: String,
  78. pub key_type: microrm::Serialized<KeyType>,
  79. pub key_state: microrm::Serialized<KeyState>,
  80. pub public_data: Vec<u8>,
  81. #[elide]
  82. pub secret_data: Vec<u8>,
  83. pub expiry: time::OffsetDateTime,
  84. }
  85. #[derive(Entity)]
  86. pub struct User {
  87. #[key]
  88. pub realm: RealmID,
  89. #[key]
  90. pub username: String,
  91. pub pending_external_auths: microrm::Serialized<Vec<ExternalAuthProvider>>,
  92. pub auth: microrm::RelationMap<AuthChallenge>,
  93. pub groups: microrm::RelationDomain<UserGroupRelation>,
  94. }
  95. #[derive(Entity)]
  96. pub struct Group {
  97. #[key]
  98. pub realm: RealmID,
  99. #[key]
  100. pub shortname: String,
  101. pub users: microrm::RelationRange<UserGroupRelation>,
  102. pub roles: microrm::RelationDomain<GroupRoleRelation>,
  103. }
  104. #[derive(Entity)]
  105. pub struct Role {
  106. #[key]
  107. pub realm: RealmID,
  108. /// key publicly-visible name for role
  109. #[key]
  110. pub shortname: String,
  111. pub groups: microrm::RelationRange<GroupRoleRelation>,
  112. }
  113. /// OAuth2 client representation
  114. #[derive(Entity)]
  115. pub struct Client {
  116. #[key]
  117. pub realm: RealmID,
  118. #[key]
  119. pub shortname: String,
  120. pub secret: String,
  121. pub access_key_type: microrm::Serialized<KeyType>,
  122. pub refresh_key_type: microrm::Serialized<KeyType>,
  123. pub direct_grant_enabled: bool,
  124. pub redirects: microrm::RelationMap<ClientRedirect>,
  125. pub scopes: microrm::RelationMap<Scope>,
  126. }
  127. #[derive(Entity)]
  128. pub struct ClientRedirect {
  129. pub redirect_pattern: String,
  130. }
  131. #[derive(Entity)]
  132. pub struct AuthCode {
  133. #[key]
  134. pub realm: RealmID,
  135. #[key]
  136. pub client: ClientID,
  137. #[key]
  138. pub code: String,
  139. pub expiry: time::OffsetDateTime,
  140. pub user: UserID,
  141. pub scopes: microrm::Serialized<Vec<String>>,
  142. pub redirect_uri: String,
  143. }
  144. /// Requested group of permissions
  145. #[derive(Entity)]
  146. pub struct Scope {
  147. #[key]
  148. pub realm: RealmID,
  149. #[key]
  150. pub shortname: String,
  151. pub roles: microrm::RelationMap<Role>,
  152. }
  153. // ----------------------------------------------------------------------
  154. // External (social) authentication
  155. // ----------------------------------------------------------------------
  156. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize, EnumString)]
  157. #[strum(serialize_all = "snake_case")]
  158. #[serde(rename_all = "snake_case")]
  159. pub enum ExternalAuthProvider {
  160. Github,
  161. GenericOIDC,
  162. }
  163. #[derive(Clone, Entity)]
  164. pub struct ExternalAuthMap {
  165. #[key]
  166. pub external_user_id: String,
  167. #[key]
  168. pub provider: microrm::Serialized<ExternalAuthProvider>,
  169. pub internal_user_id: UserID,
  170. }
  171. // ----------------------------------------------------------------------
  172. // Global container types
  173. // ----------------------------------------------------------------------
  174. #[derive(Clone, Default, Entity)]
  175. pub struct Realm {
  176. #[key]
  177. pub shortname: String,
  178. pub clients: microrm::RelationMap<Client>,
  179. pub groups: microrm::RelationMap<Group>,
  180. pub keys: microrm::RelationMap<Key>,
  181. pub roles: microrm::RelationMap<Role>,
  182. pub scopes: microrm::RelationMap<Scope>,
  183. pub users: microrm::RelationMap<User>,
  184. pub auth_codes: microrm::RelationMap<AuthCode>,
  185. pub external_auth: microrm::RelationMap<ExternalAuthMap>,
  186. pub single_use_auth: microrm::RelationMap<SingleUseAuth>,
  187. }