Launcher Auth (#450)

* Launcher Auth

* Finish auth

* final fixes
This commit is contained in:
Geometrically
2023-08-04 23:38:34 -07:00
committed by GitHub
parent a35dd67b77
commit 47e28d24c8
38 changed files with 1200 additions and 477 deletions

View File

@@ -1,4 +1,6 @@
//! Authentication flow based on Hydra
use crate::config::MODRINTH_API_URL;
use crate::state::CredentialsStore;
use crate::util::fetch::{fetch_advanced, fetch_json, FetchSemaphore};
use async_tungstenite as ws;
use chrono::{prelude::*, Duration};
@@ -10,7 +12,7 @@ use url::Url;
lazy_static! {
static ref HYDRA_URL: Url =
Url::parse("https://staging-api.modrinth.com/v2/auth/minecraft/")
Url::parse(&format!("{MODRINTH_API_URL}auth/minecraft/"))
.expect("Hydra URL parse failed");
}
@@ -40,7 +42,7 @@ struct TokenJSON {
token: String,
refresh_token: String,
expires_after: u32,
flow: String,
flow: Option<String>,
}
#[derive(Deserialize)]
@@ -68,7 +70,7 @@ pub struct HydraAuthFlow<S: AsyncRead + AsyncWrite + Unpin> {
impl HydraAuthFlow<ws::tokio::ConnectStream> {
pub async fn new() -> crate::Result<Self> {
let (socket, _) = ws::tokio::connect_async(
"wss://staging-api.modrinth.com/v2/auth/minecraft/ws",
"wss://api.modrinth.com/v2/auth/minecraft/ws",
)
.await?;
Ok(Self { socket })
@@ -96,7 +98,7 @@ impl HydraAuthFlow<ws::tokio::ConnectStream> {
pub async fn extract_credentials(
&mut self,
semaphore: &FetchSemaphore,
) -> crate::Result<Credentials> {
) -> crate::Result<(Credentials, Option<String>)> {
// Minecraft bearer token
let token_resp = self
.socket
@@ -117,14 +119,17 @@ impl HydraAuthFlow<ws::tokio::ConnectStream> {
let info = fetch_info(&token.token, semaphore).await?;
// Return structure from response
Ok(Credentials {
username: info.name,
id: info.id,
refresh_token: token.refresh_token,
access_token: token.token,
expires,
_ctor_scope: std::marker::PhantomData,
})
Ok((
Credentials {
username: info.name,
id: info.id,
refresh_token: token.refresh_token,
access_token: token.token,
expires,
_ctor_scope: std::marker::PhantomData,
},
token.flow,
))
}
}
@@ -134,10 +139,11 @@ pub async fn refresh_credentials(
) -> crate::Result<()> {
let resp = fetch_json::<TokenJSON>(
Method::POST,
"https://staging-api.modrinth.com/v2/auth/minecraft/refresh",
&format!("{MODRINTH_API_URL}auth/minecraft/refresh"),
None,
Some(serde_json::json!({ "refresh_token": credentials.refresh_token })),
semaphore,
&CredentialsStore(None),
)
.await?;
@@ -162,6 +168,7 @@ async fn fetch_info(
Some(("Authorization", &format!("Bearer {token}"))),
None,
semaphore,
&CredentialsStore(None),
)
.await?;
let value = serde_json::from_slice(&result)?;