Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ erased-serde = "0.4.11"
futures = "0.3.34"
hex = "0.4.3"
jiff = { version = "1.0.0", features = ["serde"] }
nidhogg = { version = "0.3.1", default-features = false }
nidhogg = { version = "0.3.2", default-features = false }
reqwest = { version = "0.13.1", default-features = false, features = [
"stream",
"http2",
Expand All @@ -53,6 +53,7 @@ tokio = { version = "1.54.1", features = [
"time",
] }
toml = "1.0.0"
tracing = "0.1.44"
uuid = "1.19.0"

# https://github.com/microsoft/edit/blob/main/Cargo.toml
Expand Down
4 changes: 2 additions & 2 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tls-aws-lc = ["reqwest?/rustls", "nidhogg?/tls-aws-lc"]
tls-rustls-no-provider = ["reqwest?/rustls-no-provider", "nidhogg?/tls-rustls-no-provider"]
tls-native = ["reqwest?/native-tls", "nidhogg?/tls-native"]
tls-native-vendored = ["reqwest?/native-tls-vendored", "nidhogg?/tls-native-vendored"]
yggdrasil-auth = ["dep:nidhogg"]
yggdrasil-auth = ["dep:nidhogg", "http"]
offline-server = ["yggdrasil-auth", "nidhogg/server"]

[dependencies]
Expand All @@ -44,7 +44,7 @@ serde = { workspace = true }
serde_json = { workspace = true, optional = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = "0.1.44"
tracing = { workspace = true }
uuid = { workspace = true, features = ["serde", "v4", "v5"] }

[target.'cfg(windows)'.dependencies]
Expand Down
42 changes: 4 additions & 38 deletions crates/core/src/auth/accounts/microsoft.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use crate::auth::accounts::{Account, AccountId};
use crate::auth::credential::{Credential, CredentialKind};
use crate::auth::error::Result;
use crate::auth::microsoft_protocol::{
ExchangedMinecraftSession, delete_skin, minecraft_profile, profile_textures,
refresh_microsoft_session, select_cape, unix_now, upload_skin,
};
use crate::auth::texture::{ProfileTextures, SkinModel};
use crate::auth::protocol::microsoft::{ExchangedMinecraftSession, unix_now};

const REFRESH_SKEW_SECONDS: u64 = 300;

Expand Down Expand Up @@ -41,7 +35,6 @@ impl MicrosoftAccount {
}
}

#[async_trait]
impl Account for MicrosoftAccount {
fn account_id(&self) -> AccountId {
self.account_id
Expand All @@ -59,8 +52,8 @@ impl Account for MicrosoftAccount {
unix_now().saturating_add(REFRESH_SKEW_SECONDS) >= self.expires_at
}

async fn credential(&self) -> Result<Credential> {
Ok(Credential {
fn credential(&self) -> Credential {
Credential {
username: self.username.clone(),
uuid: self.uuid.clone(),
access_token: self.access_token.clone(),
Expand All @@ -69,33 +62,6 @@ impl Account for MicrosoftAccount {
client_id: Some(self.client_id.clone()),
#[cfg(feature = "yggdrasil-auth")]
auth_server: None,
})
}

async fn refresh(&self) -> Result<Box<dyn Account>> {
let session =
refresh_microsoft_session(&self.client_id, &self.refresh_token, self.xuid.clone())
.await?;
Ok(Box::new(Self::from_session(&self.client_id, session)))
}

async fn textures(&self) -> Result<ProfileTextures> {
profile_textures(minecraft_profile(&self.access_token).await?)
}

async fn upload_skin(&self, image: Vec<u8>, model: SkinModel) -> Result<()> {
upload_skin(&self.access_token, image, model).await
}

async fn delete_skin(&self) -> Result<()> {
delete_skin(&self.access_token).await
}

async fn delete_cape(&self) -> Result<()> {
select_cape(&self.access_token, None).await
}

async fn select_cape(&self, cape_id: Option<String>) -> Result<()> {
select_cape(&self.access_token, cape_id).await
}
}
}
54 changes: 5 additions & 49 deletions crates/core/src/auth/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@ pub mod offline;
#[cfg(feature = "yggdrasil-auth")]
pub mod yggdrasil;

use async_trait::async_trait;
use std::any::Any;

use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[cfg(feature = "yggdrasil-auth")]
use crate::auth::accounts::yggdrasil::Meta;
use crate::auth::credential::Credential;
use crate::auth::error::Result;
use crate::auth::texture::{ProfileTextures, SkinModel};

fn unsupported<T>(operation: &'static str) -> Result<T> {
Err(crate::auth::error::Error::TextureUnsupported { operation })
}

/// Stable identity of a persisted account.
///
/// This is independent from the Minecraft profile UUID because Yggdrasil
/// profiles from different API roots may share the same profile ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct AccountId(Uuid);

Expand Down Expand Up @@ -57,8 +50,7 @@ impl std::fmt::Display for AccountId {
}

/// Persisted authentication state owned by one [`crate::auth::authorizers::Authorizer`].
#[async_trait]
pub trait Account: Send + Sync + erased_serde::Serialize {
pub trait Account: Send + Sync + erased_serde::Serialize + Any {
fn account_id(&self) -> AccountId;

fn authorizer_id(&self) -> &'static str;
Expand All @@ -69,41 +61,5 @@ pub trait Account: Send + Sync + erased_serde::Serialize {
false
}

async fn credential(&self) -> Result<Credential>;

async fn refresh(&self) -> Result<Box<dyn Account>>;

/// Returns the authentication service metadata when the provider exposes it.
///
/// `None` means metadata is unavailable for this account type or, for an
/// offline Yggdrasil account, that there is no remote API root.
#[cfg(feature = "yggdrasil-auth")]
async fn metadata(&self) -> Result<Option<Meta>> {
Ok(None)
}

async fn textures(&self) -> Result<ProfileTextures> {
unsupported("read textures")
}

async fn upload_skin(&self, _image: Vec<u8>, _model: SkinModel) -> Result<()> {
unsupported("upload skin")
}

async fn delete_skin(&self) -> Result<()> {
unsupported("delete skin")
}

async fn upload_cape(&self, _image: Vec<u8>) -> Result<()> {
unsupported("upload cape")
}

async fn delete_cape(&self) -> Result<()> {
unsupported("delete cape")
}

/// Selects an owned cape, or clears the active cape when `cape_id` is `None`.
async fn select_cape(&self, _cape_id: Option<String>) -> Result<()> {
unsupported("select cape")
}
fn credential(&self) -> Credential;
}
13 changes: 3 additions & 10 deletions crates/core/src/auth/accounts/offline.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::auth::accounts::{Account, AccountId};
use crate::auth::credential::{Credential, CredentialKind};
use crate::auth::error::Result;

pub const OFFLINE_AUTHORIZER_ID: &str = "offline";

Expand All @@ -26,7 +24,6 @@ impl OfflineAccount {
}
}

#[async_trait]
impl Account for OfflineAccount {
fn account_id(&self) -> AccountId {
self.account_id
Expand All @@ -40,8 +37,8 @@ impl Account for OfflineAccount {
&self.username
}

async fn credential(&self) -> Result<Credential> {
Ok(Credential {
fn credential(&self) -> Credential {
Credential {
username: self.username.clone(),
uuid: self.uuid.clone(),
access_token: String::new(),
Expand All @@ -50,11 +47,7 @@ impl Account for OfflineAccount {
client_id: None,
#[cfg(feature = "yggdrasil-auth")]
auth_server: None,
})
}

async fn refresh(&self) -> Result<Box<dyn Account>> {
Ok(Box::new(self.clone()))
}
}
}

Expand Down
Loading