Skip to content

Commit

Permalink
Added new stateless authentication that can be used ina REST api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron Sommardahl committed Jul 9, 2012
1 parent 1d3648b commit ebbc245
Show file tree
Hide file tree
Showing 6 changed files with 1,063 additions and 337 deletions.
2 changes: 2 additions & 0 deletions src/Nancy.Authentication.Forms/FormsAuthentication.cs
Expand Up @@ -316,4 +316,6 @@ private static string GetRedirectQuerystringKey(FormsAuthenticationConfiguration
}

}


}
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{211560C3-FDDF-46D6-AB0C-F3BC04B173B5}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Nancy.Authentication.Stateless</RootNamespace>
<AssemblyName>Nancy.Authentication.Stateless</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="StatelessAuthentication.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StatelessAuthenticationConfiguration.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nancy\Nancy.csproj">
<Project>{34576216-0DCA-4B0F-A0DC-9075E75A676F}</Project>
<Name>Nancy</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
64 changes: 64 additions & 0 deletions src/Nancy.Authentication.Stateless/StatelessAuthentication.cs
@@ -0,0 +1,64 @@
using System;
using Nancy.Bootstrapper;
using Nancy.Security;

namespace Nancy.Authentication.Stateless
{
/// <summary>
/// Nancy forms authentication implementation
/// </summary>
public static class StatelessAuthentication
{
/// <summary>
/// Enables forms authentication for the application
/// </summary>
/// <param name="pipelines">Pipelines to add handlers to (usually "this")</param>
/// <param name="configuration">Forms authentication configuration</param>
public static void Enable(IPipelines pipelines, StatelessAuthenticationConfiguration configuration)
{
if (pipelines == null)
{
throw new ArgumentNullException("pipelines");
}

if (configuration == null)
{
throw new ArgumentNullException("configuration");
}

if (!configuration.IsValid)
{
throw new ArgumentException("Configuration is invalid", "configuration");
}

pipelines.BeforeRequest.AddItemToStartOfPipeline(GetLoadAuthenticationHook(configuration));
}

/// <summary>
/// Gets the pre request hook for loading the authenticated user's details
/// from the cookie.
/// </summary>
/// <param name="configuration">Forms authentication configuration to use</param>
/// <returns>Pre request hook delegate</returns>
static Func<NancyContext, Response> GetLoadAuthenticationHook(StatelessAuthenticationConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}

return context =>
{
try
{
context.CurrentUser = configuration.GetUserIdentity(context);
return context.Response;
}
catch (Exception)
{
return context.Response;
}
};
}
}
}
@@ -0,0 +1,34 @@
using System;
using Nancy.Security;

namespace Nancy.Authentication.Stateless
{
/// <summary>
/// Configuration options for stateless authentication
/// </summary>
public class StatelessAuthenticationConfiguration
{
internal Func<NancyContext, IUserIdentity> GetUserIdentity;

public StatelessAuthenticationConfiguration(Func<NancyContext, IUserIdentity> getUserIdentity)
{
GetUserIdentity = getUserIdentity;
}

/// <summary>
/// Gets a value indicating whether the configuration is valid or not.
/// </summary>
public virtual bool IsValid
{
get
{
if (GetUserIdentity == null)
{
return false;
}

return true;
}
}
}
}

0 comments on commit ebbc245

Please sign in to comment.