Skip to content

Commit

Permalink
Heavy improvements in Rust, Vue TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
DIDIRUS4 committed Nov 5, 2023
1 parent 8fb0131 commit 21e5dbb
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 67 deletions.
9 changes: 4 additions & 5 deletions theseus/src/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
launcher::auth as inner,
State,
};
use chrono::Utc;

use crate::state::AuthTask;
pub use inner::Credentials;
Expand Down Expand Up @@ -41,11 +40,11 @@ pub async fn refresh(user: uuid::Uuid) -> crate::Result<Credentials> {
let state = State::get().await?;
let mut users = state.users.write().await;

Check warning on line 41 in theseus/src/api/auth.rs

View workflow job for this annotation

GitHub Actions / clippy

variable does not need to be mutable

warning: variable does not need to be mutable --> theseus/src/api/auth.rs:41:9 | 41 | let mut users = state.users.write().await; | ----^^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default

let mut credentials = users.get(user).ok_or_else(|| {
let credentials = users.get(user).ok_or_else(|| {
crate::ErrorKind::OtherError(
"You are not logged in with a Minecraft account!".to_string(),
"У вас нет аккаунта, пожалуйста добавьте его. Подробнее о добавлении оффлайн аккаунта можно узнать из статьи автора".to_string(),
)
.as_error()
.as_error()
})?;

Ok(credentials)
Expand Down Expand Up @@ -93,7 +92,7 @@ pub async fn get_user(user: uuid::Uuid) -> crate::Result<Credentials> {
crate::ErrorKind::OtherError(format!(
"Tried to get nonexistent user with ID {user}"
))
.as_error()
.as_error()
})?;
Ok(user)
}
38 changes: 35 additions & 3 deletions theseus/src/api/hydra/complete.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Main authentication flow for Hydra

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

use crate::prelude::Credentials;

Expand Down Expand Up @@ -37,7 +38,7 @@ pub async fn wait_finish(device_code: String) -> crate::Result<Credentials> {
"Error getting XBox Live token: {}",
err
))
.as_error())
.as_error())
}
xsts_token::XSTSResponse::Success { token: xsts_token } => {
// Get xsts bearer token from xsts token
Expand All @@ -54,7 +55,7 @@ pub async fn wait_finish(device_code: String) -> crate::Result<Credentials> {
// Get player info from bearer token
let player_info = player_info::fetch_info(&bearer_token).await.map_err(|_err| {
crate::ErrorKind::HydraError("No Minecraft account for profile. Make sure you own the game and have set a username through the official Minecraft launcher."
.to_string())
.to_string())
})?;

// Create credentials
Expand Down Expand Up @@ -83,3 +84,34 @@ pub async fn wait_finish(device_code: String) -> crate::Result<Credentials> {
}
}
}



pub async fn wait_offline_finish(name: &str) -> crate::Result<Credentials> {
let random_uuid = Uuid::new_v4();
let access_token = "null".to_string();
let refresh_token = "null".to_string();

let credentials = Credentials::new (
random_uuid,
name.to_string(),
access_token,
refresh_token,
chrono::Utc::now(),
);

// Put credentials into state
let state = crate::State::get().await?;
{
let mut users = state.users.write().await;
users.insert(&credentials).await?;
}

if state.settings.read().await.default_user.is_none() {
let mut settings = state.settings.write().await;
settings.default_user = Some(credentials.id);
}

Ok(credentials)
}

2 changes: 1 addition & 1 deletion theseus/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub enum ErrorKind {
#[error("Error stripping prefix: {0}")]
StripPrefixError(#[from] std::path::StripPrefixError),

#[error("Error: {0}")]
#[error("AstralRinth Ошибка: {0}")]
OtherError(String),

#[cfg(feature = "tauri")]
Expand Down
14 changes: 11 additions & 3 deletions theseus_gui/src-tauri/src/api/auth.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::api::Result;
use crate::api::{Result,};
use tauri::plugin::TauriPlugin;
use theseus::{hydra::init::DeviceLoginSuccess, prelude::*};

use theseus::{hydra, hydra::init::DeviceLoginSuccess, prelude::*};
pub fn init<R: tauri::Runtime>() -> TauriPlugin<R> {
tauri::plugin::Builder::new("auth")
.invoke_handler(tauri::generate_handler![
offline_auth_authenticate_begin,
auth_authenticate_begin_flow,
auth_authenticate_await_completion,
auth_cancel_flow,
Expand All @@ -17,6 +17,14 @@ pub fn init<R: tauri::Runtime>() -> TauriPlugin<R> {
.build()
}

/// Create new offline user
/// This is custom function from Astralium Org.
#[tauri::command]
pub async fn offline_auth_authenticate_begin(name: &str) -> Result<Credentials> {
let credentials = hydra::complete::wait_offline_finish(name).await?;
Ok(credentials)
}

/// Authenticate a user with Hydra - part 1
/// This begins the authentication flow quasi-synchronously, returning a URL to visit (that the user will sign in at)
#[tauri::command]
Expand Down
Loading

0 comments on commit 21e5dbb

Please sign in to comment.