Add AkariApi gRPC service
This commit is contained in:
89
Akari.Prototype.Server/Services/AkariService.cs
Normal file
89
Akari.Prototype.Server/Services/AkariService.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Akari.Prototype.Protos;
|
||||
using Akari.Prototype.Server.Services;
|
||||
using Akari.Prototype.Server.Utils;
|
||||
using Google.Protobuf;
|
||||
using Grpc.Core;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server
|
||||
{
|
||||
public class AkariService : AkariApi.AkariApiBase
|
||||
{
|
||||
private readonly ILogger<AkariService> _logger;
|
||||
private readonly IApplicationsManager _applications;
|
||||
|
||||
public AkariService(ILogger<AkariService> logger, IApplicationsManager applications)
|
||||
{
|
||||
_logger = logger;
|
||||
_applications = applications;
|
||||
}
|
||||
|
||||
public override Task<DecryptResponse> Decrypt(DecryptRequest request, ServerCallContext context)
|
||||
{
|
||||
if (!_applications.Contains(request.Application))
|
||||
{
|
||||
return Task.FromResult(new DecryptResponse()
|
||||
{
|
||||
ErrorMessage = "Application not found"
|
||||
});
|
||||
}
|
||||
|
||||
if (!_applications.VerifyToken(request.Application, request.Token))
|
||||
{
|
||||
return Task.FromResult(new DecryptResponse()
|
||||
{
|
||||
ErrorMessage = "Wrong token"
|
||||
});
|
||||
}
|
||||
|
||||
if (_applications.TryRetrieveKey(request.Application, request.Token, out var key))
|
||||
{
|
||||
return Task.FromResult(new DecryptResponse()
|
||||
{
|
||||
Plain = ByteString.CopyFrom(Security.AesGcmDecrypt(key, request.Encrypted.ToByteArray()))
|
||||
});
|
||||
}
|
||||
|
||||
return Task.FromResult(new DecryptResponse()
|
||||
{
|
||||
ErrorMessage = "No fingerprint auth found for this application"
|
||||
});
|
||||
}
|
||||
|
||||
public override Task<EncryptResponse> Encrypt(EncryptRequest request, ServerCallContext context)
|
||||
{
|
||||
if (!_applications.Contains(request.Application))
|
||||
{
|
||||
return Task.FromResult(new EncryptResponse()
|
||||
{
|
||||
ErrorMessage = "Application not found"
|
||||
});
|
||||
}
|
||||
|
||||
if (!_applications.VerifyToken(request.Application, request.Token))
|
||||
{
|
||||
return Task.FromResult(new EncryptResponse()
|
||||
{
|
||||
ErrorMessage = "Wrong token"
|
||||
});
|
||||
}
|
||||
|
||||
if (_applications.TryRetrieveKey(request.Application, request.Token, out var key))
|
||||
{
|
||||
return Task.FromResult(new EncryptResponse()
|
||||
{
|
||||
Encrypted = ByteString.CopyFrom(Security.AesGcmEncrypt(key, request.Plain.ToByteArray()))
|
||||
});
|
||||
}
|
||||
|
||||
return Task.FromResult(new EncryptResponse()
|
||||
{
|
||||
ErrorMessage = "No fingerprint auth found for this application"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user