From 3ace89255b77a02af21f4d863ed17534ca3ded6e Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Wed, 22 Apr 2020 14:10:25 +0100 Subject: [PATCH 1/2] Add in token renewal --- .../Services/TransactionDomainService.cs | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs index 4765d650..19de4f53 100644 --- a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Diagnostics.Eventing.Reader; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -308,21 +309,35 @@ private async Task GetMerchant(Guid estateId, return merchant; } - private async Task GetToken(CancellationToken cancellationToken) + /// + /// Gets the token. + /// + /// The cancellation token. + /// + private async Task GetToken(CancellationToken cancellationToken) { + // Get a token to talk to the estate service + String clientId = ConfigurationReader.GetValue("AppSettings", "ClientId"); + String clientSecret = ConfigurationReader.GetValue("AppSettings", "ClientSecret"); + Logger.LogInformation($"Client Id is {clientId}"); + Logger.LogInformation($"Client Secret is {clientSecret}"); + if (this.TokenResponse == null) { - // Get a token to talk to the estate service - String clientId = ConfigurationReader.GetValue("AppSettings", "ClientId"); - String clientSecret = ConfigurationReader.GetValue("AppSettings", "ClientSecret"); - - Logger.LogInformation($"Client Id is {clientId}"); - Logger.LogInformation($"Client Secret is {clientSecret}"); + TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken); + Logger.LogInformation($"Token is {token.AccessToken}"); + return token; + } + if (this.TokenResponse.Expires.UtcDateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(2)) + { + Logger.LogInformation($"Token is about to expire at {this.TokenResponse.Expires.DateTime:O}"); TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken); Logger.LogInformation($"Token is {token.AccessToken}"); - this.TokenResponse = token; + return token; } + + return this.TokenResponse; } /// From 2fd1aee7c276ef2b31336c29d0940b29f2e4e13a Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Wed, 22 Apr 2020 14:24:17 +0100 Subject: [PATCH 2/2] :| --- .../Services/TransactionDomainService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs index 19de4f53..1d1a1930 100644 --- a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs @@ -260,7 +260,7 @@ private async Task AddDeviceToMerchant(Guid estateId, String deviceIdentifier, CancellationToken cancellationToken) { - await this.GetToken(cancellationToken); + this.TokenResponse = await this.GetToken(cancellationToken); // Add the device to the merchant await this.EstateClient.AddDeviceToMerchant(this.TokenResponse.AccessToken, @@ -291,7 +291,7 @@ private String GenerateTransactionReference() private async Task GetEstate(Guid estateId, CancellationToken cancellationToken) { - await this.GetToken(cancellationToken); + this.TokenResponse = await this.GetToken(cancellationToken); EstateResponse estate = await this.EstateClient.GetEstate(this.TokenResponse.AccessToken, estateId, cancellationToken); @@ -302,7 +302,7 @@ private async Task GetMerchant(Guid estateId, Guid merchantId, CancellationToken cancellationToken) { - await this.GetToken(cancellationToken); + this.TokenResponse = await this.GetToken(cancellationToken); MerchantResponse merchant = await this.EstateClient.GetMerchant(this.TokenResponse.AccessToken, estateId, merchantId, cancellationToken);