Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/dommand #648

Merged
merged 18 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion Niconicome/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ protected override Window CreateShell()
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterDialog<CommonMessageBox>(nameof(CommonMessageBox));
containerRegistry.RegisterDialog<MainManager>(nameof(MainManager));
}

/// <summary>
Expand Down
56 changes: 56 additions & 0 deletions Niconicome/Design/Auth/cookie.pu
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@startuml
hide empty members

title Cookieローカル保管システム


interface ICookieInfo
class CookieInfo
interface ICookieStore
class CookieDBHAndler
interface IAttemptResult
interface INiconicoCookieManager
class NiconicoCookieManager
class WebViewBehavior

interface IWebview2SharedLogin
interface ICoreWebview2Handler

class V
class VM

interface ICookieInfo {
+string UserSession
+string UserSessionSecure
+Task<bool> CheckCookie()
}

interface ICookieStore {
+IAttemptResult<ICookieInfo> GetCookieInfo();
+IAttemptResult DeleteCookieInfo();
+IAttemptResult Update(ICookieInfo cookie);
}

interface INiconicoCookieManager {
+void Wire(ICoreWebview2Handler handler)
+void UnWire()
+void HandleNavigate()
+bool IsLoggedIn()
}

class NiconicoCookieManager {
-ICoreWebview2Handler handler
}

ICookieInfo <|.. CookieInfo
ICookieStore <|.. CookieDBHAndler
INiconicoCookieManager <|.. NiconicoCookieManager
ICookieStore o-- ICookieInfo
ICookieStore <.. INiconicoCookieManager

ICookieStore <.. IWebview2SharedLogin
V *-- WebViewBehavior
INiconicoCookieManager <.. WebViewBehavior


@enduml
9 changes: 9 additions & 0 deletions Niconicome/Extensions/System/System.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ public static bool IsNullOrEmpty([NotNullWhen(false)]this string? source)
return string.IsNullOrEmpty(source);
}
}

public static class BoolExtensions
{
//boolの値を逆にする
public static bool Not(this bool source)
{
return !source;
}
}
}
1 change: 1 addition & 0 deletions Niconicome/Main.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Router AppAssembly="@typeof(Main).Assembly">
<Found Context="routeData">
<Niconicome.Views.Shared.TabContainer/>
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
Expand Down
34 changes: 13 additions & 21 deletions Niconicome/Models/Auth/AutoLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Niconicome.Models.Domain.Local.Settings;
using Niconicome.Models.Domain.Local.Store.V2;
using Niconicome.Models.Domain.Niconico;
using Niconicome.Models.Domain.Niconico.UserAuth;
using Niconicome.Models.Helper.Result;
using Niconicome.Models.Local.Settings;

Expand Down Expand Up @@ -43,21 +44,18 @@ public interface IAutoLogin

class AutoLogin : IAutoLogin
{
public AutoLogin(ISession session, IAccountManager accountManager, IWebview2SharedLogin webview2SharedLogin, IFirefoxSharedLogin firefoxSharedLogin, IStoreFirefoxSharedLogin storeFirefoxSharedLogin, ISettingsContainer settingsConainer)
public AutoLogin(IWebview2SharedLogin webview2SharedLogin, IFirefoxSharedLogin firefoxSharedLogin, IStoreFirefoxSharedLogin storeFirefoxSharedLogin, ISettingsContainer settingsConainer,IStoredCookieLogin storedCookieLogin)
{
this._session = session;
this._accountManager = accountManager;
this._webview2SharedLogin = webview2SharedLogin;
this._firefoxSharedLogin = firefoxSharedLogin;
this._storeFirefoxSharedLogin = storeFirefoxSharedLogin;
this._settingsConainer = settingsConainer;
this._storedCookieLogin = storedCookieLogin;
}

#region field

private readonly ISession _session;

private readonly IAccountManager _accountManager;
private readonly IStoredCookieLogin _storedCookieLogin;

private readonly ISettingsContainer _settingsConainer;

Expand Down Expand Up @@ -94,12 +92,14 @@ public async Task<bool> LoginAsync()
bool result = false;
var type = this.GetAutoLoginType();

if (type == AutoLoginType.Normal)
//保存されているCookieでのログインを優先する
if (this._storedCookieLogin.CanLogin())
{
var cred = this._accountManager.GetUserCredential();
result = await this._session.Login(cred);
result = await this._storedCookieLogin.TryLogin();
if (result) return true;
}
else if (type == AutoLoginType.Webview2)

if (type == AutoLoginType.Webview2)
{
result = await this._webview2SharedLogin.TryLogin();
}
Expand All @@ -122,7 +122,7 @@ public async Task<bool> LoginAsync()

private AutoLoginType GetAutoLoginType()
{
IAttemptResult<ISettingInfo<string>> mResult = this._settingsConainer.GetSetting(SettingNames.AutoLoginMode, AutoLoginTypeString.Normal);
IAttemptResult<ISettingInfo<string>> mResult = this._settingsConainer.GetSetting(SettingNames.AutoLoginMode, AutoLoginTypeString.Webview2);
IAttemptResult<ISettingInfo<string>> pResult = this._settingsConainer.GetSetting(SettingNames.FirefoxProfileName, "");

if (!mResult.IsSucceeded || mResult.Data is null)
Expand All @@ -135,15 +135,10 @@ private AutoLoginType GetAutoLoginType()
return AutoLoginType.None;
}

var mode = mResult.Data.Value.IsNullOrEmpty() ? AutoLoginTypeString.Normal : mResult.Data.Value;
var mode = mResult.Data.Value.IsNullOrEmpty() ? AutoLoginTypeString.Webview2 : mResult.Data.Value;
var ffProfile = pResult.Data.Value;

if (mode == AutoLoginTypeString.Normal)
{
bool isCredencialSaved = this._accountManager.IsPasswordSaved;
if (isCredencialSaved) return AutoLoginType.Normal;
}
else if (mode == AutoLoginTypeString.Webview2)
if (mode == AutoLoginTypeString.Webview2)
{
bool canLoginWithWebview2 = this._webview2SharedLogin.CanLogin();
if (canLoginWithWebview2) return AutoLoginType.Webview2;
Expand Down Expand Up @@ -180,16 +175,13 @@ public IEnumerable<IFirefoxProfileInfo> GetFirefoxProfiles(AutoLoginType loginTy
public enum AutoLoginType
{
None,
Normal,
Webview2,
Firefox,
StoreFirefox,
}

static class AutoLoginTypeString
{
public const string Normal = "Normal";

public const string Webview2 = "Webview2";

public const string Firefox = "Firefox";
Expand Down
106 changes: 106 additions & 0 deletions Niconicome/Models/Auth/Cookie/NiconicoCookieManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Niconicome.Extensions.System;
using Niconicome.Models.Domain.Local.Handlers;
using Niconicome.Models.Domain.Local.Store.V2;
using Niconicome.Models.Domain.Niconico;
using Niconicome.Models.Domain.Niconico.UserAuth;
using Niconicome.Models.Helper.Result;

namespace Niconicome.Models.Auth.Cookie
{
interface INiconicoCookieManager
{
/// <summary>
/// Webviewをハンドリングする
/// </summary>
/// <param name="handler"></param>
void Wire(ICoreWebview2Handler handler);

/// <summary>
/// WebViewのハンドリングを解除する
/// </summary>
void UnWire();

/// <summary>
/// URL遷移をハンドリングする
/// </summary>
Task<bool> HandleNavigate();

/// <summary>
/// ログイン状態を確認する
/// </summary>
/// <returns></returns>
Task<bool> IsLoggedIn();

}

public class NiconicoCookieManager : INiconicoCookieManager
{
#region field

private ICoreWebview2Handler? handler;

private readonly INiconicoContext _context;

private readonly IStoredCookieLogin _storedCookieLogin;

#endregion

public NiconicoCookieManager(INiconicoContext context,IStoredCookieLogin storedCookieLogin)
{
this._context = context;
this._storedCookieLogin = storedCookieLogin;
}

#region Method

public void Wire(ICoreWebview2Handler handler)
{
this.handler = handler;
}

public void UnWire()
{
this.handler = null;
}

public async Task<bool> HandleNavigate()
{
if (this.handler is null) return false;

var cookies = await this.handler.GetCookiesAsync(@"https://nicovideo.jp");

string userSession = string.Empty;
string userSessionSecure = string.Empty;

foreach (var cookie in cookies)
{
if (cookie.Name == "user_session")
{
userSession = cookie.Value;
}
else if (cookie.Name == "user_session_secure")
{
userSessionSecure = cookie.Value;
}
}

if (userSession.IsNullOrEmpty() || userSessionSecure.IsNullOrEmpty()) return false;

var result = await this._context.LoginAndSaveCookieAsync(userSession, userSessionSecure);

return result.IsSucceeded;
}

public async Task<bool> IsLoggedIn()
{
return this._storedCookieLogin.CanLogin() && await this._storedCookieLogin.TryLogin();
}

#endregion
}
}
15 changes: 15 additions & 0 deletions Niconicome/Models/Auth/Error/ChromeSharedLoginError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Niconicome.Models.Domain.Utils.Error;

namespace Niconicome.Models.Auth.Error
{
public enum ChromeSharedLoginError
{
[ErrorEnum(ErrorLevel.Error, "Google Chromeからのクッキーの取得に失敗しました。")]
FailedToGetCookie,
}
}
14 changes: 3 additions & 11 deletions Niconicome/Models/Auth/FirefoxSharedLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IFirefoxSharedLogin

public class FirefoxSharedLogin : SharedLoginBase, IFirefoxSharedLogin
{
public FirefoxSharedLogin(IFirefoxCookieManager firefoxCookieManager, ILogger logger, INicoHttp http, INiconicoContext context, ICookieManager cookieManager, IFirefoxProfileManager firefoxProfileManager) : base(http, cookieManager, context)
public FirefoxSharedLogin(IFirefoxCookieManager firefoxCookieManager, ILogger logger, INiconicoContext context, IFirefoxProfileManager firefoxProfileManager) : base( context)
{
this.firefoxCookieManager = firefoxCookieManager;
this.logger = logger;
Expand Down Expand Up @@ -60,17 +60,9 @@ public async Task<bool> TryLogin(string profileName)

if (cookie.UserSession is null || cookie.UserSessionSecure is null) return false;

this.cookieManager.AddCookie("user_session", cookie.UserSession);
this.cookieManager.AddCookie("user_session_secure", cookie.UserSessionSecure);
IAttemptResult result = await this.LoginAndSaveCookieAsync(cookie.UserSession, cookie.UserSessionSecure);

var result = await this.CheckIfLoginSucceeded();

if (result)
{
await this.context.RefreshUser();
}

return result;
return result.IsSucceeded;
}

/// <summary>
Expand Down
Loading
Loading