-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from Tynab/develop
Develop
- Loading branch information
Showing
7 changed files
with
68 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
host/YANLib.HttpApi.Host/Middlewares/SwaggerBasicAuthMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using System.Threading.Tasks; | ||
using YANLib.Core; | ||
using static System.Convert; | ||
using static System.DateTime; | ||
using static System.Net.HttpStatusCode; | ||
using static System.StringSplitOptions; | ||
using static System.Text.Encoding; | ||
|
||
namespace YANLib.Middlewares; | ||
|
||
public class SwaggerBasicAuthMiddleware(RequestDelegate next, IConfiguration configuration) | ||
{ | ||
private readonly RequestDelegate _next = next; | ||
private readonly IConfiguration _configuration = configuration; | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
if (context.Request.Path.StartsWithSegments("/swagger")) | ||
{ | ||
string authHeader = context.Request.Headers["Authorization"]; | ||
|
||
if (authHeader.IsNotWhiteSpaceAndNull() && authHeader.StartsWith("Basic ")) | ||
{ | ||
var decoded = UTF8.GetString(FromBase64String(authHeader.Split(' ', 2, RemoveEmptyEntries)[1]?.Trim()))?.Split(':', 2); | ||
|
||
if (IsAuthorized(decoded[0], decoded[1])) | ||
{ | ||
await _next.Invoke(context); | ||
|
||
return; | ||
} | ||
} | ||
|
||
context.Response.Headers["WWW-Authenticate"] = "Basic"; | ||
context.Response.StatusCode = Unauthorized.ToInt(); | ||
} | ||
else | ||
{ | ||
await _next.Invoke(context); | ||
} | ||
} | ||
|
||
private bool IsAuthorized(string username, string password) => username.Equals($"{_configuration["Auth:Username"]}{Today.Day}") && password.Equals($"{_configuration["Auth:Password"]}{Now.Minute}"); | ||
} |
61 changes: 0 additions & 61 deletions
61
host/YANLib.HttpApi.Host/Middlewares/UnauthorizedHandlerMiddleware.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters