better logging for redirect

This commit is contained in:
Alex Janka 2024-02-12 09:52:53 +11:00
parent 247b67b30c
commit 83b6d25278
4 changed files with 58 additions and 9 deletions

11
Cargo.lock generated
View file

@ -26,6 +26,14 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "alex-utils"
version = "0.1.1"
source = "git+https://git.alexjanka.com/alex/alex-utils#b2fe9521edbfb530d8ab7aa8dddb72b170800c73"
dependencies = [
"log",
]
[[package]] [[package]]
name = "anstream" name = "anstream"
version = "0.6.11" version = "0.6.11"
@ -1458,8 +1466,9 @@ dependencies = [
[[package]] [[package]]
name = "tesla-auth-callback-watcher" name = "tesla-auth-callback-watcher"
version = "0.2.0" version = "0.2.1"
dependencies = [ dependencies = [
"alex-utils",
"env_logger", "env_logger",
"lazy_static", "lazy_static",
"log", "log",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "tesla-auth-callback-watcher" name = "tesla-auth-callback-watcher"
version = "0.2.0" version = "0.2.1"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -12,3 +12,4 @@ env_logger = "0.11.1"
rand = "0.8" rand = "0.8"
lazy_static = "1.4" lazy_static = "1.4"
reqwest = "0.11" reqwest = "0.11"
alex-utils = { git = "https://git.alexjanka.com/alex/alex-utils" }

View file

@ -1,7 +1,7 @@
# Maintainer: Alex Janka <alex@alexjanka.com> # Maintainer: Alex Janka <alex@alexjanka.com>
pkgname=tesla-auth-callback-watcher pkgname=tesla-auth-callback-watcher
pkgver=0.2.0 pkgver=0.2.1
pkgrel=1 pkgrel=1
pkgdesc="server for tesla auth callbacks" pkgdesc="server for tesla auth callbacks"
arch=('x86_64' 'aarch64') arch=('x86_64' 'aarch64')

View file

@ -1,7 +1,11 @@
use alex_utils::PrintErrors;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use rand::Rng; use rand::Rng;
use reqwest::header; use reqwest::header;
use rocket::response::Redirect; use rocket::{
request::Request,
response::{Redirect, Responder},
};
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
@ -95,18 +99,27 @@ fn deauthenticated(uri: &rocket::http::uri::Origin) -> &'static str {
} }
#[get("/")] #[get("/")]
fn login() -> Option<Redirect> { fn login() -> RedirOrStatic {
let state_random: String = rand::thread_rng() let state_random: String = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric) .sample_iter(&rand::distributions::Alphanumeric)
.take(16) .take(16)
.map(char::from) .map(char::from)
.collect(); .collect();
let secrets = SECRETS.as_ref()?; SECRETS.as_ref().and_then(|secrets| {
rocket::http::uri::Absolute::parse_owned(format!(
rocket::http::uri::Absolute::parse_owned(format!("https://auth.tesla.com/oauth2/v3/authorize?&client_id={}&locale=en-US&prompt=login&redirect_uri={}&response_type=code&scope=openid%20vehicle_device_data%20offline_access%20user_data%20vehicle_cmds%20vehicle_charging_cmds&state={state_random}", secrets.client_id, secrets.redirect_uri)) "https://auth.tesla.com/oauth2/v3/authorize?&client_id={}&locale=en-US&prompt=login&redirect_uri={}&response_type=code&scope=openid%20vehicle_device_data%20offline_access%20user_data%20vehicle_cmds%20vehicle_charging_cmds&state={state_random}",
.ok() secrets.client_id,
secrets.redirect_uri
))
.some_or_print_with_context("failed to parse url")
})
.map(Redirect::to) .map(Redirect::to)
.map(Into::into)
.unwrap_or(format!(
"could not create redirect: secrets {} exist",
if SECRETS.is_some() { "does" } else { "does not" }
).into())
} }
#[launch] #[launch]
@ -124,3 +137,29 @@ fn rocket() -> _ {
rocket::build().mount("/", routes![authenticated, deauthenticated, login]) rocket::build().mount("/", routes![authenticated, deauthenticated, login])
} }
enum RedirOrStatic {
Redirect(Box<Redirect>),
Static(String),
}
impl<'r> Responder<'r, 'static> for RedirOrStatic {
fn respond_to(self, request: &'r Request<'_>) -> rocket::response::Result<'static> {
match self {
RedirOrStatic::Redirect(r) => r.respond_to(request),
RedirOrStatic::Static(s) => s.respond_to(request),
}
}
}
impl From<String> for RedirOrStatic {
fn from(value: String) -> Self {
Self::Static(value)
}
}
impl From<Redirect> for RedirOrStatic {
fn from(value: Redirect) -> Self {
Self::Redirect(Box::new(value))
}
}