Skip to content

Commit

Permalink
Add Active Directory membership service
Browse files Browse the repository at this point in the history
  • Loading branch information
Ollienator committed Oct 8, 2015
1 parent f6c6a5c commit 082d357
Show file tree
Hide file tree
Showing 43 changed files with 1,212 additions and 521 deletions.
42 changes: 42 additions & 0 deletions Bonobo.Git.Server/App_Start/UnityConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace Bonobo.Git.Server.App_Start
{
/// <summary>
/// Specifies the Unity configuration for the main container.
/// </summary>
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});

/// <summary>
/// Gets the configured Unity container.
/// </summary>
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion

/// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
// container.LoadConfiguration();

// TODO: Register your types here
// container.RegisterType<IProductRepository, ProductRepository>();
}
}
}
34 changes: 34 additions & 0 deletions Bonobo.Git.Server/App_Start/UnityMvcActivator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.Unity.Mvc;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Bonobo.Git.Server.App_Start.UnityWebActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(Bonobo.Git.Server.App_Start.UnityWebActivator), "Shutdown")]

namespace Bonobo.Git.Server.App_Start
{
/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();

FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}

/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion Bonobo.Git.Server/Attributes/GitAuthorizeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override void OnAuthorization(AuthorizationContext filterContext)
string username = Uri.UnescapeDataString(value.Substring(0, value.IndexOf(':')));
string password = Uri.UnescapeDataString(value.Substring(value.IndexOf(':') + 1));

if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password) && MembershipService.ValidateUser(username, password))
if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password) && MembershipService.ValidateUser(username, password) == ValidationResult.Success)
{
httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(AuthenticationProvider.GetClaimsForUser(username)));
}
Expand Down
14 changes: 0 additions & 14 deletions Bonobo.Git.Server/Attributes/WebAuthorizeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ public new string Roles

public override void OnAuthorization(AuthorizationContext filterContext)
{
var importer = new WindowsIdentityImporter();
WindowsIdentityImporter.Import(filterContext);

if (IsWindowsUserAuthenticated(filterContext))
{
return;
}

if (filterContext.HttpContext.User == null || !(filterContext.HttpContext.User.Identity is ClaimsIdentity) || !filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result =
Expand All @@ -53,11 +45,5 @@ public override void OnAuthorization(AuthorizationContext filterContext)
}
}
}

private static bool IsWindowsUserAuthenticated(ControllerContext context)
{
var windowsIdentity = context.HttpContext.User.Identity as WindowsIdentity;
return windowsIdentity != null && windowsIdentity.IsAuthenticated;
}
}
}
68 changes: 37 additions & 31 deletions Bonobo.Git.Server/Bonobo.Git.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<MvcProjectUpgradeChecked>true</MvcProjectUpgradeChecked>
<NuGetPackageImportStamp>fbba635a</NuGetPackageImportStamp>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -122,25 +123,29 @@
<HintPath>..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Practices.Unity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Unity.3.5.1404.0\lib\net45\Microsoft.Practices.Unity.dll</HintPath>
<Reference Include="Microsoft.Practices.Unity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f, processorArchitecture=MSIL">
<HintPath>..\packages\Unity.4.0.1\lib\net45\Microsoft.Practices.Unity.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Practices.Unity.Configuration, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Unity.3.5.1404.0\lib\net45\Microsoft.Practices.Unity.Configuration.dll</HintPath>
<Reference Include="Microsoft.Practices.Unity.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f, processorArchitecture=MSIL">
<HintPath>..\packages\Unity.4.0.1\lib\net45\Microsoft.Practices.Unity.Configuration.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Practices.Unity.RegistrationByConvention, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Unity.3.5.1404.0\lib\net45\Microsoft.Practices.Unity.RegistrationByConvention.dll</HintPath>
<Reference Include="Microsoft.Practices.Unity.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f, processorArchitecture=MSIL">
<HintPath>..\packages\Unity.Mvc.4.0.1\lib\net45\Microsoft.Practices.Unity.Mvc.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Reference Include="Microsoft.Practices.Unity.RegistrationByConvention, Version=4.0.0.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f, processorArchitecture=MSIL">
<HintPath>..\packages\Unity.4.0.1\lib\net45\Microsoft.Practices.Unity.RegistrationByConvention.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
Expand All @@ -150,28 +155,25 @@
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data.SQLite, Version=1.0.98.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\System.Data.SQLite.Core.1.0.98.1\lib\net45\System.Data.SQLite.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Data.SQLite.EF6, Version=1.0.98.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\System.Data.SQLite.EF6.1.0.98.1\lib\net45\System.Data.SQLite.EF6.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Data.SQLite.Linq, Version=1.0.98.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\System.Data.SQLite.Linq.1.0.98.1\lib\net45\System.Data.SQLite.Linq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.DirectoryServices.AccountManagement" />
<Reference Include="System.Drawing" />
<Reference Include="System.ComponentModel.DataAnnotations">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
Expand All @@ -190,10 +192,6 @@
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -227,20 +225,30 @@
<Reference Include="System.EnterpriseServices" />
<Reference Include="System.Web.Mobile" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Ude">
<Reference Include="Ude, Version=0.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\UDE.CSharp.1.1.0\lib\Ude.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Unity.Mvc4, Version=1.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Unity.Mvc4.1.4.0.0\lib\net45\Unity.Mvc4.dll</HintPath>
<Reference Include="WebActivatorEx, Version=2.0.0.0, Culture=neutral, PublicKeyToken=7b26dc2a43f6a0d4, processorArchitecture=MSIL">
<HintPath>..\packages\WebActivatorEx.2.0.6\lib\net40\WebActivatorEx.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="WebGrease">
<Reference Include="WebGrease, Version=1.6.5135.21930, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\WebGrease.1.6.0\lib\WebGrease.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\UnityConfig.cs" />
<Compile Include="App_Start\UnityMvcActivator.cs" />
<Compile Include="Attributes\FileExtensionsCustom.cs" />
<Compile Include="Configuration\ActiveDirectorySettings.cs" />
<Compile Include="Configuration\AuthenticationSettings.cs" />
<Compile Include="Data\ADBackend.cs" />
<Compile Include="Data\ADBackendStore.cs" />
<Compile Include="Data\ADRepositoryRepository.cs" />
<Compile Include="Data\ADTeamRepository.cs" />
<Compile Include="Data\INameProperty.cs" />
<Compile Include="Data\Update\AddGroup.cs" />
<Compile Include="Data\Update\AddRepositoryLogo.cs" />
<Compile Include="Data\Update\SqlServer\AddGroup.cs" />
Expand All @@ -265,10 +273,14 @@
<Compile Include="Helpers\MembershipHelper.cs" />
<Compile Include="Helpers\RepositoryCommitModelHelpers.cs" />
<Compile Include="Models\RepositoryCommitTitle.cs" />
<Compile Include="Security\ADMembershipService.cs" />
<Compile Include="Security\ADRepositoryPermissionService.cs" />
<Compile Include="Security\ADRoleProvider.cs" />
<Compile Include="Security\AuthenticationProvider.cs" />
<Compile Include="Security\CookieAuthenticationProvider.cs" />
<Compile Include="Security\IAuthenticationProvider.cs" />
<Compile Include="Security\IRoleProvider.cs" />
<Compile Include="Security\ValidationResult.cs" />
<Compile Include="Startup.cs" />
<Compile Include="UnityDecoratorBuildStrategy.cs" />
<Compile Include="UnityDecoratorContainerExtension.cs" />
Expand All @@ -286,7 +298,6 @@
<Compile Include="Data\Update\SqlServer\InsertDefaultData.cs" />
<Compile Include="Data\Update\UsernamesToLower.cs" />
<Compile Include="Attributes\GitAuthorizeAttribute.cs" />
<Compile Include="Bootstrapper.cs" />
<Compile Include="Configuration\ConfigurationEntry.cs" />
<Compile Include="Configuration\UserConfiguration.cs" />
<Compile Include="Controllers\AccountController.cs" />
Expand Down Expand Up @@ -349,8 +360,6 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Security\EFRoleProvider.cs" />
<Compile Include="Security\FormsAuthenticationService.cs" />
<Compile Include="Security\IFormsAuthenticationService.cs" />
<Compile Include="Security\IMembershipService.cs" />
<Compile Include="Security\IPasswordService.cs" />
<Compile Include="Security\IRepositoryPermissionService.cs" />
Expand All @@ -359,8 +368,6 @@
<Compile Include="UnityDependencyResolver.cs" />
<Compile Include="FilterProviders\UnityFilterAttributeFilterProvider.cs" />
<Compile Include="Attributes\WindowsActionFilterAttribute.cs" />
<Compile Include="WindowsIdentityImporter.cs" />
<Compile Include="UsernameUrl.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="App_Data\Git\antiword.exe" />
Expand Down Expand Up @@ -535,7 +542,6 @@
<Content Include="Scripts\MicrosoftMvcValidation.debug.js" />
<Content Include="Scripts\MicrosoftMvcValidation.js" />
<Content Include="Scripts\RepositoryGroupToggle.js" />
<Content Include="Unity.Mvc4.README.txt" />
<Content Include="Web.config">
<SubType>Designer</SubType>
</Content>
Expand Down Expand Up @@ -726,7 +732,7 @@ if not exist "$(TargetDir)NativeBinaries\amd64" md "$(TargetDir)NativeBinaries\a
<Import Project="..\packages\System.Data.SQLite.Core.1.0.98.1\build\net45\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.98.1\build\net45\System.Data.SQLite.Core.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
<ErrorText>Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}".</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.98.1\build\net45\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.98.1\build\net45\System.Data.SQLite.Core.targets'))" />
</Target>
Expand Down
36 changes: 0 additions & 36 deletions Bonobo.Git.Server/Bootstrapper.cs

This file was deleted.

51 changes: 51 additions & 0 deletions Bonobo.Git.Server/Configuration/ActiveDirectorySettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace Bonobo.Git.Server.Configuration
{
public class ActiveDirectorySettings
{
public static string DefaultDomain { get; private set; }
public static string MemberGroupName { get; private set; }
public static string BackendPath { get; private set; }
public static IDictionary<string, string> RoleNameToGroupNameMapping { get; private set; }
public static IDictionary<string, string> TeamNameToGroupNameMapping { get; private set; }

private static IDictionary<string, string> CreateMapping(string definition)
{
Dictionary<string, string> result = new Dictionary<string, string>();

if (!String.IsNullOrEmpty(definition))
{
foreach (string entry in definition.Split(',', ';'))
{
string[] mapping = entry.Split('=');
if (mapping.Length == 2)
{
string key = mapping[0].Trim();
string value = mapping[1].Trim();
if (!String.IsNullOrEmpty(key) && !String.IsNullOrEmpty(value))
{
result.Add(key, value);
}
}
}
}

return result;
}

static ActiveDirectorySettings()
{
DefaultDomain = ConfigurationManager.AppSettings["ActiveDirectoryDefaultDomain"];
MemberGroupName = ConfigurationManager.AppSettings["ActiveDirectoryMemberGroupName"];
BackendPath = ConfigurationManager.AppSettings["ActiveDirectoryBackendPath"];

RoleNameToGroupNameMapping = CreateMapping(ConfigurationManager.AppSettings["ActiveDirectoryRoleMapping"]);
TeamNameToGroupNameMapping = CreateMapping(ConfigurationManager.AppSettings["ActiveDirectoryTeamMapping"]);
}
}
}
Loading

0 comments on commit 082d357

Please sign in to comment.