49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json.Serialization;
|
|
using MMOserver.Utils;
|
|
using Serilog;
|
|
|
|
namespace MMOserver.Api;
|
|
|
|
public class RestApi : Singleton<RestApi>
|
|
{
|
|
private const string VERIFY_URL = "https://a301.api.tolelom.xyz/api/auth/verify";
|
|
private readonly HttpClient httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
|
|
|
|
// 토큰 검증 - 성공 시 username 반환, 실패(401/타임아웃 등) 시 null 반환
|
|
public async Task<string?> VerifyTokenAsync(string token)
|
|
{
|
|
try
|
|
{
|
|
HttpResponseMessage response = await httpClient.PostAsJsonAsync(VERIFY_URL, new { token });
|
|
|
|
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
|
{
|
|
Log.Warning("[RestApi] 인증 실패 (401)");
|
|
return null;
|
|
}
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
AuthVerifyResponse? result = await response.Content.ReadFromJsonAsync<AuthVerifyResponse>();
|
|
return result?.Username;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error("[RestApi] 웹서버 통신 실패: {Message}", ex.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private sealed class AuthVerifyResponse
|
|
{
|
|
[JsonPropertyName("username")]
|
|
public string? Username
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
}
|
|
}
|