Basic Authentication Added

pull/4/head
Mark McDowall 12 years ago
parent 339e220cac
commit f1d2e0e6df

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nancy.Authentication.Basic;
using Nancy.Security;
using NzbDrone.Common;
namespace NzbDrone.Api.Authentication
{
public class AuthenticationValidator : IUserValidator
{
private readonly IConfigFileProvider _configFileProvider;
public AuthenticationValidator(IConfigFileProvider configFileProvider)
{
_configFileProvider = configFileProvider;
}
public IUserIdentity Validate(string username, string password)
{
if (_configFileProvider.BasicAuthUsername.Equals(username) &&
_configFileProvider.BasicAuthPassword.Equals(password))
{
return new NzbDroneUser { UserName = username};
}
return null;
}
}
}

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nancy.Security;
namespace NzbDrone.Api.Authentication
{
public class NzbDroneUser : IUserIdentity
{
public string UserName { get; set; }
public IEnumerable<string> Claims { get; set; }
}
}

@ -23,6 +23,9 @@ namespace NzbDrone.Api.ErrorManagement
return;
}
if (statusCode == HttpStatusCode.Unauthorized)
return;
if (context.Response.ContentType == "text/html" || context.Response.ContentType == "text/plain")
context.Response = new ErrorModel
{

@ -1,5 +1,6 @@
using System;
using Nancy;
using Nancy.Security;
namespace NzbDrone.Api.Frontend
{
@ -7,6 +8,7 @@ namespace NzbDrone.Api.Frontend
{
public IndexModule()
{
this.RequiresAuthentication();
//Serve anything that doesn't have an extension
Get[@"/(.*)"] = x => Index();
}

@ -1,12 +1,17 @@
using NLog;
using System;
using NLog;
using Nancy;
using Nancy.Authentication.Basic;
using Nancy.Bootstrapper;
using Nancy.Conventions;
using Nancy.Diagnostics;
using NzbDrone.Api.ErrorManagement;
using NzbDrone.Api.Extensions;
using NzbDrone.Api.Frontend;
using NzbDrone.Common;
using NzbDrone.Common.Composition;
using NzbDrone.Common.Messaging;
using NzbDrone.Common.Model;
using NzbDrone.Core.Instrumentation;
using NzbDrone.Core.Lifecycle;
using TinyIoC;
@ -34,6 +39,13 @@ namespace NzbDrone.Api
container.Resolve<IMessageAggregator>().PublishEvent(new ApplicationStartedEvent());
if (container.Resolve<IConfigFileProvider>().AuthenticationType == AuthenticationType.Basic)
{
pipelines.EnableBasicAuthentication(new BasicAuthenticationConfiguration(
container.Resolve<IUserValidator>(),
"NzbDrone"));
}
ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve<NzbDroneErrorPipeline>().HandleException);
}

@ -70,6 +70,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.0.16.1\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Nancy.Authentication.Basic">
<HintPath>..\packages\Nancy.Authentication.Basic.0.16.1\lib\net40\Nancy.Authentication.Basic.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.3\lib\net35\Newtonsoft.Json.dll</HintPath>
@ -86,6 +89,8 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="Authentication\AuthenticationValidator.cs" />
<Compile Include="Authentication\NzbDroneUser.cs" />
<Compile Include="AutomapperBootstraper.cs" />
<Compile Include="Calendar\CalendarModule.cs" />
<Compile Include="ClientSchema\FieldDefinitionAttribute.cs" />

@ -1,4 +1,5 @@
using Nancy;
using Nancy.Security;
namespace NzbDrone.Api
{
@ -7,6 +8,7 @@ namespace NzbDrone.Api
protected NzbDroneApiModule(string resource)
: base("/api/" + resource.Trim('/'))
{
this.RequiresAuthentication();
Options["/"] = x => new Response();
}
}

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Nancy.Security;
using NzbDrone.Api.REST;
using NzbDrone.Api.Validation;
using NzbDrone.Core.Datastore;
@ -12,7 +13,7 @@ namespace NzbDrone.Api
protected NzbDroneRestModule()
: this(new TResource().ResourceName)
{
this.RequiresAuthentication();
}
protected NzbDroneRestModule(string resource)

@ -32,9 +32,6 @@ namespace NzbDrone.Api.Series
SharedValidator.RuleFor(s => s.RootFolderId).ValidId();
SharedValidator.RuleFor(s => s.QualityProfileId).ValidId();
PostValidator.RuleFor(s => s.Title).NotEmpty();
}

@ -4,6 +4,7 @@
<package id="FluentValidation" version="4.0.0.0" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.Core" version="1.0.1" targetFramework="net40" />
<package id="Nancy" version="0.16.1" targetFramework="net40" />
<package id="Nancy.Authentication.Basic" version="0.16.1" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.3" targetFramework="net40" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
<package id="ValueInjecter" version="2.3.3" targetFramework="net40" />

@ -12,6 +12,8 @@ namespace NzbDrone.Common
int Port { get; set; }
bool LaunchBrowser { get; set; }
AuthenticationType AuthenticationType { get; set; }
string BasicAuthUsername { get; set; }
string BasicAuthPassword { get; set; }
int GetValueInt(string key, int defaultValue);
bool GetValueBoolean(string key, bool defaultValue);
string GetValue(string key, object defaultValue);
@ -32,7 +34,6 @@ namespace NzbDrone.Common
CreateDefaultConfigFile();
}
public virtual Guid Guid
{
get
@ -61,10 +62,21 @@ namespace NzbDrone.Common
public virtual AuthenticationType AuthenticationType
{
get { return (AuthenticationType)GetValueInt("AuthenticationType", 0); }
set { SetValue("AuthenticationType", (int)value); }
get { return GetValueEnum("AuthenticationType", AuthenticationType.Anonymous); }
set { SetValue("AuthenticationType", value); }
}
public virtual string BasicAuthUsername
{
get { return GetValue("BasicAuthUsername", ""); }
set { SetValue("BasicAuthUsername", value); }
}
public virtual string BasicAuthPassword
{
get { return GetValue("BasicAuthPassword", ""); }
set { SetValue("BasicAuthPassword", value); }
}
public virtual int GetValueInt(string key, int defaultValue)
{
@ -76,6 +88,11 @@ namespace NzbDrone.Common
return Convert.ToBoolean(GetValue(key, defaultValue));
}
private T GetValueEnum<T>(string key, T defaultValue)
{
return (T)Enum.Parse(typeof(T), GetValue(key, defaultValue), true);
}
public virtual string GetValue(string key, object defaultValue)
{
var xDoc = XDocument.Load(_configFile);
@ -96,7 +113,6 @@ namespace NzbDrone.Common
return defaultValue.ToString();
}
public virtual void SetValue(string key, object value)
{
var xDoc = XDocument.Load(_configFile);
@ -115,6 +131,11 @@ namespace NzbDrone.Common
xDoc.Save(_configFile);
}
private void SetValue(string key, Enum value)
{
SetValue(key, value.ToString().ToLower());
}
private void CreateDefaultConfigFile()
{
if (!File.Exists(_configFile))

@ -4,7 +4,7 @@ namespace NzbDrone.Common.Model
{
public enum AuthenticationType
{
Anonymous = 0,
Windows = 1
Anonymous,
Basic
}
}

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Nancy" version="0.16.1" targetFramework="net40" />
<package id="Nancy.Authentication.Basic" version="0.16.1" targetFramework="net40" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
</packages>
Loading…
Cancel
Save