use derive_empty_traits::EmptyTraits;
use resolver_api::{HasResponse, Resolve};
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
use webauthn_rs_proto::{
PublicKeyCredential, RequestChallengeResponse,
};
use crate::entities::user::User;
pub trait KomodoAuthRequest: HasResponse {}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct JwtResponse {
pub user_id: String,
pub jwt: String,
}
#[typeshare(serialized_as = "any")]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct _RequestChallengeResponse(pub RequestChallengeResponse);
#[cfg(feature = "openapi")]
impl utoipa::PartialSchema for _RequestChallengeResponse {
fn schema()
-> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
utoipa::schema!(#[inline] std::collections::HashMap<String, serde_json::Value>).into()
}
}
#[cfg(feature = "openapi")]
impl utoipa::ToSchema for _RequestChallengeResponse {}
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(tag = "type", content = "data")]
pub enum JwtOrTwoFactor {
Jwt(JwtResponse),
Passkey(_RequestChallengeResponse),
Totp {},
}
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(tag = "type", content = "data")]
pub enum UserIdOrTwoFactor {
UserId(String),
Passkey(_RequestChallengeResponse),
Totp {},
}
#[typeshare]
#[derive(
Serialize, Deserialize, Debug, Clone, Resolve, EmptyTraits,
)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[empty_traits(KomodoAuthRequest)]
#[response(GetLoginOptionsResponse)]
#[error(serror::Error)]
pub struct GetLoginOptions {}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct GetLoginOptionsResponse {
pub local: bool,
pub github: bool,
pub google: bool,
pub oidc: bool,
pub registration_disabled: bool,
}
#[typeshare]
#[derive(
Serialize, Deserialize, Debug, Clone, Resolve, EmptyTraits,
)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[empty_traits(KomodoAuthRequest)]
#[response(SignUpLocalUserResponse)]
#[error(serror::Error)]
pub struct SignUpLocalUser {
pub username: String,
pub password: String,
}
#[typeshare]
pub type SignUpLocalUserResponse = JwtResponse;
#[typeshare]
#[derive(
Serialize, Deserialize, Debug, Clone, Resolve, EmptyTraits,
)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[empty_traits(KomodoAuthRequest)]
#[response(LoginLocalUserResponse)]
#[error(serror::Error)]
pub struct LoginLocalUser {
pub username: String,
pub password: String,
}
#[typeshare]
pub type LoginLocalUserResponse = JwtOrTwoFactor;
#[typeshare]
#[derive(
Serialize, Deserialize, Debug, Clone, Resolve, EmptyTraits,
)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[empty_traits(KomodoAuthRequest)]
#[response(ExchangeForJwtResponse)]
#[error(serror::Error)]
pub struct ExchangeForJwt {}
#[typeshare]
pub type ExchangeForJwtResponse = JwtResponse;
#[typeshare]
#[derive(
Serialize, Deserialize, Debug, Clone, Resolve, EmptyTraits,
)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[empty_traits(KomodoAuthRequest)]
#[response(CompleteTotpLoginResponse)]
#[error(serror::Error)]
pub struct CompleteTotpLogin {
pub code: String,
}
#[typeshare]
pub type CompleteTotpLoginResponse = JwtResponse;
#[typeshare(serialized_as = "any")]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct _PublicKeyCredential(pub PublicKeyCredential);
#[cfg(feature = "openapi")]
impl utoipa::PartialSchema for _PublicKeyCredential {
fn schema()
-> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
utoipa::schema!(#[inline] std::collections::HashMap<String, serde_json::Value>).into()
}
}
#[cfg(feature = "openapi")]
impl utoipa::ToSchema for _PublicKeyCredential {}
#[typeshare]
#[derive(
Serialize, Deserialize, Debug, Clone, Resolve, EmptyTraits,
)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[empty_traits(KomodoAuthRequest)]
#[response(CompletePasskeyLoginResponse)]
#[error(serror::Error)]
pub struct CompletePasskeyLogin {
pub credential: _PublicKeyCredential,
}
#[typeshare]
pub type CompletePasskeyLoginResponse = JwtResponse;
#[typeshare]
#[derive(
Serialize, Deserialize, Debug, Clone, Resolve, EmptyTraits,
)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[empty_traits(KomodoAuthRequest)]
#[response(GetUserResponse)]
#[error(serror::Error)]
pub struct GetUser {}
#[typeshare]
pub type GetUserResponse = User;