use serde::{Deserialize, Serialize}; use time::format_description::well_known::Rfc3339; use utoipa::ToSchema; use uuid::Uuid; #[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema)] #[serde(rename_all = "lowercase")] pub enum AccessKeyScope { Read, Write, Repo, Admin, All, } impl From for AccessKeyScope { fn from(s: db::user::user_access_keys::AccessKeyScope) -> Self { use db::user::user_access_keys::AccessKeyScope as Db; match s { Db::Read => AccessKeyScope::Read, Db::Write => AccessKeyScope::Write, Db::Repo => AccessKeyScope::Repo, Db::Admin => AccessKeyScope::Admin, Db::All => AccessKeyScope::All, } } } impl From for db::user::user_access_keys::AccessKeyScope { fn from(s: AccessKeyScope) -> Self { use db::user::user_access_keys::AccessKeyScope as Db; match s { AccessKeyScope::Read => Db::Read, AccessKeyScope::Write => Db::Write, AccessKeyScope::Repo => Db::Repo, AccessKeyScope::Admin => Db::Admin, AccessKeyScope::All => Db::All, } } } #[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema)] #[serde(rename_all = "snake_case")] pub enum SshKeyType { SshRsa, SshEd25519, EcdsaSha2Nistp256, } impl From for SshKeyType { fn from(t: db::user::user_ssh_keys::SshKeyType) -> Self { use db::user::user_ssh_keys::SshKeyType as Db; match t { Db::SshRsa => SshKeyType::SshRsa, Db::SshEd25519 => SshKeyType::SshEd25519, Db::EcdsaSha2Nistp256 => SshKeyType::EcdsaSha2Nistp256, } } } impl From for db::user::user_ssh_keys::SshKeyType { fn from(t: SshKeyType) -> Self { use db::user::user_ssh_keys::SshKeyType as Db; match t { SshKeyType::SshRsa => Db::SshRsa, SshKeyType::SshEd25519 => Db::SshEd25519, SshKeyType::EcdsaSha2Nistp256 => Db::EcdsaSha2Nistp256, } } } #[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema)] #[serde(rename_all = "snake_case")] pub enum PushPlatform { WebPush, AndroidFcm, IosApns, } impl From for PushPlatform { fn from(p: db::user::user_push_subscriptions::PushSubscriptionPlatform) -> Self { use db::user::user_push_subscriptions::PushSubscriptionPlatform as Db; match p { Db::WebPush => PushPlatform::WebPush, Db::AndroidFcm => PushPlatform::AndroidFcm, Db::IosApns => PushPlatform::IosApns, } } } impl From for db::user::user_push_subscriptions::PushSubscriptionPlatform { fn from(p: PushPlatform) -> Self { use db::user::user_push_subscriptions::PushSubscriptionPlatform as Db; match p { PushPlatform::WebPush => Db::WebPush, PushPlatform::AndroidFcm => Db::AndroidFcm, PushPlatform::IosApns => Db::IosApns, } } } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct RecoveryCodeView { pub id: Uuid, pub used_at: Option, pub expires_at: Option, pub created_at: String, } impl From for RecoveryCodeView { fn from(m: db::user::user_recovery_codes::Model) -> Self { Self { id: m.id, used_at: m.used_at.map(|t| t.format(&Rfc3339).unwrap_or_default()), expires_at: m.expires_at.map(|t| t.format(&Rfc3339).unwrap_or_default()), created_at: m.created_at.format(&Rfc3339).unwrap_or_default(), } } } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct RegenerateRecoveryResp { pub codes: Vec, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct AccessKeyView { pub id: Uuid, pub name: String, pub token_last_eight: String, pub scope: AccessKeyScope, pub created_at: String, pub updated_at: String, } impl From for AccessKeyView { fn from(m: db::user::user_access_keys::Model) -> Self { Self { id: m.id, name: m.name, token_last_eight: m.token_last_eight, scope: m.scope.into(), created_at: m.created_at.format(&Rfc3339).unwrap_or_default(), updated_at: m.updated_at.format(&Rfc3339).unwrap_or_default(), } } } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct CreateAccessKeyCmd { pub name: String, pub scope: AccessKeyScope, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct CreateAccessKeyResp { pub key: AccessKeyView, pub token: String, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct SshKeyView { pub id: Uuid, pub name: String, pub key_type: SshKeyType, pub fingerprint: String, pub last_used_at: Option, pub created_at: String, } impl From for SshKeyView { fn from(m: db::user::user_ssh_keys::Model) -> Self { Self { id: m.id, name: m.name, key_type: m.key_type.into(), fingerprint: m.fingerprint, last_used_at: m .last_used_at .map(|t| t.format(&Rfc3339).unwrap_or_default()), created_at: m.created_at.format(&Rfc3339).unwrap_or_default(), } } } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct AddSshKeyCmd { pub name: String, pub content: String, pub key_type: Option, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct PushSubscriptionView { pub id: Uuid, pub platform: PushPlatform, pub endpoint: String, pub user_agent: Option, pub created_at: String, } impl From for PushSubscriptionView { fn from(m: db::user::user_push_subscriptions::Model) -> Self { Self { id: m.id, platform: m.platform.into(), endpoint: m.endpoint, user_agent: m.user_agent, created_at: m.created_at.format(&Rfc3339).unwrap_or_default(), } } } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct UpsertPushCmd { pub endpoint: String, pub p256dh_key: String, pub auth_key: String, pub platform: PushPlatform, pub user_agent: Option, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct TwoFactorStatusResp { pub enabled: bool, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct TwoFactorSetupResp { pub secret: String, pub otpauth_uri: String, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct EnableTwoFactorCmd { pub code: String, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct DisableTwoFactorCmd { pub code: String, }