Skip to content

Commit

Permalink
Merge pull request #24 from 64Soft/develop
Browse files Browse the repository at this point in the history
Added some error handling when http server cannot be started
  • Loading branch information
64Soft authored Aug 20, 2021
2 parents 5868959 + f6a9a38 commit f612488
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 41 deletions.
22 changes: 18 additions & 4 deletions src/NRatings.Client/Auxiliary/AuthHttpServer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
Expand All @@ -15,19 +16,32 @@ public class AuthHttpServer : IDisposable

public AuthHttpServer()
{
this.http = new HttpListener();
this.http.Prefixes.Add(serverUri);
this.http.Start();
try
{
this.http = new HttpListener();
this.http.Prefixes.Add(serverUri);
this.http.Start();
}
catch (Exception ex)
{
this.http = null;
Debug.WriteLine(ex);
}
}

public bool IsRunning() => this.http?.IsListening ?? false;

public async Task<HttpListenerContext> GetHttpListenerContextAsync()
{
if (this.http == null)
throw new Exception("Local authentication server not started");

return await this.http.GetContextAsync();
}

public void Dispose()
{
this.http.Stop();
this.http?.Stop();
}
}
}
59 changes: 33 additions & 26 deletions src/NRatings.Client/Domain/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,46 @@ public static class UserManager

public static async Task<UserLoginResult> LoginAsync(Form callingForm = null)
{
//first try to use a possible refresh token to obtain a new access token
await RefreshAccessToken();

if(HasValidAccessToken())
return new UserLoginResult(true);

//if user previously started login process but closed browser, an existing NativeBrowser object is still awaiting the result. If that is the case, just reopen the browser with the startUrl
if (loginOngoing)
Process.Start(nativeBrowser.StartUrl);
else
try
{
loginOngoing = true;
var loginResult = await GetAuth0Client(callingForm).LoginAsync(extraParameters: GetAuth0Params());
loginOngoing = false;
//first try to use a possible refresh token to obtain a new access token
await RefreshAccessToken();

if (!loginResult.IsError)
{
if (loginResult.User?.Identity is ClaimsIdentity identity)
{
user = identity;
Program.UserSettings.SaveAccessToken(loginResult.AccessToken, loginResult.AccessTokenExpiration);
Program.UserSettings.SaveRefreshToken(loginResult.RefreshToken);
if (HasValidAccessToken())
return new UserLoginResult(true);

return new UserLoginResult(true);
}
}
//if user previously started login process but closed browser, an existing NativeBrowser object is still awaiting the result. If that is the case, just reopen the browser with the startUrl
if (loginOngoing)
Process.Start(nativeBrowser.StartUrl);
else
{
return new UserLoginResult(false, loginResult.Error);
loginOngoing = true;
var loginResult = await GetAuth0Client(callingForm).LoginAsync(extraParameters: GetAuth0Params());
loginOngoing = false;

if (!loginResult.IsError)
{
if (loginResult.User?.Identity is ClaimsIdentity identity)
{
user = identity;
Program.UserSettings.SaveAccessToken(loginResult.AccessToken, loginResult.AccessTokenExpiration);
Program.UserSettings.SaveRefreshToken(loginResult.RefreshToken);

return new UserLoginResult(true);
}
}
else
{
return new UserLoginResult(false, loginResult.Error);
}
}
}

return null;
return null;
}
catch (Exception ex)
{
return new UserLoginResult(false, ex.Message);
}
}


Expand Down
12 changes: 10 additions & 2 deletions src/NRatings.Client/GUI/frmUserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,16 @@ private async void butLogin_Click(object sender, EventArgs e)

private async void butLogout_Click(object sender, EventArgs e)
{
await UserManager.LogOutAsync(this);
await SetUserInfoAsync();
try
{
await UserManager.LogOutAsync(this);
await SetUserInfoAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error on logout", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
}
}
34 changes: 25 additions & 9 deletions src/NRatings.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Configuration;
using System.Net;
using System.Windows.Forms;
using NRatings.Client.Auxiliary;
Expand All @@ -18,22 +19,37 @@ static class Program
[STAThread]
static void Main()
{
using (HttpServer = new AuthHttpServer())
try
{
UserSettingsManager.CreateFoldersIfNeeded();
using (HttpServer = new AuthHttpServer())
{
if (!HttpServer.IsRunning())
MessageBox.Show(
$@"NRatings could not start a local http server on {ConfigurationManager.AppSettings["AuthHttpServer"]} to manage authentication calls. You will not be able to fetch racing data.{Environment.NewLine}
Make sure this port is not used by any other application on your PC when you run NRatings.{Environment.NewLine}
You can use TCPView (https://docs.microsoft.com/en-us/sysinternals/downloads/tcpview) to determine which other app is using this port.",
"Error starting authentication mechanism", MessageBoxButtons.OK, MessageBoxIcon.Error);

UserSettings = UserSettings.Read(); //READ THE USERSETTINGS FIRST
UserSettingsManager.CreateFoldersIfNeeded();

if (UserSettings == null)
UserSettings = new UserSettings();
UserSettings = UserSettings.Read(); //READ THE USERSETTINGS FIRST

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
if (UserSettings == null)
UserSettings = new UserSettings();

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Application.Run(new frmNR2003Ratings());
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new frmNR2003Ratings());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
}
}

0 comments on commit f612488

Please sign in to comment.