Skip to content

Commit

Permalink
Fix RemoteControlController.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Jul 27, 2023
1 parent 3d33fe1 commit 8104bfa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
28 changes: 27 additions & 1 deletion Server/API/AlertsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,20 @@ public async Task<IActionResult> Create(AlertOptions alertOptions)
{
try
{
await _emailSender.SendEmailAsync(alertOptions.EmailTo,
if (string.IsNullOrWhiteSpace(alertOptions.EmailTo))
{
return BadRequest("Email address is required to send email.");
}
if (string.IsNullOrWhiteSpace(alertOptions.EmailSubject))
{
return BadRequest("Email subject is required to send email.");
}
if (string.IsNullOrWhiteSpace(alertOptions.EmailBody))
{
return BadRequest("Email body is required to send email.");
}
await _emailSender.SendEmailAsync(
alertOptions.EmailTo,
alertOptions.EmailSubject,
alertOptions.EmailBody,
orgId.ToString());
Expand All @@ -81,6 +94,19 @@ await _emailSender.SendEmailAsync(alertOptions.EmailTo,
{
try
{
if (string.IsNullOrWhiteSpace(alertOptions.ApiRequestUrl))
{
return BadRequest("API request URL is required to send API request.");
}
if (string.IsNullOrWhiteSpace(alertOptions.ApiRequestMethod))
{
return BadRequest("API request method is required to send API request.");
}
if (string.IsNullOrWhiteSpace(alertOptions.ApiRequestBody))
{
return BadRequest("API request body is required to send API request.");
}

using var httpClient = _httpClientFactory.CreateClient();
using var request = new HttpRequestMessage(
new HttpMethod(alertOptions.ApiRequestMethod),
Expand Down
20 changes: 13 additions & 7 deletions Server/API/RemoteControlController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public async Task<IActionResult> Get(string deviceID)
}

[HttpPost]
[Obsolete("This method is deprecated. Use the GET method along with API keys instead.")]
public async Task<IActionResult> Post([FromBody] RemoteControlRequest rcRequest)
{
if (!_appConfig.AllowApiLogin)
Expand Down Expand Up @@ -111,15 +112,15 @@ public async Task<IActionResult> Post([FromBody] RemoteControlRequest rcRequest)
return BadRequest();
}

private async Task<IActionResult> InitiateRemoteControl(string deviceID, string orgID)
private async Task<IActionResult> InitiateRemoteControl(string deviceID, string orgId)
{
if (!_serviceSessionCache.TryGetByDeviceId(deviceID, out var targetDevice) ||
!_serviceSessionCache.TryGetConnectionId(deviceID, out var serviceConnectionId))
{
return NotFound("The target device couldn't be found.");
}

if (targetDevice.OrganizationID != orgID)
if (targetDevice.OrganizationID != orgId)
{
return Unauthorized();
}
Expand All @@ -141,7 +142,7 @@ private async Task<IActionResult> InitiateRemoteControl(string deviceID, string

var sessionCount = _remoteControlSessionCache.Sessions
.OfType<RemoteControlSessionEx>()
.Count(x => x.OrganizationId == orgID);
.Count(x => x.OrganizationId == orgId);

if (sessionCount > _appConfig.RemoteControlSessionLimit)
{
Expand All @@ -157,7 +158,7 @@ private async Task<IActionResult> InitiateRemoteControl(string deviceID, string
UserConnectionId = HttpContext.Connection.Id,
AgentConnectionId = serviceConnectionId,
DeviceId = deviceID,
OrganizationId = orgID
OrganizationId = orgId
};

_remoteControlSessionCache.AddOrUpdate($"{sessionId}", session, (k, v) =>
Expand All @@ -171,15 +172,20 @@ private async Task<IActionResult> InitiateRemoteControl(string deviceID, string
return session;
});

var orgName = _dataService.GetOrganizationNameById(orgID);
var orgNameResult = await _dataService.GetOrganizationNameById(orgId);

if (!orgNameResult.IsSuccess)
{
return BadRequest("Failed to resolve organization name.");
}

await _serviceHub.Clients.Client(serviceConnectionId).SendAsync("RemoteControl",
sessionId,
accessKey,
HttpContext.Connection.Id,
string.Empty,
orgName,
orgID);
orgNameResult.Value,
orgId);

var waitResult = await session.WaitForSessionReady(TimeSpan.FromSeconds(30));
if (!waitResult)
Expand Down
2 changes: 1 addition & 1 deletion Server/Pages/ApiKeys.razor
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ else

private async Task CreateNewKey()
{
var secret = RandomGenerator.GenerateString(36);
var secret = RandomGenerator.GenerateString(48);
var secretHash = new PasswordHasher<string>().HashPassword(string.Empty, secret);

var result = await DataService.CreateApiToken(UserName, _createKeyName, secretHash);
Expand Down
16 changes: 8 additions & 8 deletions Shared/Models/AlertOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ namespace Remotely.Shared.Models;

public class AlertOptions
{
public string AlertDeviceID { get; set; } = string.Empty;
public string AlertMessage { get; set; } = string.Empty;
public string ApiRequestBody { get; set; } = string.Empty;
public required string AlertDeviceID { get; set; }
public required string AlertMessage { get; set; }
public string? ApiRequestBody { get; set; }
public Dictionary<string, string> ApiRequestHeaders { get; set; } = new();
public string ApiRequestMethod { get; set; } = string.Empty;
public string ApiRequestUrl { get; set; } = string.Empty;
public string EmailBody { get; set; } = string.Empty;
public string EmailSubject { get; set; } = string.Empty;
public string EmailTo { get; set; } = string.Empty;
public string? ApiRequestMethod { get; set; }
public string? ApiRequestUrl { get; set; }
public string? EmailBody { get; set; }
public string? EmailSubject { get; set; }
public string? EmailTo { get; set; }
public bool ShouldAlert { get; set; }
public bool ShouldEmail { get; set; }
public bool ShouldSendApiRequest { get; set; }
Expand Down

0 comments on commit 8104bfa

Please sign in to comment.