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

Commit

Permalink
Merge pull request #69 from AppliedIS/require-https
Browse files Browse the repository at this point in the history
require all authenticated activity to be over https
  • Loading branch information
MrMatt57 committed Oct 21, 2016
2 parents 9259a07 + cd91b42 commit 9436f9d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 29 deletions.
2 changes: 1 addition & 1 deletion DOL.WHD.Section14c.Api/App_Start/Startup.Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void ConfigureAuth(IAppBuilder app)
Provider = new ApplicationOAuthProvider(PublicClientId),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(Convert.ToDouble(ConfigurationManager.AppSettings["AccessTokenExpireTimeSpanMinutes"])),
// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = true
AllowInsecureHttp = false
};

app.Use(async (context, next) =>
Expand Down
24 changes: 2 additions & 22 deletions DOL.WHD.Section14c.Api/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using DOL.WHD.Section14c.Api.Filters;
using DOL.WHD.Section14c.Business;
using DOL.WHD.Section14c.Business.Services;
using DOL.WHD.Section14c.DataAccess.Identity;
Expand All @@ -17,7 +17,7 @@

namespace DOL.WHD.Section14c.Api.Controllers
{
[Authorize]
[AuthorizeHttps]
[RoutePrefix("api/Account")]
public class AccountController : ApiController
{
Expand All @@ -33,7 +33,6 @@ public ApplicationUserManager UserManager
}

// GET api/Account/UserInfo
[HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
[Route("UserInfo")]
public async Task<UserInfoViewModel> GetUserInfo()
{
Expand Down Expand Up @@ -97,25 +96,6 @@ public async Task<IHttpActionResult> ChangePassword(ChangePasswordBindingModel m
return Ok();
}

// POST api/Account/SetPassword
[Route("SetPassword")]
public async Task<IHttpActionResult> SetPassword(SetPasswordBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);

if (!result.Succeeded)
{
return GetErrorResult(result);
}

return Ok();
}

// POST api/Account/RemoveLogin
[Route("RemoveLogin")]
public async Task<IHttpActionResult> RemoveLogin(RemoveLoginBindingModel model)
Expand Down
5 changes: 2 additions & 3 deletions DOL.WHD.Section14c.Api/Controllers/AttachmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;
using DOL.WHD.Section14c.Api.Filters;
using DOL.WHD.Section14c.Api.Providers;
using DOL.WHD.Section14c.Business;
using DOL.WHD.Section14c.Domain.Models.Submission;

namespace DOL.WHD.Section14c.Api.Controllers
{
[Authorize]
[AuthorizeHttps]
[RoutePrefix("api/attachment")]
public class AttachmentController : ApiController
{
Expand Down
5 changes: 2 additions & 3 deletions DOL.WHD.Section14c.Api/Controllers/SaveController.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using DOL.WHD.Section14c.Api.Filters;
using DOL.WHD.Section14c.Business;
using DOL.WHD.Section14c.Domain.ViewModels;
using Microsoft.AspNet.Identity;
using Newtonsoft.Json.Linq;

namespace DOL.WHD.Section14c.Api.Controllers
{
[Authorize]
[AuthorizeHttps]
[RoutePrefix("api/save")]
public class SaveController : ApiController
{
Expand Down
1 change: 1 addition & 0 deletions DOL.WHD.Section14c.Api/DOL.WHD.Section14c.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
<Compile Include="Controllers\AttachmentController.cs" />
<Compile Include="Controllers\ResponseController.cs" />
<Compile Include="Controllers\SaveController.cs" />
<Compile Include="Filters\AuthorizeHttps.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
Expand Down
37 changes: 37 additions & 0 deletions DOL.WHD.Section14c.Api/Filters/AuthorizeHttps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;

namespace DOL.WHD.Section14c.Api.Filters
{
public class AuthorizeHttps : AuthorizeAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if (SkipAuthorization(actionContext))
return;

if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)
{
ReasonPhrase = "HTTPS Required"
};
}
else
{
base.OnAuthorization(actionContext);
}
}

// from AuthorizeAttribute
private static bool SkipAuthorization(HttpActionContext actionContext)
{
if (!actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any<AllowAnonymousAttribute>())
return actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any<AllowAnonymousAttribute>();
return true;
}
}
}

0 comments on commit 9436f9d

Please sign in to comment.