Skip to content

Commit a6761fc

Browse files
authored
Merge pull request #256 from kiebor/master
Updated OpenAPI definition to add JWT bearer and authentication.
2 parents 90e2a4a + fef497b commit a6761fc

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.Hosting;
77
using Microsoft.IdentityModel.Tokens;
8+
using Microsoft.OpenApi.Models;
9+
using Swashbuckle.AspNetCore.SwaggerGen;
810
using System.IdentityModel.Tokens.Jwt;
911

1012
namespace BotSharp.OpenAPI;
@@ -18,8 +20,8 @@ public static class BotSharpOpenApiExtensions
1820
/// <param name="services"></param>
1921
/// <param name="config"></param>
2022
/// <returns></returns>
21-
public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection services,
22-
IConfiguration config,
23+
public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection services,
24+
IConfiguration config,
2325
string[] origins,
2426
IHostEnvironment env,
2527
bool enableValidation)
@@ -62,7 +64,31 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
6264

6365
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
6466
services.AddEndpointsApiExplorer();
65-
services.AddSwaggerGen();
67+
services.AddSwaggerGen(
68+
c =>
69+
{
70+
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
71+
{
72+
In = ParameterLocation.Header,
73+
Description = "Please insert JWT with Bearer into field",
74+
Name = "Authorization",
75+
Type = SecuritySchemeType.ApiKey
76+
});
77+
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
78+
{
79+
new OpenApiSecurityScheme
80+
{
81+
Reference = new OpenApiReference
82+
{
83+
Type = ReferenceType.SecurityScheme,
84+
Id = "Bearer"
85+
}
86+
},
87+
Array.Empty<string>()
88+
}
89+
});
90+
}
91+
);
6692

6793
services.AddHttpContextAccessor();
6894

@@ -94,6 +120,7 @@ public static IApplicationBuilder UseBotSharpOpenAPI(this IApplicationBuilder ap
94120
app.UseCors(policy);
95121

96122
app.UseSwagger();
123+
97124
if (env.IsDevelopment())
98125
{
99126
app.UseSwaggerUI();
@@ -103,7 +130,7 @@ public static IApplicationBuilder UseBotSharpOpenAPI(this IApplicationBuilder ap
103130
app.UseAuthentication();
104131

105132
app.UseRouting();
106-
133+
107134
app.UseAuthorization();
108135

109136
app.UseEndpoints(
@@ -150,3 +177,4 @@ public static IApplicationBuilder UseBotSharpUI(this IApplicationBuilder app, bo
150177
return app;
151178
}
152179
}
180+

src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.ComponentModel.DataAnnotations;
2+
13
namespace BotSharp.OpenAPI.Controllers;
24

35
[Authorize]
@@ -12,10 +14,15 @@ public UserController(IUserService userService)
1214

1315
[AllowAnonymous]
1416
[HttpPost("/token")]
15-
public async Task<ActionResult<Token>> GetToken()
17+
public async Task<ActionResult<Token>> GetToken([FromHeader(Name = "Authorization")][Required] string authcode)
1618
{
17-
var authcode = Request.Headers["Authorization"].ToString();
18-
var token = await _userService.GetToken(authcode.Split(' ')[1]);
19+
if (authcode.Contains(' '))
20+
{
21+
authcode = authcode.Split(' ')[1];
22+
}
23+
24+
var token = await _userService.GetToken(authcode);
25+
1926
if (token == null)
2027
{
2128
return Unauthorized();

0 commit comments

Comments
 (0)