use serde::Deserialize;
use serde_json::Value;
pub type TokenPurpose = String;
pub type ParentGroup = String;
pub type PermissionScope = String;
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct AuthResponse {
pub token: String,
#[serde(default)]
pub token_type: Option<String>,
#[serde(default)]
pub expires: Option<i64>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl std::fmt::Debug for AuthResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AuthResponse")
.field("token", &"[redacted]")
.field("token_type", &self.token_type)
.field("expires", &self.expires)
.field("extra", &self.extra)
.finish()
}
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct TokenResponse {
pub token: String,
pub token_type: TokenPurpose,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl std::fmt::Debug for TokenResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TokenResponse")
.field("token", &"[redacted]")
.field("token_type", &self.token_type)
.field("extra", &self.extra)
.finish()
}
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct UserResponse {
pub merchant_id: String,
pub name: String,
pub email: String,
pub role_id: String,
pub is_two_factor_auth_setup: bool,
pub profile_id: String,
pub entity_type: String,
pub version: String,
#[serde(default)]
pub verification_days_left: Option<i64>,
#[serde(default)]
pub recovery_codes_left: Option<i64>,
#[serde(default)]
pub theme_id: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct LoginHistoryEntry {
pub id: String,
pub event_type: String,
pub auth_method: String,
pub created_at: String,
#[serde(default)]
pub failure_reason: Option<String>,
#[serde(default)]
pub ip_address: Option<String>,
#[serde(default)]
pub user_agent: Option<String>,
#[serde(default)]
pub country_code: Option<String>,
#[serde(default)]
pub city: Option<String>,
#[serde(default)]
pub latitude: Option<f64>,
#[serde(default)]
pub longitude: Option<f64>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct LoginHistoryResponse {
pub entries: Vec<LoginHistoryEntry>,
pub total_count: i64,
pub offset: i64,
pub limit: i64,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct UserSessionEntry {
pub id: String,
pub auth_method: String,
pub created_at: String,
pub last_seen_at: String,
pub expires_at: String,
pub is_current: bool,
#[serde(default)]
pub ip_address: Option<String>,
#[serde(default)]
pub user_agent: Option<String>,
#[serde(default)]
pub country_code: Option<String>,
#[serde(default)]
pub city: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct UserSessionListResponse {
pub sessions: Vec<UserSessionEntry>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct UserSessionRevokeResponse {
pub revoked: bool,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct InviteUsersResponse {
pub email: String,
pub is_email_sent: bool,
#[serde(default)]
pub password: Option<String>,
#[serde(default)]
pub error: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl std::fmt::Debug for InviteUsersResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("InviteUsersResponse")
.field("email", &self.email)
.field("is_email_sent", &self.is_email_sent)
.field("password", &self.password.as_ref().map(|_| "[redacted]"))
.field("error", &self.error)
.field("extra", &self.extra)
.finish()
}
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct AddUserResponse {
pub user_id: String,
pub email: String,
pub name: String,
pub is_verified: bool,
pub is_active: bool,
#[serde(default)]
pub password: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl std::fmt::Debug for AddUserResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AddUserResponse")
.field("user_id", &self.user_id)
.field("email", &self.email)
.field("name", &self.name)
.field("is_verified", &self.is_verified)
.field("is_active", &self.is_active)
.field("password", &self.password.as_ref().map(|_| "[redacted]"))
.field("extra", &self.extra)
.finish()
}
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MinimalRoleInfo {
pub role_id: String,
pub role_name: String,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct UserInLineage {
pub user_id: String,
pub email: String,
pub roles: Vec<MinimalRoleInfo>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct ParentGroupInfo {
pub name: ParentGroup,
pub resources: Vec<String>,
pub scopes: Vec<PermissionScope>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct TotpSecret {
pub secret: String,
pub totp_url: String,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl std::fmt::Debug for TotpSecret {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TotpSecret")
.field("secret", &"[redacted]")
.field("totp_url", &"[redacted]")
.field("extra", &self.extra)
.finish()
}
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct TotpResponse {
#[serde(default)]
pub secret: Option<TotpSecret>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl std::fmt::Debug for TotpResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TotpResponse")
.field("secret", &self.secret.as_ref().map(|_| "[redacted]"))
.field("extra", &self.extra)
.finish()
}
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct RecoveryCodesResponse {
pub recovery_codes: Vec<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl std::fmt::Debug for RecoveryCodesResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RecoveryCodesResponse")
.field("recovery_codes", &"[redacted]")
.field("recovery_codes_len", &self.recovery_codes.len())
.field("extra", &self.extra)
.finish()
}
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct PhoneOtpResponse {
pub message: String,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct PhoneOtpVerifyResponse {
pub verified: bool,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[cfg(test)]
mod tests {
use super::*;
fn de<T: serde::de::DeserializeOwned>(json: &str) -> T {
serde_json::from_str(json).expect("fixture deserializes")
}
#[test]
fn auth_response_debug_redacts_the_token() {
let res: AuthResponse =
de(r#"{"token":"jwt-super-secret","token_type":"bearer","expires":3600}"#);
let rendered = format!("{res:?}");
assert!(!rendered.contains("jwt-super-secret"), "{rendered}");
assert!(rendered.contains("bearer"), "{rendered}");
assert!(rendered.contains("3600"), "{rendered}");
}
#[test]
fn auth_response_keeps_unknown_fields_in_extra() {
let res: AuthResponse = de(r#"{"token":"t","brand_new_field":"kept"}"#);
assert_eq!(res.extra["brand_new_field"], "kept");
assert_eq!(res.token_type, None);
}
#[test]
fn token_response_debug_redacts_the_token() {
let res: TokenResponse = de(r#"{"token":"spt-super-secret","token_type":"totp"}"#);
let rendered = format!("{res:?}");
assert!(!rendered.contains("spt-super-secret"), "{rendered}");
assert!(rendered.contains("totp"), "{rendered}");
}
#[test]
fn invite_users_response_debug_redacts_the_password() {
let res: InviteUsersResponse = de(
r#"{"email":"bob@example.com","is_email_sent":false,"password":"temp-pw-1234","error":"smtp down"}"#,
);
let rendered = format!("{res:?}");
assert!(!rendered.contains("temp-pw-1234"), "{rendered}");
assert!(rendered.contains("bob@example.com"), "{rendered}");
assert!(rendered.contains("smtp down"), "{rendered}");
assert!(rendered.contains("Some("), "{rendered}");
}
#[test]
fn invite_users_response_debug_shows_none_when_no_password() {
let res: InviteUsersResponse = de(r#"{"email":"bob@example.com","is_email_sent":true}"#);
assert!(format!("{res:?}").contains("password: None"));
}
#[test]
fn add_user_response_debug_redacts_the_password() {
let res: AddUserResponse = de(
r#"{"user_id":"usr_1","email":"bob@example.com","name":"Bob","is_verified":false,"is_active":true,"password":"generated-pw-9000"}"#,
);
let rendered = format!("{res:?}");
assert!(!rendered.contains("generated-pw-9000"), "{rendered}");
assert!(rendered.contains("usr_1"), "{rendered}");
}
#[test]
fn add_user_response_debug_shows_none_when_password_omitted() {
let res: AddUserResponse = de(
r#"{"user_id":"usr_1","email":"bob@example.com","name":"Bob","is_verified":false,"is_active":true}"#,
);
assert!(format!("{res:?}").contains("password: None"));
}
#[test]
fn totp_secret_debug_redacts_the_secret_and_url() {
let res: TotpSecret = de(
r#"{"secret":"JBSWY3DPEHPK3PXP","totp_url":"otpauth://totp/x?secret=JBSWY3DPEHPK3PXP"}"#,
);
let rendered = format!("{res:?}");
assert!(!rendered.contains("JBSWY3DPEHPK3PXP"), "{rendered}");
assert!(!rendered.contains("otpauth"), "{rendered}");
}
#[test]
fn totp_response_debug_redacts_the_secret() {
let res: TotpResponse = de(
r#"{"secret":{"secret":"JBSWY3DPEHPK3PXP","totp_url":"otpauth://totp/x?secret=JBSWY3DPEHPK3PXP"}}"#,
);
let rendered = format!("{res:?}");
assert!(!rendered.contains("JBSWY3DPEHPK3PXP"), "{rendered}");
assert!(!rendered.contains("otpauth"), "{rendered}");
assert!(rendered.contains("Some("), "{rendered}");
}
#[test]
fn totp_response_debug_shows_none_when_already_enrolled() {
let res: TotpResponse = de(r#"{"secret":null}"#);
assert!(format!("{res:?}").contains("secret: None"));
}
#[test]
fn recovery_codes_response_debug_redacts_the_codes() {
let res: RecoveryCodesResponse =
de(r#"{"recovery_codes":["aaaa-bbbb","cccc-dddd","eeee-ffff"]}"#);
let rendered = format!("{res:?}");
assert!(!rendered.contains("aaaa-bbbb"), "{rendered}");
assert!(!rendered.contains("eeee-ffff"), "{rendered}");
assert!(rendered.contains("recovery_codes_len: 3"), "{rendered}");
}
#[test]
fn user_response_parses_optional_fields_and_keeps_extras() {
let res: UserResponse = de(
r#"{"merchant_id":"mer_1","name":"Ada","email":"ada@example.com","role_id":"role_1",
"is_two_factor_auth_setup":true,"profile_id":"pro_1","entity_type":"merchant",
"version":"v2","theme_id":"dark","new_api_field":7}"#,
);
assert_eq!(res.theme_id.as_deref(), Some("dark"));
assert_eq!(res.verification_days_left, None);
assert_eq!(res.extra["new_api_field"], 7);
}
#[test]
fn login_history_parses_null_geo_fields() {
let res: LoginHistoryResponse = de(
r#"{"entries":[{"id":"lh_1","event_type":"signin_failure","failure_reason":"totp_invalid",
"auth_method":"totp","ip_address":"127.0.0.1","user_agent":null,"country_code":null,
"city":null,"latitude":null,"longitude":null,"created_at":"2026-01-01T00:00:00Z"}],
"total_count":1,"offset":0,"limit":25}"#,
);
assert_eq!(res.total_count, 1);
assert_eq!(
res.entries[0].failure_reason.as_deref(),
Some("totp_invalid")
);
assert_eq!(res.entries[0].latitude, None);
assert_eq!(res.entries[0].ip_address.as_deref(), Some("127.0.0.1"));
}
#[test]
fn session_list_parses_the_current_flag() {
let res: UserSessionListResponse = de(
r#"{"sessions":[{"id":"ses_1","auth_method":"password","ip_address":null,
"user_agent":null,"country_code":null,"city":null,
"created_at":"2026-01-01T00:00:00Z","last_seen_at":"2026-01-02T00:00:00Z",
"expires_at":"2026-01-08T00:00:00Z","is_current":true}]}"#,
);
assert_eq!(res.sessions.len(), 1);
assert!(res.sessions[0].is_current);
assert_eq!(res.sessions[0].last_seen_at, "2026-01-02T00:00:00Z");
}
#[test]
fn user_in_lineage_parses_nested_roles() {
let res: UserInLineage = de(r#"{"user_id":"usr_1","email":"bob@example.com",
"roles":[{"role_id":"role_1","role_name":"Admin"}]}"#);
assert_eq!(res.roles[0].role_name, "Admin");
}
#[test]
fn parent_group_info_parses_scopes() {
let res: ParentGroupInfo = de(
r#"{"name":"Operations","resources":["payment","refund"],"scopes":["Read","Write"]}"#,
);
assert_eq!(res.name, "Operations");
assert_eq!(res.resources, vec!["payment", "refund"]);
assert_eq!(res.scopes, vec!["Read", "Write"]);
}
}