Skip to content

Commit 5f6b0a9

Browse files
MisterJamestonysurma
authored andcommitted
Add token protection middleware (#966)
* Creating middleware and options * Adding extension method for middleware * Configure middleware at startup * Fixing async bits and token provider * Using clean up and fixing policy name
1 parent 85ef227 commit 5f6b0a9

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using AllReady.Models;
2+
using Microsoft.AspNet.Builder;
3+
using Microsoft.AspNet.Http;
4+
using Microsoft.AspNet.Identity;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace AllReady.Security.Middleware
9+
{
10+
public static class TokenProtectedResourceExtensions
11+
{
12+
// extension method for easy wiring of middleware
13+
public static IApplicationBuilder UseTokenProtection(
14+
this IApplicationBuilder builder, TokenProtectedResourceOptions options)
15+
{
16+
return builder.UseMiddleware<TokenProtectedResource>(options);
17+
}
18+
}
19+
20+
public class TokenProtectedResource
21+
{
22+
private RequestDelegate _next;
23+
private TokenProtectedResourceOptions _options;
24+
25+
public TokenProtectedResource(RequestDelegate next, TokenProtectedResourceOptions options)
26+
{
27+
_next = next;
28+
_options = options;
29+
}
30+
31+
public async Task Invoke(HttpContext httpContext, UserManager<ApplicationUser> manager)
32+
{
33+
if (httpContext.Request.Path.StartsWithSegments(_options.Path))
34+
{
35+
var headers = httpContext.Request.Headers;
36+
if (!(headers.ContainsKey("ApiUser") && headers.ContainsKey("ApiToken")))
37+
{
38+
await httpContext.Authentication.ChallengeAsync();
39+
return;
40+
}
41+
42+
var apiUser = headers.FirstOrDefault(h => h.Key == "ApiUser").Value;
43+
var token = headers.FirstOrDefault(h => h.Key == "ApiToken").Value;
44+
45+
var user = await manager.FindByNameAsync(apiUser).ConfigureAwait(false);
46+
var authorized = await manager.VerifyUserTokenAsync(user, "Default", "api-request-injest", token).ConfigureAwait(false);
47+
48+
if (!authorized)
49+
{
50+
await httpContext.Authentication.ChallengeAsync();
51+
return;
52+
}
53+
}
54+
55+
await _next(httpContext);
56+
}
57+
58+
}
59+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Microsoft.AspNet.Http;
2+
3+
namespace AllReady.Security.Middleware
4+
{
5+
public class TokenProtectedResourceOptions
6+
{
7+
public PathString Path { get; set; }
8+
public string PolicyName { get; set; }
9+
}
10+
}

AllReadyApp/Web-App/AllReady/Startup.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Microsoft.Extensions.DependencyInjection;
2121
using Microsoft.Extensions.Logging;
2222
using Microsoft.Extensions.PlatformAbstractions;
23+
using AllReady.Security.Middleware;
2324

2425
namespace AllReady
2526
{
@@ -237,6 +238,13 @@ public async void Configure(IApplicationBuilder app,
237238
// Add cookie-based authentication to the request pipeline.
238239
app.UseIdentity();
239240

241+
// Add token-based protection to the request inject pipeline
242+
app.UseTokenProtection(new TokenProtectedResourceOptions
243+
{
244+
Path = "api/request",
245+
PolicyName = "api-request-injest"
246+
});
247+
240248
// Add authentication middleware to the request pipeline. You can configure options such as Id and Secret in the ConfigureServices method.
241249
// For more information see http://go.microsoft.com/fwlink/?LinkID=532715
242250
if (Configuration["Authentication:Facebook:AppId"] != null)

0 commit comments

Comments
 (0)