Skip to content

Commit 9be84cf

Browse files
authored
Merge pull request #6 from sventhiel/master
merge scalar into master
2 parents 04031e3 + f52f8af commit 9be84cf

31 files changed

+308
-13538
lines changed

Vaelastrasz.Library.Tests/Vaelastrasz.Library.Tests.csproj

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

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
1313
<PackageReference Include="NUnit" Version="4.4.0" />
1414
<PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
1515
<PackageReference Include="NUnit.Analyzers" Version="4.11.2">
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace Vaelastrasz.Library.Attributes
5+
{
6+
public class ValidEnumAttribute : ValidationAttribute
7+
{
8+
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
9+
{
10+
if (value == null)
11+
return ValidationResult.Success;
12+
13+
var type = value.GetType();
14+
15+
if (!type.IsEnum)
16+
return ValidationResult.Success;
17+
18+
if (!Enum.IsDefined(type, value))
19+
{
20+
return new ValidationResult($"Invalid enum value '{value}' for type '{type.Name}'.");
21+
}
22+
23+
return ValidationResult.Success;
24+
}
25+
}
26+
}

Vaelastrasz.Library/Vaelastrasz.Library.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Authors>Sven Thiel</Authors>
88
<Title>Vaelastrasz.Library</Title>
99
<IncludeSymbols>True</IncludeSymbols>
10-
<Version>5.0.2</Version>
10+
<Version>6.0.0</Version>
1111
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>
1313
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Vaelastrasz.Server.Tests/Vaelastrasz.Server.Tests.csproj

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

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
1313
<PackageReference Include="NUnit" Version="4.4.0" />
1414
<PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
1515
<PackageReference Include="NUnit.Analyzers" Version="4.11.2">

Vaelastrasz.Server/Authentication/BasicAuthenticationHandler.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using LiteDB;
22
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Authorization;
34
using Microsoft.Extensions.Options;
45
using System.Security.Claims;
56
using System.Text;
67
using System.Text.Encodings.Web;
8+
using Vaelastrasz.Library.Exceptions;
79
using Vaelastrasz.Library.Extensions;
810
using Vaelastrasz.Server.Configurations;
911
using Vaelastrasz.Server.Services;
@@ -23,11 +25,17 @@ public BasicAuthenticationHandler(
2325
ConnectionString connectionString) : base(options, logger, encoder)
2426
{
2527
_connectionString = connectionString;
26-
_admins = configuration.GetSection("Admins").Get<List<Admin>>()!;
28+
_admins = configuration.GetSection("Admins").Get<List<Admin>>() ?? [];
2729
}
2830

2931
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
3032
{
33+
var endpoint = Context.GetEndpoint();
34+
if (endpoint?.Metadata?.GetMetadata<IAllowAnonymous>() != null)
35+
{
36+
return AuthenticateResult.NoResult();
37+
}
38+
3139
var authHeader = Request.Headers["Authorization"].ToString();
3240
if (authHeader != null && authHeader.StartsWith("basic", StringComparison.OrdinalIgnoreCase))
3341
{
@@ -76,13 +84,13 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
7684

7785
Response.StatusCode = 401;
7886
Response.Headers.Append("www-authenticate", "Basic Authorization");
79-
return AuthenticateResult.Fail(new UnauthorizedAccessException());
87+
return AuthenticateResult.Fail(new UnauthorizedException());
8088
}
8189
else
8290
{
8391
Response.StatusCode = 401;
8492
Response.Headers.Append("www-authenticate", "Basic Authorization");
85-
return AuthenticateResult.Fail(new UnauthorizedAccessException());
93+
return AuthenticateResult.Fail(new UnauthorizedException());
8694
}
8795
}
8896
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using DataAnnotationsExtensions;
2+
using Microsoft.OpenApi;
3+
using System.ComponentModel.DataAnnotations;
4+
5+
namespace Vaelastrasz.Server.Configurations
6+
{
7+
public class OpenApiInfoConfiguration
8+
{
9+
public string Title { get; set; }
10+
public string Description { get; set; }
11+
12+
[System.ComponentModel.DataAnnotations.Url]
13+
public string TermsOfService { get; set; }
14+
15+
public OpenApiContactConfiguration Contact { get; set; }
16+
17+
public OpenApiLicenseConfiguration License { get; set; }
18+
19+
20+
public OpenApiInfo GetOpenApiInfo()
21+
{
22+
if(Validator.TryValidateObject(this, new ValidationContext(this), null, validateAllProperties: true))
23+
{
24+
return new OpenApiInfo
25+
{
26+
Title = this.Title,
27+
Description = this.Description,
28+
TermsOfService = string.IsNullOrWhiteSpace(this.TermsOfService) ? null : new Uri(this.TermsOfService),
29+
Contact = this.Contact == null ? null : new OpenApiContact
30+
{
31+
Name = this.Contact.Name,
32+
Email = this.Contact.Email,
33+
Url = string.IsNullOrWhiteSpace(this.Contact.Url) ? null : new Uri(this.Contact.Url)
34+
},
35+
License = this.License == null ? null : new OpenApiLicense
36+
{
37+
Name = this.License.Name,
38+
Url = string.IsNullOrWhiteSpace(this.License.Url) ? null : new Uri(this.License.Url)
39+
}
40+
};
41+
}
42+
43+
return new OpenApiInfo();
44+
}
45+
46+
}
47+
48+
public class OpenApiContactConfiguration
49+
{
50+
public string Name { get; set; }
51+
52+
[EmailAddress]
53+
public string Email { get; set; }
54+
55+
[System.ComponentModel.DataAnnotations.Url]
56+
public string Url { get; set; }
57+
}
58+
59+
public class OpenApiLicenseConfiguration
60+
{
61+
public string Name { get; set; }
62+
63+
[System.ComponentModel.DataAnnotations.Url]
64+
public string Url { get; set; }
65+
}
66+
}

Vaelastrasz.Server/Configurations/UpdatePropertiesConfiguration.cs

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

Vaelastrasz.Server/Controllers/AccountsController.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public AccountsController(ILogger<AccountsController> logger, ConnectionString c
3434
/// Weitere Informationen finden Sie in der <see href="https://github.com/sventhiel/Vaelastrasz/tree/master/Vaelastrasz.Server#accounts">Dokumentation</see>.
3535
/// </remarks>
3636
[HttpDelete("accounts/{id}")]
37+
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
38+
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
3739
public async Task<IActionResult> DeleteByIdAsync(long id)
3840
{
3941
if (!User.IsInRole("admin"))
@@ -59,6 +61,8 @@ public async Task<IActionResult> DeleteByIdAsync(long id)
5961
/// Weitere Informationen finden Sie in der <see href="https://github.com/sventhiel/Vaelastrasz/tree/master/Vaelastrasz.Server#accounts">Dokumentation</see>.
6062
/// </remarks>
6163
[HttpGet("accounts")]
64+
[ProducesResponseType(typeof(List<ReadAccountModel>), StatusCodes.Status200OK)]
65+
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
6266
public async Task<IActionResult> GetAsync()
6367
{
6468
if (!User.IsInRole("admin"))
@@ -86,6 +90,8 @@ public async Task<IActionResult> GetAsync()
8690
/// Weitere Informationen finden Sie in der <see href="https://github.com/sventhiel/Vaelastrasz/tree/master/Vaelastrasz.Server#accounts">Dokumentation</see>.
8791
/// </remarks>
8892
[HttpGet("accounts/{id}")]
93+
[ProducesResponseType(typeof(ReadAccountModel), StatusCodes.Status200OK)]
94+
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
8995
public async Task<IActionResult> GetByIdAsync(long id)
9096
{
9197
if (!User.IsInRole("admin"))
@@ -113,6 +119,7 @@ public async Task<IActionResult> GetByIdAsync(long id)
113119
/// </remarks>
114120
[HttpPost("accounts")]
115121
[ProducesResponseType(typeof(ReadAccountModel), StatusCodes.Status201Created)]
122+
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
116123
public async Task<IActionResult> PostAsync(CreateAccountModel model)
117124
{
118125
if (!User.IsInRole("admin"))
@@ -145,6 +152,8 @@ public async Task<IActionResult> PostAsync(CreateAccountModel model)
145152
/// Weitere Informationen finden Sie in der <see href="https://github.com/sventhiel/Vaelastrasz/tree/master/Vaelastrasz.Server#accounts">Dokumentation</see>.
146153
/// </remarks>
147154
[HttpPut("accounts/{id}")]
155+
[ProducesResponseType(typeof(ReadAccountModel), StatusCodes.Status200OK)]
156+
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
148157
public async Task<IActionResult> PutByIdAsync(long id, UpdateAccountModel model)
149158
{
150159
if (!User.IsInRole("admin"))

Vaelastrasz.Server/Controllers/CreateDataCiteModelsController.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,25 @@ namespace Vaelastrasz.Server.Controllers
1111
public class CreateDataCiteModelsController : ControllerBase
1212
{
1313
private readonly ILogger<CreateDataCiteModelsController> _logger;
14-
private UpdateProperties _updateProperties;
1514
private ConnectionString _connectionString;
1615

1716
public CreateDataCiteModelsController(ILogger<CreateDataCiteModelsController> logger, IConfiguration configuration, ConnectionString connectionString)
1817
{
1918
_connectionString = connectionString;
20-
_updateProperties = configuration.GetSection("UpdateProperties").Get<UpdateProperties>()!;
2119
_logger = logger;
2220
}
2321

2422
/// <summary>
25-
///
23+
///
2624
/// </summary>
2725
/// <param name="property">Derzeit wird 'Creators' und 'Contributors' unterstützt.</param>
2826
/// <param name="model"></param>
2927
/// <returns></returns>
3028
[HttpPost("createdatacitemodels/prepare/{property}")]
31-
[ProducesResponseType(typeof(CreateDataCiteModel), StatusCodes.Status201Created)]
29+
[ProducesResponseType(typeof(CreateDataCiteModel), 201)]
3230
public async Task<IActionResult> PostAsync(string property, CreateDataCiteModel model)
3331
{
3432
return Ok(model.Update(property));
3533
}
3634
}
37-
}
35+
}

Vaelastrasz.Server/Controllers/DOIsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class DOIsController : ControllerBase
2020
public DOIsController(ILogger<DataCiteController> logger, IConfiguration configuration, ConnectionString connectionString)
2121
{
2222
_connectionString = connectionString;
23-
_admins = configuration.GetSection("Admins").Get<List<Admin>>()!;
23+
_admins = configuration.GetSection("Admins").Get<List<Admin>>() ?? [];
2424
_logger = logger;
2525
}
2626

0 commit comments

Comments
 (0)