forked from NancyFx/Nancy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new stateless authentication that can be used ina REST api.
- Loading branch information
Byron Sommardahl
committed
Jul 9, 2012
1 parent
1d3648b
commit ebbc245
Showing
6 changed files
with
1,063 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,4 +316,6 @@ private static string GetRedirectQuerystringKey(FormsAuthenticationConfiguration | |
} | ||
|
||
} | ||
|
||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/Nancy.Authentication.Forms/Nancy.Authentication.Forms.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Nancy.Authentication.Stateless/Nancy.Authentication.Stateless.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
64
src/Nancy.Authentication.Stateless/StatelessAuthentication.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Nancy.Authentication.Stateless/StatelessAuthenticationConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.