You've already forked AstralRinth
forked from didirus/AstralRinth
Migrate to SQLite for Internal Launcher Data (#1300)
* initial migration * barebones profiles * Finish profiles * Add back file watcher * UI support progress * Finish most of cache * Fix options page * Fix forge, finish modrinth auth * Accounts, process cache * Run SQLX prepare * Finish * Run lint + actions * Fix version to be compat with windows * fix lint * actually fix lint * actually fix lint again
This commit is contained in:
@@ -1,56 +1,30 @@
|
||||
use crate::state::{
|
||||
ModrinthAuthFlow, ModrinthCredentials, ModrinthCredentialsResult,
|
||||
};
|
||||
use crate::ErrorKind;
|
||||
use crate::state::{ModrinthCredentials, ModrinthCredentialsResult};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[tracing::instrument]
|
||||
pub async fn authenticate_begin_flow(provider: &str) -> crate::Result<String> {
|
||||
let state = crate::State::get().await?;
|
||||
|
||||
// Don't start an uncompleteable new flow if there's an existing locked one
|
||||
let mut write: tokio::sync::RwLockWriteGuard<'_, Option<ModrinthAuthFlow>> =
|
||||
state.modrinth_auth_flow.write().await;
|
||||
|
||||
let mut flow = ModrinthAuthFlow::new(provider).await?;
|
||||
let url = flow.prepare_login_url().await?;
|
||||
|
||||
*write = Some(flow);
|
||||
|
||||
Ok(url)
|
||||
pub fn authenticate_begin_flow(provider: &str) -> String {
|
||||
crate::state::get_login_url(provider)
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub async fn authenticate_await_complete_flow(
|
||||
pub async fn authenticate_finish_flow(
|
||||
response: HashMap<String, Value>,
|
||||
) -> crate::Result<ModrinthCredentialsResult> {
|
||||
let state = crate::State::get().await?;
|
||||
|
||||
let mut write = state.modrinth_auth_flow.write().await;
|
||||
if let Some(ref mut flow) = *write {
|
||||
let creds = flow.extract_credentials(&state.fetch_semaphore).await?;
|
||||
let creds = crate::state::finish_login_flow(
|
||||
response,
|
||||
&state.api_semaphore,
|
||||
&state.pool,
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let ModrinthCredentialsResult::Credentials(creds) = &creds {
|
||||
let mut write = state.credentials.write().await;
|
||||
write.login(creds.clone()).await?;
|
||||
}
|
||||
|
||||
Ok(creds)
|
||||
} else {
|
||||
Err(ErrorKind::OtherError(
|
||||
"No active Modrinth authenication flow!".to_string(),
|
||||
)
|
||||
.into())
|
||||
if let ModrinthCredentialsResult::Credentials(creds) = &creds {
|
||||
creds.upsert(&state.pool).await?;
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub async fn cancel_flow() -> crate::Result<()> {
|
||||
let state = crate::State::get().await?;
|
||||
let mut write = state.modrinth_auth_flow.write().await;
|
||||
if let Some(ref mut flow) = *write {
|
||||
flow.close().await?;
|
||||
}
|
||||
*write = None;
|
||||
Ok(())
|
||||
Ok(creds)
|
||||
}
|
||||
|
||||
pub async fn login_password(
|
||||
@@ -63,13 +37,13 @@ pub async fn login_password(
|
||||
username,
|
||||
password,
|
||||
challenge,
|
||||
&state.fetch_semaphore,
|
||||
&state.api_semaphore,
|
||||
&state.pool,
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let ModrinthCredentialsResult::Credentials(creds) = &creds {
|
||||
let mut write = state.credentials.write().await;
|
||||
write.login(creds.clone()).await?;
|
||||
creds.upsert(&state.pool).await?;
|
||||
}
|
||||
|
||||
Ok(creds)
|
||||
@@ -82,10 +56,10 @@ pub async fn login_2fa(
|
||||
) -> crate::Result<ModrinthCredentials> {
|
||||
let state = crate::State::get().await?;
|
||||
let creds =
|
||||
crate::state::login_2fa(code, flow, &state.fetch_semaphore).await?;
|
||||
crate::state::login_2fa(code, flow, &state.api_semaphore, &state.pool)
|
||||
.await?;
|
||||
|
||||
let mut write = state.credentials.write().await;
|
||||
write.login(creds.clone()).await?;
|
||||
creds.upsert(&state.pool).await?;
|
||||
|
||||
Ok(creds)
|
||||
}
|
||||
@@ -105,32 +79,24 @@ pub async fn create_account(
|
||||
password,
|
||||
challenge,
|
||||
sign_up_newsletter,
|
||||
&state.fetch_semaphore,
|
||||
&state.api_semaphore,
|
||||
&state.pool,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut write = state.credentials.write().await;
|
||||
write.login(creds.clone()).await?;
|
||||
creds.upsert(&state.pool).await?;
|
||||
|
||||
Ok(creds)
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub async fn refresh() -> crate::Result<()> {
|
||||
let state = crate::State::get().await?;
|
||||
|
||||
let mut write = state.credentials.write().await;
|
||||
crate::state::refresh_credentials(&mut write, &state.fetch_semaphore)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub async fn logout() -> crate::Result<()> {
|
||||
let state = crate::State::get().await?;
|
||||
let mut write = state.credentials.write().await;
|
||||
write.logout().await?;
|
||||
let current = ModrinthCredentials::get_active(&state.pool).await?;
|
||||
|
||||
if let Some(current) = current {
|
||||
ModrinthCredentials::remove(¤t.user_id, &state.pool).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -138,7 +104,9 @@ pub async fn logout() -> crate::Result<()> {
|
||||
#[tracing::instrument]
|
||||
pub async fn get_credentials() -> crate::Result<Option<ModrinthCredentials>> {
|
||||
let state = crate::State::get().await?;
|
||||
let read = state.credentials.read().await;
|
||||
let current =
|
||||
ModrinthCredentials::get_and_refresh(&state.pool, &state.api_semaphore)
|
||||
.await?;
|
||||
|
||||
Ok(read.0.clone())
|
||||
Ok(current)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user