Skip to content

Commit

Permalink
#89 Reimplement Account Authentication not using internal Web Browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrodan committed Feb 17, 2021
1 parent ac892f7 commit bbb913e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions KeeAnywhere/OAuth2/OidcFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using KeeAnywhere.StorageProviders;
using KeePassLib.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
Expand All @@ -24,8 +25,7 @@ public class OidcFlow
private string m_clientId;
private string m_clientSecret;

public int StartPort { get; set; }
public int EndPort { get; set; }
public IEnumerable<int> Ports { get; set; }

public Action<LoginRequest> PrepareLoginRequest { get; set; }

Expand Down Expand Up @@ -119,7 +119,7 @@ private OidcClientOptions CreateBaseOptions()

private OidcSystemBrowser CreateBrowser()
{
return new OidcSystemBrowser(StartPort, EndPort);
return new OidcSystemBrowser(Ports);
}

private OidcWaitForm CreateWaitForm()
Expand Down
25 changes: 11 additions & 14 deletions KeeAnywhere/OAuth2/OidcSystemBrowser.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using IdentityModel.OidcClient.Browser;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
Expand All @@ -15,30 +17,25 @@ public class OidcSystemBrowser : IBrowser
private readonly string _redirectUri;
private readonly HttpListener _listener;

public OidcSystemBrowser(int minPort = 0, int maxPort = 0)
public OidcSystemBrowser(IEnumerable<int> ports = null)
{
if (!CreateListener(minPort, maxPort, out _redirectUri, out _listener))
if (!CreateListener(out _redirectUri, out _listener, ports))
{
throw new Exception("No unused port found!");
}
}

private static bool CreateListener(int minPort, int maxPort, out string redirectUri, out HttpListener listener)
private static bool CreateListener(out string redirectUri, out HttpListener listener, IEnumerable<int> ports = null)
{
// IANA suggested range for dynamic or private ports
//const int MinPort = 49215;
//const int MaxPort = 65535;
if (minPort == 0)
if (ports == null)
{
minPort = 49215;
// IANA suggested range for dynamic or private ports
//const int MinPort = 49215;
//const int MaxPort = 65535;
ports = Enumerable.Range(49215, 65535);
}

if (maxPort == 0)
{
maxPort = 65535;
}

for (var port = minPort; port < maxPort; port++)
foreach (var port in ports)
{
redirectUri = CreateRedirectUri(port);
listener = new HttpListener();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using db = Dropbox.Api;
using KeeAnywhere.Configuration;
using KeeAnywhere.OAuth2;
using System.Linq;

namespace KeeAnywhere.StorageProviders.Dropbox
{
Expand Down Expand Up @@ -52,7 +53,11 @@ public async Task<AccountConfiguration> CreateAccount()
"files.content.read"
};

var browser = new OidcSystemBrowser(50001, 50005);
var ports = Enumerable.Range(49306, 49315)
.Concat(Enumerable.Range(49996, 50005))
.Concat(Enumerable.Range(63900, 63910));

var browser = new OidcSystemBrowser(ports);

var redirectUri = browser.RedirectUri;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using KeeAnywhere.Configuration;
using KeeAnywhere.OAuth2;
Expand All @@ -16,15 +17,14 @@ public HiDriveStorageConfigurator()
_authenticator = HiDriveHelper.GetAuthenticator();
}


public async Task<AccountConfiguration> CreateAccount_Oidc()
{
var f = new OidcWaitForm();
f.InitEx(StorageType.HiDrive);
f.Show();


var browser = new OidcSystemBrowser(50001, 50020);
var browser = new OidcSystemBrowser(Enumerable.Range(50001, 50020));

var redirectUri = browser.RedirectUri;

Expand Down

0 comments on commit bbb913e

Please sign in to comment.