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 Swagger to allow for authorization through bearer token. Adde… #51

Merged
merged 1 commit into from
Dec 3, 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
51 changes: 51 additions & 0 deletions Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace SimpleWebAppReact.Controllers;

using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

/* A work in progress controller that wraps basic Okta provided endpoints, such as /token. will be only used for development */
[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IConfiguration _configuration;

public AuthController(IHttpClientFactory httpClientFactory, IConfiguration configuration)
{
_httpClientFactory = httpClientFactory;
_configuration = configuration;
}

[HttpPost("get-token")]
public async Task<IActionResult> GetToken([FromBody] LoginRequest request)
{
var client = _httpClientFactory.CreateClient("Okta");

var body = new StringBuilder();
body.Append($"grant_type=password");
body.Append($"&username={request.Username}");
body.Append($"&password={request.Password}");
body.Append($"&scope=openid");

var content = new StringContent(body.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded");

var response = await client.PostAsync("v1/token", content);

if (response.IsSuccessStatusCode)
{
var tokenResponse = await response.Content.ReadAsStringAsync();
return Ok(tokenResponse);
}

return BadRequest("Failed to retrieve token");
}
}

public class LoginRequest
{
public string Username { get; set; }

Check warning on line 49 in Controllers/AuthController.cs

View workflow job for this annotation

GitHub Actions / test

Non-nullable property 'Username' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string Password { get; set; }

Check warning on line 50 in Controllers/AuthController.cs

View workflow job for this annotation

GitHub Actions / test

Non-nullable property 'Password' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
36 changes: 33 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using SimpleWebAppReact;
using SimpleWebAppReact.Services;

Expand All @@ -13,10 +15,39 @@
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo()
{
Title = "Housing Backend Auth",
Version = "v1"
});
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
In = ParameterLocation.Header,
Description = "Please enter bearer token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new List<string>()
}
});
});
builder.Services.AddSingleton<MongoDbService>();
builder.Services.AddHttpClient<BuildingOutlineService>();

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
Expand All @@ -39,7 +70,6 @@
{
app.UseHsts();
}

// Configure logging
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Application started.");
Expand Down
Loading