TelemetryPublisherFirestore.cs: Force re-authorization on failure
This commit is contained in:
parent
cbd12c5304
commit
b3dddaacd2
1 changed files with 19 additions and 14 deletions
|
@ -32,7 +32,7 @@ namespace NucuCar.Telemetry.Publishers
|
||||||
protected HttpClient HttpClient;
|
protected HttpClient HttpClient;
|
||||||
|
|
||||||
private string _idToken;
|
private string _idToken;
|
||||||
private DateTime _nextExpiresTime;
|
private DateTime _authorizationExpiryTime;
|
||||||
|
|
||||||
// Variables used for authentication
|
// Variables used for authentication
|
||||||
private readonly string _webEmail;
|
private readonly string _webEmail;
|
||||||
|
@ -74,17 +74,6 @@ namespace NucuCar.Telemetry.Publishers
|
||||||
|
|
||||||
private async Task SetupAuthorization()
|
private async Task SetupAuthorization()
|
||||||
{
|
{
|
||||||
// If there are no credentials or partial credentials supplies there must be no authorization.
|
|
||||||
if (_webApiKey == null || _webEmail == null || _webPassword == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Check if the token is about to expire in the next 5 minutes.
|
|
||||||
if (DateTime.UtcNow.AddMinutes(5) < _nextExpiresTime)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://cloud.google.com/identity-platform/docs/use-rest-api#section-sign-in-email-password
|
// https://cloud.google.com/identity-platform/docs/use-rest-api#section-sign-in-email-password
|
||||||
var requestUrl = $"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={_webApiKey}";
|
var requestUrl = $"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={_webApiKey}";
|
||||||
var data = new Dictionary<string, object>()
|
var data = new Dictionary<string, object>()
|
||||||
|
@ -102,7 +91,7 @@ namespace NucuCar.Telemetry.Publishers
|
||||||
_idToken = jsonContent.GetProperty("idToken").ToString();
|
_idToken = jsonContent.GetProperty("idToken").ToString();
|
||||||
// Setup next expire.
|
// Setup next expire.
|
||||||
var expiresIn = double.Parse(jsonContent.GetProperty("expiresIn").ToString());
|
var expiresIn = double.Parse(jsonContent.GetProperty("expiresIn").ToString());
|
||||||
_nextExpiresTime = DateTime.UtcNow.AddSeconds(expiresIn);
|
_authorizationExpiryTime = DateTime.UtcNow.AddSeconds(expiresIn);
|
||||||
HttpClient.Authorization(_idToken);
|
HttpClient.Authorization(_idToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -111,6 +100,22 @@ namespace NucuCar.Telemetry.Publishers
|
||||||
Logger?.LogDebug($"{response?.Content}");
|
Logger?.LogDebug($"{response?.Content}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task CheckAndSetupAuthorization()
|
||||||
|
{
|
||||||
|
// If there are no credentials or partial credentials supplies there must be no authorization.
|
||||||
|
if (_webApiKey == null || _webEmail == null || _webPassword == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Check if the token is about to expire in the next 5 minutes.
|
||||||
|
if (DateTime.UtcNow.AddMinutes(5) < _authorizationExpiryTime)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await SetupAuthorization();
|
||||||
|
}
|
||||||
|
|
||||||
public override async Task PublishAsync(CancellationToken cancellationToken)
|
public override async Task PublishAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
@ -124,7 +129,7 @@ namespace NucuCar.Telemetry.Publishers
|
||||||
HttpResponseMessage responseMessage = null;
|
HttpResponseMessage responseMessage = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await SetupAuthorization();
|
await CheckAndSetupAuthorization();
|
||||||
responseMessage = await HttpClient.PostAsync("", data);
|
responseMessage = await HttpClient.PostAsync("", data);
|
||||||
}
|
}
|
||||||
// ArgumentException occurs during json serialization errors.
|
// ArgumentException occurs during json serialization errors.
|
||||||
|
|
Loading…
Reference in a new issue