Add AkariApi gRPC service
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
|
||||
<Protobuf Include="Protos\akari.proto" GrpcServices="Server" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
37
Akari.Prototype.Server/Protos/akari.proto
Normal file
37
Akari.Prototype.Server/Protos/akari.proto
Normal file
@@ -0,0 +1,37 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option csharp_namespace = "Akari.Prototype.Protos";
|
||||
|
||||
package akari;
|
||||
|
||||
service AkariApi {
|
||||
rpc Encrypt (EncryptRequest) returns (EncryptResponse);
|
||||
|
||||
rpc Decrypt (DecryptRequest) returns (DecryptResponse);
|
||||
}
|
||||
|
||||
message EncryptRequest {
|
||||
string application = 1;
|
||||
string token = 2;
|
||||
bytes plain = 3;
|
||||
}
|
||||
|
||||
message EncryptResponse {
|
||||
oneof response {
|
||||
string error_message = 1;
|
||||
bytes encrypted = 2;
|
||||
}
|
||||
}
|
||||
|
||||
message DecryptRequest {
|
||||
string application = 1;
|
||||
string token = 2;
|
||||
bytes encrypted = 3;
|
||||
}
|
||||
|
||||
message DecryptResponse {
|
||||
oneof response {
|
||||
string error_message = 1;
|
||||
bytes plain = 2;
|
||||
}
|
||||
}
|
||||
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"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
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 GreeterService : Greeter.GreeterBase
|
||||
{
|
||||
private readonly ILogger<GreeterService> _logger;
|
||||
public GreeterService(ILogger<GreeterService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
|
||||
{
|
||||
return Task.FromResult(new HelloReply
|
||||
{
|
||||
Message = "Hello " + request.Name
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ namespace Akari.Prototype.Server
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapGrpcService<GreeterService>();
|
||||
endpoints.MapGrpcService<AkariService>();
|
||||
|
||||
endpoints.MapGet("/", async context =>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user