1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* Copyright © 2014-2024 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception
*/
package com.github.androidpasswordstore.autofillparser
import android.os.Build
import androidx.annotation.RequiresApi
import com.github.androidpasswordstore.autofillparser.CertaintyLevel.Certain
import com.github.androidpasswordstore.autofillparser.CertaintyLevel.Likely
private inline fun <T> Pair<T, T>.all(predicate: T.() -> Boolean) =
predicate(first) && predicate(second)
private inline fun <T> Pair<T, T>.any(predicate: T.() -> Boolean) =
predicate(first) || predicate(second)
private inline fun <T> Pair<T, T>.none(predicate: T.() -> Boolean) =
!predicate(first) && !predicate(second)
/**
* The strategy used to detect [AutofillScenario] s; expressed using the DSL implemented in
* [AutofillDsl].
*/
@RequiresApi(Build.VERSION_CODES.O)
internal val autofillStrategy = strategy {
// Match two new password fields, an optional current password field right below or above, and
// an optional username field with autocomplete hint.
// TODO: Introduce a custom fill/generate/update flow for this scenario
rule {
newPassword {
takePair { all { hasHintNewPassword } }
breakTieOnPair { any { isFocused } }
}
currentPassword(optional = true) {
takeSingle { alreadyMatched ->
val adjacentToNewPasswords =
directlyPrecedes(alreadyMatched) || directlyFollows(alreadyMatched)
// The Autofill framework has not hint that applies to current passwords only.
// In this scenario, we have already matched fields a pair of fields with a specific
// new password hint, so we take a generic Autofill password hint to mean a current
// password.
(hasAutocompleteHintCurrentPassword || hasAutofillHintPassword) && adjacentToNewPasswords
}
}
username(optional = true) {
takeSingle { hasHintUsername }
breakTieOnSingle { alreadyMatched -> directlyPrecedes(alreadyMatched) }
breakTieOnSingle { isFocused }
}
}
// Match a single focused current password field and hidden username field with autocomplete
// hint. This configuration is commonly used in two-step login flows to allow password managers
// to save the username.
// See:
// https://www.chromium.org/developers/design-documents/form-styles-that-chromium-understands
// Note: The username is never filled in this scenario since usernames are generally only filled
// in visible fields.
rule {
username(matchHidden = true) { takeSingle { couldBeTwoStepHiddenUsername } }
currentPassword { takeSingle { hasAutocompleteHintCurrentPassword && isFocused } }
}
// Match a single current password field and optional username field with autocomplete hint.
rule {
currentPassword {
takeSingle { hasAutocompleteHintCurrentPassword }
breakTieOnSingle { isFocused }
}
username(optional = true) {
takeSingle { hasHintUsername }
breakTieOnSingle { alreadyMatched -> directlyPrecedes(alreadyMatched) }
breakTieOnSingle { isFocused }
}
}
// Match two adjacent password fields, implicitly understood as new passwords, and optional
// username field.
rule {
newPassword {
takePair { all { passwordCertainty >= Likely } }
breakTieOnPair { all { passwordCertainty >= Certain } }
breakTieOnPair { any { isFocused } }
}
username(optional = true) {
takeSingle { usernameCertainty >= Likely }
breakTieOnSingle { usernameCertainty >= Certain }
breakTieOnSingle { alreadyMatched -> directlyPrecedes(alreadyMatched) }
breakTieOnSingle { isFocused }
}
}
// Match a single password field and optional username field.
rule {
genericPassword {
takeSingle { passwordCertainty >= Likely }
breakTieOnSingle { passwordCertainty >= Certain }
breakTieOnSingle { isFocused }
}
username(optional = true) {
takeSingle { usernameCertainty >= Likely }
breakTieOnSingle { usernameCertainty >= Certain }
breakTieOnSingle { alreadyMatched -> directlyPrecedes(alreadyMatched) }
breakTieOnSingle { isFocused }
}
}
// Match a single focused new password field and optional preceding username field.
// This rule can apply in single origin mode since it only fills into a single focused password
// field.
rule(applyInSingleOriginMode = true) {
newPassword { takeSingle { hasHintNewPassword && isFocused } }
username(optional = true) {
takeSingle { alreadyMatched ->
usernameCertainty >= Likely && directlyPrecedes(alreadyMatched.singleOrNull())
}
}
}
// Match a single focused current password field and optional preceding username field.
// This rule can apply in single origin mode since it only fills into a single focused password
// field.
rule(applyInSingleOriginMode = true) {
currentPassword { takeSingle { hasAutocompleteHintCurrentPassword && isFocused } }
username(optional = true) {
takeSingle { alreadyMatched ->
usernameCertainty >= Likely && directlyPrecedes(alreadyMatched.singleOrNull())
}
}
}
// Match a single focused password field and optional preceding username field.
// This rule can apply in single origin mode since it only fills into a single focused password
// field.
rule(applyInSingleOriginMode = true) {
genericPassword { takeSingle { passwordCertainty >= Likely && isFocused } }
username(optional = true) {
takeSingle { alreadyMatched ->
usernameCertainty >= Likely && directlyPrecedes(alreadyMatched.singleOrNull())
}
}
}
// Match a focused username field with autocomplete hint directly followed by a hidden password
// field, which is a common scenario in two-step login flows. No tie breakers are used to limit
// filling of hidden password fields to scenarios where this is clearly warranted.
rule {
username { takeSingle { hasHintUsername && isFocused } }
currentPassword(matchHidden = true) {
takeSingle { alreadyMatched ->
directlyFollows(alreadyMatched.singleOrNull()) && couldBeTwoStepHiddenPassword
}
}
}
// Match a single focused OTP field.
rule(applyInSingleOriginMode = true) {
otp { takeSingle { otpCertainty >= Likely && isFocused } }
}
// Match a single focused username field without a password field.
rule(applyInSingleOriginMode = true) {
username {
takeSingle { usernameCertainty >= Likely && isFocused }
breakTieOnSingle { usernameCertainty >= Certain }
breakTieOnSingle { hasHintUsername }
}
}
// Fallback rule for the case of a login form with a password field and other fields that are
// not recognized by any other rule. If one of the other fields is focused and we return no
// response, the system will not invoke the service again if focus later changes to the password
// field. Hence, we must mark it as fillable now.
// This rule can apply in single origin mode since even though the password field may not be
// focused at the time the rule runs, the fill suggestion will only show if it ever receives
// focus.
rule(applyInSingleOriginMode = true) {
currentPassword { takeSingle { hasAutocompleteHintCurrentPassword } }
}
// See above.
rule(applyInSingleOriginMode = true) { genericPassword { takeSingle { true } } }
// Match any focused password field with optional username field on manual request.
rule(applyInSingleOriginMode = true, applyOnManualRequestOnly = true) {
genericPassword { takeSingle { isFocused } }
username(optional = true) {
takeSingle { alreadyMatched ->
usernameCertainty >= Likely && directlyPrecedes(alreadyMatched.singleOrNull())
}
}
}
// Match any focused username field on manual request.
rule(applyInSingleOriginMode = true, applyOnManualRequestOnly = true) {
username { takeSingle { isFocused } }
}
}
|