Skip to content

Commit

Permalink
Codacy issues fix
Browse files Browse the repository at this point in the history
  • Loading branch information
amanpras committed Sep 5, 2024
1 parent 1e6fde5 commit 630b928
Showing 1 changed file with 74 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ limitations under the License.
using Ginger.ValidationRules;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using Microsoft.IdentityModel.Tokens;
using System.Text;

namespace Ginger.ExternalConfigurations
{
Expand All @@ -37,17 +39,16 @@ namespace Ginger.ExternalConfigurations
/// </summary>
public partial class GingerAnalyticsConfigurationPage : GingerUIPage
{
public DateTime validTo;

private GingerAnalyticsConfiguration gingerAnalyticsUserConfig;
public DateTime validTo = DateTime.MinValue;
private GingerAnalyticsConfiguration gingerAnalyticsUserConfig = null;
public GingerAnalyticsConfigurationPage()
{
InitializeComponent();
Init();
}
private void Init()
{
gingerAnalyticsUserConfig = !WorkSpace.Instance.SolutionRepository.GetAllRepositoryItems<GingerAnalyticsConfiguration>().Any() ? new GingerAnalyticsConfiguration() : WorkSpace.Instance.SolutionRepository.GetFirstRepositoryItem<GingerAnalyticsConfiguration>();
gingerAnalyticsUserConfig = WorkSpace.Instance.SolutionRepository.GetAllRepositoryItems<GingerAnalyticsConfiguration>().Count == 0 ? new GingerAnalyticsConfiguration() : WorkSpace.Instance.SolutionRepository.GetFirstRepositoryItem<GingerAnalyticsConfiguration>();
gingerAnalyticsUserConfig.StartDirtyTracking();
SetControls();

Expand Down Expand Up @@ -76,47 +77,55 @@ private void ApplyValidationRules()

private async void xTestConBtn_Click(object sender, RoutedEventArgs e)
{
GingerCoreNET.GeneralLib.General.CreateGingerAnalyticsConfiguration(gingerAnalyticsUserConfig);
if (IsTokenValid())
if (AreRequiredFieldsEmpty())
{
Reporter.ToUser(eUserMsgKey.GingerAnalyticsConnectionSuccess);
Reporter.ToUser(eUserMsgKey.RequiredFieldsEmpty);
return;
}

if (string.IsNullOrEmpty(gingerAnalyticsUserConfig.AccountUrl) || string.IsNullOrEmpty(gingerAnalyticsUserConfig.IdentityServiceURL)
|| string.IsNullOrEmpty(gingerAnalyticsUserConfig.ClientId) || string.IsNullOrEmpty(gingerAnalyticsUserConfig.ClientSecret))
GingerCoreNET.GeneralLib.General.CreateGingerAnalyticsConfiguration(gingerAnalyticsUserConfig);

if (IsTokenValid())
{
Reporter.ToUser(eUserMsgKey.RequiredFieldsEmpty);
Reporter.ToUser(eUserMsgKey.GingerAnalyticsConnectionSuccess);
return;
}

bool isAuthorized = await HandleTokenAuthorization();
ShowConnectionResult(isAuthorized);
}

if (gingerAnalyticsUserConfig != null && string.IsNullOrEmpty(gingerAnalyticsUserConfig.Token))
private bool AreRequiredFieldsEmpty()
{
return string.IsNullOrEmpty(gingerAnalyticsUserConfig.AccountUrl)
|| string.IsNullOrEmpty(gingerAnalyticsUserConfig.IdentityServiceURL)
|| string.IsNullOrEmpty(gingerAnalyticsUserConfig.ClientId)
|| string.IsNullOrEmpty(gingerAnalyticsUserConfig.ClientSecret);
}

private async Task<bool> HandleTokenAuthorization()
{
if (string.IsNullOrEmpty(gingerAnalyticsUserConfig.Token))
{
return await RequestToken(gingerAnalyticsUserConfig.ClientId, gingerAnalyticsUserConfig.ClientSecret, gingerAnalyticsUserConfig.IdentityServiceURL);
}

bool isAuthorized = await RequestToken(gingerAnalyticsUserConfig.ClientId,gingerAnalyticsUserConfig.ClientSecret,
gingerAnalyticsUserConfig.IdentityServiceURL);
return true;
}

if (isAuthorized)
{
Reporter.ToUser(eUserMsgKey.GingerAnalyticsConnectionSuccess);
}
else
{
Reporter.ToUser(eUserMsgKey.GingerAnalyticsConnectionFail);
}
}
else if (gingerAnalyticsUserConfig != null && !string.IsNullOrEmpty(gingerAnalyticsUserConfig.Token))
private static void ShowConnectionResult(bool isAuthorized)
{
if (isAuthorized)
{
Reporter.ToUser(eUserMsgKey.GingerAnalyticsConnectionSuccess);
}
else
{
Reporter.ToUser(eUserMsgKey.GingerAnalyticsConnectionFail);
}

}


private void xClientSecretTextBox_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
if (!EncryptionHandler.IsStringEncrypted(xClientSecretTextBox.ValueTextBox.Text))
Expand All @@ -135,53 +144,61 @@ private void xClientIdTextBox_LostKeyboardFocus(object sender, System.Windows.In
}
}


public async Task<bool> RequestToken(string clientId, string clientSecret, string address)
{
try
{
HttpClientHandler handler = new HttpClientHandler() { UseProxy = false };

var client = new HttpClient(handler);


clientId = ValueExpression.CredentialsCalculation(gingerAnalyticsUserConfig.ClientId);

clientSecret = ValueExpression.CredentialsCalculation(gingerAnalyticsUserConfig.ClientSecret);

address = ValueExpression.CredentialsCalculation(gingerAnalyticsUserConfig.IdentityServiceURL);
HttpClientHandler handler = new HttpClientHandler() { UseProxy = false};

var disco = await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
using (var client = new HttpClient(handler))
{
Address = address,
Policy =
{
RequireHttps = true,
ValidateIssuerName = true
}
});

var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,

ClientId = clientId,
ClientSecret = clientSecret,
});
clientId = ValueExpression.CredentialsCalculation(gingerAnalyticsUserConfig.ClientId);
clientSecret = ValueExpression.CredentialsCalculation(gingerAnalyticsUserConfig.ClientSecret);
address = ValueExpression.CredentialsCalculation(gingerAnalyticsUserConfig.IdentityServiceURL);

var disco = await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
{
Address = address,
Policy =
{
RequireHttps = true,
ValidateIssuerName = true
}
});

if (disco.IsError)
{
Reporter.ToLog(eLogLevel.ERROR, $"Discovery document error: {disco.Error}");
return false;
}

var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = clientId,
ClientSecret = clientSecret
});

if (tokenResponse.IsError)
{
Reporter.ToLog(eLogLevel.ERROR, $"Token request error: {tokenResponse.Error}");
return false;
}

if (tokenResponse.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
validTo = DateTime.UtcNow.AddMinutes(60);
gingerAnalyticsUserConfig.Token = tokenResponse.AccessToken;
return true;
}
else
{
return false;
}
}
catch (HttpRequestException httpEx)
{
Reporter.ToLog(eLogLevel.ERROR, "HTTP request failed", httpEx);
return false;
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.ERROR, "Failed to connect to the server", ex);
Reporter.ToLog(eLogLevel.ERROR, "Unexpected error during token request", ex);
return false;
}
}
Expand Down

0 comments on commit 630b928

Please sign in to comment.