Skip to content

Commit

Permalink
Fixed Basic Auth support to handle link clicks, redirects, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch Connors authored and Mitch Connors committed Jun 17, 2011
1 parent be36ec5 commit e4fba42
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
16 changes: 9 additions & 7 deletions WebKitBrowserTest/WebBrowserTabPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@

namespace WebKitBrowserTest
{
public class TestScriptObject
{
public void f()
{
MessageBox.Show("Hey!");
}
}


public partial class WebBrowserTabPage : TabPage
{
Expand Down Expand Up @@ -130,5 +124,13 @@ public void Stop()
browser.Stop();
statusLabel.Text = "Stopped";
}
}

public class TestScriptObject
{
public void f()
{
MessageBox.Show("Hey!");
}
}
}
30 changes: 28 additions & 2 deletions WebKitCore/WebKitBrowserCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class WebKitBrowserCore : IWebKitBrowser
private WebDownloadDelegate downloadDelegate;
private WebPolicyDelegate policyDelegate;
private WebUIDelegate uiDelegate;
private WebResourceLoadDelegate resourceLoadDelegate;

#region WebKitBrowser events

Expand Down Expand Up @@ -127,10 +128,28 @@ public class WebKitBrowserCore : IWebKitBrowser

#region Public properties

private string userName;
private string password;
private bool pdPasswordEnabled;

/// <summary>
/// The HTTP Basic Authentication UserName
/// </summary>
public string UserName { get; set; }
public string UserName
{
get
{
return userName;
}
set
{
if (policyDelegate == null)
pdPasswordEnabled = true;
else
this.policyDelegate.pwdEnabled = (!string.IsNullOrEmpty(value));
userName = value;
}
}

/// <summary>
/// The HTTP Basic Authentication Password
Expand Down Expand Up @@ -599,12 +618,15 @@ private void InitializeWebKit()
downloadDelegate = new WebDownloadDelegate();
Marshal.AddRef(Marshal.GetIUnknownForObject(downloadDelegate));

policyDelegate = new WebPolicyDelegate(AllowNavigation, AllowDownloads, AllowNewWindows);
policyDelegate = new WebPolicyDelegate(AllowNavigation, AllowDownloads, AllowNewWindows, this) { pwdEnabled = pdPasswordEnabled };
Marshal.AddRef(Marshal.GetIUnknownForObject(policyDelegate));

uiDelegate = new WebUIDelegate(this);
Marshal.AddRef(Marshal.GetIUnknownForObject(uiDelegate));

resourceLoadDelegate = new WebResourceLoadDelegate();
Marshal.AddRef(Marshal.GetIUnknownForObject(resourceLoadDelegate));

webNotificationCenter = new WebNotificationCenter();
Marshal.AddRef(Marshal.GetIUnknownForObject(webNotificationCenter)); // TODO: find out if this is really needed
webNotificationObserver = new WebNotificationObserver();
Expand All @@ -616,6 +638,7 @@ private void InitializeWebKit()
webView.setFrameLoadDelegate(frameLoadDelegate);
webView.setDownloadDelegate(downloadDelegate);
webView.setUIDelegate(uiDelegate);
webView.setResourceLoadDelegate(resourceLoadDelegate);

webView.setHostWindow(this.host.Handle.ToInt32());

Expand Down Expand Up @@ -894,6 +917,9 @@ public void Navigate(string url)
request.setValue("Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(
string.Format("{0}:{1}", UserName, Password))), "Authorization");




webView.mainFrame().loadRequest((WebURLRequest)request);

activationContext.Deactivate();
Expand Down
21 changes: 21 additions & 0 deletions WebKitCore/WebPolicyDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ internal class WebPolicyDelegate : IWebPolicyDelegate
public bool AllowDownloads;
public bool AllowNewWindows;
public bool AllowNavigation;
private WebKitBrowserCore wbc;
public bool pwdEnabled;

// so that we can load and display the first page
public bool AllowInitialNavigation;



public WebPolicyDelegate(bool AllowNavigation, bool AllowDownloads, bool AllowNewWindows)
{
this.AllowDownloads = AllowDownloads;
Expand All @@ -49,6 +53,11 @@ public WebPolicyDelegate(bool AllowNavigation, bool AllowDownloads, bool AllowNe
AllowInitialNavigation = true;
}

public WebPolicyDelegate(bool AllowNavigation, bool AllowDownloads, bool AllowNewWindows, WebKitBrowserCore b) : this(AllowNavigation, AllowDownloads, AllowNewWindows)
{
wbc = b;
}

#region IWebPolicyDelegate Members

public void decidePolicyForMIMEType(WebView WebView, string type, IWebURLRequest request, webFrame frame, IWebPolicyDecisionListener listener)
Expand All @@ -68,10 +77,22 @@ public void decidePolicyForMIMEType(WebView WebView, string type, IWebURLRequest
}
}


public void decidePolicyForNavigationAction(WebView WebView, CFDictionaryPropertyBag actionInformation, IWebURLRequest request, webFrame frame, IWebPolicyDecisionListener listener)
{
if (AllowNavigation || AllowInitialNavigation)
{
//use basic authentication if username and password are supplied.
if (pwdEnabled && string.IsNullOrEmpty(request.valueForHTTPHeaderField("Authorization")) && wbc!=null)
{

wbc.Navigate(request.url());
listener.ignore();
return;
}

listener.use();
}
else
listener.ignore();
}
Expand Down

0 comments on commit e4fba42

Please sign in to comment.