client-api: Add support for account locking

According to MSC3939.
This commit is contained in:
Kévin Commaille 2024-09-29 10:37:29 +02:00 committed by Kévin Commaille
parent 9f8cdecae6
commit a9267360cb
3 changed files with 32 additions and 1 deletions

View File

@ -36,6 +36,7 @@ Improvements:
- This is a breaking change, but only for users of `unstable-msc3575` - This is a breaking change, but only for users of `unstable-msc3575`
- Add the `get_login_token` field to `Capabilities`, according to a - Add the `get_login_token` field to `Capabilities`, according to a
clarification in the spec. clarification in the spec.
- Add support for account locking, according to MSC3939.
Bug fixes: Bug fixes:

View File

@ -204,6 +204,9 @@ pub enum ErrorKind {
#[cfg(feature = "unstable-msc3843")] #[cfg(feature = "unstable-msc3843")]
Unactionable, Unactionable,
/// M_USER_LOCKED
UserLocked,
#[doc(hidden)] #[doc(hidden)]
_Custom { errcode: PrivOwnedStr, extra: Extra }, _Custom { errcode: PrivOwnedStr, extra: Extra },
} }
@ -280,6 +283,7 @@ impl AsRef<str> for ErrorKind {
Self::WrongRoomKeysVersion { .. } => "M_WRONG_ROOM_KEYS_VERSION", Self::WrongRoomKeysVersion { .. } => "M_WRONG_ROOM_KEYS_VERSION",
#[cfg(feature = "unstable-msc3843")] #[cfg(feature = "unstable-msc3843")]
Self::Unactionable => "M_UNACTIONABLE", Self::Unactionable => "M_UNACTIONABLE",
Self::UserLocked => "M_USER_LOCKED",
Self::_Custom { errcode, .. } => &errcode.0, Self::_Custom { errcode, .. } => &errcode.0,
} }
} }
@ -927,4 +931,28 @@ mod tests {
}) })
); );
} }
#[test]
fn serialize_user_locked() {
let error = Error::new(
http::StatusCode::UNAUTHORIZED,
ErrorBody::Standard {
kind: ErrorKind::UserLocked,
message: "This account has been locked".to_owned(),
},
);
let response = error.try_into_http_response::<Vec<u8>>().unwrap();
assert_eq!(response.status(), http::StatusCode::UNAUTHORIZED);
let json_body: JsonValue = from_json_slice(response.body()).unwrap();
assert_eq!(
json_body,
json!({
"errcode": "M_USER_LOCKED",
"error": "This account has been locked",
"soft_logout": true,
})
);
}
} }

View File

@ -252,6 +252,7 @@ impl<'de> Visitor<'de> for ErrorKindVisitor {
}, },
#[cfg(feature = "unstable-msc3843")] #[cfg(feature = "unstable-msc3843")]
ErrCode::Unactionable => ErrorKind::Unactionable, ErrCode::Unactionable => ErrorKind::Unactionable,
ErrCode::UserLocked => ErrorKind::UserLocked,
ErrCode::_Custom(errcode) => ErrorKind::_Custom { errcode, extra }, ErrCode::_Custom(errcode) => ErrorKind::_Custom { errcode, extra },
}) })
} }
@ -310,6 +311,7 @@ enum ErrCode {
WrongRoomKeysVersion, WrongRoomKeysVersion,
#[cfg(feature = "unstable-msc3843")] #[cfg(feature = "unstable-msc3843")]
Unactionable, Unactionable,
UserLocked,
_Custom(PrivOwnedStr), _Custom(PrivOwnedStr),
} }
@ -330,7 +332,7 @@ impl Serialize for ErrorKind {
let mut st = serializer.serialize_map(None)?; let mut st = serializer.serialize_map(None)?;
st.serialize_entry("errcode", self.as_ref())?; st.serialize_entry("errcode", self.as_ref())?;
match self { match self {
Self::UnknownToken { soft_logout: true } => { Self::UnknownToken { soft_logout: true } | Self::UserLocked => {
st.serialize_entry("soft_logout", &true)?; st.serialize_entry("soft_logout", &true)?;
} }
Self::LimitExceeded { retry_after: Some(RetryAfter::Delay(duration)) } => { Self::LimitExceeded { retry_after: Some(RetryAfter::Delay(duration)) } => {