사용자 검증기 구조 분리: 내부(Family) / 외부(Extra) 검증 구분
- IUserValidator.Validate: bool 반환 → void (인증 실패 시 예외로 처리) - BaronSSOOption: Validator → ExtraUserValidator로 명명, FamilyValidator(기본 DefaultFamilyUserValidator) 추가 - DefaultFamilyUserValidator 신규: Center/Family 테넌트 사용자 통과, 그 외 InvalidUserException - BaronSSO.SignInAsync: Family/Extra 검증기 적용 흐름 정리 - InvalidUserException: UserInfo 기반 생성자 - Sample(MainWindow/SampleSettings/SimpleUserValidator) 갱신 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
using BaronSoftware.SSO.Exceptions;
|
||||
|
||||
namespace BaronSoftware.SSO
|
||||
{
|
||||
/// <summary>
|
||||
@@ -36,37 +34,45 @@ namespace BaronSoftware.SSO
|
||||
}
|
||||
|
||||
/// <summary>웹뷰 로그인 창을 띄워 인증합니다.</summary>
|
||||
public async Task SignInAsync() => await SignInAsync(null);
|
||||
public async Task SignInAsync()
|
||||
{
|
||||
UserInfo? user = null;
|
||||
if (option.EnableAutoLogin)
|
||||
user = UserInfo.FromSsoFile();
|
||||
|
||||
await SignInAsync(user?.RefreshToken);
|
||||
}
|
||||
|
||||
/// <summary>웹뷰 로그인 창을 띄워 인증합니다.</summary>
|
||||
public async Task SignInAsync(string refreshToken)
|
||||
{
|
||||
UserInfo user = null;
|
||||
if(option.EnableAutoLogin)
|
||||
user = UserInfo.FromSsoFile();
|
||||
|
||||
var isConnected = await CheckConnection();
|
||||
if(!isConnected && option.EnableOffline)
|
||||
if (isConnected)
|
||||
{
|
||||
var token = await client.LoginAsync(refreshToken);
|
||||
var userJson = await client.GetUserInfoAsync(token.AccessToken);
|
||||
if (string.IsNullOrEmpty(userJson))
|
||||
throw new InvalidOperationException($"Failed to get userinfo : {token.ToString()}");
|
||||
|
||||
user = UserInfo.FromJson(userJson, token);
|
||||
if (user == null)
|
||||
throw new InvalidUserException("Not found authorized last user.");
|
||||
option?.Validator?.Validate(user);
|
||||
return;
|
||||
throw new InvalidUserException($"Broken user token. Token {token.ToString()}, UserJson {userJson}");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!option.EnableOffline)
|
||||
throw new InvalidOperationException("Network isn't available.");
|
||||
|
||||
user = UserInfo.FromSsoFile();
|
||||
if (user == null)
|
||||
throw new InvalidUserException("Not found sso data for offline.");
|
||||
}
|
||||
|
||||
var token = await client.LoginAsync(refreshToken);
|
||||
var userJson = await client.GetUserInfoAsync(token.AccessToken);
|
||||
if (string.IsNullOrEmpty(userJson))
|
||||
throw new InvalidOperationException($"Failed to get userinfo : {token.ToString()}");
|
||||
|
||||
user = UserInfo.FromJson(userJson, token);
|
||||
|
||||
if (user == null || option?.Validator == null)
|
||||
throw new NullReferenceException("user or validator option is null");
|
||||
|
||||
// 가족사가 아니고, 인증도 실패 시
|
||||
if(!user.IsFamily()&& !option.Validator.Validate(user))
|
||||
throw new InvalidUserException("Failed to authorize user");
|
||||
if (user.IsFamily())
|
||||
option?.FamilyValidator?.Validate(user);
|
||||
else
|
||||
option?.ExtraUserValidator?.Validate(user);
|
||||
|
||||
CurrentUser = user;
|
||||
CurrentUser.Save();
|
||||
|
||||
@@ -38,9 +38,16 @@ namespace BaronSoftware.SSO
|
||||
/// </summary>
|
||||
public bool EnableAutoLogin { get; set; } = true;
|
||||
|
||||
private IUserValidator familyValidator;
|
||||
public IUserValidator FamilyValidator
|
||||
{
|
||||
get => familyValidator ?? new DefaultFamilyUserValidator();
|
||||
set => familyValidator = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 사용자 인증에 대한 추가 검증이 필요한 경우, IUserValidator 인터페이스를 구현하여 Validator 속성에 할당할 수 있습니다.
|
||||
/// </summary>
|
||||
public required IUserValidator? Validator { get; set; }
|
||||
public required IUserValidator? ExtraUserValidator { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,13 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BaronSoftware.SSO.Exceptions
|
||||
namespace BaronSoftware.SSO
|
||||
{
|
||||
internal class InvalidUserException : Exception
|
||||
{
|
||||
public class InvalidUserException : Exception
|
||||
{
|
||||
public UserInfo userinfo { get; }
|
||||
|
||||
public InvalidUserException(UserInfo userinfo) { }
|
||||
public InvalidUserException() { }
|
||||
public InvalidUserException(string message) : base(message) { }
|
||||
public InvalidUserException(string message, Exception inner) : base(message, inner) { }
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace BaronSoftware.SSO
|
||||
{
|
||||
internal class DefaultFamilyUserValidator : IUserValidator
|
||||
{
|
||||
public void Validate(UserInfo user)
|
||||
{
|
||||
if (user.IsCenter() || user.IsFamily())
|
||||
return;
|
||||
|
||||
throw new InvalidUserException(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,10 @@ namespace BaronSoftware.SSO
|
||||
public interface IUserValidator
|
||||
{
|
||||
/// <summary>
|
||||
/// 인증 실패 시 예외를 던지세요
|
||||
/// 사용자 인증 처리기.
|
||||
/// 인증 실패 시, 반드시 예외를 던져서 처리하세요
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
public bool Validate(UserInfo user);
|
||||
public void Validate(UserInfo user);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user