Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

Commit

Permalink
fix : allow xsrf cookies to be sent over non-secure connections
Browse files Browse the repository at this point in the history
  • Loading branch information
mrellipse committed Aug 25, 2017
1 parent e970a6e commit 887203f
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/server/Config/ContainerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public ContainerRegistry()
};

For<IConfiguration>().Use(WebApp.Configuration).Singleton();
// For<DbContextBase>().Use<NpgSqlContext>();
For<DbContextBase>().Use<MsSqlContext>();
//For<DbContextBase>().Use<NpgSqlContext>();
//For<DbContextBase>().Use<MsSqlContext>();
For<Filters.ApiResultFilter>();
For<Filters.ApiExceptionFilter>().Use(() => new Filters.ApiExceptionFilter(targets));
For<Filters.IdentityMappingFilter>();
Expand Down
1 change: 1 addition & 0 deletions src/server/Config/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class AntiForgeryConfig
public string ClientName { get; set; }
public string CookieName { get; set; }
public string HeaderName { get; set; }
public bool RequireSsl { get; set; }
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/server/Middleware/AntiforgeryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Toucan.Server
{
Expand All @@ -30,12 +31,13 @@ public static void UseAntiforgeryMiddleware(this IApplicationBuilder app, string
if (tokenSet.RequestToken != null)
{
context.Response.Cookies.Append(clientName, tokenSet.RequestToken, new CookieOptions() { HttpOnly = false, Secure = true });
var options = context.RequestServices.GetRequiredService<IOptions<AppConfig>>().Value;
var cookieOptions = new CookieOptions() { HttpOnly = false, Secure = options.Server.AntiForgery.RequireSsl };
context.Response.Cookies.Append(clientName, tokenSet.RequestToken, cookieOptions);
}
}
await next.Invoke();
});
}

}
}
}
6 changes: 2 additions & 4 deletions src/server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
services.Configure<Toucan.Data.Config>(WebApp.Configuration.GetSection("data")); // configuration
services.Configure<Toucan.Server.Config>(WebApp.Configuration.GetSection("server"));

services.ConfigureMvc(WebApp.Configuration.GetTypedSection<Config.AntiForgeryConfig>("server:antiForgery"));
services.AddMemoryCache();


services.ConfigureMvc(WebApp.Configuration.GetTypedSection<Config.AntiForgeryConfig>("server:antiForgery"));
services.ConfigureAuthentication(tokenProvider, new string[] { "admin" });

// services.AddDbContext<NpgSqlContext>(options =>
// {
// {
// string assemblyName = typeof(Toucan.Data.Config).GetAssemblyName();
// options.UseNpgsql(dataConfig.ConnectionString, s => s.MigrationsAssembly(assemblyName));
// });
Expand Down
3 changes: 2 additions & 1 deletion src/server/app.development.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"antiForgery": {
"cookieName": "XSRF-COOKIE",
"headerName": "X-XSRF-TOKEN",
"clientName": "XSRF-TOKEN"
"clientName": "XSRF-TOKEN",
"requireSsl": false
}
},
"service": {
Expand Down
3 changes: 2 additions & 1 deletion src/server/app.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"antiForgery": {
"cookieName": "XSRF-COOKIE",
"headerName": "X-XSRF-TOKEN",
"clientName": "XSRF-TOKEN"
"clientName": "XSRF-TOKEN",
"requireSsl": true
}
},
"service": {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "toucan.ui",
"version": "2.0.0",
"version": "2.0.1",
"description": "UI project for Toucan template",
"keywords": [
"vuejs",
Expand Down

0 comments on commit 887203f

Please sign in to comment.