mirror of
https://github.com/spacebarchat/server.git
synced 2026-10-24 02:03:03 +00:00
Local changes
This commit is contained in:
1 parent
a632666203
commit
abb1b570a4
73 files changed
+2067
-64
No files matched your search
@@ -1,22 +1,22 @@
|
||||
using System.Buffers.Text;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using ArcaneLibs.Extensions;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Spacebar.AdminAPI.Extensions;
|
||||
using Spacebar.Db.Contexts;
|
||||
using Spacebar.Db.Models;
|
||||
|
||||
namespace Spacebar.AdminAPI.Middleware;
|
||||
|
||||
public class AuthenticationMiddleware(RequestDelegate next) {
|
||||
public async Task Invoke(HttpContext context) {
|
||||
if(Environment.GetEnvironmentVariable("SB_ADMIN_API_DISABLE_AUTH") == "true") {
|
||||
private static Dictionary<string, User> _userCache = new();
|
||||
private static Dictionary<string, DateTime> _userCacheExpiry = new();
|
||||
|
||||
public async Task InvokeAsync(HttpContext context, IServiceProvider sp) {
|
||||
if (context.Request.Path.StartsWithSegments("/ping")) {
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!context.Request.Headers.ContainsKey("Authorization")) {
|
||||
context.Response.StatusCode = 401;
|
||||
await context.Response.WriteAsync("Authorization header is missing");
|
||||
@@ -46,8 +46,33 @@ public class AuthenticationMiddleware(RequestDelegate next) {
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine(res.ClaimsIdentity.Claims.Select(x => $"{x.Type} : {x.Value}").ToJson());
|
||||
User user;
|
||||
if (_userCacheExpiry.ContainsKey(token) && _userCacheExpiry[token] < DateTime.Now) {
|
||||
_userCache.Remove(token);
|
||||
_userCacheExpiry.Remove(token);
|
||||
}
|
||||
|
||||
if (!_userCache.ContainsKey(token)) {
|
||||
var db = sp.GetRequiredService<SpacebarDbContext>();
|
||||
user = await db.Users.FindAsync(res.ClaimsIdentity.Claims.First(x => x.Type == "id").Value)
|
||||
?? throw new InvalidOperationException();
|
||||
_userCache[token] = user;
|
||||
_userCacheExpiry[token] = DateTime.Now.AddMinutes(5);
|
||||
}
|
||||
|
||||
user = _userCache[token];
|
||||
if (user.Disabled) {
|
||||
context.Response.StatusCode = 403;
|
||||
await context.Response.WriteAsync("User is disabled");
|
||||
return;
|
||||
}
|
||||
|
||||
if (user.Deleted) {
|
||||
context.Response.StatusCode = 403;
|
||||
await context.Response.WriteAsync("User is deleted");
|
||||
return;
|
||||
}
|
||||
|
||||
await next(context);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user