blob: d78b26ab6dcb91bbe3f8777faea1237ae0db0afb [file] [log] [blame]
Xiyuan Xia6e380582015-05-05 15:16:08 -07001package com.android.internal.widget;
2
Artur Satayev2a9f3b82019-12-10 17:47:55 +00003import android.compat.annotation.UnsupportedAppUsage;
Xiyuan Xia6e380582015-05-05 15:16:08 -07004import android.os.AsyncTask;
5
Andres Morales23974272015-05-14 22:42:26 -07006import com.android.internal.widget.LockPatternUtils.RequestThrottledException;
7
Nancy Zheng94754602016-10-07 10:41:00 -07008import java.util.ArrayList;
Xiyuan Xia6e380582015-05-05 15:16:08 -07009import java.util.List;
10
11/**
12 * Helper class to check/verify PIN/Password/Pattern asynchronously.
13 */
14public final class LockPatternChecker {
15 /**
16 * Interface for a callback to be invoked after security check.
17 */
18 public interface OnCheckCallback {
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070019
20 /**
21 * Invoked as soon as possible we know that the credentials match. This will be called
22 * earlier than {@link #onChecked} but only if the credentials match.
23 */
24 default void onEarlyMatched() {}
25
Xiyuan Xia6e380582015-05-05 15:16:08 -070026 /**
27 * Invoked when a security check is finished.
28 *
29 * @param matched Whether the PIN/Password/Pattern matches the stored one.
Andres Morales23974272015-05-14 22:42:26 -070030 * @param throttleTimeoutMs The amount of time in ms to wait before reattempting
31 * the call. Only non-0 if matched is false.
Xiyuan Xia6e380582015-05-05 15:16:08 -070032 */
Andres Morales23974272015-05-14 22:42:26 -070033 void onChecked(boolean matched, int throttleTimeoutMs);
Jorim Jaggied3032b2016-10-07 16:49:34 +020034
35 /**
36 * Called when the underlying AsyncTask was cancelled.
37 */
38 default void onCancelled() {}
Xiyuan Xia6e380582015-05-05 15:16:08 -070039 }
40
41 /**
42 * Interface for a callback to be invoked after security verification.
43 */
44 public interface OnVerifyCallback {
45 /**
46 * Invoked when a security verification is finished.
47 *
48 * @param attestation The attestation that the challenge was verified, or null.
Andres Morales23974272015-05-14 22:42:26 -070049 * @param throttleTimeoutMs The amount of time in ms to wait before reattempting
50 * the call. Only non-0 if attestation is null.
Xiyuan Xia6e380582015-05-05 15:16:08 -070051 */
Andres Morales23974272015-05-14 22:42:26 -070052 void onVerified(byte[] attestation, int throttleTimeoutMs);
Xiyuan Xia6e380582015-05-05 15:16:08 -070053 }
54
55 /**
56 * Verify a pattern asynchronously.
57 *
58 * @param utils The LockPatternUtils instance to use.
59 * @param pattern The pattern to check.
60 * @param challenge The challenge to verify against the pattern.
61 * @param userId The user to check against the pattern.
62 * @param callback The callback to be invoked with the verification result.
63 */
64 public static AsyncTask<?, ?, ?> verifyPattern(final LockPatternUtils utils,
65 final List<LockPatternView.Cell> pattern,
66 final long challenge,
67 final int userId,
68 final OnVerifyCallback callback) {
69 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
Andres Morales23974272015-05-14 22:42:26 -070070 private int mThrottleTimeout;
Nancy Zheng94754602016-10-07 10:41:00 -070071 private List<LockPatternView.Cell> patternCopy;
72
73 @Override
74 protected void onPreExecute() {
75 // Make a copy of the pattern to prevent race conditions.
76 // No need to clone the individual cells because they are immutable.
77 patternCopy = new ArrayList(pattern);
78 }
Andres Morales23974272015-05-14 22:42:26 -070079
Xiyuan Xia6e380582015-05-05 15:16:08 -070080 @Override
81 protected byte[] doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -070082 try {
Nancy Zheng94754602016-10-07 10:41:00 -070083 return utils.verifyPattern(patternCopy, challenge, userId);
Andres Morales23974272015-05-14 22:42:26 -070084 } catch (RequestThrottledException ex) {
85 mThrottleTimeout = ex.getTimeoutMs();
86 return null;
87 }
Xiyuan Xia6e380582015-05-05 15:16:08 -070088 }
89
90 @Override
91 protected void onPostExecute(byte[] result) {
Andres Morales23974272015-05-14 22:42:26 -070092 callback.onVerified(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -070093 }
94 };
95 task.execute();
96 return task;
97 }
98
99 /**
100 * Checks a pattern asynchronously.
101 *
102 * @param utils The LockPatternUtils instance to use.
103 * @param pattern The pattern to check.
104 * @param userId The user to check against the pattern.
105 * @param callback The callback to be invoked with the check result.
106 */
107 public static AsyncTask<?, ?, ?> checkPattern(final LockPatternUtils utils,
108 final List<LockPatternView.Cell> pattern,
109 final int userId,
110 final OnCheckCallback callback) {
111 AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
Andres Morales23974272015-05-14 22:42:26 -0700112 private int mThrottleTimeout;
Nancy Zheng94754602016-10-07 10:41:00 -0700113 private List<LockPatternView.Cell> patternCopy;
114
115 @Override
116 protected void onPreExecute() {
117 // Make a copy of the pattern to prevent race conditions.
118 // No need to clone the individual cells because they are immutable.
119 patternCopy = new ArrayList(pattern);
120 }
Andres Morales23974272015-05-14 22:42:26 -0700121
Xiyuan Xia6e380582015-05-05 15:16:08 -0700122 @Override
123 protected Boolean doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -0700124 try {
Nancy Zheng94754602016-10-07 10:41:00 -0700125 return utils.checkPattern(patternCopy, userId, callback::onEarlyMatched);
Andres Morales23974272015-05-14 22:42:26 -0700126 } catch (RequestThrottledException ex) {
127 mThrottleTimeout = ex.getTimeoutMs();
128 return false;
129 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700130 }
131
132 @Override
133 protected void onPostExecute(Boolean result) {
Andres Morales23974272015-05-14 22:42:26 -0700134 callback.onChecked(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -0700135 }
Jorim Jaggied3032b2016-10-07 16:49:34 +0200136
137 @Override
138 protected void onCancelled() {
139 callback.onCancelled();
140 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700141 };
142 task.execute();
143 return task;
144 }
145
146 /**
147 * Verify a password asynchronously.
148 *
149 * @param utils The LockPatternUtils instance to use.
150 * @param password The password to check.
151 * @param challenge The challenge to verify against the pattern.
152 * @param userId The user to check against the pattern.
153 * @param callback The callback to be invoked with the verification result.
Rich Canningsf64ec632019-02-21 12:40:36 -0800154 *
155 * @deprecated Pass the password as a byte array.
Xiyuan Xia6e380582015-05-05 15:16:08 -0700156 */
Rich Canningsf64ec632019-02-21 12:40:36 -0800157 @Deprecated
Xiyuan Xia6e380582015-05-05 15:16:08 -0700158 public static AsyncTask<?, ?, ?> verifyPassword(final LockPatternUtils utils,
159 final String password,
160 final long challenge,
161 final int userId,
162 final OnVerifyCallback callback) {
Rich Canningsf64ec632019-02-21 12:40:36 -0800163 byte[] passwordBytes = password != null ? password.getBytes() : null;
164 return verifyPassword(utils, passwordBytes, challenge, userId, callback);
165 }
166
167 /**
168 * Verify a password asynchronously.
169 *
170 * @param utils The LockPatternUtils instance to use.
171 * @param password The password to check.
172 * @param challenge The challenge to verify against the pattern.
173 * @param userId The user to check against the pattern.
174 * @param callback The callback to be invoked with the verification result.
175 */
176 public static AsyncTask<?, ?, ?> verifyPassword(final LockPatternUtils utils,
177 final byte[] password,
178 final long challenge,
179 final int userId,
180 final OnVerifyCallback callback) {
Xiyuan Xia6e380582015-05-05 15:16:08 -0700181 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
Andres Morales23974272015-05-14 22:42:26 -0700182 private int mThrottleTimeout;
183
Xiyuan Xia6e380582015-05-05 15:16:08 -0700184 @Override
185 protected byte[] doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -0700186 try {
187 return utils.verifyPassword(password, challenge, userId);
188 } catch (RequestThrottledException ex) {
189 mThrottleTimeout = ex.getTimeoutMs();
190 return null;
191 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700192 }
193
194 @Override
195 protected void onPostExecute(byte[] result) {
Andres Morales23974272015-05-14 22:42:26 -0700196 callback.onVerified(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -0700197 }
198 };
199 task.execute();
200 return task;
201 }
202
203 /**
Ricky Wai53940d42016-04-05 15:29:24 +0100204 * Verify a password asynchronously.
205 *
206 * @param utils The LockPatternUtils instance to use.
207 * @param password The password to check.
208 * @param challenge The challenge to verify against the pattern.
209 * @param userId The user to check against the pattern.
210 * @param callback The callback to be invoked with the verification result.
211 */
212 public static AsyncTask<?, ?, ?> verifyTiedProfileChallenge(final LockPatternUtils utils,
Rich Canningsf64ec632019-02-21 12:40:36 -0800213 final byte[] password,
Ricky Wai53940d42016-04-05 15:29:24 +0100214 final boolean isPattern,
215 final long challenge,
216 final int userId,
217 final OnVerifyCallback callback) {
218 AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
219 private int mThrottleTimeout;
220
221 @Override
222 protected byte[] doInBackground(Void... args) {
223 try {
224 return utils.verifyTiedProfileChallenge(password, isPattern, challenge, userId);
225 } catch (RequestThrottledException ex) {
226 mThrottleTimeout = ex.getTimeoutMs();
227 return null;
228 }
229 }
230
231 @Override
232 protected void onPostExecute(byte[] result) {
233 callback.onVerified(result, mThrottleTimeout);
234 }
235 };
236 task.execute();
237 return task;
238 }
239
240 /**
Xiyuan Xia6e380582015-05-05 15:16:08 -0700241 * Checks a password asynchronously.
242 *
243 * @param utils The LockPatternUtils instance to use.
244 * @param password The password to check.
245 * @param userId The user to check against the pattern.
246 * @param callback The callback to be invoked with the check result.
Rich Canningsf64ec632019-02-21 12:40:36 -0800247 * @deprecated Pass passwords as byte[]
Xiyuan Xia6e380582015-05-05 15:16:08 -0700248 */
Andrei Onea15884392019-03-22 17:28:11 +0000249 @UnsupportedAppUsage
Rich Canningsf64ec632019-02-21 12:40:36 -0800250 @Deprecated
Xiyuan Xia6e380582015-05-05 15:16:08 -0700251 public static AsyncTask<?, ?, ?> checkPassword(final LockPatternUtils utils,
252 final String password,
253 final int userId,
254 final OnCheckCallback callback) {
Rich Canningsf64ec632019-02-21 12:40:36 -0800255 byte[] passwordBytes = password != null ? password.getBytes() : null;
256 return checkPassword(utils, passwordBytes, userId, callback);
257 }
258
259 /**
260 * Checks a password asynchronously.
261 *
262 * @param utils The LockPatternUtils instance to use.
263 * @param passwordBytes The password to check.
264 * @param userId The user to check against the pattern.
265 * @param callback The callback to be invoked with the check result.
266 */
267 public static AsyncTask<?, ?, ?> checkPassword(final LockPatternUtils utils,
268 final byte[] passwordBytes,
269 final int userId,
270 final OnCheckCallback callback) {
Xiyuan Xia6e380582015-05-05 15:16:08 -0700271 AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
Andres Morales23974272015-05-14 22:42:26 -0700272 private int mThrottleTimeout;
273
Xiyuan Xia6e380582015-05-05 15:16:08 -0700274 @Override
275 protected Boolean doInBackground(Void... args) {
Andres Morales23974272015-05-14 22:42:26 -0700276 try {
Rich Canningsf64ec632019-02-21 12:40:36 -0800277 return utils.checkPassword(passwordBytes, userId, callback::onEarlyMatched);
Andres Morales23974272015-05-14 22:42:26 -0700278 } catch (RequestThrottledException ex) {
279 mThrottleTimeout = ex.getTimeoutMs();
280 return false;
281 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700282 }
283
284 @Override
285 protected void onPostExecute(Boolean result) {
Andres Morales23974272015-05-14 22:42:26 -0700286 callback.onChecked(result, mThrottleTimeout);
Xiyuan Xia6e380582015-05-05 15:16:08 -0700287 }
Jorim Jaggied3032b2016-10-07 16:49:34 +0200288
289 @Override
290 protected void onCancelled() {
291 callback.onCancelled();
292 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700293 };
294 task.execute();
295 return task;
296 }
297}