You've already forked AstralRinth
forked from didirus/AstralRinth
* initial commit * merge fixes * added sanitizing * linter * Improve sign in UI * simple simple! * bump version --------- Co-authored-by: CodexAdrian <83074853+CodexAdrian@users.noreply.github.com> Co-authored-by: Jai A <jaiagr+gpg@pm.me>
46 lines
1.4 KiB
Rust
46 lines
1.4 KiB
Rust
//! Login route for Hydra, redirects to the Microsoft login page before going to the redirect route
|
|
use std::collections::HashMap;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{hydra::MicrosoftError, util::fetch::REQWEST_CLIENT};
|
|
|
|
use super::MICROSOFT_CLIENT_ID;
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct DeviceLoginSuccess {
|
|
pub device_code: String,
|
|
pub user_code: String,
|
|
pub verification_uri: String,
|
|
pub expires_in: u64,
|
|
pub interval: u64,
|
|
pub message: String,
|
|
}
|
|
|
|
pub async fn init() -> crate::Result<DeviceLoginSuccess> {
|
|
// Get the initial URL
|
|
let client_id = MICROSOFT_CLIENT_ID;
|
|
|
|
// Get device code
|
|
// Define the parameters
|
|
let mut params = HashMap::new();
|
|
params.insert("client_id", client_id);
|
|
params.insert("scope", "XboxLive.signin offline_access");
|
|
|
|
// urlencoding::encode("XboxLive.signin offline_access"));
|
|
let req = REQWEST_CLIENT.post("https://login.microsoftonline.com/consumers/oauth2/v2.0/devicecode")
|
|
.header("Content-Type", "application/x-www-form-urlencoded").form(¶ms).send().await?;
|
|
|
|
match req.status() {
|
|
reqwest::StatusCode::OK => Ok(req.json().await?),
|
|
_ => {
|
|
let microsoft_error = req.json::<MicrosoftError>().await?;
|
|
Err(crate::ErrorKind::HydraError(format!(
|
|
"Error from Microsoft: {:?}",
|
|
microsoft_error.error_description
|
|
))
|
|
.into())
|
|
}
|
|
}
|
|
}
|