Compare commits
No commits in common. "e75c62bab5344e21d33e6c751da3925c95bb5ad6" and "247b67b30c637468d96b78042bafce7675cb8c9f" have entirely different histories.
e75c62bab5
...
247b67b30c
11
Cargo.lock
generated
11
Cargo.lock
generated
|
@ -26,14 +26,6 @@ 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"
|
||||||
|
@ -1466,9 +1458,8 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tesla-auth-callback-watcher"
|
name = "tesla-auth-callback-watcher"
|
||||||
version = "0.2.1"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"alex-utils",
|
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"log",
|
"log",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tesla-auth-callback-watcher"
|
name = "tesla-auth-callback-watcher"
|
||||||
version = "0.2.1"
|
version = "0.2.0"
|
||||||
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,4 +12,3 @@ 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" }
|
|
||||||
|
|
|
@ -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.1
|
pkgver=0.2.0
|
||||||
pkgrel=1
|
pkgrel=1
|
||||||
pkgdesc="server for tesla auth callbacks"
|
pkgdesc="server for tesla auth callbacks"
|
||||||
arch=('x86_64' 'aarch64')
|
arch=('x86_64' 'aarch64')
|
||||||
|
|
77
src/main.rs
77
src/main.rs
|
@ -1,31 +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::{
|
use rocket::response::Redirect;
|
||||||
request::Request,
|
|
||||||
response::{Redirect, Responder},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
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 {
|
struct Secrets {
|
||||||
client_id: String,
|
client_id: String,
|
||||||
client_secret: String,
|
client_secret: String,
|
||||||
|
@ -115,51 +95,32 @@ fn deauthenticated(uri: &rocket::http::uri::Origin) -> &'static str {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn login() -> RedirOrStatic {
|
fn login() -> Option<Redirect> {
|
||||||
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();
|
||||||
|
|
||||||
SECRETS.as_ref().and_then(|secrets| {
|
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}",
|
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))
|
||||||
secrets.client_id,
|
.ok()
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum RedirOrStatic {
|
#[launch]
|
||||||
Redirect(Box<Redirect>),
|
fn rocket() -> _ {
|
||||||
Static(String),
|
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 {
|
rocket::build().mount("/", routes![authenticated, deauthenticated, login])
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue