Since I am currently in the process of updating AuthorizationServer and its samples – I thought it would be the right time to throw away my custom Nancy extensions and use Damian Hickey’s nice Nancy.MSOwinSecurity package instead. The outcome is quite pleasing.
The Startup class now looks exactly like the Web API one (OWIN FTW yay):
public class Startup
{
public void Configuration(IAppBuilder app)
{
// no mapping of incoming claims to Microsoft types
JwtSecurityTokenHandler.InboundClaimTypeMap = ClaimMappings.None;
// validate JWT tokens from AuthorizationServer
app.UseJsonWebToken(
issuer: Constants.AS.IssuerName,
audience: Constants.Audience,
signingKey: Constants.AS.SigningKey);
// claims transformation
app.UseClaimsTransformation(new ClaimsTransformer().Transform);
app.UseNancy();
}
}
…and the controller *cough* module looks like this:
public class IdentityModule : NancyModule
{
public IdentityModule() : base(“/api/identity”)
{
this.RequiresMSOwinAuthentication();
Get[“/”] = _ =>
{
var user = Context.GetMSOwinUser();
var claims = from c in user.Claims
select new ViewClaim
{
Type = c.Type,
Value = c.Value
};
return Response.AsJson<IEnumerable<ViewClaim>>(claims);
};
}
}
Full sample here.
This would look better if we had extension properties in C#, but glad you’re happy with it.
(Still have on my backlog your suggestions for richer resource azn!)
Lemme know – we can maybe design something at the OWIN level – so it works for Web API and Nancy.
I currently have this in Nancy to deal with OWIN authorization:
https://github.com/CumpsD/CC.TheBench/blob/master/src/CC.TheBench.Frontend.Web/Bootstrapper.cs#L122-L132
Flowing the OWIN user into Nancy, and then use standard Nancy Security mechanisms.
Will this work with AuthorizationServer as well? Wiring it up in the OWIN pipeline, and then just keep using what I have (checking ClaimsPrincipal coming from OWIN)
I think so – AS is simply issuing a JWT token – the middleware in Nancy picks that up and turns it into a ClaimsPrincipal – from there it’s all the same ;)
Ok, will give it a shot (somewhere on my backlog :p)