NancyFx/Nancy — pull request #944 by tischlda (2013-02-03 12:46:23+00:00)
Title: Security extensions

This pull request adds various security related helper methods:

## 1. Extension methods for IUserIdentity

Some helper methods to simplify some common checks on IUserIdentity objects.

### Examples

Instead of

if (Context.CurrentUser != null
&& !String.IsNullOrWhiteSpace(Context.CurrentUser.UserName)
{
...
}

You can now use

if (Context.CurrentUser.IsAuthenticated())
{
...
}

Instead of

if (Context.CurrentUser != null
&& context.CurrentUser.Claims != null
&& context.CurrentUser.Claims.Contains("foobar"))
{
...
}

You can now use

if (Context.CurrentUser.HasClaim("foobar"))
{
...
}

# 2. Route security

I had the requirement to have only some routes of a module secured (e.g. everyone can GET, only authenticated can POST), so I added extension methods similar to those for module security:

### Examples

Get["/route"] = parameters => MyGetRouteHandler;

Post["/route"] = this.RequiresAuthentication(
parameters => { ... };
);

Post["/route2"] = this.RequiresClaim("foobar"
parameters => { ... };
);

## 3. Refactoring of ModuleSecurity to use same functions as RouteSecurity