mirror of
https://github.com/freedbygrace/ActiveDirectoryToRestApi.git
synced 2026-07-26 11:28:20 +00:00
Add authentication option
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
namespace ActiveDirectoryToRestApi.Authentication;
|
||||
|
||||
public class ApiKeyAuthFilter : IAuthorizationFilter
|
||||
{
|
||||
private readonly Configuration _configuration;
|
||||
|
||||
public ApiKeyAuthFilter(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration.GetSection(nameof(Configuration)).Get<Configuration>();
|
||||
}
|
||||
|
||||
public void OnAuthorization(AuthorizationFilterContext context)
|
||||
{
|
||||
if (_configuration.AuthEnabled)
|
||||
{
|
||||
if (!context.HttpContext.Request.Headers.TryGetValue("Authorization", out var extractedAuthToken))
|
||||
{
|
||||
context.Result = new UnauthorizedObjectResult("Authorization token was not provided");
|
||||
return;
|
||||
}
|
||||
|
||||
var authToken = _configuration.Authorization;
|
||||
|
||||
if (!authToken.Equals(extractedAuthToken))
|
||||
{
|
||||
context.Result = new UnauthorizedObjectResult("Unauthorized client");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace ActiveDirectoryToRestApi.Configurations;
|
||||
|
||||
public class Configuration
|
||||
{
|
||||
public string Authorization { get; set; }
|
||||
public bool AuthEnabled { get; set; }
|
||||
}
|
||||
@@ -2,6 +2,7 @@ namespace ActiveDirectoryToRestApi.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
[ServiceFilter(typeof(ApiKeyAuthFilter))]
|
||||
public class GroupController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<GroupController> _logger;
|
||||
|
||||
@@ -2,6 +2,7 @@ namespace ActiveDirectoryToRestApi.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
[ServiceFilter(typeof(ApiKeyAuthFilter))]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<UserController> _logger;
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
global using ActiveDirectoryToRestApi.Configurations;
|
||||
global using ActiveDirectoryToRestApi.Authentication;
|
||||
global using ActiveDirectoryToRestApi.Configurations;
|
||||
global using ActiveDirectoryToRestApi.Extensions;
|
||||
global using ActiveDirectoryToRestApi.Models;
|
||||
global using ActiveDirectoryToRestApi.Services;
|
||||
global using Microsoft.AspNetCore.Mvc;
|
||||
global using Microsoft.AspNetCore.Mvc.Filters;
|
||||
global using Microsoft.OpenApi.Models;
|
||||
global using System.ComponentModel;
|
||||
global using System.ComponentModel.DataAnnotations;
|
||||
global using System.DirectoryServices.Protocols;
|
||||
|
||||
@@ -2,7 +2,31 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSwaggerGen(s =>
|
||||
{
|
||||
s.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||
{
|
||||
Description = "Enter the token with the `Bearer` prefix, e.g. \"Bearer a1b2c3d4\"",
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Name = "Authorization",
|
||||
In = ParameterLocation.Header,
|
||||
Scheme = "ApiKeyScheme",
|
||||
});
|
||||
var scheme = new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "Bearer"
|
||||
},
|
||||
In = ParameterLocation.Header
|
||||
};
|
||||
var requirement = new OpenApiSecurityRequirement
|
||||
{
|
||||
{ scheme, new List<string>() }
|
||||
};
|
||||
s.AddSecurityRequirement(requirement);
|
||||
});
|
||||
builder.Services.AddSingleton(provider =>
|
||||
{
|
||||
var configs = builder.Configuration.GetSection(nameof(LdapConfigs)).Get<LdapConfigs>();
|
||||
@@ -18,6 +42,7 @@ builder.Services.AddSingleton(provider =>
|
||||
});
|
||||
builder.Services.AddSingleton<LdapService>();
|
||||
builder.Services.AddTransient<AuthUserService>();
|
||||
builder.Services.AddScoped<ApiKeyAuthFilter>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@@ -12,5 +12,9 @@
|
||||
"User": "ldapAdmin",
|
||||
"Pass": "Thi$1sMyPassw0rd",
|
||||
"DistinguishedName": "DC=ad,DC=zimbres,DC=com"
|
||||
},
|
||||
"Configuration": {
|
||||
"Authorization": "Bearer v63M9FlOhqAohJ7oGmFrhVIj",
|
||||
"AuthEnabled": "false"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user