Skip to content

Commit 154a19c

Browse files
committed
2 parents 70f203a + e11fadd commit 154a19c

25 files changed

Lines changed: 161 additions & 69 deletions

File tree

Sources/EasyExtensions.AspNetCore.Authorization/Controllers/BaseAuthController.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,17 @@ public virtual TimeSpan GetCookieExpirationTime()
379379
return TimeSpan.FromDays(30);
380380
}
381381

382+
/// <summary>
383+
/// Retrieves additional token claims for the specified user to be included in authentication tokens.
384+
/// </summary>
385+
/// <param name="userId">The unique identifier of the user for whom to retrieve additional token claims.</param>
386+
/// <returns>An enumerable collection of key-value pairs representing additional claims to be added to the user's
387+
/// authentication token. The collection is empty if no additional claims are available.</returns>
388+
public virtual IEnumerable<KeyValuePair<string, string>> GetAdditionalTokenClaims(Guid userId)
389+
{
390+
return [];
391+
}
392+
382393
private string CreateAccessToken(Guid userId, IEnumerable<string> roles)
383394
{
384395
return _tokenProvider.CreateToken(cb =>
@@ -388,6 +399,10 @@ private string CreateAccessToken(Guid userId, IEnumerable<string> roles)
388399
{
389400
cb.Add(ClaimTypes.Role, role);
390401
}
402+
foreach (var claim in GetAdditionalTokenClaims(userId))
403+
{
404+
cb.Add(claim.Key, claim.Value);
405+
}
391406
return cb;
392407
});
393408
}

Sources/EasyExtensions.AspNetCore.Authorization/EasyExtensions.AspNetCore.Authorization.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="LICENSE.md" />
3232
<ProjectReference Include="..\EasyExtensions.AspNetCore\EasyExtensions.AspNetCore.csproj" />
3333
<ProjectReference Include="..\EasyExtensions\EasyExtensions.csproj" />
34-
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="10.0.2" />
35-
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.2" />
36-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.2" />
37-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.102" PrivateAssets="All" />
34+
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="10.0.3" />
35+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.3" />
36+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.3" />
37+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.103" PrivateAssets="All" />
3838
</ItemGroup>
3939

4040
</Project>

Sources/EasyExtensions.AspNetCore.Sentry/EasyExtensions.AspNetCore.Sentry.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
<None Include="packageIcon.png" Pack="true" PackagePath="\" />
3131
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="LICENSE.md" />
3232
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
33-
<PackageReference Include="Sentry.AspNetCore" Version="6.0.0" />
33+
<PackageReference Include="Sentry.AspNetCore" Version="6.1.0" />
3434
<ProjectReference Include="..\EasyExtensions.AspNetCore\EasyExtensions.AspNetCore.csproj" />
35-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.102" PrivateAssets="All" />
35+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.103" PrivateAssets="All" />
3636
</ItemGroup>
3737

3838
</Project>

Sources/EasyExtensions.AspNetCore.Stack/EasyExtensions.AspNetCore.Stack.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<ProjectReference Include="..\EasyExtensions.EntityFrameworkCore\EasyExtensions.EntityFrameworkCore.csproj" />
3636
<ProjectReference Include="..\EasyExtensions.Quartz\EasyExtensions.Quartz.csproj" />
3737
<PackageReference Include="EasyVault" Version="1.0.10" />
38-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.102" PrivateAssets="All" />
38+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.103" PrivateAssets="All" />
3939
</ItemGroup>
4040

4141
</Project>

Sources/EasyExtensions.AspNetCore/EasyExtensions.AspNetCore.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="LICENSE.md" />
3232
<ProjectReference Include="..\EasyExtensions\EasyExtensions.csproj" />
3333
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.3.9" />
34-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.102" PrivateAssets="All" />
35-
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="10.0.2" />
36-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.2" />
34+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.103" PrivateAssets="All" />
35+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="10.0.3" />
36+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.3" />
3737
</ItemGroup>
3838

3939
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025–2026 Vadim Belov <https://belov.us>
3+
4+
using System.Net;
5+
6+
namespace EasyExtensions.AspNetCore.Exceptions
7+
{
8+
/// <summary>
9+
/// Represents an exception that is thrown when access to a specified object is denied due to insufficient
10+
/// permissions.
11+
/// </summary>
12+
/// <param name="objectName">The name of the object for which access was denied. This value is used to identify the resource involved in the
13+
/// access violation.</param>
14+
/// <param name="message">The error message that explains the reason for the exception. If not specified, a default message of "Access
15+
/// denied" is used.</param>
16+
public class AccessDeniedException(string objectName, string message = "Access denied")
17+
: WebApiException(HttpStatusCode.Forbidden, objectName, message) { }
18+
19+
/// <summary>
20+
/// Represents an exception that is thrown when access to a resource of type T is denied, typically corresponding to
21+
/// an HTTP 403 Forbidden response.
22+
/// </summary>
23+
/// <typeparam name="T">The type of the resource for which access was denied.</typeparam>
24+
/// <param name="message">The error message that describes the reason for the access denial.</param>
25+
public class AccessDeniedException<T>(string message = "Access denied")
26+
: AccessDeniedException(typeof(T).Name, message) { }
27+
}

Sources/EasyExtensions.AspNetCore/Exceptions/AccessException.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

Sources/EasyExtensions.AspNetCore/Exceptions/BadRequestException.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@
66
namespace EasyExtensions.AspNetCore.Exceptions
77
{
88
/// <summary>
9-
/// Represents an exception that is thrown when a request is unauthorized or invalid for a specified object.
9+
/// Represents an exception that is thrown to indicate a bad request (HTTP 400) in a web API operation.
1010
/// </summary>
11-
/// <param name="objectName">The name of the object associated with the unauthorized or invalid request.</param>
12-
public class BadRequestException(string objectName)
13-
: WebApiException(HttpStatusCode.BadRequest, objectName, "Bad request")
14-
{ }
11+
/// <param name="objectName">The name of the object or entity associated with the bad request. This value is used to provide context for the
12+
/// error.</param>
13+
/// <param name="message">The error message that describes the reason for the bad request. The default is "Bad request".</param>
14+
public class BadRequestException(string objectName, string message = "Bad request")
15+
: WebApiException(HttpStatusCode.BadRequest, objectName, message) { }
16+
17+
/// <summary>
18+
/// Represents an exception that is thrown to indicate a bad request error associated with a specific resource type.
19+
/// </summary>
20+
/// <typeparam name="T">The type of the resource or entity related to the bad request.</typeparam>
21+
/// <param name="message">The error message that describes the reason for the bad request. The default is "Bad request".</param>
22+
public class BadRequestException<T>(string message = "Bad request")
23+
: BadRequestException(typeof(T).Name, message) { }
1524
}

Sources/EasyExtensions.AspNetCore/Exceptions/DuplicateException.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ namespace EasyExtensions.AspNetCore.Exceptions
1212
/// to indicate that a resource with the specified name already exists. Use this exception to signal duplicate
1313
/// creation attempts in resource management scenarios.</remarks>
1414
/// <param name="objectName">The name of the object that caused the conflict.</param>
15-
public class DuplicateException(string objectName)
16-
: WebApiException(HttpStatusCode.Conflict, objectName, "Object already exists")
17-
{ }
15+
/// <param name="message">The error message that describes the reason for the conflict. The default is "Object already exists."</param>
16+
public class DuplicateException(string objectName, string message = "Object already exists")
17+
: WebApiException(HttpStatusCode.Conflict, objectName, message) { }
18+
19+
/// <summary>
20+
/// Represents an exception that is thrown when an attempt is made to create or add a duplicate object of the
21+
/// specified type.
22+
/// </summary>
23+
/// <typeparam name="T">The type of the object that caused the duplication error.</typeparam>
24+
/// <param name="message">The error message that explains the reason for the exception. The default is "Object already exists."</param>
25+
public class DuplicateException<T>(string message = "Object already exists")
26+
: DuplicateException(typeof(T).Name, message) { }
1827
}

Sources/EasyExtensions.AspNetCore/Exceptions/EntityNotFoundException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ namespace EasyExtensions.AspNetCore.Exceptions
1010
/// </summary>
1111
/// <param name="objectName">The name of the entity that was not found. This value is included in the exception details to identify the
1212
/// missing entity.</param>
13-
public class EntityNotFoundException(string objectName)
14-
: WebApiException(HttpStatusCode.NotFound, objectName, "Entity was not found")
15-
{ }
13+
/// <param name="message">The error message that describes the reason for the exception. The default is "Entity was not found".</param>
14+
public class EntityNotFoundException(string objectName, string message = "Entity was not found")
15+
: WebApiException(HttpStatusCode.NotFound, objectName, message) { }
16+
17+
/// <summary>
18+
/// Represents an exception that is thrown when an entity of the specified type cannot be found.
19+
/// </summary>
20+
/// <typeparam name="T">The type of the entity that was not found.</typeparam>
21+
/// <param name="message">The error message that explains the reason for the exception. If not specified, a default message is used.</param>
22+
public class EntityNotFoundException<T>(string message = "Entity was not found")
23+
: EntityNotFoundException(typeof(T).Name, message) { }
1624
}

0 commit comments

Comments
 (0)