From 25f869851f994a3e89bb15ab51ef10241853c20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szak=C3=A1ts=20Alp=C3=A1r=20Zsolt?= Date: Wed, 13 Aug 2025 18:54:09 +0200 Subject: [PATCH] Correct JSON --- Source/ProofOfConcept/Models/Token.cs | 18 ++++++++---------- Source/ProofOfConcept/Program.cs | 16 ++++++++++++++++ .../Services/TeslaAuthenticatorService.cs | 10 +++++++++- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Source/ProofOfConcept/Models/Token.cs b/Source/ProofOfConcept/Models/Token.cs index 85cbffa..b4b67b1 100644 --- a/Source/ProofOfConcept/Models/Token.cs +++ b/Source/ProofOfConcept/Models/Token.cs @@ -1,15 +1,13 @@ +using System.Text.Json.Serialization; + namespace ProofOfConcept.Models; public record Token { - public string AccessToken { get; init; } - public DateTimeOffset Expires { get; init; } - public string TokenType { get; init; } - - public Token(string AccessToken, int ExpiresIn, string TokenType, DateTimeOffset? received = null) - { - this.AccessToken = AccessToken; - this.Expires = received?.AddSeconds(ExpiresIn) ?? DateTimeOffset.Now.AddSeconds(ExpiresIn); - this.TokenType = TokenType; - } + public string AccessToken { get; init; } = String.Empty; + public DateTimeOffset Expires { get; init; } = DateTimeOffset.Now; + public string TokenType { get; init; } = String.Empty; + + //For JSON deserialization + public int ExpiresIn { get => (int)DateTimeOffset.Now.Subtract(Expires).TotalSeconds; init => Expires = DateTimeOffset.Now.AddSeconds(value); } } \ No newline at end of file diff --git a/Source/ProofOfConcept/Program.cs b/Source/ProofOfConcept/Program.cs index d3ac4f1..6aae7a5 100644 --- a/Source/ProofOfConcept/Program.cs +++ b/Source/ProofOfConcept/Program.cs @@ -1,5 +1,7 @@ +using System.Text.Json; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; +using ProofOfConcept.Models; using ProofOfConcept.Services; using ProofOfConcept.Utilities; @@ -33,6 +35,20 @@ if (app.Environment.IsDevelopment()) { app.MapOpenApi(); app.MapGet("/GetPartnerAuthenticationToken", ([FromServices] TeslaAuthenticatorService service) => service.GetPartnerAuthenticationTokenAsync()); + app.MapGet("/PartnerToken", ([FromQueryAttribute] string json, [FromServices] IMemoryCache memoryCache) => + { + var serializerOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower + }; + + Token? token = JsonSerializer.Deserialize(json, serializerOptions); + if (token is not null) + memoryCache.Set(Keys.TeslaPartnerToken, token, token.Expires.Subtract(TimeSpan.FromSeconds(5))); + + return JsonSerializer.Serialize(token, new JsonSerializerOptions() { WriteIndented = true }); + }); app.MapGet("/CheckRegisteredApplication", ([FromServices] TeslaAuthenticatorService service) => service.CheckApplicationRegistrationAsync()); app.MapGet("/RegisterApplication", ([FromServices] TeslaAuthenticatorService service) => service.RegisterApplicationAsync()); } diff --git a/Source/ProofOfConcept/Services/TeslaAuthenticatorService.cs b/Source/ProofOfConcept/Services/TeslaAuthenticatorService.cs index 5038589..8fb8ef9 100644 --- a/Source/ProofOfConcept/Services/TeslaAuthenticatorService.cs +++ b/Source/ProofOfConcept/Services/TeslaAuthenticatorService.cs @@ -18,12 +18,20 @@ public class TeslaAuthenticatorService const string authEndpointURL = "https://fleet-auth.prd.vn.cloud.tesla.com/oauth2/v3/token"; const string euBaseURL = "https://fleet-api.prd.eu.vn.cloud.tesla.com"; + private JsonSerializerOptions serializerOptions; + public TeslaAuthenticatorService(ILogger logger, IOptions options, IHttpClientFactory httpClientFactory, IMemoryCache memoryCache) { this.logger = logger; this.httpClientFactory = httpClientFactory; this.memoryCache = memoryCache; this.configuration = options.Value; + + serializerOptions = new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower + }; } /// Asynchronously retrieves an authentication token from the Tesla partner API. @@ -39,7 +47,7 @@ public class TeslaAuthenticatorService // URL to request the token const string audience = euBaseURL; - + // Prepare the form-urlencoded body Dictionary formData = new Dictionary {