71 lines
2.7 KiB
C#
71 lines
2.7 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.IdentityModel.JsonWebTokens;
|
|
|
|
using ServerBase;
|
|
using ServerCommon;
|
|
using ServerCommon.BusinessLogDomain;
|
|
|
|
namespace BrokerApiCore;
|
|
|
|
public class PlanetService
|
|
{
|
|
private readonly PlanetInfoRepo m_planet_info_repo;
|
|
private readonly JwtGenerator m_jwt_generator;
|
|
private readonly JwtParser m_jwt_parser;
|
|
private PlanetProviderLogActor m_log_actor;
|
|
|
|
public PlanetService(PlanetInfoRepo planetInfoRepo, JwtGenerator jwtGenerator, JwtParser jwtParser)
|
|
{
|
|
m_planet_info_repo = planetInfoRepo;
|
|
m_jwt_generator = jwtGenerator;
|
|
m_jwt_parser = jwtParser;
|
|
}
|
|
|
|
public async Task<string?> auth(string planetId, string planetSecretKey)
|
|
{
|
|
var (result, planet_info) = await m_planet_info_repo.findOne(planetId);
|
|
Guard.Against.resultFail(result, ServerErrorCode.PlanetIdNotFound, () => "플래닛의 id가 존재하지 않음");
|
|
Guard.Against.isNull(planet_info, ServerErrorCode.PlanetIdNotFound, () => "플래닛의 id가 존재하지 않음");
|
|
Guard.Against.isFalse(planet_info.SecretKey == planetSecretKey,
|
|
ServerErrorCode.PlanetSecretKeyDoesNotMatched, () => "플래닛에 제공한 엑세스키와 맞지 않음");
|
|
|
|
var access_token = m_jwt_generator.generateAccessToken(planetId, planet_info.ServerType);
|
|
|
|
// 비즈니스 로그
|
|
m_log_actor = new PlanetProviderLogActor(planetId);
|
|
BusinessLogger.collectLog(m_log_actor,
|
|
new PlanetProviderAuthBusinessLog(
|
|
new LogActionEx(LogActionType.BrokerApiPlanetAuth),
|
|
new PlanetProviderAuthLogData { PlanetId = planetId, ExpireDateTime = DateTime.UtcNow }));
|
|
return access_token;
|
|
}
|
|
|
|
public (string, string) validate(string jwt)
|
|
{
|
|
if (jwt == m_jwt_generator.JwtOption.JwtTestPassToken)
|
|
{
|
|
return ("new_earth", "caliverse");
|
|
}
|
|
|
|
var principal = m_jwt_parser.parseToken(jwt);
|
|
Guard.Against.isNull(principal, ServerErrorCode.InvalidPlanetJwt, () => "jwt parsing error");
|
|
|
|
var exp_claim = principal.FindFirst(JwtRegisteredClaimNames.Exp)?.Value;
|
|
Guard.Against.isNull(principal, ServerErrorCode.InvalidPlanetJwt, () => "no JwtRegisteredClaimNames.Exp value");
|
|
|
|
var exp_time = DateTimeOffset.FromUnixTimeSeconds(long.Parse(exp_claim ?? string.Empty));
|
|
Guard.Against.isFalse(exp_time > DateTimeOffset.UtcNow, ServerErrorCode.ExpiredPlanetJwt,
|
|
() => "Jwt has expired");
|
|
|
|
var planet_id = principal.FindFirstValue(JwtRegisteredClaimNames.Sid);
|
|
Guard.Against.isNullOrEmptyOrWhiteSpace(planet_id, ServerErrorCode.InvalidPlanetJwt,
|
|
() => "jwt parsing error no sub");
|
|
|
|
var planet_server_type = principal.FindFirstValue(JwtRegisteredClaimNames.Typ);
|
|
Guard.Against.isNullOrEmptyOrWhiteSpace(planet_server_type, ServerErrorCode.InvalidPlanetJwt,
|
|
() => "jwt parsing error no typ");
|
|
|
|
return (planet_id, planet_server_type);
|
|
}
|
|
}
|