feat : 토큰 인증 RestApi 구현 / Token기반 haskKey구현 / Dummy, User Token체크 분기
This commit is contained in:
48
MMOTestServer/MMOserver/Api/RestApi.cs
Normal file
48
MMOTestServer/MMOserver/Api/RestApi.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user