mirror of
https://github.com/bitwarden/android.git
synced 2024-12-25 02:18:27 +03:00
prefix keychain key with appid
This commit is contained in:
parent
64506a7080
commit
6c56e44b61
2 changed files with 17 additions and 12 deletions
|
@ -12,30 +12,33 @@ namespace Bit.iOS.Core.Services
|
||||||
{
|
{
|
||||||
public class KeyChainStorageService : IStorageService
|
public class KeyChainStorageService : IStorageService
|
||||||
{
|
{
|
||||||
private readonly string _keyFormat = "bwKeyChainStorage:{0}";
|
private readonly string _keyFormat = "bwKeyChainStorage:{0}:{1}";
|
||||||
private readonly string _service;
|
private readonly string _service;
|
||||||
private readonly string _group;
|
private readonly string _group;
|
||||||
|
private readonly Func<Task<string>> _getAppId;
|
||||||
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||||
};
|
};
|
||||||
|
|
||||||
public KeyChainStorageService(string service, string group)
|
public KeyChainStorageService(string service, string group, Func<Task<string>> getAppId)
|
||||||
{
|
{
|
||||||
_service = service;
|
_service = service;
|
||||||
_group = group;
|
_group = group;
|
||||||
|
_getAppId = getAppId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<T> GetAsync<T>(string key)
|
public async Task<T> GetAsync<T>(string key)
|
||||||
{
|
{
|
||||||
var formattedKey = string.Format(_keyFormat, key);
|
var appId = await _getAppId.Invoke();
|
||||||
|
var formattedKey = string.Format(_keyFormat, appId, key);
|
||||||
byte[] dataBytes = null;
|
byte[] dataBytes = null;
|
||||||
using(var existingRecord = GetKeyRecord(formattedKey))
|
using(var existingRecord = GetKeyRecord(formattedKey))
|
||||||
using(var record = SecKeyChain.QueryAsRecord(existingRecord, out SecStatusCode resultCode))
|
using(var record = SecKeyChain.QueryAsRecord(existingRecord, out SecStatusCode resultCode))
|
||||||
{
|
{
|
||||||
if(resultCode == SecStatusCode.ItemNotFound)
|
if(resultCode == SecStatusCode.ItemNotFound)
|
||||||
{
|
{
|
||||||
return Task.FromResult((T)(object)null);
|
return (T)(object)null;
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckError(resultCode);
|
CheckError(resultCode);
|
||||||
|
@ -45,11 +48,11 @@ namespace Bit.iOS.Core.Services
|
||||||
var dataString = Encoding.UTF8.GetString(dataBytes);
|
var dataString = Encoding.UTF8.GetString(dataBytes);
|
||||||
if(typeof(T) == typeof(string))
|
if(typeof(T) == typeof(string))
|
||||||
{
|
{
|
||||||
return Task.FromResult((T)(object)dataString);
|
return (T)(object)dataString;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return Task.FromResult(JsonConvert.DeserializeObject<T>(dataString, _jsonSettings));
|
return JsonConvert.DeserializeObject<T>(dataString, _jsonSettings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +74,8 @@ namespace Bit.iOS.Core.Services
|
||||||
dataString = JsonConvert.SerializeObject(obj, _jsonSettings);
|
dataString = JsonConvert.SerializeObject(obj, _jsonSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
var formattedKey = string.Format(_keyFormat, key);
|
var appId = await _getAppId.Invoke();
|
||||||
|
var formattedKey = string.Format(_keyFormat, appId, key);
|
||||||
var dataBytes = Encoding.UTF8.GetBytes(dataString);
|
var dataBytes = Encoding.UTF8.GetBytes(dataString);
|
||||||
using(var data = NSData.FromArray(dataBytes))
|
using(var data = NSData.FromArray(dataBytes))
|
||||||
using(var newRecord = GetKeyRecord(formattedKey, data))
|
using(var newRecord = GetKeyRecord(formattedKey, data))
|
||||||
|
@ -81,9 +85,10 @@ namespace Bit.iOS.Core.Services
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task RemoveAsync(string key)
|
public async Task RemoveAsync(string key)
|
||||||
{
|
{
|
||||||
var formattedKey = string.Format(_keyFormat, key);
|
var appId = await _getAppId.Invoke();
|
||||||
|
var formattedKey = string.Format(_keyFormat, appId, key);
|
||||||
using(var record = GetExistingRecord(formattedKey))
|
using(var record = GetExistingRecord(formattedKey))
|
||||||
{
|
{
|
||||||
if(record != null)
|
if(record != null)
|
||||||
|
@ -91,7 +96,6 @@ namespace Bit.iOS.Core.Services
|
||||||
CheckError(SecKeyChain.Remove(record));
|
CheckError(SecKeyChain.Remove(record));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Task.FromResult(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private SecRecord GetKeyRecord(string key, NSData data = null)
|
private SecRecord GetKeyRecord(string key, NSData data = null)
|
||||||
|
|
|
@ -53,7 +53,8 @@ namespace Bit.iOS.Core.Utilities
|
||||||
var broadcasterService = new BroadcasterService();
|
var broadcasterService = new BroadcasterService();
|
||||||
var messagingService = new MobileBroadcasterMessagingService(broadcasterService);
|
var messagingService = new MobileBroadcasterMessagingService(broadcasterService);
|
||||||
var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo());
|
var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo());
|
||||||
var secureStorageService = new KeyChainStorageService(AppId, AccessGroup);
|
var secureStorageService = new KeyChainStorageService(AppId, AccessGroup,
|
||||||
|
() => ServiceContainer.Resolve<IAppIdService>("appIdService").GetAppIdAsync());
|
||||||
var cryptoPrimitiveService = new CryptoPrimitiveService();
|
var cryptoPrimitiveService = new CryptoPrimitiveService();
|
||||||
var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage);
|
var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage);
|
||||||
var deviceActionService = new DeviceActionService(mobileStorageService, messagingService);
|
var deviceActionService = new DeviceActionService(mobileStorageService, messagingService);
|
||||||
|
|
Loading…
Reference in a new issue