forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.7 MiB·37e985d·Tahinli·2 hours ago·363 commits
| 1 | pub type Id = String; |
| 2 | |
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] |
| 4 | #[serde(rename_all = "lowercase")] |
| 5 | pub enum Visibility { |
| 6 | Public, |
| 7 | Private, |
| 8 | } |
| 9 | |
| 10 | #[derive( |
| 11 | Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize, |
| 12 | )] |
| 13 | #[serde(rename_all = "lowercase")] |
| 14 | pub enum Role { |
| 15 | Read, |
| 16 | Triage, |
| 17 | Write, |
| 18 | Maintain, |
| 19 | Admin, |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] |
| 23 | #[serde(rename_all = "lowercase")] |
| 24 | pub enum AccountKind { |
| 25 | User, |
| 26 | Org, |
| 27 | } |
| 28 | |
| 29 | #[derive(Clone, PartialEq)] |
| 30 | pub struct User { |
| 31 | pub id: Id, |
| 32 | pub username: String, |
| 33 | pub email: Option<String>, |
| 34 | pub password_hash: String, |
| 35 | pub is_admin: bool, |
| 36 | pub created_at: String, |
| 37 | pub about: String, |
| 38 | pub display_name: Option<String>, |
| 39 | pub timezone: String, |
| 40 | pub avatar_hash: Option<String>, |
| 41 | pub theme: String, |
| 42 | pub language: String, |
| 43 | pub accent: String, |
| 44 | pub accent_dark: String, |
| 45 | pub base_light: String, |
| 46 | pub base_dark: String, |
| 47 | pub sidebar_side: String, |
| 48 | pub kind: AccountKind, |
| 49 | pub email_notifications: bool, |
| 50 | } |
| 51 | |
| 52 | impl User { |
| 53 | pub fn is_org(&self) -> bool { |
| 54 | self.kind == AccountKind::Org |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #[derive(Debug, Clone, PartialEq)] |
| 59 | pub struct Repo { |
| 60 | pub id: Id, |
| 61 | pub owner_id: Id, |
| 62 | pub owner_name: String, |
| 63 | pub name: String, |
| 64 | pub description: String, |
| 65 | pub visibility: Visibility, |
| 66 | pub default_branch: String, |
| 67 | pub archived: bool, |
| 68 | pub created_at: String, |
| 69 | pub fork_of: Option<Id>, |
| 70 | } |
| 71 | |
| 72 | #[derive(Debug, Clone, PartialEq)] |
| 73 | pub struct SshKey { |
| 74 | pub id: Id, |
| 75 | pub user_id: Id, |
| 76 | pub title: String, |
| 77 | pub algorithm: String, |
| 78 | pub key_blob: Vec<u8>, |
| 79 | pub fingerprint: String, |
| 80 | pub created_at: String, |
| 81 | pub last_used_at: Option<String>, |
| 82 | } |
| 83 | |
| 84 | #[derive(Debug, Clone, PartialEq)] |
| 85 | pub struct AccessToken { |
| 86 | pub id: Id, |
| 87 | pub user_id: Id, |
| 88 | pub name: String, |
| 89 | pub scopes: Vec<String>, |
| 90 | pub expires_at: Option<String>, |
| 91 | pub created_at: String, |
| 92 | pub last_used_at: Option<String>, |
| 93 | } |
| 94 | |
| 95 | #[derive(Clone, PartialEq)] |
| 96 | pub struct Session { |
| 97 | pub id: Id, |
| 98 | pub user_id: Id, |
| 99 | pub csrf_token: String, |
| 100 | pub created_at: String, |
| 101 | pub expires_at: String, |
| 102 | pub last_seen: String, |
| 103 | pub user_agent: Option<String>, |
| 104 | pub ip: Option<String>, |
| 105 | pub last_ip: Option<String>, |
| 106 | } |
| 107 | |
| 108 | const REDACTED: &str = "<redacted>"; |
| 109 | |
| 110 | impl std::fmt::Debug for User { |
| 111 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 112 | f.debug_struct("User") |
| 113 | .field("id", &self.id) |
| 114 | .field("username", &self.username) |
| 115 | .field("email", &self.email) |
| 116 | .field("password_hash", &REDACTED) |
| 117 | .field("is_admin", &self.is_admin) |
| 118 | .field("created_at", &self.created_at) |
| 119 | .field("about", &self.about) |
| 120 | .field("timezone", &self.timezone) |
| 121 | .field("avatar_hash", &self.avatar_hash) |
| 122 | .field("theme", &self.theme) |
| 123 | .field("language", &self.language) |
| 124 | .field("accent", &self.accent) |
| 125 | .field("accent_dark", &self.accent_dark) |
| 126 | .field("base_light", &self.base_light) |
| 127 | .field("base_dark", &self.base_dark) |
| 128 | .field("sidebar_side", &self.sidebar_side) |
| 129 | .field("kind", &self.kind) |
| 130 | .finish() |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl std::fmt::Debug for Session { |
| 135 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 136 | f.debug_struct("Session") |
| 137 | .field("id", &self.id) |
| 138 | .field("user_id", &self.user_id) |
| 139 | .field("csrf_token", &REDACTED) |
| 140 | .field("created_at", &self.created_at) |
| 141 | .field("expires_at", &self.expires_at) |
| 142 | .field("last_seen", &self.last_seen) |
| 143 | .field("user_agent", &self.user_agent) |
| 144 | .field("ip", &self.ip) |
| 145 | .field("last_ip", &self.last_ip) |
| 146 | .finish() |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | string_enum!(Visibility { |
| 151 | Public = "public", |
| 152 | Private = "private", |
| 153 | }); |
| 154 | |
| 155 | string_enum!(Role { |
| 156 | Read = "read", |
| 157 | Triage = "triage", |
| 158 | Write = "write", |
| 159 | Maintain = "maintain", |
| 160 | Admin = "admin", |
| 161 | }); |
| 162 | |
| 163 | #[cfg(test)] |
| 164 | mod tests { |
| 165 | use super::*; |
| 166 | |
| 167 | #[test] |
| 168 | fn debug_output_redacts_secrets() { |
| 169 | let user = User { |
| 170 | id: "u1".into(), |
| 171 | username: "tahinli".into(), |
| 172 | email: None, |
| 173 | password_hash: "$argon2id$SECRETHASH".into(), |
| 174 | is_admin: true, |
| 175 | created_at: "now".into(), |
| 176 | about: String::new(), |
| 177 | display_name: None, |
| 178 | timezone: "UTC".into(), |
| 179 | avatar_hash: None, |
| 180 | theme: "auto".into(), |
| 181 | language: "auto".into(), |
| 182 | accent: crate::store::ui_prefs::DEFAULT_ACCENT.into(), |
| 183 | accent_dark: crate::store::ui_prefs::DEFAULT_ACCENT.into(), |
| 184 | base_light: crate::store::ui_prefs::DEFAULT_BASE.into(), |
| 185 | base_dark: crate::store::ui_prefs::DEFAULT_BASE.into(), |
| 186 | sidebar_side: crate::store::ui_prefs::DEFAULT_SIDEBAR.into(), |
| 187 | kind: AccountKind::User, |
| 188 | email_notifications: true, |
| 189 | }; |
| 190 | let session = Session { |
| 191 | id: "s1".into(), |
| 192 | user_id: "u1".into(), |
| 193 | csrf_token: "CSRFSECRET".into(), |
| 194 | created_at: "now".into(), |
| 195 | expires_at: "later".into(), |
| 196 | last_seen: "now".into(), |
| 197 | user_agent: None, |
| 198 | ip: None, |
| 199 | last_ip: None, |
| 200 | }; |
| 201 | let dbg = format!("{user:?} {session:?}"); |
| 202 | assert!(dbg.contains("\"<redacted>\"")); |
| 203 | assert!(!dbg.contains("SECRETHASH")); |
| 204 | assert!(!dbg.contains("CSRFSECRET")); |
| 205 | assert!(dbg.contains("tahinli")); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn visibility_and_role_round_trip_and_keep_stored_literals() { |
| 210 | let visibility = [ |
| 211 | (Visibility::Public, "public"), |
| 212 | (Visibility::Private, "private"), |
| 213 | ]; |
| 214 | for (v, literal) in visibility { |
| 215 | assert_eq!(v.as_str(), literal, "case visibility {literal}"); |
| 216 | assert_eq!( |
| 217 | literal.parse::<Visibility>().ok(), |
| 218 | Some(v), |
| 219 | "case visibility {literal}" |
| 220 | ); |
| 221 | } |
| 222 | assert!( |
| 223 | "Public".parse::<Visibility>().is_err(), |
| 224 | "case visibility casing" |
| 225 | ); |
| 226 | |
| 227 | let roles = [ |
| 228 | (Role::Read, "read"), |
| 229 | (Role::Triage, "triage"), |
| 230 | (Role::Write, "write"), |
| 231 | (Role::Maintain, "maintain"), |
| 232 | (Role::Admin, "admin"), |
| 233 | ]; |
| 234 | for (r, literal) in roles { |
| 235 | assert_eq!(r.as_str(), literal, "case role {literal}"); |
| 236 | assert_eq!(literal.parse::<Role>().ok(), Some(r), "case role {literal}"); |
| 237 | } |
| 238 | assert!("owner".parse::<Role>().is_err(), "case role owner"); |
| 239 | } |
| 240 | } |