Compare commits

..

No commits in common. "e75c62bab5344e21d33e6c751da3925c95bb5ad6" and "247b67b30c637468d96b78042bafce7675cb8c9f" have entirely different histories.

4 changed files with 22 additions and 71 deletions

11
Cargo.lock generated
View file

@ -26,14 +26,6 @@ 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"
@ -1466,9 +1458,8 @@ dependencies = [
[[package]]
name = "tesla-auth-callback-watcher"
version = "0.2.1"
version = "0.2.0"
dependencies = [
"alex-utils",
"env_logger",
"lazy_static",
"log",

View file

@ -1,6 +1,6 @@
[package]
name = "tesla-auth-callback-watcher"
version = "0.2.1"
version = "0.2.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -12,4 +12,3 @@ 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.1
pkgver=0.2.0
pkgrel=1
pkgdesc="server for tesla auth callbacks"
arch=('x86_64' 'aarch64')

View file

@ -1,31 +1,11 @@
use alex_utils::PrintErrors;
use lazy_static::lazy_static;
use rand::Rng;
use reqwest::header;
use rocket::{
request::Request,
response::{Redirect, Responder},
};
use rocket::response::Redirect;
#[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,
@ -115,51 +95,32 @@ fn deauthenticated(uri: &rocket::http::uri::Origin) -> &'static str {
}
#[get("/")]
fn login() -> RedirOrStatic {
fn login() -> Option<Redirect> {
let state_random: String = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(16)
.map(char::from)
.collect();
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")
})
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()
.map(Redirect::to)
.map(Into::into)
.unwrap_or(format!(
"could not create redirect: secrets {} exist",
if SECRETS.is_some() { "does" } else { "does not" }
).into())
}
enum RedirOrStatic {
Redirect(Box<Redirect>),
Static(String),
}
#[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();
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))
}
rocket::build().mount("/", routes![authenticated, deauthenticated, login])
}