diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-10-16 22:54:54 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-16 22:54:54 -0500 |
| commit | 4cef62e8e4aa04e44048eb67e5091c12a73d2a09 (patch) | |
| tree | 1c3b03bad262bdcab878cd42d445676290000bea /azalea-protocol | |
| parent | 993914d175609e5d291e7caafc1983379642e7fe (diff) | |
| download | azalea-drasl-4cef62e8e4aa04e44048eb67e5091c12a73d2a09.tar.xz | |
Microsoft Authentication (#29)
* a
* try to do more work on auth signing (untested)
* well auth works when i remove the d= so
* auth stuff
* sessionserver stuff
* add auth in azalea-protocol/client
* caching*
refreshing microsoft auth tokens isn't implemented yet, also i haven't tested it
* how did i not notice that i had the code duplicated
* fix cache
* add refreshing msa token
* replace some printlns with log::trace
* auth works!
* Update main.rs
* fix clippy warnings
Diffstat (limited to 'azalea-protocol')
| -rw-r--r-- | azalea-protocol/README.md | 2 | ||||
| -rw-r--r-- | azalea-protocol/src/connect.rs | 63 |
2 files changed, 57 insertions, 8 deletions
diff --git a/azalea-protocol/README.md b/azalea-protocol/README.md index a210e4a6..7bc1f4c0 100644 --- a/azalea-protocol/README.md +++ b/azalea-protocol/README.md @@ -1,6 +1,6 @@ # Azalea Protocol -Send and receive Minecraft packets. You should probably use `azalea` or `azalea-client` instead. +A low-level crate to send and receive Minecraft packets. You should probably use `azalea` or `azalea-client` instead. The goal is to only support the latest Minecraft version in order to ease development. diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index d7b9bd1d..03c56471 100644 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -2,32 +2,36 @@ use crate::packets::game::{ClientboundGamePacket, ServerboundGamePacket}; use crate::packets::handshake::{ClientboundHandshakePacket, ServerboundHandshakePacket}; +use crate::packets::login::clientbound_hello_packet::ClientboundHelloPacket; use crate::packets::login::{ClientboundLoginPacket, ServerboundLoginPacket}; use crate::packets::status::{ClientboundStatusPacket, ServerboundStatusPacket}; use crate::packets::ProtocolPacket; use crate::read::{read_packet, ReadPacketError}; use crate::write::write_packet; use crate::ServerIpAddress; +use azalea_auth::sessionserver::SessionServerError; use azalea_crypto::{Aes128CfbDec, Aes128CfbEnc}; use bytes::BytesMut; use std::fmt::Debug; use std::marker::PhantomData; use thiserror::Error; +use tokio::io::AsyncWriteExt; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; use tokio::net::TcpStream; +use uuid::Uuid; pub struct ReadConnection<R: ProtocolPacket> { - pub read_stream: OwnedReadHalf, + read_stream: OwnedReadHalf, buffer: BytesMut, - pub compression_threshold: Option<u32>, - pub dec_cipher: Option<Aes128CfbDec>, + compression_threshold: Option<u32>, + dec_cipher: Option<Aes128CfbDec>, _reading: PhantomData<R>, } pub struct WriteConnection<W: ProtocolPacket> { - pub write_stream: OwnedWriteHalf, - pub compression_threshold: Option<u32>, - pub enc_cipher: Option<Aes128CfbEnc>, + write_stream: OwnedWriteHalf, + compression_threshold: Option<u32>, + enc_cipher: Option<Aes128CfbEnc>, _writing: PhantomData<W>, } @@ -64,6 +68,10 @@ where ) .await } + + pub async fn shutdown(&mut self) -> std::io::Result<()> { + self.write_stream.shutdown().await + } } impl<R, W> Connection<R, W> @@ -145,13 +153,54 @@ impl Connection<ClientboundLoginPacket, ServerboundLoginPacket> { pub fn set_encryption_key(&mut self, key: [u8; 16]) { // minecraft has a cipher decoder and encoder, i don't think it matters though? let (enc_cipher, dec_cipher) = azalea_crypto::create_cipher(&key); - self.writer.enc_cipher = Some(enc_cipher); self.reader.dec_cipher = Some(dec_cipher); + self.writer.enc_cipher = Some(enc_cipher); } pub fn game(self) -> Connection<ClientboundGamePacket, ServerboundGamePacket> { Connection::from(self) } + + /// Authenticate with Minecraft's servers, which is required to join + /// online-mode servers. This must happen when you get a + /// `ClientboundLoginPacket::Hello` packet. + /// + /// ```no_run + /// let token = azalea_auth::auth(azalea_auth::AuthOpts { + /// ..Default::default() + /// }) + /// .await; + /// let player_data = azalea_auth::get_profile(token).await; + /// + /// let mut connection = azalea::Connection::new(&server_address).await?; + /// + /// // transition to the login state, in a real program we would have done a handshake first + /// connection.login(); + /// + /// match connection.read().await? { + /// ClientboundLoginPacket::Hello(p) => { + /// // tell Mojang we're joining the server + /// connection.authenticate(&token, player_data.uuid, p).await?; + /// } + /// _ => {} + /// } + /// ``` + pub async fn authenticate( + &self, + access_token: &str, + uuid: &Uuid, + private_key: [u8; 16], + packet: ClientboundHelloPacket, + ) -> Result<(), SessionServerError> { + azalea_auth::sessionserver::join( + access_token, + &packet.public_key, + &private_key, + uuid, + &packet.server_id, + ) + .await + } } // rust doesn't let us implement From because allegedly it conflicts with |
