This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add impersonation / transient grant support during report callback ex…
…ecution based on current user / transient granting status
- Loading branch information
1 parent
003e607
commit b90860b
Showing
4 changed files
with
152 additions
and
26 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
92 changes: 92 additions & 0 deletions
92
src/Serenity.Extensions/Modules/Reporting/HtmlToPdf/HtmlReportCallbackUrlInterceptor.cs
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,92 @@ | ||
using Microsoft.AspNetCore.DataProtection; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Serenity.Reporting; | ||
|
||
/// <summary> | ||
/// Implementation for <see cref="IReportExecutor" /> that uses callback report cookie | ||
/// to impersonate / transient grant permissions | ||
/// </summary> | ||
public class HtmlReportCallbackUrlInterceptor( | ||
ILogger<HtmlReportCallbackUrlBuilder> logger, | ||
IPermissionService permissionService = null, | ||
IUserAccessor userAccessor = null, | ||
IUserClaimCreator userClaimCreator = null, | ||
IHttpContextAccessor httpContextAccessor = null, | ||
IDataProtectionProvider dataProtectionProvider = null) : IReportCallbackInterceptor | ||
{ | ||
public ReportRenderResult InterceptCallback(ReportRenderOptions options, Func<ReportRenderOptions, ReportRenderResult> action) | ||
{ | ||
IImpersonator impersonator = userAccessor as IImpersonator; | ||
ITransientGrantor transientGrantor = permissionService as ITransientGrantor; | ||
bool undoImpersonate = false; | ||
bool undoGrant = false; | ||
try | ||
{ | ||
try | ||
{ | ||
if (dataProtectionProvider != null && | ||
(impersonator != null || transientGrantor != null) && | ||
httpContextAccessor?.HttpContext?.Request?.Cookies?.TryGetValue( | ||
HtmlReportCallbackUrlBuilder.ReportAuthCookieName, out var token) == true && | ||
!string.IsNullOrEmpty(token)) | ||
{ | ||
var protector = dataProtectionProvider.CreateProtector(HtmlReportCallbackUrlBuilder.ReportAuthCookieName); | ||
var tokenBytes = WebEncoders.Base64UrlDecode(token); | ||
var ticket = protector.Unprotect(tokenBytes); | ||
|
||
using var ms = new System.IO.MemoryStream(ticket); | ||
using var br = new System.IO.BinaryReader(ms); | ||
var dt = DateTime.FromBinary(br.ReadInt64()); | ||
if (dt > DateTime.UtcNow) | ||
{ | ||
var username = br.ReadString(); | ||
if (impersonator != null && | ||
!string.IsNullOrEmpty(username) && | ||
userClaimCreator != null && | ||
userAccessor?.User?.Identity?.Name != username) | ||
{ | ||
var principal = userClaimCreator.CreatePrincipal(username, "ReportImpersonation"); | ||
impersonator.Impersonate(principal); | ||
undoImpersonate = true; | ||
} | ||
|
||
if (transientGrantor != null) | ||
{ | ||
var count = br.ReadInt32(); | ||
if (count == -1) | ||
{ | ||
transientGrantor.GrantAll(); | ||
undoGrant = true; | ||
} | ||
else if (count > 0 && count < 10000) | ||
{ | ||
var perms = new string[count]; | ||
for (var i = 0; i < count; i++) | ||
perms[i] = br.ReadString(); | ||
transientGrantor.Grant(perms); | ||
undoGrant = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
// ignore errors while decrypting / deserializing / applying ticket | ||
logger.LogError(ex, "Error decrypting/applying report auth ticket"); | ||
} | ||
|
||
return action(options); | ||
} | ||
finally | ||
{ | ||
if (undoImpersonate) | ||
impersonator.UndoImpersonate(); | ||
if (undoGrant) | ||
transientGrantor.UndoGrant(); | ||
} | ||
} | ||
} |
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