mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-04 15:25:11 +00:00
In order to solve some dependency issues that keep cropping up, MC should be moved back into the Essentials repo and loaded automatically on startup. This will allow for all plugins that use the MC Messengers library to use the same version without fear of overwriting a dll due to loading of plugin libraries.
77 lines
2.7 KiB
C#
77 lines
2.7 KiB
C#
using PepperDash.Core;
|
|
using System;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace PepperDash.Essentials.Services
|
|
{
|
|
|
|
public class MobileControlApiService
|
|
{
|
|
private readonly HttpClient _client;
|
|
|
|
public MobileControlApiService(string apiUrl)
|
|
{
|
|
var handler = new HttpClientHandler
|
|
{
|
|
AllowAutoRedirect = false,
|
|
ServerCertificateCustomValidationCallback = (req, cert, certChain, errors) => true
|
|
};
|
|
|
|
_client = new HttpClient(handler);
|
|
}
|
|
|
|
public async Task<AuthorizationResponse> SendAuthorizationRequest(string apiUrl, string grantCode, string systemUuid)
|
|
{
|
|
try
|
|
{
|
|
var request = new HttpRequestMessage(HttpMethod.Get, $"{apiUrl}/system/{systemUuid}/authorize?grantCode={grantCode}");
|
|
|
|
Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "Sending authorization request to {host}", null, request.RequestUri);
|
|
|
|
var response = await _client.SendAsync(request);
|
|
|
|
var authResponse = new AuthorizationResponse
|
|
{
|
|
Authorized = response.StatusCode == System.Net.HttpStatusCode.OK
|
|
};
|
|
|
|
if (authResponse.Authorized)
|
|
{
|
|
return authResponse;
|
|
}
|
|
|
|
if (response.StatusCode == System.Net.HttpStatusCode.Moved)
|
|
{
|
|
var location = response.Headers.Location;
|
|
|
|
authResponse.Reason = $"ERROR: Mobile Control API has moved. Please adjust configuration to \"{location}\"";
|
|
|
|
return authResponse;
|
|
}
|
|
|
|
var responseString = await response.Content.ReadAsStringAsync();
|
|
|
|
switch (responseString)
|
|
{
|
|
case "codeNotFound":
|
|
authResponse.Reason = $"Authorization failed. Code not found for system UUID {systemUuid}";
|
|
break;
|
|
case "uuidNotFound":
|
|
authResponse.Reason = $"Authorization failed. System UUID {systemUuid} not found. Check Essentials configuration.";
|
|
break;
|
|
default:
|
|
authResponse.Reason = $"Authorization failed. Response {response.StatusCode}: {responseString}";
|
|
break;
|
|
}
|
|
|
|
return authResponse;
|
|
} catch(Exception ex)
|
|
{
|
|
Debug.LogMessage(ex, "Error authorizing with Mobile Control");
|
|
return new AuthorizationResponse { Authorized = false, Reason = ex.Message };
|
|
}
|
|
}
|
|
}
|
|
}
|