Bitwarden-app-android-iphon.../src/iOS.Core/Utilities/ASHelpers.cs

88 lines
3.4 KiB
C#
Raw Normal View History

2019-06-27 20:07:25 +02:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AuthenticationServices;
using Bit.Core.Abstractions;
using Bit.Core.Models.View;
2019-06-27 22:22:58 +02:00
using Bit.Core.Utilities;
2019-06-27 20:07:25 +02:00
namespace Bit.iOS.Core.Utilities
{
public static class ASHelpers
{
2019-06-27 22:22:58 +02:00
public static async Task ReplaceAllIdentities()
2019-06-27 20:07:25 +02:00
{
if(await AutofillEnabled())
{
2019-07-22 13:09:51 +02:00
var storageService = ServiceContainer.Resolve<IStorageService>("storageService");
var lockService = ServiceContainer.Resolve<ILockService>("lockService");
if(await lockService.IsLockedAsync())
{
await storageService.SaveAsync(Constants.AutofillNeedsIdentityReplacementKey, true);
return;
}
var cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
2019-06-27 20:07:25 +02:00
var identities = new List<ASPasswordCredentialIdentity>();
var ciphers = await cipherService.GetAllDecryptedAsync();
foreach(var cipher in ciphers)
{
var identity = ToCredentialIdentity(cipher);
if(identity != null)
{
identities.Add(identity);
}
}
if(identities.Any())
{
await ASCredentialIdentityStore.SharedStore?.ReplaceCredentialIdentitiesAsync(identities.ToArray());
2019-07-22 13:09:51 +02:00
await storageService.SaveAsync(Constants.AutofillNeedsIdentityReplacementKey, false);
2019-06-27 20:07:25 +02:00
}
}
}
public static async Task<bool> IdentitiesCanIncremental()
{
var state = await ASCredentialIdentityStore.SharedStore?.GetCredentialIdentityStoreStateAsync();
return state != null && state.Enabled && state.SupportsIncrementalUpdates;
}
public static async Task<bool> AutofillEnabled()
{
var state = await ASCredentialIdentityStore.SharedStore?.GetCredentialIdentityStoreStateAsync();
return state != null && state.Enabled;
}
2019-06-27 22:22:58 +02:00
public static async Task<ASPasswordCredentialIdentity> GetCipherIdentityAsync(string cipherId)
2019-06-27 20:07:25 +02:00
{
2019-06-27 22:22:58 +02:00
var cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
2019-06-27 20:07:25 +02:00
var cipher = await cipherService.GetAsync(cipherId);
if(cipher == null)
{
return null;
}
var cipherView = await cipher.DecryptAsync();
return ToCredentialIdentity(cipherView);
}
public static ASPasswordCredentialIdentity ToCredentialIdentity(CipherView cipher)
{
if(!cipher?.Login?.Uris?.Any() ?? true)
{
return null;
}
2019-11-19 13:47:01 +01:00
var uri = cipher.Login.Uris.FirstOrDefault(u => u.Match != Bit.Core.Enums.UriMatchType.Never)?.Uri;
2019-06-27 20:07:25 +02:00
if(string.IsNullOrWhiteSpace(uri))
{
return null;
}
var username = cipher.Login.Username;
if(string.IsNullOrWhiteSpace(username))
{
return null;
}
var serviceId = new ASCredentialServiceIdentifier(uri, ASCredentialServiceIdentifierType.Url);
return new ASPasswordCredentialIdentity(serviceId, username, cipher.Id);
}
}
}