Files
hermes-server/Requests/CreateConnection.cs

50 lines
2.0 KiB
C#

using HermesSocketLibrary.Socket.Data;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class CreateConnection : IRequest
{
public string Name => "create_connection";
public string[] RequiredKeys => ["name", "type", "client_id", "access_token", "grant_type", "scope", "expiration"];
private ILogger _logger;
public CreateConnection(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["client_id"].ToString()!;
string accessToken = data["access_token"].ToString()!;
string grantType = data["grant_type"].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.Set(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."));
}
}
}