Skip to main content

Crate minecraft_msa_auth

Crate minecraft_msa_auth 

Source
Expand description

This crate allows you to authenticate into Minecraft online services using a Microsoft Oauth2 token. You can integrate it with oauth2-rs and build interactive authentication flows.

By default the flow is asynchronous and uses reqwest as the HTTP client, but any other client can be plugged in by implementing the HttpClient trait and disabling the default reqwest feature.

Enabling the is_sync feature turns the whole API synchronous: the HttpClient trait and the flow methods lose their async, and the reqwest implementation switches to [reqwest::blocking::Client]. This is aimed at small launchers that do not want an async runtime, ideally combined with the ureq-backed [UreqClient] available behind the ureq feature.

The example below assumes the default asynchronous mode.

§Example

let client = BasicClient::new(ClientId::new(client_id))
    .set_auth_uri(AuthUrl::new(MSA_AUTHORIZE_URL.to_string())?)
    .set_token_uri(TokenUrl::new(MSA_TOKEN_URL.to_string())?)
    .set_device_authorization_url(DeviceAuthorizationUrl::new(DEVICE_CODE_URL.to_string())?);

// oauth2 bundles its own reqwest version, which may differ from the one
// minecraft-msa-auth is built against.
let oauth_http_client = oauth2::reqwest::Client::new();
let details: StandardDeviceAuthorizationResponse = client
    .exchange_device_code()
    .add_scope(Scope::new("XboxLive.signin offline_access".to_string()))
    .request_async(&oauth_http_client)
    .await?;

println!(
    "Open this URL in your browser:\n{}\nand enter the code: {}",
    details.verification_uri().to_string(),
    details.user_code().secret().to_string()
);

let token = client
    .exchange_device_access_token(&details)
    .request_async(&oauth_http_client, tokio::time::sleep, None)
    .await?;
println!("microsoft token: {:?}", token);

let mc_flow = MinecraftAuthorizationFlow::new(Client::new());
let mc_token = mc_flow.exchange_microsoft_token(token.access_token().secret()).await?;
println!("minecraft token: {:?}", mc_token);

Structs§

MinecraftAccessToken
Represents a Minecraft access token
MinecraftAuthenticationResponse
The response from Minecraft when attempting to authenticate with an xbox token
MinecraftAuthorizationFlow
The flow for authenticating with a Microsoft access token and getting a Minecraft access token.

Enums§

MinecraftAccessTokenError
MinecraftAuthorizationError
Represents an error that can occur when authenticating with Minecraft.
MinecraftTokenType
Represents the token type of a Minecraft access token

Traits§

HttpClient
An HTTP client capable of executing the requests made by MinecraftAuthorizationFlow.

Type Aliases§

HttpRequest
An HTTP request executed by an HttpClient.
HttpResponse
An HTTP response returned by an HttpClient.