Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated OpenAPI definition to add JWT bearer and authentication. #256

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.IdentityModel.Tokens.Jwt;

namespace BotSharp.OpenAPI;
Expand All @@ -18,8 +20,8 @@ public static class BotSharpOpenApiExtensions
/// <param name="services"></param>
/// <param name="config"></param>
/// <returns></returns>
public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection services,
IConfiguration config,
public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection services,
IConfiguration config,
string[] origins,
IHostEnvironment env,
bool enableValidation)
Expand Down Expand Up @@ -62,7 +64,31 @@ public static class BotSharpOpenApiExtensions

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddSwaggerGen(
c =>
{
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT with Bearer into field",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
}
);

services.AddHttpContextAccessor();

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

app.UseSwagger();

if (env.IsDevelopment())
{
app.UseSwaggerUI();
Expand All @@ -103,7 +130,7 @@ public static IApplicationBuilder UseBotSharpOpenAPI(this IApplicationBuilder ap
app.UseAuthentication();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(
Expand Down Expand Up @@ -150,3 +177,4 @@ public static IApplicationBuilder UseBotSharpUI(this IApplicationBuilder app, bo
return app;
}
}

13 changes: 10 additions & 3 deletions src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.ComponentModel.DataAnnotations;

namespace BotSharp.OpenAPI.Controllers;

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

[AllowAnonymous]
[HttpPost("/token")]
public async Task<ActionResult<Token>> GetToken()
public async Task<ActionResult<Token>> GetToken([FromHeader(Name = "Authorization")][Required] string authcode)
{
var authcode = Request.Headers["Authorization"].ToString();
var token = await _userService.GetToken(authcode.Split(' ')[1]);
if (authcode.Contains(' '))
{
authcode = authcode.Split(' ')[1];
}

var token = await _userService.GetToken(authcode);

if (token == null)
{
return Unauthorized();
Expand Down