-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #662 from immense/tech/admin-auth-policies
Use recommended policy-based authorization for Org Admin and Server Admin pages.
- Loading branch information
Showing
19 changed files
with
593 additions
and
492 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Remotely.Server.Auth; | ||
|
||
public class OrganizationAdminRequirement : IAuthorizationRequirement | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Remotely.Shared.Models; | ||
using System.Threading.Tasks; | ||
|
||
namespace Remotely.Server.Auth; | ||
|
||
public class OrganizationAdminRequirementHandler : AuthorizationHandler<OrganizationAdminRequirement> | ||
{ | ||
private readonly UserManager<RemotelyUser> _userManager; | ||
|
||
public OrganizationAdminRequirementHandler(UserManager<RemotelyUser> userManager) | ||
{ | ||
_userManager = userManager; | ||
} | ||
|
||
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, OrganizationAdminRequirement requirement) | ||
{ | ||
if (context.User.Identity?.IsAuthenticated != true) | ||
{ | ||
context.Fail(); | ||
return; | ||
} | ||
|
||
var user = await _userManager.GetUserAsync(context.User); | ||
if (user?.IsAdministrator != true) | ||
{ | ||
context.Fail(); | ||
return; | ||
} | ||
|
||
context.Succeed(requirement); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Remotely.Server.Auth; | ||
|
||
public static class PolicyNames | ||
{ | ||
public const string TwoFactorRequired = nameof(TwoFactorRequired); | ||
public const string OrganizationAdminRequired = nameof(OrganizationAdminRequired); | ||
public const string ServerAdminRequired = nameof(ServerAdminRequired); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Remotely.Server.Auth; | ||
|
||
public class ServerAdminRequirement : IAuthorizationRequirement | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#nullable enable | ||
|
||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Remotely.Shared.Models; | ||
using System.Threading.Tasks; | ||
|
||
namespace Remotely.Server.Auth; | ||
|
||
public class ServerAdminRequirementHandler : AuthorizationHandler<ServerAdminRequirement> | ||
{ | ||
private readonly UserManager<RemotelyUser> _userManager; | ||
|
||
public ServerAdminRequirementHandler(UserManager<RemotelyUser> userManager) | ||
{ | ||
_userManager = userManager; | ||
} | ||
|
||
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, ServerAdminRequirement requirement) | ||
{ | ||
if (context.User.Identity?.IsAuthenticated != true) | ||
{ | ||
context.Fail(); | ||
return; | ||
} | ||
|
||
var user = await _userManager.GetUserAsync(context.User); | ||
if (user?.IsServerAdmin != true) | ||
{ | ||
context.Fail(); | ||
return; | ||
} | ||
|
||
context.Succeed(requirement); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.