Files
hermes-server/Requests/UpdateConnection.cs

50 lines
1.9 KiB
C#

using HermesSocketLibrary.Socket.Data;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class UpdateConnection : IRequest
{
public string Name => "update_connection";
public string[] RequiredKeys => ["name", "type", "clientId", "accessToken", "grantType", "scope", "expiration"];
private ILogger _logger;
public UpdateConnection(ILogger logger)
{
_logger = logger;
}
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string name = data["name"].ToString()!;
string type = data["type"].ToString()!;
string clientId = data["clientId"].ToString()!;
string accessToken = data["accessToken"].ToString()!;
string grantType = data["grantType"].ToString()!;
string scope = data["scope"].ToString()!;
if (!DateTime.TryParse(data["expiration"].ToString()!, out var expiresAt))
return Task.FromResult(RequestResult.Failed("Expiration needs to be a date time string."));
var connection = new Connection()
{
UserId = channel.Id,
Name = name,
Type = type,
ClientId = clientId,
AccessToken = accessToken,
GrantType = grantType,
Scope = scope,
ExpiresAt = expiresAt,
};
bool result = channel.Connections.Modify(name, connection);
if (result)
{
_logger.Information($"Added connection to channel [name: {name}][type: {type}][scope: {scope}][expiration: {expiresAt}][channel: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(connection));
}
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}