90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using Akari.Prototype.Server.Services;
|
|
using Akari.Prototype.Server.Utils;
|
|
using Akari.Prototype.Shared.Protos;
|
|
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"
|
|
});
|
|
}
|
|
}
|
|
}
|