Skip to content

Commit

Permalink
Add Basic authentication for remote feeds.
Browse files Browse the repository at this point in the history
  • Loading branch information
mavnn authored and jmarnold committed Oct 22, 2013
1 parent ec67490 commit aa65079
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ripple/Model/Feed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public Feed(string url, UpdateMode mode, NugetStability stability)
public UpdateMode Mode { get; set; }
[XmlAttribute]
public NugetStability Stability { get; set; }
[XmlAttribute]
public string Password { get; set; }
[XmlAttribute]
public string Username { get; set; }

public INugetFeed GetNugetFeed()
{
Expand Down
11 changes: 11 additions & 0 deletions src/ripple/Model/FeedRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FubuCore;
using FubuCore.Util;
using NuGet;
using ripple.Nuget;

namespace ripple.Model
Expand Down Expand Up @@ -46,6 +48,15 @@ private INugetFeed buildFeed(Feed feed)
return buildFileSystemFeed(feed);
}

if (!String.IsNullOrEmpty(feed.Username) && !String.IsNullOrEmpty(feed.Password))
{
if (HttpClient.DefaultCredentialProvider.GetType() != NugetCredentialsProvider.Instance.GetType())
{
HttpClient.DefaultCredentialProvider = NugetCredentialsProvider.Instance;
}
NugetCredentialsProvider.Instance.AddCredentials(feed.Url, new NetworkCredential(feed.Username, feed.Password));
}

if (feed.Mode == UpdateMode.Fixed)
{
return new NugetFeed(feed.Url, feed.Stability);
Expand Down
53 changes: 53 additions & 0 deletions src/ripple/Nuget/NugetCredentialsProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using NuGet;

namespace ripple.Nuget
{
public class NugetCredentialsProvider : ICredentialProvider
{
private readonly Dictionary<string, ICredentials> _credentials = new Dictionary<string, ICredentials>();
private static readonly Lazy<NugetCredentialsProvider> lazy = new Lazy<NugetCredentialsProvider>(() => new NugetCredentialsProvider());

private NugetCredentialsProvider() {}

public static NugetCredentialsProvider Instance { get { return lazy.Value; } }

public void AddCredentials(string key, ICredentials credentials)
{
_credentials.Add(key, credentials);
}

public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
{
return _credentials[uri.OriginalString];
}

public bool TryGetCredentials(string uri, out ICredentials credentials)
{
if (_credentials.ContainsKey(uri))
{
credentials = _credentials[uri];
return true;
}
credentials = null;
return false;
}

public bool TryGetRootCredentials(string uri, out ICredentials credentials)
{
var matchingRoot =
_credentials.Keys
.SingleOrDefault(credUrl => uri.StartsWith(credUrl, StringComparison.InvariantCultureIgnoreCase));
if (!String.IsNullOrEmpty(matchingRoot))
{
credentials = _credentials[matchingRoot];
return true;
}
credentials = null;
return false;
}
}
}
5 changes: 5 additions & 0 deletions src/ripple/Nuget/NugetFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ private bool isOnline()
{
using (var client = new WebClient())
{
ICredentials credentials;
if (NugetCredentialsProvider.Instance.TryGetCredentials(_url, out credentials))
{
client.Credentials = credentials;
}
using (var stream = client.OpenRead(_url))
{
return true;
Expand Down
6 changes: 6 additions & 0 deletions src/ripple/Nuget/UrlNugetDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public INugetFile DownloadTo(SolutionMode mode, string filename)
var client = new WebClient();

Console.WriteLine("Downloading {0} to {1}", Url, filename);
ICredentials creds;
if (NugetCredentialsProvider.Instance.TryGetRootCredentials(Url, out creds))
{
client.Credentials = creds;
}

client.DownloadFile(Url, filename);

return new NugetFile(filename, mode);
Expand Down
1 change: 1 addition & 0 deletions src/ripple/ripple.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<Compile Include="Nuget\INugetFilter.cs" />
<Compile Include="Nuget\INugetFinder.cs" />
<Compile Include="Nuget\NoMaxVersionSpecFinder.cs" />
<Compile Include="Nuget\NugetCredentialsProvider.cs" />
<Compile Include="Nuget\NugetProblem.cs" />
<Compile Include="Nuget\NugetResult.cs" />
<Compile Include="Model\NulloDependencyStrategy.cs" />
Expand Down

0 comments on commit aa65079

Please sign in to comment.