Skip to main content

elemental_core/auth/providers/
mod.rs

1#[cfg(feature = "microsoft-auth")]
2pub mod microsoft;
3pub mod offline;
4#[cfg(feature = "yggdrasil-auth")]
5pub mod yggdrasil;
6
7use std::any::Any;
8
9use async_trait::async_trait;
10use serde::de::DeserializeOwned;
11
12use crate::auth::accounts::Account;
13use crate::auth::error::{Error, Result};
14use crate::auth::texture::{ProfileTextures, SkinModel};
15
16fn unsupported<T>(operation: &'static str) -> Result<T> {
17    Err(Error::TextureUnsupported { operation })
18}
19
20#[async_trait]
21pub trait TypedAccountProvider: Send + Sync {
22    type Account: Account + DeserializeOwned + 'static;
23
24    fn authorizer_id(&self) -> &'static str;
25
26    async fn refresh(&self, account: &Self::Account) -> Result<Self::Account>;
27}
28
29#[async_trait]
30pub trait TypedTextureProvider: TypedAccountProvider {
31    async fn textures(&self, account: &Self::Account) -> Result<ProfileTextures>;
32
33    async fn upload_skin(
34        &self,
35        _account: &Self::Account,
36        _image: Vec<u8>,
37        _model: SkinModel,
38    ) -> Result<()> {
39        unsupported("upload skin")
40    }
41
42    async fn delete_skin(&self, _account: &Self::Account) -> Result<()> {
43        unsupported("delete skin")
44    }
45
46    async fn upload_cape(&self, _account: &Self::Account, _image: Vec<u8>) -> Result<()> {
47        unsupported("upload cape")
48    }
49
50    async fn delete_cape(&self, _account: &Self::Account) -> Result<()> {
51        unsupported("delete cape")
52    }
53
54    async fn select_cape(&self, _account: &Self::Account, _cape_id: Option<String>) -> Result<()> {
55        unsupported("select cape")
56    }
57}
58
59#[async_trait]
60pub trait AccountProvider: Send + Sync {
61    fn authorizer_id(&self) -> &'static str;
62
63    fn decode(&self, data: &str) -> std::result::Result<Box<dyn Account>, serde_json::Error>;
64
65    async fn refresh(&self, account: &dyn Account) -> Result<Box<dyn Account>>;
66}
67
68#[async_trait]
69pub trait TextureProvider: AccountProvider {
70    async fn textures(&self, account: &dyn Account) -> Result<ProfileTextures>;
71
72    async fn upload_skin(
73        &self,
74        account: &dyn Account,
75        image: Vec<u8>,
76        model: SkinModel,
77    ) -> Result<()>;
78
79    async fn delete_skin(&self, account: &dyn Account) -> Result<()>;
80
81    async fn upload_cape(&self, account: &dyn Account, image: Vec<u8>) -> Result<()>;
82
83    async fn delete_cape(&self, account: &dyn Account) -> Result<()>;
84
85    async fn select_cape(&self, account: &dyn Account, cape_id: Option<String>) -> Result<()>;
86}
87
88fn downcast_account<'a, P>(provider: &P, account: &'a dyn Account) -> Result<&'a P::Account>
89where
90    P: TypedAccountProvider + ?Sized,
91{
92    (account as &dyn Any)
93        .downcast_ref::<P::Account>()
94        .ok_or_else(|| Error::AccountTypeMismatch {
95            expected_authorizer: provider.authorizer_id(),
96            actual_authorizer: account.authorizer_id().to_owned(),
97        })
98}
99
100#[async_trait]
101impl<P> AccountProvider for P
102where
103    P: TypedAccountProvider,
104{
105    fn authorizer_id(&self) -> &'static str {
106        TypedAccountProvider::authorizer_id(self)
107    }
108
109    fn decode(&self, data: &str) -> std::result::Result<Box<dyn Account>, serde_json::Error> {
110        serde_json::from_str::<P::Account>(data)
111            .map(|account| Box::new(account) as Box<dyn Account>)
112    }
113
114    async fn refresh(&self, account: &dyn Account) -> Result<Box<dyn Account>> {
115        let typed = downcast_account(self, account)?;
116        let refreshed = TypedAccountProvider::refresh(self, typed).await?;
117        Ok(Box::new(refreshed))
118    }
119}
120
121#[async_trait]
122impl<P> TextureProvider for P
123where
124    P: TypedTextureProvider,
125{
126    async fn textures(&self, account: &dyn Account) -> Result<ProfileTextures> {
127        let typed = downcast_account(self, account)?;
128        TypedTextureProvider::textures(self, typed).await
129    }
130
131    async fn upload_skin(
132        &self,
133        account: &dyn Account,
134        image: Vec<u8>,
135        model: SkinModel,
136    ) -> Result<()> {
137        let typed = downcast_account(self, account)?;
138        TypedTextureProvider::upload_skin(self, typed, image, model).await
139    }
140
141    async fn delete_skin(&self, account: &dyn Account) -> Result<()> {
142        let typed = downcast_account(self, account)?;
143        TypedTextureProvider::delete_skin(self, typed).await
144    }
145
146    async fn upload_cape(&self, account: &dyn Account, image: Vec<u8>) -> Result<()> {
147        let typed = downcast_account(self, account)?;
148        TypedTextureProvider::upload_cape(self, typed, image).await
149    }
150
151    async fn delete_cape(&self, account: &dyn Account) -> Result<()> {
152        let typed = downcast_account(self, account)?;
153        TypedTextureProvider::delete_cape(self, typed).await
154    }
155
156    async fn select_cape(&self, account: &dyn Account, cape_id: Option<String>) -> Result<()> {
157        let typed = downcast_account(self, account)?;
158        TypedTextureProvider::select_cape(self, typed, cape_id).await
159    }
160}