From 6ac6d05f5feba7a77e5981ae7816b1e8a2194e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szak=C3=A1ts=20Alp=C3=A1r=20Zsolt?= Date: Wed, 15 Oct 2025 19:13:50 +0200 Subject: [PATCH] Simplifies authentication logic and adds VIN. Removes temporary test endpoints and refactors the authorize endpoint. Adds support for VIN notifications for a new vehicle by adding it to the dictionary. --- Source/ProofOfConcept/Program.cs | 104 +----------------- .../Services/MessageProcessor.cs | 6 +- 2 files changed, 6 insertions(+), 104 deletions(-) diff --git a/Source/ProofOfConcept/Program.cs b/Source/ProofOfConcept/Program.cs index d8be6ce..273fcbc 100644 --- a/Source/ProofOfConcept/Program.cs +++ b/Source/ProofOfConcept/Program.cs @@ -174,7 +174,7 @@ if (app.Environment.IsDevelopment()) }); app.MapGet("/CheckRegisteredApplication", ([FromServices] ITeslaAuthenticatorService service) => service.CheckApplicationRegistrationAsync()); app.MapGet("/RegisterApplication", ([FromServices] ITeslaAuthenticatorService service) => service.RegisterApplicationAsync()); - app.MapGet("/Authorize", async ([FromQuery] string redirect, [FromServices] IHttpContextAccessor contextAccessor) => await (contextAccessor.HttpContext!).ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = redirect })); + app.MapGet("/Authorize", async ([FromQuery] string redirect, [FromServices] IHttpContextAccessor contextAccessor) => await (contextAccessor.HttpContext!).ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties() { RedirectUri = redirect })); app.MapGet("/KeyPairing", () => Results.Redirect("https://tesla.com/_ak/automatic-parking.app")); app.MapGet("/Tokens", async (IHttpContextAccessor httpContextAccessor) => { @@ -339,108 +339,6 @@ if (app.Environment.IsDevelopment()) }); } -app.MapGet("/RePair", async ([FromServices] ILogger logger, [FromServices] IHttpClientFactory httpClientFactory) => -{ - string access_token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6InFEc3NoM2FTV0cyT05YTTdLMzFWV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BUeQthQ8pFUk9-fDzsZhwkTjrGYtvpQKZK-pn5GCLIKLib4AemsidCtfOlObjgqTd6wf_Tdb2dkbt-ACNIueTcmfXt-eFUZVRySwrvOb5pOWAUkjTUCpW074ySJjj_TDYheQHA9aTZsDJWCUNHC-51qnawiUvh-LwYWasfFhQZQisSfSusCgpGvHRVsyuLbOtd2fQ"; - - HttpClient client = httpClientFactory.CreateClient("InsecureClient"); - client.BaseAddress = new Uri("https://tesla_command_proxy"); - client.DefaultRequestHeaders.Add("Authorization", $"Bearer {access_token}"); - - //Get fleet_status endpoint - string vin = "5YJ3E7EB7KF291652"; - - var resp = await client.DeleteAsync($"/api/1/vehicles/{vin}/fleet_telemetry_config"); - logger.LogInformation("Fleet telemetry remove response ({ResponseStatusCode}): {ResponseContent}): ", resp.StatusCode, await resp.Content.ReadAsStringAsync());; - - //Get CA from validate server file - string fileContent = await File.ReadAllTextAsync("Resources/validate_server.json"); - ValidationModel? vm = JsonSerializer.Deserialize(fileContent); - - TelemetryConfigRequest configRequest = new TelemetryConfigRequest() - { - Vins = new List() { vin }, - Config = new TelemetryConfig() - { - Hostname = "tesla-telemetry.automatic-parking.app", - Port = 443, - CertificateAuthority = vm?.CA ?? "EMPTY", - Fields = new Dictionary() - { - { "Gear", new TelemetryFieldConfig() { IntervalSeconds = 60 } }, - { "Locked", new TelemetryFieldConfig() { IntervalSeconds = 60 } }, - { "DriverSeatOccupied", new TelemetryFieldConfig() { IntervalSeconds = 60 } }, - { "GpsState", new TelemetryFieldConfig() { IntervalSeconds = 60 } }, - { "Location", new TelemetryFieldConfig() { IntervalSeconds = 60 } }, - } - } - }; - logger.LogInformation("Config request: {configRequest}", JsonSerializer.Serialize(configRequest, new JsonSerializerOptions() { WriteIndented = true })); - - HttpResponseMessage response = await client.PostAsJsonAsync("/api/1/vehicles/fleet_telemetry_config", configRequest); - - return Results.Ok(response.Content.ReadAsStringAsync()); -}); - -app.MapGet("/Status", async ([FromServices] ILogger logger, [FromServices] IHttpClient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SSuJ5aI76pcaDg9saxrhIhg17KjmEC4gL90ByDk6P7KUMp_xot0FN1Vtwy3C8_NDltebhZdM2emR5N7QHXdP4OYAQNvHwanRwBUeQthQ8pFUk9-fDzsZhwkTjrGYtvpQKZK-pn5GCLIKLib4AemsidCtfOlObjgqTd6wf_Tdb2dkbt-ACNIueTcmfXt-eFUZVRySwrvOb5pOWAUkjTUCpW074ySJjj_TDYheQHA9aTZsDJWCUNHC-51qnawiUvh-LwYWasfFhQZQisSfSusCgpGvHRVsyuLbOtd2fQ"; - - HttpClient client = httpClientFactory.CreateClient("InsecureClient"); - client.BaseAddress = new Uri("https://tesla_command_proxy"); - client.DefaultRequestHeaders.Add("Authorization", $"Bearer {access_token}"); - - //Get fleet_status endpoint - string vin = "5YJ3E7EB7KF291652"; - - var requestObject = new { vins = new string[] { vin } }; - HttpResponseMessage statusResponse = await client.PostAsJsonAsync("/api/1/vehicles/fleet_status", requestObject); - string statusResponseContent = await statusResponse.Content.ReadAsStringAsync(); - logger.LogTrace("Fleet status response: {statusResponseContent}", statusResponseContent); - - HttpResponseMessage rspmsg = await client.GetAsync($"/api/1/vehicles/{vin}/fleet_telemetry_config"); - string rsp = await rspmsg.Content.ReadAsStringAsync(); - logger.LogInformation("Telemetry config: {response}", rsp); - - HttpResponseMessage responseMessage = await client.GetAsync($"/api/1/vehicles/{vin}/fleet_telemetry_errors"); - string response = await responseMessage.Content.ReadAsStringAsync(); - logger.LogInformation("Telemetry errors: {response}", response); - - StringBuilder sb = new StringBuilder(); - sb.AppendLine("Fleet status:"); - sb.AppendLine(statusResponseContent); - sb.AppendLine(); - sb.AppendLine("Telemetry config:"); - sb.AppendLine(rsp); - sb.AppendLine(); - sb.AppendLine("Telemetry errors:"); - sb.AppendLine(response); - - return Results.Ok(sb.ToString()); -}); - -app.MapGet("/TestStartParking", async ([FromQuery] double latitude, [FromQuery] double longitude, [FromServices] ILogger logger, [FromServices] MessageProcessor messageProcessor) => -{ - logger.LogTrace("Test Start Parking..."); - - await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "location", $"{{\"latitude\":{latitude},\"longitude\":{longitude}}}"); - await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "gear", "P"); - await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "locked", "true"); - await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "driverseatoccupied", "false"); - - logger.LogInformation("All messages sent"); -}); - -app.MapGet("/TestStopParking", async ([FromServices] ILogger logger, [FromServices] MessageProcessor messageProcessor) => -{ - logger.LogTrace("Test Stop Parking..."); - - await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "locked", "false"); - await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "gear", "D"); - - logger.LogInformation("All messages sent"); -}); - app.MapGet("/Zone", async ([FromQuery] double latitude, [FromQuery] double longitude, [FromServices] ILogger logger, [FromServices] ZoneDeterminatorService zoneDeterminator) => { logger.LogTrace("Getting zone for: {latitude}, {longitude}...", latitude, longitude); diff --git a/Source/ProofOfConcept/Services/MessageProcessor.cs b/Source/ProofOfConcept/Services/MessageProcessor.cs index 30bf881..d0eadb9 100644 --- a/Source/ProofOfConcept/Services/MessageProcessor.cs +++ b/Source/ProofOfConcept/Services/MessageProcessor.cs @@ -153,7 +153,11 @@ public class MessageProcessor public class MessageProcessorConfiguration { public string PushoverAPIKey { get; set; } = "acr9fqxafqeqjpr4apryh17m4ak24b"; - public Dictionary VinNotifications { get; set; } = new Dictionary() { { "5YJ3E7EB7KF291652", "u2ouaqqu5gd9f1bq3rmrtwriumaffu"} /*Zoli*/ }; + public Dictionary VinNotifications { get; set; } = new Dictionary() + { + { "5YJ3E7EB7KF291652", "u2ouaqqu5gd9f1bq3rmrtwriumaffu"}, /*Zoli*/ + { "LRW3E7EK4NC482668", "udbz5g2hi24m4wcanx44qqkwf7r1c7" } + }; } file static class DateTimeOffsetExtensions