Compare commits

...

2 commits

Author SHA1 Message Date
Alex Janka e75c62bab5 reorganise 2024-02-12 09:54:06 +11:00
Alex Janka 83b6d25278 better logging for redirect 2024-02-12 09:52:53 +11:00
4 changed files with 72 additions and 23 deletions

11
Cargo.lock generated
View file

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

View file

@ -1,6 +1,6 @@
[package]
name = "tesla-auth-callback-watcher"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
# 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"
lazy_static = "1.4"
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>
pkgname=tesla-auth-callback-watcher
pkgver=0.2.0
pkgver=0.2.1
pkgrel=1
pkgdesc="server for tesla auth callbacks"
arch=('x86_64' 'aarch64')

View file

@ -1,11 +1,31 @@
use alex_utils::PrintErrors;
use lazy_static::lazy_static;
use rand::Rng;
use reqwest::header;
use rocket::response::Redirect;
use rocket::{
request::Request,
response::{Redirect, Responder},
};
#[macro_use]
extern crate rocket;
#[launch]
fn rocket() -> _ {
env_logger::builder()
.format_module_path(false)
.format_timestamp(
if std::env::var("LOG_TIMESTAMP").is_ok_and(|v| v == "false") {
None
} else {
Some(env_logger::TimestampPrecision::Seconds)
},
)
.init();
rocket::build().mount("/", routes![authenticated, deauthenticated, login])
}
struct Secrets {
client_id: String,
client_secret: String,
@ -95,32 +115,51 @@ fn deauthenticated(uri: &rocket::http::uri::Origin) -> &'static str {
}
#[get("/")]
fn login() -> Option<Redirect> {
fn login() -> RedirOrStatic {
let state_random: String = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(16)
.map(char::from)
.collect();
let secrets = SECRETS.as_ref()?;
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))
.ok()
SECRETS.as_ref().and_then(|secrets| {
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
))
.some_or_print_with_context("failed to parse url")
})
.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]
fn rocket() -> _ {
env_logger::builder()
.format_module_path(false)
.format_timestamp(
if std::env::var("LOG_TIMESTAMP").is_ok_and(|v| v == "false") {
None
} else {
Some(env_logger::TimestampPrecision::Seconds)
},
)
.init();
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))
}
}