Code:
public class GameforgeAuthenticator : IGameforgeAuthenticator
{
private const string URL = "https://spark.gameforge.com/api/v1";
private const string PLATFORM_GAME_ID = "dd4e22d6-00d1-44b9-8126-d8b40e0cd7c9";
private const string USER_AGENT = "GameforgeClient/2.0.48";
private const string MEDIA_TYPE = "application/json";
private readonly HttpClient _httpClient;
private readonly ISerializer _serializer;
private readonly IDeserializer _deserializer;
public GameforgeAuthenticator(ISerializer serializer, IDeserializer deserializer)
{
_httpClient = new HttpClient();
_serializer = serializer;
_deserializer = deserializer;
}
public async Task<string> GetAuthenticationToken(string username, string password, Language language)
{
string serialized = _serializer.Serialize(new AuthForm
{
GfLang = language.GfLang,
Username = username,
Password = password,
Locale = language.Locale,
PlatformGameId = PLATFORM_GAME_ID
});
using var request = new HttpRequestMessage(HttpMethod.Post, $"{URL}/auth/thin/sessions")
{
Content = new StringContent(serialized, Encoding.UTF8, MEDIA_TYPE)
};
HttpResponseMessage response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
return string.Empty;
}
string content = await response.Content.ReadAsStringAsync();
var jsonContent = _deserializer.Deserialize<Dictionary<string, string>>(content);
return jsonContent.GetValueOrDefault("token") ?? string.Empty;
}
public async Task<IEnumerable<GameforgeAccount>> GetAccounts(string authenticationToken, Guid installationId)
{
using var request = new HttpRequestMessage(HttpMethod.Get, $"{URL}/user/accounts");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationToken);
request.Headers.Add("TNT-Installation-Id", installationId.ToString());
request.Headers.Add("User-Agent", USER_AGENT);
HttpResponseMessage response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
return new List<GameforgeAccount>();
}
string content = await response.Content.ReadAsStringAsync();
var jsonContent = _deserializer.Deserialize<Dictionary<string, GameforgeAccount>>(content);
return jsonContent?.Values.ToArray() ?? Array.Empty<GameforgeAccount>();
}
public async Task<string> GetSessionToken(string authenticationToken, GameforgeAccount account, Guid installationId)
{
using var request = new HttpRequestMessage(HttpMethod.Post, $"{URL}/auth/thin/codes");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationToken);
request.Headers.Add("TNT-Installation-Id", installationId.ToString());
request.Headers.Add("User-Agent", USER_AGENT);
string json = _serializer.Serialize(new SessionForm
{
PlatformGameAccountId = account.Id
});
request.Content = new StringContent(json, Encoding.UTF8, MEDIA_TYPE);
HttpResponseMessage response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
return string.Empty;
}
string content = await response.Content.ReadAsStringAsync();
var jsonContent = _deserializer.Deserialize<Dictionary<string, string>>(content);
return (jsonContent.GetValueOrDefault("code") ?? string.Empty).ToHex();
}
}