diff --git a/Source/ProofOfConcept/Program.cs b/Source/ProofOfConcept/Program.cs index 9b2025d..3aeaaa5 100644 --- a/Source/ProofOfConcept/Program.cs +++ b/Source/ProofOfConcept/Program.cs @@ -172,8 +172,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 (IHttpContextAccessor contextAccessor) => await (contextAccessor.HttpContext!).ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/Tesla" })); - app.MapGet("/KeyPairing", () => Results.Redirect("https://tesla.com/_ak/tesla-connector.automatic-parking.app")); - app.MapGet("/KeyPairing2", () => Results.Redirect("https://tesla.com/_ak/automatic-parking.app")); + app.MapGet("/KeyPairing", () => Results.Redirect("https://tesla.com/_ak/automatic-parking.app")); app.MapGet("/Tokens", async (IHttpContextAccessor httpContextAccessor) => { var ctx = httpContextAccessor.HttpContext; @@ -252,9 +251,22 @@ if (app.Environment.IsDevelopment()) //Get cars VehiclesEnvelope? vehiclesEnvelope = await client.GetFromJsonAsync("/api/1/vehicles"); - string[] vins = vehiclesEnvelope?.Response.Select(x => x.Vin ?? "").Where(v => !String.IsNullOrWhiteSpace(v)).ToArray() ?? Array.Empty(); - logger.LogCritical("User has access to {count} cars: {vins}", vins.Length, String.Join(", ", vins)); + string[] vinNumbers = vehiclesEnvelope?.Response.Select(x => x.Vin ?? "").Where(v => !String.IsNullOrWhiteSpace(v)).ToArray() ?? Array.Empty(); + logger.LogCritical("User has access to {count} cars: {vins}", vinNumbers.Length, String.Join(", ", vinNumbers)); + if (vinNumbers.Length == 0) + return Results.Ok("No cars found"); + + //Check if key pairing is required + var requestObject = new { vins = vinNumbers }; + HttpResponseMessage statusResponse = await client.PostAsJsonAsync("/api/1/vehicles/fleet_status", requestObject); + string statusResponseContent = await statusResponse.Content.ReadAsStringAsync(); + logger.LogTrace("Status response: {statusResponseContent}", statusResponseContent); + + FleetResponse? fleetResponse = JsonSerializer.Deserialize(statusResponseContent); + + if (!fleetResponse?.KeyPairedVins.Any() ?? false) + return Results.Redirect("/KeyPairing"); //Get CA from validate server file string fileContent = await File.ReadAllTextAsync("Resources/validate_server.json"); @@ -262,7 +274,7 @@ if (app.Environment.IsDevelopment()) TelemetryConfigRequest configRequest = new TelemetryConfigRequest() { - Vins = new List(vins), + Vins = new List(vinNumbers), Config = new TelemetryConfig() { Hostname = "tesla-connector.automatic-parking.app", @@ -280,9 +292,6 @@ if (app.Environment.IsDevelopment()) }; logger.LogInformation("Config request: {configRequest}", JsonSerializer.Serialize(configRequest, new JsonSerializerOptions() { WriteIndented = true })); - if (vins.Length == 0) - return Results.Ok("No cars found"); - HttpResponseMessage response = await client.PostAsJsonAsync("/api/1/vehicles/fleet_telemetry_config", configRequest); return Results.Ok(response.Content.ReadAsStringAsync()); });