|
| 1 | +/* |
| 2 | + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: GPL-3.0-only |
| 4 | + */ |
| 5 | +package com.zeapo.pwdstore.autofill.oreo |
| 6 | + |
| 7 | +import android.content.Context |
| 8 | +import android.content.IntentSender |
| 9 | +import android.os.Build |
| 10 | +import android.os.Bundle |
| 11 | +import android.service.autofill.Dataset |
| 12 | +import android.service.autofill.FillCallback |
| 13 | +import android.service.autofill.FillResponse |
| 14 | +import android.service.autofill.SaveInfo |
| 15 | +import android.widget.RemoteViews |
| 16 | +import androidx.annotation.RequiresApi |
| 17 | +import com.github.ajalt.timberkt.e |
| 18 | +import com.github.michaelbull.result.fold |
| 19 | +import com.github.androidpasswordstore.autofillparser.AutofillAction |
| 20 | +import com.github.androidpasswordstore.autofillparser.AutofillScenario |
| 21 | +import com.github.androidpasswordstore.autofillparser.Credentials |
| 22 | +import com.github.androidpasswordstore.autofillparser.FillableForm |
| 23 | +import com.github.androidpasswordstore.autofillparser.fillWith |
| 24 | +import com.zeapo.pwdstore.autofill.oreo.ui.AutofillDecryptActivity |
| 25 | +import com.zeapo.pwdstore.autofill.oreo.ui.AutofillFilterView |
| 26 | +import com.zeapo.pwdstore.autofill.oreo.ui.AutofillPublisherChangedActivity |
| 27 | +import com.zeapo.pwdstore.autofill.oreo.ui.AutofillSaveActivity |
| 28 | +import com.zeapo.pwdstore.autofill.oreo.ui.AutofillSmsActivity |
| 29 | +import java.io.File |
| 30 | + |
| 31 | +@RequiresApi(Build.VERSION_CODES.O) |
| 32 | +class AutofillResponseBuilder(form: FillableForm) { |
| 33 | + private val formOrigin = form.formOrigin |
| 34 | + private val scenario = form.scenario |
| 35 | + private val ignoredIds = form.ignoredIds |
| 36 | + private val saveFlags = form.saveFlags |
| 37 | + private val clientState = form.toClientState() |
| 38 | + |
| 39 | + // We do not offer save when the only relevant field is a username field or there is no field. |
| 40 | + private val scenarioSupportsSave = |
| 41 | + scenario.fieldsToSave.minus(listOfNotNull(scenario.username)).isNotEmpty() |
| 42 | + private val canBeSaved = saveFlags != null && scenarioSupportsSave |
| 43 | + |
| 44 | + private fun makePlaceholderDataset( |
| 45 | + remoteView: RemoteViews, |
| 46 | + intentSender: IntentSender, |
| 47 | + action: AutofillAction |
| 48 | + ): Dataset { |
| 49 | + return Dataset.Builder(remoteView).run { |
| 50 | + fillWith(scenario, action, credentials = null) |
| 51 | + setAuthentication(intentSender) |
| 52 | + build() |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private fun makeMatchDataset(context: Context, file: File): Dataset? { |
| 57 | + if (scenario.fieldsToFillOn(AutofillAction.Match).isEmpty()) return null |
| 58 | + val remoteView = makeFillMatchRemoteView(context, file, formOrigin) |
| 59 | + val intentSender = AutofillDecryptActivity.makeDecryptFileIntentSender(file, context) |
| 60 | + return makePlaceholderDataset(remoteView, intentSender, AutofillAction.Match) |
| 61 | + } |
| 62 | + |
| 63 | + private fun makeSearchDataset(context: Context): Dataset? { |
| 64 | + if (scenario.fieldsToFillOn(AutofillAction.Search).isEmpty()) return null |
| 65 | + val remoteView = makeSearchAndFillRemoteView(context, formOrigin) |
| 66 | + val intentSender = |
| 67 | + AutofillFilterView.makeMatchAndDecryptFileIntentSender(context, formOrigin) |
| 68 | + return makePlaceholderDataset(remoteView, intentSender, AutofillAction.Search) |
| 69 | + } |
| 70 | + |
| 71 | + private fun makeGenerateDataset(context: Context): Dataset? { |
| 72 | + if (scenario.fieldsToFillOn(AutofillAction.Generate).isEmpty()) return null |
| 73 | + val remoteView = makeGenerateAndFillRemoteView(context, formOrigin) |
| 74 | + val intentSender = AutofillSaveActivity.makeSaveIntentSender(context, null, formOrigin) |
| 75 | + return makePlaceholderDataset(remoteView, intentSender, AutofillAction.Generate) |
| 76 | + } |
| 77 | + |
| 78 | + private fun makeFillOtpFromSmsDataset(context: Context): Dataset? { |
| 79 | + if (scenario.fieldsToFillOn(AutofillAction.FillOtpFromSms).isEmpty()) return null |
| 80 | + if (!AutofillSmsActivity.shouldOfferFillFromSms(context)) return null |
| 81 | + val remoteView = makeFillOtpFromSmsRemoteView(context, formOrigin) |
| 82 | + val intentSender = AutofillSmsActivity.makeFillOtpFromSmsIntentSender(context) |
| 83 | + return makePlaceholderDataset(remoteView, intentSender, AutofillAction.FillOtpFromSms) |
| 84 | + } |
| 85 | + |
| 86 | + private fun makePublisherChangedDataset( |
| 87 | + context: Context, |
| 88 | + publisherChangedException: AutofillPublisherChangedException |
| 89 | + ): Dataset { |
| 90 | + val remoteView = makeWarningRemoteView(context) |
| 91 | + val intentSender = AutofillPublisherChangedActivity.makePublisherChangedIntentSender( |
| 92 | + context, publisherChangedException |
| 93 | + ) |
| 94 | + return makePlaceholderDataset(remoteView, intentSender, AutofillAction.Match) |
| 95 | + } |
| 96 | + |
| 97 | + private fun makePublisherChangedResponse( |
| 98 | + context: Context, |
| 99 | + publisherChangedException: AutofillPublisherChangedException |
| 100 | + ): FillResponse { |
| 101 | + return FillResponse.Builder().run { |
| 102 | + addDataset(makePublisherChangedDataset(context, publisherChangedException)) |
| 103 | + setIgnoredIds(*ignoredIds.toTypedArray()) |
| 104 | + build() |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // TODO: Support multi-step authentication flows in apps via FLAG_DELAY_SAVE |
| 109 | + // See: https://developer.android.com/reference/android/service/autofill/SaveInfo#FLAG_DELAY_SAVE |
| 110 | + private fun makeSaveInfo(): SaveInfo? { |
| 111 | + if (!canBeSaved) return null |
| 112 | + check(saveFlags != null) |
| 113 | + val idsToSave = scenario.fieldsToSave.map { it.autofillId }.toTypedArray() |
| 114 | + if (idsToSave.isEmpty()) return null |
| 115 | + var saveDataTypes = SaveInfo.SAVE_DATA_TYPE_PASSWORD |
| 116 | + if (scenario.username != null) { |
| 117 | + saveDataTypes = saveDataTypes or SaveInfo.SAVE_DATA_TYPE_USERNAME |
| 118 | + } |
| 119 | + return SaveInfo.Builder(saveDataTypes, idsToSave).run { |
| 120 | + setFlags(saveFlags) |
| 121 | + build() |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private fun makeFillResponse(context: Context, matchedFiles: List<File>): FillResponse? { |
| 126 | + var hasDataset = false |
| 127 | + return FillResponse.Builder().run { |
| 128 | + for (file in matchedFiles) { |
| 129 | + makeMatchDataset(context, file)?.let { |
| 130 | + hasDataset = true |
| 131 | + addDataset(it) |
| 132 | + } |
| 133 | + } |
| 134 | + makeSearchDataset(context)?.let { |
| 135 | + hasDataset = true |
| 136 | + addDataset(it) |
| 137 | + } |
| 138 | + makeGenerateDataset(context)?.let { |
| 139 | + hasDataset = true |
| 140 | + addDataset(it) |
| 141 | + } |
| 142 | + makeFillOtpFromSmsDataset(context)?.let { |
| 143 | + hasDataset = true |
| 144 | + addDataset(it) |
| 145 | + } |
| 146 | + if (!hasDataset) return null |
| 147 | + makeSaveInfo()?.let { setSaveInfo(it) } |
| 148 | + setClientState(clientState) |
| 149 | + setIgnoredIds(*ignoredIds.toTypedArray()) |
| 150 | + build() |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Creates and returns a suitable [FillResponse] to the Autofill framework. |
| 156 | + */ |
| 157 | + fun fillCredentials(context: Context, callback: FillCallback) { |
| 158 | + AutofillMatcher.getMatchesFor(context, formOrigin).fold( |
| 159 | + success = { matchedFiles -> |
| 160 | + callback.onSuccess(makeFillResponse(context, matchedFiles)) |
| 161 | + }, |
| 162 | + failure = { e -> |
| 163 | + e(e) |
| 164 | + callback.onSuccess(makePublisherChangedResponse(context, e)) |
| 165 | + } |
| 166 | + ) |
| 167 | + } |
| 168 | + |
| 169 | + companion object { |
| 170 | + fun makeFillInDataset( |
| 171 | + context: Context, |
| 172 | + credentials: Credentials, |
| 173 | + clientState: Bundle, |
| 174 | + action: AutofillAction |
| 175 | + ): Dataset { |
| 176 | + val remoteView = makePlaceholderRemoteView(context) |
| 177 | + val scenario = AutofillScenario.fromBundle(clientState) |
| 178 | + // Before Android P, Datasets used for fill-in had to come with a RemoteViews, even |
| 179 | + // though they are never shown. |
| 180 | + val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { |
| 181 | + Dataset.Builder() |
| 182 | + } else { |
| 183 | + Dataset.Builder(remoteView) |
| 184 | + } |
| 185 | + return builder.run { |
| 186 | + if (scenario != null) fillWith(scenario, action, credentials) |
| 187 | + else e { "Failed to recover scenario from client state" } |
| 188 | + build() |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments