Skip to content

Commit

Permalink
Fix Unauthorised redirect (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Jan 23, 2020
1 parent df83e41 commit 17ef1e7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ConfigureOrganizationPolicy(IOptions<OrganizationOptions> options)

public void Configure(AuthorizationOptions options)
{
options.AddPolicy("RequireOrganization", policy =>
options.AddPolicy(Startup.RequireOrganizationPolicy, policy =>
{
policy.AddRequirements(new OrganizationRequirement(_options.Value.RequiredOrganization));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task<IActionResult> Login(string returnUrl = "/")
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync();
return RedirectToPage("/Unauthorized");
return RedirectToPage("/Login");
}
}
}
13 changes: 13 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Pages/Login.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@page
@model LoginModel
@attribute [Microsoft.AspNetCore.Authorization.AllowAnonymous]
@{
ViewData["Title"] = "Login";
}

<p>Please login using your GitHub account</p>

<a class="login-button btn btn-outline-dark" asp-action="Login" asp-controller="Account" asp-route-returnUrl="@Model.ReturnUrl">
<img height="30" width="30" src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" />
Login with GitHub
</a>
20 changes: 20 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Pages/Login.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace APIViewWeb.Pages
{
public class LoginModel : PageModel
{
[BindProperty(SupportsGet = true, Name = "returnurl")]
public string ReturnUrl { get; set; } = "/";

public IActionResult OnGetAsync()
{
if (User.Identity.IsAuthenticated)
return Redirect(ReturnUrl);

return Page();
}
}
}
10 changes: 5 additions & 5 deletions src/dotnet/APIView/APIViewWeb/Pages/Unauthorized.cshtml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
@page
@model APIViewWeb.Pages.UnauthorizedModel
@attribute [Microsoft.AspNetCore.Authorization.AllowAnonymous]
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@{
ViewData["Title"] = "Unauthorized";
var names = string.Join(" or ", Model.Options.RequiredOrganization);
}

<p>You're currently not logged into a GitHub account or your GitHub account is not publicly affiliated with @names organizations.</p>
<p>Your GitHub account is not publicly affiliated with @names organizations.</p>
<p>
In order to use the site, please sign into another account - or,
request to join one of the following organizations:
ensure <b>public</b> membership in one of the following organizations:
<ul>
@foreach (var organization in Model.Options.RequiredOrganization)
{
<li>
<a href="https://github.com/@organization">@organization</a>
<a href="https://github.com/orgs/@organization/people?utf8=✓&query=@(User.GetGitHubLogin())#org-members-table">@organization</a>
</li>
}
</ul>
Expand All @@ -26,5 +26,5 @@

<a class="login-button btn btn-outline-dark" asp-action="Login" asp-controller="Account" asp-route-returnUrl="@Model.ReturnUrl">
<img height="30" width="30" src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" />
Login with GitHub
Refresh GitHub login
</a>
24 changes: 14 additions & 10 deletions src/dotnet/APIView/APIViewWeb/Pages/Unauthorized.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authorization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Options;
Expand All @@ -7,23 +8,26 @@ namespace APIViewWeb.Pages
{
public class UnauthorizedModel : PageModel
{
private readonly IAuthorizationService _authorizationService;
public OrganizationOptions Options { get; }

[BindProperty(SupportsGet = true)]
public string ReturnUrl { get; private set; }
[BindProperty(SupportsGet = true, Name = "returnurl")]
public string ReturnUrl { get; set; } = "/";

public UnauthorizedModel(IOptions<OrganizationOptions> options)
public UnauthorizedModel(IOptions<OrganizationOptions> options, IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
Options = options.Value;
}

public IActionResult OnGet()
public async Task<IActionResult> OnGetAsync()
{
if (User.Identity.IsAuthenticated)
{
return RedirectToPage("./Assemblies/Index");
}
ReturnUrl = Request.Query["returnurl"];
var authorizationResult =
await _authorizationService.AuthorizeAsync(User, null, Startup.RequireOrganizationPolicy);

if (authorizationResult.Succeeded)
return Redirect(ReturnUrl);

return Page();
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/dotnet/APIView/APIViewWeb/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace APIViewWeb
{
public class Startup
{
public static string RequireOrganizationPolicy = "RequireOrganization";

public static string VersionHash { get; set; }

static Startup()
Expand Down Expand Up @@ -64,7 +66,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddRazorPages(options =>
{
options.Conventions.AuthorizeFolder("/Assemblies", "RequireOrganization");
options.Conventions.AuthorizeFolder("/Assemblies", RequireOrganizationPolicy);
options.Conventions.AddPageRoute("/Assemblies/Index", "");
});

Expand All @@ -87,7 +89,11 @@ public void ConfigureServices(IServiceCollection services)
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options => options.LoginPath = options.AccessDeniedPath = "/Unauthorized")
.AddCookie(options =>
{
options.LoginPath = "/Login";
options.AccessDeniedPath = "/Unauthorized";
})
.AddOAuth("GitHub", options =>
{
options.ClientId = Configuration["Github:ClientId"];
Expand Down

0 comments on commit 17ef1e7

Please sign in to comment.