From 31a0845b669bab6f2c32303c9ff1c7f51e9594a2 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 18 Aug 2021 20:33:26 +0200 Subject: [PATCH] client-api: Add auth_type and session accessors to IncomingAuthData --- crates/ruma-client-api/CHANGELOG.md | 4 ++ crates/ruma-client-api/src/r0/uiaa.rs | 68 ++++++++++++++++++++------- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/crates/ruma-client-api/CHANGELOG.md b/crates/ruma-client-api/CHANGELOG.md index f0b39db6..620e861d 100644 --- a/crates/ruma-client-api/CHANGELOG.md +++ b/crates/ruma-client-api/CHANGELOG.md @@ -1,5 +1,9 @@ # [unreleased] +Improvements + +* Add `auth_type` and `session` accessors to `uiaa::IncomingAuthData` + # 0.12.1 Improvements: diff --git a/crates/ruma-client-api/src/r0/uiaa.rs b/crates/ruma-client-api/src/r0/uiaa.rs index 9a42180e..91cc9556 100644 --- a/crates/ruma-client-api/src/r0/uiaa.rs +++ b/crates/ruma-client-api/src/r0/uiaa.rs @@ -69,30 +69,62 @@ impl<'a> AuthData<'a> { /// Returns the value of the `type` field, if it exists. pub fn auth_type(&self) -> Option<&'a str> { match self { - AuthData::Password(_) => Some("m.login.password"), - AuthData::ReCaptcha(_) => Some("m.login.recaptcha"), - AuthData::Token(_) => Some("m.login.token"), - AuthData::OAuth2(_) => Some("m.login.oauth2"), - AuthData::EmailIdentity(_) => Some("m.login.email.identity"), - AuthData::Msisdn(_) => Some("m.login.msisdn"), - AuthData::Dummy(_) => Some("m.login.dummy"), - AuthData::FallbackAcknowledgement(_) => None, - AuthData::_Custom(c) => Some(c.auth_type), + Self::Password(_) => Some("m.login.password"), + Self::ReCaptcha(_) => Some("m.login.recaptcha"), + Self::Token(_) => Some("m.login.token"), + Self::OAuth2(_) => Some("m.login.oauth2"), + Self::EmailIdentity(_) => Some("m.login.email.identity"), + Self::Msisdn(_) => Some("m.login.msisdn"), + Self::Dummy(_) => Some("m.login.dummy"), + Self::FallbackAcknowledgement(_) => None, + Self::_Custom(c) => Some(c.auth_type), } } /// Returns the value of the `session` field, if it exists. pub fn session(&self) -> Option<&'a str> { match self { - AuthData::Password(x) => x.session, - AuthData::ReCaptcha(x) => x.session, - AuthData::Token(x) => x.session, - AuthData::OAuth2(x) => x.session, - AuthData::EmailIdentity(x) => x.session, - AuthData::Msisdn(x) => x.session, - AuthData::Dummy(x) => x.session, - AuthData::FallbackAcknowledgement(x) => Some(x.session), - AuthData::_Custom(x) => x.session, + Self::Password(x) => x.session, + Self::ReCaptcha(x) => x.session, + Self::Token(x) => x.session, + Self::OAuth2(x) => x.session, + Self::EmailIdentity(x) => x.session, + Self::Msisdn(x) => x.session, + Self::Dummy(x) => x.session, + Self::FallbackAcknowledgement(x) => Some(x.session), + Self::_Custom(x) => x.session, + } + } +} + +impl IncomingAuthData { + /// Returns the value of the `type` field, if it exists. + pub fn auth_type(&self) -> Option<&str> { + match self { + Self::Password(_) => Some("m.login.password"), + Self::ReCaptcha(_) => Some("m.login.recaptcha"), + Self::Token(_) => Some("m.login.token"), + Self::OAuth2(_) => Some("m.login.oauth2"), + Self::EmailIdentity(_) => Some("m.login.email.identity"), + Self::Msisdn(_) => Some("m.login.msisdn"), + Self::Dummy(_) => Some("m.login.dummy"), + Self::FallbackAcknowledgement(_) => None, + Self::_Custom(c) => Some(&c.auth_type), + } + } + + /// Returns the value of the `session` field, if it exists. + pub fn session(&self) -> Option<&str> { + match self { + Self::Password(x) => x.session.as_deref(), + Self::ReCaptcha(x) => x.session.as_deref(), + Self::Token(x) => x.session.as_deref(), + Self::OAuth2(x) => x.session.as_deref(), + Self::EmailIdentity(x) => x.session.as_deref(), + Self::Msisdn(x) => x.session.as_deref(), + Self::Dummy(x) => x.session.as_deref(), + Self::FallbackAcknowledgement(x) => Some(&x.session), + Self::_Custom(x) => x.session.as_deref(), } } }