Skip to content

Commit d5740d6

Browse files
committed
Allows to disable authorization for internal actions
1 parent e459762 commit d5740d6

File tree

25 files changed

+116
-44
lines changed

25 files changed

+116
-44
lines changed

src/DragonFly.API.Core/ClientAPI/AssetApiStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public async Task<Result> UploadAsync(Guid assetId, string mimetype, Stream stre
3939
return await Client.SendAsync(requestMessage).ReadResultFromJsonAsync();
4040
}
4141

42-
public async Task<Result<Stream>> GetStreamAsync(Asset asset)
42+
public async Task<Result<Stream>> GetStreamAsync(Guid assetId)
4343
{
44-
return await Client.GetStreamAsync($"api/asset/{asset.Id}/download");
44+
return await Client.GetStreamAsync($"api/asset/{assetId}/download");
4545
}
4646

4747
public async Task<Result<Asset?>> GetAssetAsync(Guid id)

src/DragonFly.API.Core/DragonFly.API.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
</ItemGroup>
4545

4646
<ItemGroup>
47-
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.0" />
48-
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
47+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.1" />
48+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.1" />
4949
</ItemGroup>
5050
</Project>

src/DragonFly.API/Rest/AssetApiExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private static async Task<IResult> MapDownload(HttpContext context, IAssetStorag
104104

105105
context.Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue() { Public = true, MaxAge = TimeSpan.FromDays(30) };
106106

107-
Stream assetStream = await storage.GetStreamAsync(asset);
107+
Stream assetStream = await storage.GetStreamAsync(asset.Id);
108108

109109
return TypedResults.Stream(assetStream, contentType: asset.MimeType, entityTag: etag, enableRangeProcessing: true);
110110
}

src/DragonFly.App.WebAssembly.Client/DragonFly.App.WebAssembly.Client.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
10+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.1" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

src/DragonFly.AspNetCore.WebAssembly/DragonFly.AspNetCore.WebAssembly.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
</ItemGroup>
3838

3939
<ItemGroup>
40-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.0" />
40+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.1" />
4141
</ItemGroup>
4242
</Project>

src/DragonFly.AspNetCore/Permissions/AuthorizationExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// MIT License
44

55
using DragonFly.AspNetCore;
6+
using DragonFly.AspNetCore.Permissions;
67
using Microsoft.AspNetCore.Authorization;
78
using SmartResults;
89
using System.Security.Claims;
@@ -32,4 +33,11 @@ public static async Task<Result> AuthorizeAsync(this IAuthorizationService autho
3233
return Result.Failed(new PermissionError(permission));
3334
}
3435
}
36+
37+
public static IDisposable DisableAuthorization(this IDragonFlyApi api)
38+
{
39+
var context = api.ServiceProvider.GetRequiredService<IPrincipalContext>();
40+
41+
return new DisableAuthorization(context);
42+
}
3543
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) usercode
2+
// https://github.com/usercode/DragonFly
3+
// MIT License
4+
5+
using System.Security.Claims;
6+
7+
namespace DragonFly.AspNetCore.Permissions;
8+
9+
public class DisableAuthorization : IDisposable
10+
{
11+
public DisableAuthorization(IPrincipalContext context)
12+
{
13+
Context = context;
14+
OldPrincipal = Context.Current;
15+
16+
Context.Current = null;
17+
}
18+
19+
/// <summary>
20+
/// Context
21+
/// </summary>
22+
private IPrincipalContext Context { get; }
23+
24+
/// <summary>
25+
/// OldPrincipal
26+
/// </summary>
27+
private ClaimsPrincipal? OldPrincipal { get; }
28+
29+
public void Dispose()
30+
{
31+
Context.Current = OldPrincipal;
32+
}
33+
}

src/DragonFly.AspNetCore/Permissions/Storages/AssetPermissionStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public async Task<Result> DeleteAsync(Asset asset)
5656
return await Api.AuthorizeAsync(PrincipalContext.Current, AssetPermissions.ReadAsset).ThenAsync(x => Storage.GetAssetAsync(id));
5757
}
5858

59-
public async Task<Result<Stream>> GetStreamAsync(Asset asset)
59+
public async Task<Result<Stream>> GetStreamAsync(Guid assetId)
6060
{
61-
return await Api.AuthorizeAsync(PrincipalContext.Current, AssetPermissions.DownloadAsset).ThenAsync(x => Storage.GetStreamAsync(asset));
61+
return await Api.AuthorizeAsync(PrincipalContext.Current, AssetPermissions.DownloadAsset).ThenAsync(x => Storage.GetStreamAsync(assetId));
6262
}
6363

6464
public async Task<Result> PublishAsync(Asset asset)

src/DragonFly.Client.WebAssembly/DragonFly.Client.WebAssembly.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
</ItemGroup>
4141

4242
<ItemGroup>
43-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
43+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.1" />
4444
</ItemGroup>
4545
</Project>

src/DragonFly.Client/DragonFly.Client.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050
</ItemGroup>
5151

5252
<ItemGroup>
53-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="9.0.0" />
54-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="9.0.0" />
53+
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="9.0.1" />
54+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="9.0.1" />
5555
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.175" PrivateAssets="All" />
5656
</ItemGroup>
5757

5858
<ItemGroup>
59-
<PackageReference Include="BlazorStrap" Version="5.2.103.122024" />
59+
<PackageReference Include="BlazorStrap" Version="5.2.103.250102" />
6060
<PackageReference Include="BlazorStrap.V5" Version="5.2.103.122024" />
6161
</ItemGroup>
6262

0 commit comments

Comments
 (0)