fix auth for grpc calls

This commit is contained in:
Stanley Dimant
2022-08-24 23:01:03 +02:00
parent 5736bcf074
commit 66e2b3db82
3 changed files with 19 additions and 5 deletions

View File

@@ -2,9 +2,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Threading.Tasks; using System.Threading.Tasks;
using Google.Protobuf; using Google.Protobuf;
using Grpc.Core;
using MareSynchronos.API; using MareSynchronos.API;
using MareSynchronosShared.Authentication; using MareSynchronosShared.Authentication;
using MareSynchronosShared.Models; using MareSynchronosShared.Models;
@@ -38,7 +40,11 @@ namespace MareSynchronosServer.Hubs
var ownFiles = await _dbContext.Files.Where(f => f.Uploaded && f.Uploader.UID == AuthenticatedUserId).ToListAsync().ConfigureAwait(false); var ownFiles = await _dbContext.Files.Where(f => f.Uploaded && f.Uploader.UID == AuthenticatedUserId).ToListAsync().ConfigureAwait(false);
var request = new DeleteFilesRequest(); var request = new DeleteFilesRequest();
request.Hash.AddRange(ownFiles.Select(f => f.Hash)); request.Hash.AddRange(ownFiles.Select(f => f.Hash));
_ = await _fileServiceClient.DeleteFilesAsync(request).ConfigureAwait(false); Metadata headers = new Metadata()
{
{ "Authorization", Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Authentication)?.Value }
};
_ = await _fileServiceClient.DeleteFilesAsync(request, headers).ConfigureAwait(false);
} }
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
@@ -52,7 +58,11 @@ namespace MareSynchronosServer.Hubs
FileSizeRequest request = new FileSizeRequest(); FileSizeRequest request = new FileSizeRequest();
request.Hash.AddRange(hashes); request.Hash.AddRange(hashes);
var grpcResponse = await _fileServiceClient.GetFileSizesAsync(request).ConfigureAwait(false); Metadata headers = new Metadata()
{
{ "Authorization", Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Authentication)?.Value }
};
var grpcResponse = await _fileServiceClient.GetFileSizesAsync(request, headers).ConfigureAwait(false);
foreach (var hash in grpcResponse.HashToFileSize) foreach (var hash in grpcResponse.HashToFileSize)
{ {
@@ -201,7 +211,11 @@ namespace MareSynchronosServer.Hubs
File.Delete(tempFileName); File.Delete(tempFileName);
req.Hash = computedHashString; req.Hash = computedHashString;
req.Uploader = AuthenticatedUserId; req.Uploader = AuthenticatedUserId;
_ = await _fileServiceClient.UploadFileAsync(req).ConfigureAwait(false); Metadata headers = new Metadata()
{
{ "Authorization", Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Authentication)?.Value }
};
_ = await _fileServiceClient.UploadFileAsync(req, headers).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -49,7 +49,8 @@ namespace MareSynchronosShared.Authentication
var claims = new List<Claim> var claims = new List<Claim>
{ {
new(ClaimTypes.NameIdentifier, uid) new(ClaimTypes.NameIdentifier, uid),
new(ClaimTypes.Authentication, authHeader)
}; };
var identity = new ClaimsIdentity(claims, nameof(SecretKeyGrpcAuthenticationHandler)); var identity = new ClaimsIdentity(claims, nameof(SecretKeyGrpcAuthenticationHandler));

View File

@@ -8,7 +8,6 @@ using Microsoft.Extensions.Logging;
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MareSynchronosStaticFilesServer; namespace MareSynchronosStaticFilesServer;