2023-01-22 22:15:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
2023-01-27 23:32:12 +00:00
|
|
|
|
using System.Net.Http.Headers;
|
2023-01-22 22:15:55 +00:00
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using Selector.SignalR;
|
|
|
|
|
|
|
|
|
|
namespace Selector.MAUI.Services;
|
|
|
|
|
|
|
|
|
|
public interface ISelectorNetClient
|
|
|
|
|
{
|
|
|
|
|
Task<SelectorNetClient.TokenResponse> GetToken(string username, string password);
|
|
|
|
|
Task<SelectorNetClient.TokenResponse> GetToken(string currentKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SelectorNetClient : ISelectorNetClient
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpClient _client;
|
|
|
|
|
private readonly string _baseUrl;
|
|
|
|
|
private readonly NowHubClient _nowClient;
|
2023-01-28 13:34:10 +00:00
|
|
|
|
private readonly PastHubClient _pastClient;
|
2023-01-22 22:15:55 +00:00
|
|
|
|
|
2023-01-28 13:34:10 +00:00
|
|
|
|
public SelectorNetClient(HttpClient client, NowHubClient nowClient, PastHubClient pastClient)
|
2023-01-22 22:15:55 +00:00
|
|
|
|
{
|
|
|
|
|
_client = client;
|
|
|
|
|
_nowClient = nowClient;
|
2023-01-28 13:34:10 +00:00
|
|
|
|
_pastClient = pastClient;
|
2023-01-22 22:15:55 +00:00
|
|
|
|
|
|
|
|
|
//var baseOverride = Environment.GetEnvironmentVariable("SELECTOR_BASE_URL");
|
|
|
|
|
|
|
|
|
|
//if (!string.IsNullOrWhiteSpace(baseOverride))
|
|
|
|
|
//{
|
|
|
|
|
// _baseUrl = baseOverride;
|
|
|
|
|
//}
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
// _baseUrl = "https://selector.sarsoo.xyz";
|
|
|
|
|
//}
|
|
|
|
|
|
2023-01-27 23:32:12 +00:00
|
|
|
|
//_baseUrl = "http://localhost:5000";
|
|
|
|
|
_baseUrl = "https://selector.sarsoo.xyz";
|
2023-01-22 22:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<TokenResponse> GetToken(string username, string password)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNullOrEmpty(username);
|
|
|
|
|
ArgumentNullException.ThrowIfNullOrEmpty(password);
|
|
|
|
|
|
|
|
|
|
var result = await _client.PostAsync(_baseUrl + "/api/auth/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "Username", username },
|
|
|
|
|
{ "Password", password }
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return FormTokenResponse(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<TokenResponse> GetToken(string currentKey)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNullOrEmpty(currentKey);
|
|
|
|
|
|
2023-01-27 23:32:12 +00:00
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, _baseUrl + "/api/auth/token");
|
|
|
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", currentKey);
|
|
|
|
|
|
|
|
|
|
var result = await _client.SendAsync(request);
|
2023-01-22 22:15:55 +00:00
|
|
|
|
|
|
|
|
|
return FormTokenResponse(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TokenResponse FormTokenResponse(HttpResponseMessage result)
|
|
|
|
|
{
|
|
|
|
|
var ret = new TokenResponse();
|
|
|
|
|
|
|
|
|
|
switch (result.StatusCode)
|
|
|
|
|
{
|
|
|
|
|
case HttpStatusCode.BadRequest:
|
|
|
|
|
ret.Status = TokenResponseStatus.Malformed;
|
|
|
|
|
break;
|
|
|
|
|
case HttpStatusCode.NotFound:
|
|
|
|
|
ret.Status = TokenResponseStatus.UserSearchFailed;
|
|
|
|
|
break;
|
|
|
|
|
case HttpStatusCode.Unauthorized:
|
|
|
|
|
ret.Status = TokenResponseStatus.BadCreds;
|
|
|
|
|
break;
|
|
|
|
|
case HttpStatusCode.OK:
|
|
|
|
|
ret.Status = TokenResponseStatus.OK;
|
|
|
|
|
ret.Token = result.Content.ReadFromJsonAsync<TokenNetworkResponse>().Result.Token;
|
|
|
|
|
_nowClient.Token = ret.Token;
|
2023-01-28 13:34:10 +00:00
|
|
|
|
_pastClient.Token = ret.Token;
|
2023-01-22 22:15:55 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TokenResponse
|
|
|
|
|
{
|
|
|
|
|
public string Token { get; set; }
|
|
|
|
|
public TokenResponseStatus Status { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TokenNetworkResponse
|
|
|
|
|
{
|
|
|
|
|
public string Token { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum TokenResponseStatus
|
|
|
|
|
{
|
|
|
|
|
Malformed, UserSearchFailed, BadCreds, OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TokenModel
|
|
|
|
|
{
|
|
|
|
|
public string Username { get; set; }
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|