Add state parameter to Mastodon query

O01eg
Feb 2, 2025, 3:56 PM
564OK4ZCPPAEBXUISDP5526O5OJIXKOPDEUBKWNKTT4KQ3TOJOCQC

Dependencies

Change contents

  • edit in src/pages/mod.rs at line 49
    [3.1548]
    [9.1056]
    /// Cache for OAuth state for domain
    pub cache_mastodon_state: Mutex<TtlCache<String, String>>,
  • edit in src/pages/log_in.rs at line 548
    [3.1]
    [2.7336]
    let state = Uuid::new_v4().to_string();
  • replacement in src/pages/log_in.rs at line 550
    [2.7436][2.7436:7562]()
    let location = format!("https://{}/oauth/authorize?client_id={}&scope=read:accounts&redirect_uri={}&response_type=code",
    [2.7436]
    [2.7562]
    let location = format!("https://{}/oauth/authorize?client_id={}&scope=read:accounts&redirect_uri={}&response_type=code&state={}",
  • replacement in src/pages/log_in.rs at line 553
    [2.7664][2.7664:7759]()
    pct_str::PctString::encode(website_redirect.chars(), pct_str::URIReserved).as_str(),);
    [2.7664]
    [3.1531]
    pct_str::PctString::encode(website_redirect.chars(), pct_str::URIReserved).as_str(),
    state);
    {
    let mut cache = data.cache_mastodon_state.lock().await;
    cache.insert(
    state,
    domain,
    std::time::Duration::from_secs(data.cache_duration_sec),
    );
    }
  • edit in src/pages/log_in.rs at line 584
    [2.8207]
    [2.8207]
    let mut state = None;
  • edit in src/pages/log_in.rs at line 589
    [2.8373]
    [2.8373]
    } else if k == "state" {
    state = Some(v);
  • replacement in src/pages/log_in.rs at line 594
    [2.8403][2.8403:8471]()
    let code = match code {
    Some(r) => r,
    None => {
    [2.8403]
    [2.8471]
    let (code, state) = match (code, state) {
    (Some(c), Some(s)) => (c, s),
    _ => {
  • replacement in src/pages/log_in.rs at line 598
    [2.8495][2.8495:8554]()
    "Mastodon not redirected with code in {}",
    [2.8495]
    [2.8554]
    "Mastodon not redirected with code or state in {}",
  • edit in src/pages/log_in.rs at line 605
    [2.8693]
    [2.8693]
    let cached_data = {
    let mut cache = data.cache_mastodon_state.lock().await;
    cache.remove(state)
    };
    if cached_data.is_none_or(|x| x != domain) {
    log::warn!("Unknown state for mastodon redirect: {}", state);
    return HttpResponse::BadRequest().body("Incorrect");
    }
  • edit in src/main.rs at line 227
    [3.7053]
    [9.1123]
    cache_mastodon_state: tokio::sync::Mutex::new(ttl_cache::TtlCache::new(cache_capacity)),