adjust server to use authentication where needed
This commit is contained in:
2
MareAPI
2
MareAPI
Submodule MareAPI updated: 714d990c0b...50a447c4d0
@@ -7,6 +7,8 @@ using System.Text.Encodings.Web;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MareSynchronosServer.Data;
|
using MareSynchronosServer.Data;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
@@ -20,8 +22,13 @@ namespace MareSynchronosServer.Authentication
|
|||||||
|
|
||||||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||||
{
|
{
|
||||||
if (!Request.Headers.ContainsKey("Authorization"))
|
var endpoint = Context.GetEndpoint();
|
||||||
|
var endpointMetaData = endpoint?.Metadata?.GetMetadata<IAllowAnonymous>();
|
||||||
|
|
||||||
|
if (!Request.Headers.ContainsKey("Authorization") && endpointMetaData == null)
|
||||||
return AuthenticateResult.Fail("Failed Authorization");
|
return AuthenticateResult.Fail("Failed Authorization");
|
||||||
|
else if (!Request.Headers.ContainsKey("Authorization") && endpointMetaData != null)
|
||||||
|
return AuthenticateResult.NoResult();
|
||||||
|
|
||||||
var authHeader = Request.Headers["Authorization"].ToString();
|
var authHeader = Request.Headers["Authorization"].ToString();
|
||||||
|
|
||||||
@@ -37,6 +44,10 @@ namespace MareSynchronosServer.Authentication
|
|||||||
{
|
{
|
||||||
return AuthenticateResult.Fail("Failed Authorization");
|
return AuthenticateResult.Fail("Failed Authorization");
|
||||||
}
|
}
|
||||||
|
else if (endpointMetaData != null && uid == null)
|
||||||
|
{
|
||||||
|
return AuthenticateResult.NoResult();
|
||||||
|
}
|
||||||
|
|
||||||
var claims = new List<Claim> {
|
var claims = new List<Claim> {
|
||||||
new Claim(ClaimTypes.NameIdentifier, uid)
|
new Claim(ClaimTypes.NameIdentifier, uid)
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MareSynchronos.API;
|
using MareSynchronos.API;
|
||||||
using MareSynchronosServer.Authentication;
|
using MareSynchronosServer.Authentication;
|
||||||
@@ -82,12 +79,6 @@ namespace MareSynchronosServer.Hubs
|
|||||||
return otherEntries.Select(e => e.User.CharacterIdentification).Distinct().ToList();
|
return otherEntries.Select(e => e.User.CharacterIdentification).Distinct().ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HubMethodName(Api.InvokeUserGetOnlineUsers)]
|
|
||||||
public async Task<int> GetOnlineUsers()
|
|
||||||
{
|
|
||||||
return await _dbContext.Users.CountAsync(u => !string.IsNullOrEmpty(u.CharacterIdentification));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||||
[HubMethodName(Api.InvokeUserGetPairedClients)]
|
[HubMethodName(Api.InvokeUserGetPairedClients)]
|
||||||
public async Task<List<ClientPairDto>> GetPairedClients()
|
public async Task<List<ClientPairDto>> GetPairedClients()
|
||||||
@@ -139,7 +130,7 @@ namespace MareSynchronosServer.Hubs
|
|||||||
MareMetrics.UserPushDataTo.Inc(visibleCharacterIds.Count);
|
MareMetrics.UserPushDataTo.Inc(visibleCharacterIds.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HubMethodName(Api.InvokeUserRegister)]
|
/*[HubMethodName(Api.InvokeUserRegister)]
|
||||||
public async Task<string> Register()
|
public async Task<string> Register()
|
||||||
{
|
{
|
||||||
using var sha256 = SHA256.Create();
|
using var sha256 = SHA256.Create();
|
||||||
@@ -177,7 +168,7 @@ namespace MareSynchronosServer.Hubs
|
|||||||
|
|
||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
return computedHash;
|
return computedHash;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ using System.Security.Claims;
|
|||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MareSynchronos.API;
|
using MareSynchronos.API;
|
||||||
|
using MareSynchronosServer.Authentication;
|
||||||
using MareSynchronosServer.Data;
|
using MareSynchronosServer.Data;
|
||||||
using MareSynchronosServer.Metrics;
|
using MareSynchronosServer.Metrics;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http.Features;
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -14,6 +16,8 @@ using Microsoft.Extensions.Logging;
|
|||||||
|
|
||||||
namespace MareSynchronosServer.Hubs
|
namespace MareSynchronosServer.Hubs
|
||||||
{
|
{
|
||||||
|
[AllowAnonymous]
|
||||||
|
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||||
public partial class MareHub : Hub
|
public partial class MareHub : Hub
|
||||||
{
|
{
|
||||||
private readonly SystemInfoService _systemInfoService;
|
private readonly SystemInfoService _systemInfoService;
|
||||||
@@ -29,6 +33,7 @@ namespace MareSynchronosServer.Hubs
|
|||||||
_dbContext = mareDbContext;
|
_dbContext = mareDbContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[AllowAnonymous]
|
||||||
[HubMethodName(Api.InvokeHeartbeat)]
|
[HubMethodName(Api.InvokeHeartbeat)]
|
||||||
public async Task<ConnectionDto> Heartbeat(string? characterIdentification)
|
public async Task<ConnectionDto> Heartbeat(string? characterIdentification)
|
||||||
{
|
{
|
||||||
@@ -44,7 +49,6 @@ namespace MareSynchronosServer.Hubs
|
|||||||
|
|
||||||
if (!string.IsNullOrEmpty(userId) && !isBanned && !string.IsNullOrEmpty(characterIdentification))
|
if (!string.IsNullOrEmpty(userId) && !isBanned && !string.IsNullOrEmpty(characterIdentification))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Connection from " + userId);
|
|
||||||
var user = (await _dbContext.Users.SingleAsync(u => u.UID == userId));
|
var user = (await _dbContext.Users.SingleAsync(u => u.UID == userId));
|
||||||
if (!string.IsNullOrEmpty(user.CharacterIdentification) && characterIdentification != user.CharacterIdentification)
|
if (!string.IsNullOrEmpty(user.CharacterIdentification) && characterIdentification != user.CharacterIdentification)
|
||||||
{
|
{
|
||||||
@@ -77,11 +81,13 @@ namespace MareSynchronosServer.Hubs
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HubMethodName(Api.InvokeGetSystemInfo)]
|
[HubMethodName(Api.InvokeGetSystemInfo)]
|
||||||
|
[AllowAnonymous]
|
||||||
public async Task<SystemInfoDto> GetSystemInfo()
|
public async Task<SystemInfoDto> GetSystemInfo()
|
||||||
{
|
{
|
||||||
return _systemInfoService.SystemInfoDto;
|
return _systemInfoService.SystemInfoDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[AllowAnonymous]
|
||||||
public override Task OnConnectedAsync()
|
public override Task OnConnectedAsync()
|
||||||
{
|
{
|
||||||
var feature = Context.Features.Get<IHttpConnectionFeature>();
|
var feature = Context.Features.Get<IHttpConnectionFeature>();
|
||||||
@@ -90,6 +96,7 @@ namespace MareSynchronosServer.Hubs
|
|||||||
return base.OnConnectedAsync();
|
return base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[AllowAnonymous]
|
||||||
public override async Task OnDisconnectedAsync(Exception exception)
|
public override async Task OnDisconnectedAsync(Exception exception)
|
||||||
{
|
{
|
||||||
MareMetrics.Connections.Dec();
|
MareMetrics.Connections.Dec();
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ using Microsoft.AspNetCore.SignalR;
|
|||||||
using Prometheus;
|
using Prometheus;
|
||||||
using WebSocketOptions = Microsoft.AspNetCore.Builder.WebSocketOptions;
|
using WebSocketOptions = Microsoft.AspNetCore.Builder.WebSocketOptions;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.EntityFrameworkCore.Migrations;
|
|
||||||
|
|
||||||
namespace MareSynchronosServer
|
namespace MareSynchronosServer
|
||||||
{
|
{
|
||||||
@@ -63,6 +62,7 @@ namespace MareSynchronosServer
|
|||||||
options.DefaultScheme = SecretKeyAuthenticationHandler.AuthScheme;
|
options.DefaultScheme = SecretKeyAuthenticationHandler.AuthScheme;
|
||||||
})
|
})
|
||||||
.AddScheme<AuthenticationSchemeOptions, SecretKeyAuthenticationHandler>(SecretKeyAuthenticationHandler.AuthScheme, options => { });
|
.AddScheme<AuthenticationSchemeOptions, SecretKeyAuthenticationHandler>(SecretKeyAuthenticationHandler.AuthScheme, options => { });
|
||||||
|
services.AddAuthorization(options => options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
@@ -90,19 +90,18 @@ namespace MareSynchronosServer
|
|||||||
KeepAliveInterval = TimeSpan.FromSeconds(10),
|
KeepAliveInterval = TimeSpan.FromSeconds(10),
|
||||||
};
|
};
|
||||||
|
|
||||||
app.UseStaticFiles(new StaticFileOptions()
|
|
||||||
{
|
|
||||||
FileProvider = new PhysicalFileProvider(Configuration["CacheDirectory"]),
|
|
||||||
RequestPath = "/cache",
|
|
||||||
ServeUnknownFileTypes = true
|
|
||||||
});
|
|
||||||
|
|
||||||
app.UseHttpMetrics();
|
app.UseHttpMetrics();
|
||||||
app.UseWebSockets(webSocketOptions);
|
app.UseWebSockets(webSocketOptions);
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseStaticFiles(new StaticFileOptions()
|
||||||
|
{
|
||||||
|
FileProvider = new PhysicalFileProvider(Configuration["CacheDirectory"]),
|
||||||
|
RequestPath = "/cache",
|
||||||
|
ServeUnknownFileTypes = true
|
||||||
|
});
|
||||||
|
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user