Add authorization and key-pairing
All checks were successful
Build, Push and Run Container / build (push) Successful in 30s

This commit is contained in:
2025-08-16 20:40:27 +02:00
parent 8c801c88ce
commit a7ea7ff632
4 changed files with 81 additions and 6 deletions

View File

@@ -11,7 +11,30 @@ using SzakatsA.Result;
namespace ProofOfConcept.Services;
public class TeslaAuthenticatorService
public interface ITeslaAuthenticatorService
{
/// Asynchronously retrieves an authentication token from the Tesla partner API.
/// This method sends a POST request with client credentials to obtain an
/// OAuth2 access token that grants access to Tesla partner services.
/// <return>Returns a string containing the authentication token received from the API.</return>
/// <exception cref="HttpRequestException">Thrown when the HTTP request to the API fails.</exception>
Task<Result<Token>> AcquirePartnerAuthenticationTokenAsync();
/// Retrieves a cached partner authentication token or acquires a new one if not available.
/// This method first attempts to fetch the token from the memory cache. If the token is not found
/// or has expired, it invokes the method to retrieve a new token from the Tesla partner API and stores it in the cache.
/// The cached token is set to expire slightly earlier than its actual expiration time to avoid unexpected token expiry during usage.
/// <return>Returns a Token object containing the authentication token and its expiration details.</return>
/// <exception cref="HttpRequestException">Thrown when the HTTP request to acquire a new partner token fails.</exception>
/// <exception cref="Exception">Thrown for any unexpected errors during the token retrieval or caching process.</exception>
Task<Result<Token>> GetPartnerAuthenticationTokenAsync();
Task<Result> RegisterApplicationAsync(CancellationToken cancellationToken = default(CancellationToken));
Task<Result<bool>> CheckApplicationRegistrationAsync(CancellationToken cancellationToken = default(CancellationToken));
string GetAplicationAuthorizationURL();
}
public class TeslaAuthenticatorService : ITeslaAuthenticatorService
{
private readonly ILogger<TeslaAuthenticatorService> logger;
private readonly TeslaAuthenticatorServiceConfiguration configuration;
@@ -281,10 +304,10 @@ public class TeslaAuthenticatorService
StringBuilder sb = new StringBuilder();
sb.Append("https://auth.tesla.com/oauth2/v3/authorize?response_type=code");
sb.AppendFormat("&client_id={0}", this.configuration.ClientID);
sb.AppendFormat("&redirect_uri={0}");
sb.AppendFormat("&redirect_uri={0}", "https://automatic-parking.app/token-exchange");
sb.AppendFormat("&scope=openid offline_access vehicle_device_data vehicle_location");
sb.AppendFormat("&state=1234567890");
sb.AppendFormat("&nonce=1234567890");
sb.AppendFormat("&state={0}", "");
sb.AppendFormat("&nonce={0}", "");
sb.AppendFormat("&prompt_missing_scopes=true");
sb.AppendFormat("&require_requested_scopes=true");
sb.AppendFormat("&show_keypair_step=true");