Skip to content

Commit

Permalink
gergo/errorLoggingInAddAcount (#2111)
Browse files Browse the repository at this point in the history
feat(core): add detailed logging to the add account flow in core
  • Loading branch information
gjedlicska authored Feb 2, 2023
1 parent f2ea0fd commit df3a324
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions Core/Core/Credentials/AccountManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,17 @@ public static void ChangeDefaultAccount(string id)
/// <returns></returns>
public static async Task AddAccount(string server = "")
{
Log.Debug("Starting to add account for {serverUrl}", server);
server = server.TrimEnd(new[] { '/' });

if (string.IsNullOrEmpty(server))
if (string.IsNullOrEmpty(server)) {
server = GetDefaultServerUrl();
Log.Debug("Changed server url to the default usl {serverUrl}", server);
}

var accessCode = "";
var challenge = GenerateChallenge();
Log.Debug("Starting auth process for {server}/authn/verify/sca/{challenge}", server, challenge);
Process.Start(
new ProcessStartInfo($"{server}/authn/verify/sca/{challenge}") { UseShellExecute = true }
);
Expand All @@ -359,23 +363,25 @@ public static async Task AddAccount(string server = "")
{
if (!HttpListener.IsSupported)
{
Console.WriteLine(
Log.Warning(
"Windows XP SP2 or Server 2003 is required to use the HttpListener class."
);
return;
}

listener = new HttpListener();
listener.Prefixes.Add("http://localhost:29363/");
var localUrl = "http://localhost:29363/";
listener.Prefixes.Add(localUrl);

listener.Start();
Console.WriteLine("Listening...");
Log.Debug("Listening for auth redirects on {localUrl}", localUrl);
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;

accessCode = request.QueryString["access_code"];
Log.Debug("Got access code {accessCode}", accessCode);
var message = "";
if (accessCode != null)
{
Expand All @@ -395,19 +401,25 @@ public static async Task AddAccount(string server = "")
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
Log.Debug("Processed finished processing the access code.");
listener.Stop();
}
catch (Exception ex) { }
catch (Exception ex)
{
Log.Error(ex, "Getting access code flow failed with {exceptionMessage}", ex.Message);
}
});

//Timeout
if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
{
Log.Information("Local auth flow completed within the timeout window. Access code is {accessCode}", accessCode);
// task completed within timeout
}
else
{
// nada
Log.Warning("Local auth flow failed to complete within the timeout window. Access code is {accessCode}", accessCode);
}

if (string.IsNullOrEmpty(accessCode))
Expand All @@ -425,7 +437,7 @@ public static async Task AddAccount(string server = "")
serverInfo = userResponse.serverInfo,
userInfo = userResponse.user
};

Log.Information("Successfully created account for {serverUrl}", server);
account.serverInfo.url = server;

//if the account already exists it will not be added again
Expand Down

0 comments on commit df3a324

Please sign in to comment.