Skip to main content

im_core/auth/
service.rs

1use crate::internal::auth::session::{AsyncSessionProvider, SessionProvider};
2
3pub struct AuthService<'a> {
4    client: &'a crate::core::ImClient,
5}
6
7impl<'a> AuthService<'a> {
8    pub(crate) fn new(client: &'a crate::core::ImClient) -> Self {
9        Self { client }
10    }
11
12    pub fn login(&self) -> crate::ImResult<super::SessionBundle> {
13        self.ensure_session(super::AuthScope::UserProfile)
14    }
15
16    pub async fn login_async(&self) -> crate::ImResult<super::SessionBundle> {
17        self.ensure_session_async(super::AuthScope::UserProfile)
18            .await
19    }
20
21    pub fn ensure_session(&self, scope: super::AuthScope) -> crate::ImResult<super::SessionBundle> {
22        SessionProvider::ensure_session(&self.provider(), scope)
23    }
24
25    pub async fn ensure_session_async(
26        &self,
27        scope: super::AuthScope,
28    ) -> crate::ImResult<super::SessionBundle> {
29        AsyncSessionProvider::ensure_session(&self.provider(), scope).await
30    }
31
32    pub fn refresh_session(&self) -> crate::ImResult<super::SessionUpdate> {
33        SessionProvider::refresh_session(&self.provider())
34    }
35
36    pub async fn refresh_session_async(&self) -> crate::ImResult<super::SessionUpdate> {
37        AsyncSessionProvider::refresh_session(&self.provider()).await
38    }
39
40    pub fn status(&self) -> crate::ImResult<super::AuthStatus> {
41        SessionProvider::status(&self.provider())
42    }
43
44    pub async fn status_async(&self) -> crate::ImResult<super::AuthStatus> {
45        AsyncSessionProvider::status(&self.provider()).await
46    }
47
48    fn provider(&self) -> crate::internal::auth::session::FileSessionProvider<'a> {
49        crate::internal::auth::session::FileSessionProvider::new(self.client)
50    }
51}