bitwarden-android/src/Android/Autofill/AutofillService.cs

87 lines
3.1 KiB
C#
Raw Normal View History

using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Service.Autofill;
using Bit.App.Abstractions;
using Bit.App.Enums;
using System.Linq;
using XLabs.Ioc;
namespace Bit.Android.Autofill
{
[Service(Permission = Manifest.Permission.BindAutofillService, Label = "bitwarden")]
[IntentFilter(new string[] { "android.service.autofill.AutofillService" })]
[MetaData("android.autofill", Resource = "@xml/autofillservice")]
[Register("com.x8bit.bitwarden.Autofill.AutofillService")]
public class AutofillService : global::Android.Service.Autofill.AutofillService
{
private ICipherService _cipherService;
private ILockService _lockService;
public async override void OnFillRequest(FillRequest request, CancellationSignal cancellationSignal, FillCallback callback)
{
var structure = request.FillContexts?.LastOrDefault()?.Structure;
if(structure == null)
{
return;
}
var parser = new Parser(structure);
parser.ParseForFill();
2017-11-17 18:05:13 +03:00
if(!parser.FieldCollection.Fields.Any() || string.IsNullOrWhiteSpace(parser.Uri) ||
2017-11-17 18:18:18 +03:00
parser.Uri == "androidapp://com.x8bit.bitwarden" || parser.Uri == "androidapp://android")
{
return;
}
if(_lockService == null)
{
_lockService = Resolver.Resolve<ILockService>();
}
var isLocked = (await _lockService.GetLockTypeAsync(false)) != LockType.None;
2017-11-17 17:21:12 +03:00
if(isLocked)
{
2017-11-17 17:21:12 +03:00
var authResponse = AutofillHelpers.BuildAuthResponse(this, parser.FieldCollection, parser.Uri);
callback.OnSuccess(authResponse);
return;
}
if(_cipherService == null)
{
_cipherService = Resolver.Resolve<ICipherService>();
}
// build response
var items = await AutofillHelpers.GetFillItemsAsync(_cipherService, parser.Uri);
var response = AutofillHelpers.BuildFillResponse(this, parser.FieldCollection, items);
callback.OnSuccess(response);
}
public override void OnSaveRequest(SaveRequest request, SaveCallback callback)
{
var structure = request.FillContexts?.LastOrDefault()?.Structure;
if(structure == null)
{
return;
}
var parser = new Parser(structure);
parser.ParseForSave();
var intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("autofillFramework", true);
intent.PutExtra("autofillFrameworkSave", true);
intent.PutExtra("autofillFrameworkType", (int)CipherType.Login);
intent.PutExtra("autofillFrameworkUri", parser.Uri);
intent.PutExtra("autofillFrameworkUsername", "username");
intent.PutExtra("autofillFrameworkPassword", "pass123");
intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTop);
StartActivity(intent);
}
}
}