Skip to content

Commit c207345

Browse files
HugoMariofrantuma
authored andcommitted
updated sample
1 parent f6bd6c1 commit c207345

File tree

9 files changed

+107
-89
lines changed

9 files changed

+107
-89
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.3-SNAPSHOT
1+
2.4.6-SNAPSHOT

samples/server/petstore/aspnetcore/src/IO.Swagger/Controllers/PetApi.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,23 @@
1010

1111
using System;
1212
using System.Collections.Generic;
13-
using System.Linq;
14-
using System.Net;
15-
using System.Threading.Tasks;
16-
using Microsoft.AspNetCore.Http;
1713
using Microsoft.AspNetCore.Mvc;
18-
using Microsoft.AspNetCore.WebUtilities;
19-
using Microsoft.Extensions.Logging;
20-
using Microsoft.Extensions.Primitives;
14+
using Swashbuckle.AspNetCore.Annotations;
2115
using Swashbuckle.AspNetCore.SwaggerGen;
2216
using Newtonsoft.Json;
2317
using System.ComponentModel.DataAnnotations;
2418
using IO.Swagger.Attributes;
19+
using IO.Swagger.Security;
20+
using Microsoft.AspNetCore.Authorization;
2521
using IO.Swagger.Models;
2622

2723
namespace IO.Swagger.Controllers
2824
{
2925
/// <summary>
3026
///
3127
/// </summary>
32-
public class PetApiController : Controller
28+
[ApiController]
29+
public class PetApiController : ControllerBase
3330
{
3431
/// <summary>
3532
/// Add a new pet to the store
@@ -142,6 +139,7 @@ public virtual IActionResult FindPetsByTags([FromQuery][Required()]List<string>
142139
/// <response code="404">Pet not found</response>
143140
[HttpGet]
144141
[Route("/v2/pet/{petId}")]
142+
[Authorize(AuthenticationSchemes = ApiKeyAuthenticationHandler.SchemeName)]
145143
[ValidateModelState]
146144
[SwaggerOperation("GetPetById")]
147145
[SwaggerResponse(statusCode: 200, type: typeof(Pet), description: "successful operation")]

samples/server/petstore/aspnetcore/src/IO.Swagger/Controllers/StoreApi.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,23 @@
1010

1111
using System;
1212
using System.Collections.Generic;
13-
using System.Linq;
14-
using System.Net;
15-
using System.Threading.Tasks;
16-
using Microsoft.AspNetCore.Http;
1713
using Microsoft.AspNetCore.Mvc;
18-
using Microsoft.AspNetCore.WebUtilities;
19-
using Microsoft.Extensions.Logging;
20-
using Microsoft.Extensions.Primitives;
14+
using Swashbuckle.AspNetCore.Annotations;
2115
using Swashbuckle.AspNetCore.SwaggerGen;
2216
using Newtonsoft.Json;
2317
using System.ComponentModel.DataAnnotations;
2418
using IO.Swagger.Attributes;
19+
using IO.Swagger.Security;
20+
using Microsoft.AspNetCore.Authorization;
2521
using IO.Swagger.Models;
2622

2723
namespace IO.Swagger.Controllers
2824
{
2925
/// <summary>
3026
///
3127
/// </summary>
32-
public class StoreApiController : Controller
28+
[ApiController]
29+
public class StoreApiController : ControllerBase
3330
{
3431
/// <summary>
3532
/// Delete purchase order by ID
@@ -61,6 +58,7 @@ public virtual IActionResult DeleteOrder([FromRoute][Required]string orderId)
6158
/// <response code="200">successful operation</response>
6259
[HttpGet]
6360
[Route("/v2/store/inventory")]
61+
[Authorize(AuthenticationSchemes = ApiKeyAuthenticationHandler.SchemeName)]
6462
[ValidateModelState]
6563
[SwaggerOperation("GetInventory")]
6664
[SwaggerResponse(statusCode: 200, type: typeof(Dictionary<string, int?>), description: "successful operation")]

samples/server/petstore/aspnetcore/src/IO.Swagger/Controllers/UserApi.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,23 @@
1010

1111
using System;
1212
using System.Collections.Generic;
13-
using System.Linq;
14-
using System.Net;
15-
using System.Threading.Tasks;
16-
using Microsoft.AspNetCore.Http;
1713
using Microsoft.AspNetCore.Mvc;
18-
using Microsoft.AspNetCore.WebUtilities;
19-
using Microsoft.Extensions.Logging;
20-
using Microsoft.Extensions.Primitives;
14+
using Swashbuckle.AspNetCore.Annotations;
2115
using Swashbuckle.AspNetCore.SwaggerGen;
2216
using Newtonsoft.Json;
2317
using System.ComponentModel.DataAnnotations;
2418
using IO.Swagger.Attributes;
19+
using IO.Swagger.Security;
20+
using Microsoft.AspNetCore.Authorization;
2521
using IO.Swagger.Models;
2622

2723
namespace IO.Swagger.Controllers
2824
{
2925
/// <summary>
3026
///
3127
/// </summary>
32-
public class UserApiController : Controller
28+
[ApiController]
29+
public class UserApiController : ControllerBase
3330
{
3431
/// <summary>
3532
/// Create user

samples/server/petstore/aspnetcore/src/IO.Swagger/IO.Swagger.csproj

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,18 @@
22
<PropertyGroup>
33
<Description>IO.Swagger</Description>
44
<Copyright>IO.Swagger</Copyright>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<PreserveCompilationContext>true</PreserveCompilationContext>
88
<AssemblyName>IO.Swagger</AssemblyName>
99
<PackageId>IO.Swagger</PackageId>
1010
</PropertyGroup>
1111
<ItemGroup>
12-
<Folder Include="wwwroot\"/>
12+
<PackageReference Include="Microsoft.AspNetCore.App" />
13+
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0"/>
14+
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" />
1315
</ItemGroup>
1416
<ItemGroup>
15-
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3"/>
16-
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.1.0"/>
17-
<PackageReference Include="Newtonsoft.Json" Version="10.0.3"/>
18-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1"/>
19-
</ItemGroup>
20-
<ItemGroup>
21-
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0"/>
22-
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1"/>
23-
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0"/>
17+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
2418
</ItemGroup>
2519
</Project>
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
61
using Microsoft.AspNetCore.Hosting;
7-
using Microsoft.AspNetCore.Builder;
82
using Microsoft.AspNetCore;
93

104
namespace IO.Swagger
@@ -20,17 +14,16 @@ public class Program
2014
/// <param name="args"></param>
2115
public static void Main(string[] args)
2216
{
23-
BuildWebHost(args).Run();
17+
CreateWebHostBuilder(args).Build().Run();
2418
}
2519

2620
/// <summary>
27-
/// Build Web Host
21+
/// Create the web host builder.
2822
/// </summary>
2923
/// <param name="args"></param>
30-
/// <returns>Webhost</returns>
31-
public static IWebHost BuildWebHost(string[] args) =>
24+
/// <returns>IWebHostBuilder</returns>
25+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
3226
WebHost.CreateDefaultBuilder(args)
33-
.UseStartup<Startup>()
34-
.Build();
27+
.UseStartup<Startup>();
3528
}
3629
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Net.Http.Headers;
3+
using System.Security.Claims;
4+
using System.Text;
5+
using System.Text.Encodings.Web;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Authentication;
8+
using Microsoft.Extensions.Logging;
9+
using Microsoft.Extensions.Options;
10+
11+
namespace IO.Swagger.Security
12+
{
13+
/// <summary>
14+
/// class to handle api_key security.
15+
/// </summary>
16+
public class ApiKeyAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
17+
{
18+
/// <summary>
19+
/// scheme name for authentication handler.
20+
/// </summary>
21+
public const string SchemeName = "ApiKey";
22+
23+
public ApiKeyAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
24+
{
25+
}
26+
27+
/// <summary>
28+
/// verify that require api key header exist and handle authorization.
29+
/// </summary>
30+
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
31+
{
32+
if (!Request.Headers.ContainsKey("api_key"))
33+
{
34+
return AuthenticateResult.Fail("Missing Authorization Header");
35+
}
36+
37+
// do magic here!
38+
39+
var claims = new[] {
40+
new Claim(ClaimTypes.NameIdentifier, "changeme"),
41+
new Claim(ClaimTypes.Name, "changeme"),
42+
};
43+
var identity = new ClaimsIdentity(claims, Scheme.Name);
44+
var principal = new ClaimsPrincipal(identity);
45+
var ticket = new AuthenticationTicket(principal, Scheme.Name);
46+
47+
return AuthenticateResult.Success(ticket);
48+
}
49+
}
50+
}

samples/server/petstore/aspnetcore/src/IO.Swagger/Startup.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
using Swashbuckle.AspNetCore.Swagger;
2121
using Swashbuckle.AspNetCore.SwaggerGen;
2222
using IO.Swagger.Filters;
23+
using IO.Swagger.Security;
24+
25+
using Microsoft.AspNetCore.Authentication;
2326

2427
namespace IO.Swagger
2528
{
@@ -58,7 +61,12 @@ public void ConfigureServices(IServiceCollection services)
5861
opts.SerializerSettings.Converters.Add(new StringEnumConverter {
5962
CamelCaseText = true
6063
});
61-
});
64+
})
65+
.AddXmlSerializerFormatters();
66+
67+
services.AddAuthentication(ApiKeyAuthenticationHandler.SchemeName)
68+
.AddScheme<AuthenticationSchemeOptions, ApiKeyAuthenticationHandler>(ApiKeyAuthenticationHandler.SchemeName, null);
69+
6270

6371
services
6472
.AddSwaggerGen(c =>

0 commit comments

Comments
 (0)