-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Media: Add protection to restrict access to media in recycle bin (closes #2931) #20378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+757
−34
Merged
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
993903b
Add MoveFile it IFileSystem and implement on file systems.
AndyButland 81a59d1
Rename media file on move to recycle bin.
AndyButland b13c2ff
Rename file on restore from recycle bin.
AndyButland d0dea01
Add configuration to enabled recycle bin media protection.
AndyButland 0c4b36c
Expose backoffice authentication as cookie for non-backoffice usage.
AndyButland 2a29f57
Display protected image when viewing image cropper in the backoffice …
AndyButland f90ec76
Code tidy and comments.
AndyButland 9830543
Apply suggestions from code review
AndyButland 8d17749
Introduced helper class to DRY up repeated code between image cropper…
AndyButland e548f26
Merge branch 'v16/feature/media-recycle-bin-protection' of https://gi…
AndyButland a3e23b5
Merge branch 'main' into v16/feature/media-recycle-bin-protection
AndyButland 29e4698
Reverted client-side and management API updates.
AndyButland 8a7ac51
Moved update of path to media file in recycle bin with deleted suffix…
AndyButland 9e428db
Merge branch 'main' into v16/feature/media-recycle-bin-protection
AndyButland 7c4440a
Separate integration tests for add and remove.
AndyButland bc82345
Use interpolated strings.
AndyButland 2e742b0
Renamed variable.
AndyButland af3674f
Merge branch 'main' into v16/feature/media-recycle-bin-protection
AndyButland f77738b
Move EnableMediaRecycleBinProtection to ContentSettings.
AndyButland 97e8632
Tidied up comments.
AndyButland 996435a
Added TODO for 18.
AndyButland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...co.Cms.Api.Common/Security/ExposeBackOfficeAuthenticationOpenIddictServerEventsHandler.cs
This file contains hidden or 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,84 @@ | ||
| using System.Security.Claims; | ||
| using Microsoft.AspNetCore.Authentication; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.Options; | ||
| using OpenIddict.Server; | ||
| using Umbraco.Cms.Core.Configuration.Models; | ||
| using Umbraco.Cms.Core.Security; | ||
| using Umbraco.Extensions; | ||
|
|
||
| namespace Umbraco.Cms.Infrastructure.Security; | ||
|
|
||
| /// <summary> | ||
| /// Provides OpenIddict server event handlers to expose back-office authentication via a custom authentication scheme. | ||
| /// </summary> | ||
| public class ExposeBackOfficeAuthenticationOpenIddictServerEventsHandler : IOpenIddictServerHandler<OpenIddictServerEvents.GenerateTokenContext>, | ||
| IOpenIddictServerHandler<OpenIddictServerEvents.ApplyRevocationResponseContext> | ||
| { | ||
| private readonly IHttpContextAccessor _httpContextAccessor; | ||
| private readonly string[] _claimTypes; | ||
| private readonly TimeSpan _timeOut; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ExposeBackOfficeAuthenticationOpenIddictServerEventsHandler"/> class. | ||
| /// </summary> | ||
| public ExposeBackOfficeAuthenticationOpenIddictServerEventsHandler( | ||
| IHttpContextAccessor httpContextAccessor, | ||
| IOptions<GlobalSettings> globalSettings, | ||
| IOptions<BackOfficeIdentityOptions> backOfficeIdentityOptions) | ||
| { | ||
| _httpContextAccessor = httpContextAccessor; | ||
| _timeOut = globalSettings.Value.TimeOut; | ||
|
|
||
| // These are the type identifiers for the claims required by the principal | ||
| // for the custom authentication scheme. | ||
| // We make available the ID, user name and allowed applications (sections) claims. | ||
| _claimTypes = | ||
| [ | ||
| backOfficeIdentityOptions.Value.ClaimsIdentity.UserIdClaimType, | ||
| backOfficeIdentityOptions.Value.ClaimsIdentity.UserNameClaimType, | ||
| Core.Constants.Security.AllowedApplicationsClaimType, | ||
| ]; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| /// <remarks> | ||
| /// Event handler for when access tokens are generated (created or refreshed). | ||
| /// </remarks> | ||
| public async ValueTask HandleAsync(OpenIddictServerEvents.GenerateTokenContext context) | ||
| { | ||
| // Only proceed if this is a back-office sign-in. | ||
| if (context.Principal.Identity?.AuthenticationType != Core.Constants.Security.BackOfficeAuthenticationType) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Create a new principal with the claims from the authenticated principal. | ||
| var principal = new ClaimsPrincipal( | ||
| new ClaimsIdentity( | ||
| context.Principal.Claims.Where(claim => _claimTypes.Contains(claim.Type)), | ||
| Core.Constants.Security.BackOfficeExposedAuthenticationType)); | ||
|
|
||
| // Sign-in the new principal for the custom authentication scheme. | ||
| await _httpContextAccessor | ||
| .GetRequiredHttpContext() | ||
| .SignInAsync(Core.Constants.Security.BackOfficeExposedAuthenticationType, principal, GetAuthenticationProperties()); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| /// <remarks> | ||
| /// Event handler for when access tokens are revoked. | ||
| /// </remarks> | ||
| public async ValueTask HandleAsync(OpenIddictServerEvents.ApplyRevocationResponseContext context) | ||
| => await _httpContextAccessor | ||
| .GetRequiredHttpContext() | ||
| .SignOutAsync(Core.Constants.Security.BackOfficeExposedAuthenticationType, GetAuthenticationProperties()); | ||
|
|
||
| private AuthenticationProperties GetAuthenticationProperties() | ||
| => new() | ||
| { | ||
| IsPersistent = true, | ||
| IssuedUtc = DateTimeOffset.UtcNow, | ||
| ExpiresUtc = DateTimeOffset.UtcNow.Add(_timeOut) | ||
| }; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.