feat: authenticate

This commit is contained in:
newt 2024-11-01 23:37:40 +00:00
commit 4fc017401f
5 changed files with 1619 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
.env

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"rust-analyzer.cargo.features": "all"
}

1523
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

18
Cargo.toml Normal file
View file

@ -0,0 +1,18 @@
[package]
name = "echoed"
publish = false
version = "0.1.0"
edition = "2021"
[features]
dotenvy = ["dep:dotenvy"]
[dependencies]
color-eyre = "0.6.3"
cookie_store = "0.21.0"
dotenvy = { version = "0.15.7", optional = true }
ijson = "0.1.3"
reqwest = { version = "0.12.9", features = ["json", "cookies", "multipart"] }
reqwest_cookie_store = "0.8.0"
tokio = { version = "1.41.0", features = ["macros", "rt-multi-thread"] }
url = "2.5.2"

73
src/main.rs Normal file
View file

@ -0,0 +1,73 @@
use color_eyre::eyre::eyre;
use color_eyre::Result;
use reqwest::multipart::Form;
use reqwest::{Client, Url};
use reqwest_cookie_store::CookieStoreMutex;
use std::env;
use std::sync::Arc;
const DEFAULT_BASE_URL: &str = "https://echo360.org.uk";
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
#[cfg(feature = "dotenvy")]
dotenvy::dotenv()?;
let jar = Arc::new(CookieStoreMutex::default());
let client = Client::builder().cookie_provider(jar.clone()).build()?;
let base_url = Url::parse(DEFAULT_BASE_URL)?;
let domain = base_url.domain().unwrap();
// get the csrf token
client.get(base_url.clone()).send().await?;
let csrf_token = jar
.lock()
.unwrap()
.get(domain, "/", "PLAY_SESSION")
.map(|cookie| {
let cookie_value = cookie.value().to_string();
cookie_value
.split("&")
.nth(1)
.map(|part| part.split("=").last())
.flatten()
.map(|x| x.to_string())
})
.flatten()
.ok_or(eyre!("No csrf token found"))?;
// authenticate
client
.post(format!("{base_url}login"))
.query(&[("csrfToken", csrf_token)])
.multipart(
Form::new()
.text("email", env::var("ECHO360_EMAIL")?)
.text("password", env::var("ECHO360_PASSWORD")?),
)
.send()
.await?;
// get institution id
let insitution_id = jar
.lock()
.unwrap()
.get(domain, "/", "PLAY_SESSION")
.map(|cookie| {
let cookie_value = cookie.value().to_string();
cookie_value
.split("&")
.nth(1)
.map(|part| part.split("=").last())
.flatten()
.map(|x| x.to_string())
})
.flatten()
.ok_or(eyre!("No institution id found"))?;
println!("Institution ID: {}", insitution_id);
Ok(())
}