mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-05-09 11:37:15 +02:00
117 lines
3.9 KiB
C#
117 lines
3.9 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using TINK.Model.Connector;
|
|
|
|
namespace TINK.Model.User.Account
|
|
{
|
|
public class StoreLegacy : IStore
|
|
{
|
|
/// <summary>
|
|
/// Holds the name of the application.
|
|
/// </summary>
|
|
private const string M_STR_APPNAME = "TINKApp";
|
|
|
|
/// <summary> Holds the id of the session. </summary>
|
|
private const string KEY_SESSIONID = "SessionId";
|
|
|
|
/// <summary> Holds the id of the session. </summary>
|
|
private const string KEY_GROUP = "Group";
|
|
|
|
/// <summary> Holds the id of the session. </summary>
|
|
private const string KEY_DEBUGLEVEL = "DebugLevel";
|
|
|
|
public StoreLegacy(string copriHostHash)
|
|
{
|
|
m_strCopriHostHash = copriHostHash
|
|
?? throw new ArgumentException("Can not construct account object. Copri hash must not be null.");
|
|
}
|
|
|
|
private string m_strCopriHostHash;
|
|
|
|
/// <summary>
|
|
/// Reads mail address and password from account store.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<IAccount> Load()
|
|
{
|
|
#if !WINDOWS_UWP
|
|
#if !__IOS__
|
|
var xamAccountStore = Xamarin.Auth.AccountStore.Create("System.Char[]").FindAccountsForService($"{M_STR_APPNAME}_{m_strCopriHostHash}").FirstOrDefault();
|
|
#else
|
|
var xamAccountStore = Xamarin.Auth.AccountStore.Create().FindAccountsForService($"{M_STR_APPNAME}_{m_strCopriHostHash}").FirstOrDefault();
|
|
#endif
|
|
if (xamAccountStore == null)
|
|
{
|
|
// Nothing t do if account cannot be accessed.
|
|
return new EmptyAccount();
|
|
}
|
|
|
|
return new Account(
|
|
xamAccountStore.Username,
|
|
string.Empty,
|
|
xamAccountStore.Properties.ContainsKey(KEY_SESSIONID) ? xamAccountStore.Properties[KEY_SESSIONID] : null,
|
|
xamAccountStore.Properties.ContainsKey(KEY_GROUP) && !string.IsNullOrEmpty(xamAccountStore.Properties[KEY_GROUP]) ? JsonConvert.DeserializeObject<IEnumerable<string>>(xamAccountStore.Properties[KEY_GROUP]) : new string[0],
|
|
xamAccountStore.Properties.ContainsKey(KEY_DEBUGLEVEL) && !string.IsNullOrEmpty(xamAccountStore.Properties[KEY_DEBUGLEVEL]) ? Permissions.Parse<Permissions>(xamAccountStore.Properties[KEY_DEBUGLEVEL]) : Permissions.None);
|
|
#else
|
|
return new Account(
|
|
string.Empty,
|
|
string.Empty,
|
|
string.Empty,
|
|
new List<string>(),
|
|
null);
|
|
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes mail address and password to account store.
|
|
/// </summary>
|
|
/// <param name="account"></param>
|
|
public async Task Save(IAccount account)
|
|
{
|
|
#if !WINDOWS_UWP
|
|
Xamarin.Auth.Account xamAccount = new Xamarin.Auth.Account
|
|
{
|
|
Username = account.Mail
|
|
};
|
|
|
|
xamAccount.Properties.Add(KEY_SESSIONID, account?.SessionCookie);
|
|
xamAccount.Properties.Add(KEY_GROUP, JsonConvert.SerializeObject(account?.Group ?? new string[0]));
|
|
xamAccount.Properties.Add(KEY_DEBUGLEVEL, account.DebugLevel.ToString());
|
|
#if !__IOS__
|
|
Xamarin.Auth.AccountStore.Create("System.Char[]").Save(xamAccount, $"{M_STR_APPNAME}_{m_strCopriHostHash}");
|
|
#else
|
|
Xamarin.Auth.AccountStore.Create().Save(xamAccount, $"{M_STR_APPNAME}_{m_strCopriHostHash}");
|
|
#endif
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes mail address and password from account store.
|
|
/// </summary>
|
|
public IAccount Delete(IAccount account)
|
|
{
|
|
#if !WINDOWS_UWP
|
|
#if !__IOS__
|
|
var xamAccountStore = Xamarin.Auth.AccountStore.Create("System.Char[]").FindAccountsForService($"{M_STR_APPNAME}_{m_strCopriHostHash}").FirstOrDefault();
|
|
#else
|
|
var xamAccountStore = Xamarin.Auth.AccountStore.Create().FindAccountsForService($"{M_STR_APPNAME}_{m_strCopriHostHash}").FirstOrDefault();
|
|
#endif
|
|
if (xamAccountStore == null)
|
|
{
|
|
return new EmptyAccount();
|
|
}
|
|
#if !__IOS__
|
|
Xamarin.Auth.AccountStore.Create("System.Char[]").Delete(xamAccountStore, $"{M_STR_APPNAME}_{m_strCopriHostHash}");
|
|
#else
|
|
Xamarin.Auth.AccountStore.Create().Delete(xamAccountStore, $"{M_STR_APPNAME}_{m_strCopriHostHash}");
|
|
#endif
|
|
#endif
|
|
return new EmptyAccount();
|
|
}
|
|
}
|
|
}
|