Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 1 | package com.android.internal.widget; |
| 2 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame^] | 3 | import android.annotation.UnsupportedAppUsage; |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 4 | import android.os.AsyncTask; |
| 5 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 6 | import com.android.internal.widget.LockPatternUtils.RequestThrottledException; |
| 7 | |
Nancy Zheng | 9475460 | 2016-10-07 10:41:00 -0700 | [diff] [blame] | 8 | import java.util.ArrayList; |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 9 | import java.util.List; |
| 10 | |
| 11 | /** |
| 12 | * Helper class to check/verify PIN/Password/Pattern asynchronously. |
| 13 | */ |
| 14 | public final class LockPatternChecker { |
| 15 | /** |
| 16 | * Interface for a callback to be invoked after security check. |
| 17 | */ |
| 18 | public interface OnCheckCallback { |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 19 | |
| 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 Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Invoked when a security check is finished. |
| 28 | * |
| 29 | * @param matched Whether the PIN/Password/Pattern matches the stored one. |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 30 | * @param throttleTimeoutMs The amount of time in ms to wait before reattempting |
| 31 | * the call. Only non-0 if matched is false. |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 32 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 33 | void onChecked(boolean matched, int throttleTimeoutMs); |
Jorim Jaggi | ed3032b | 2016-10-07 16:49:34 +0200 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Called when the underlying AsyncTask was cancelled. |
| 37 | */ |
| 38 | default void onCancelled() {} |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 39 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 49 | * @param throttleTimeoutMs The amount of time in ms to wait before reattempting |
| 50 | * the call. Only non-0 if attestation is null. |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 51 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 52 | void onVerified(byte[] attestation, int throttleTimeoutMs); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 53 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 70 | private int mThrottleTimeout; |
Nancy Zheng | 9475460 | 2016-10-07 10:41:00 -0700 | [diff] [blame] | 71 | 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 79 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 80 | @Override |
| 81 | protected byte[] doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 82 | try { |
Nancy Zheng | 9475460 | 2016-10-07 10:41:00 -0700 | [diff] [blame] | 83 | return utils.verifyPattern(patternCopy, challenge, userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 84 | } catch (RequestThrottledException ex) { |
| 85 | mThrottleTimeout = ex.getTimeoutMs(); |
| 86 | return null; |
| 87 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void onPostExecute(byte[] result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 92 | callback.onVerified(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 93 | } |
| 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 112 | private int mThrottleTimeout; |
Nancy Zheng | 9475460 | 2016-10-07 10:41:00 -0700 | [diff] [blame] | 113 | 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 Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 121 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 122 | @Override |
| 123 | protected Boolean doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 124 | try { |
Nancy Zheng | 9475460 | 2016-10-07 10:41:00 -0700 | [diff] [blame] | 125 | return utils.checkPattern(patternCopy, userId, callback::onEarlyMatched); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 126 | } catch (RequestThrottledException ex) { |
| 127 | mThrottleTimeout = ex.getTimeoutMs(); |
| 128 | return false; |
| 129 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected void onPostExecute(Boolean result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 134 | callback.onChecked(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 135 | } |
Jorim Jaggi | ed3032b | 2016-10-07 16:49:34 +0200 | [diff] [blame] | 136 | |
| 137 | @Override |
| 138 | protected void onCancelled() { |
| 139 | callback.onCancelled(); |
| 140 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 141 | }; |
| 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 Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 154 | * |
| 155 | * @deprecated Pass the password as a byte array. |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 156 | */ |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 157 | @Deprecated |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 158 | public static AsyncTask<?, ?, ?> verifyPassword(final LockPatternUtils utils, |
| 159 | final String password, |
| 160 | final long challenge, |
| 161 | final int userId, |
| 162 | final OnVerifyCallback callback) { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 163 | 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 Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 181 | AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 182 | private int mThrottleTimeout; |
| 183 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 184 | @Override |
| 185 | protected byte[] doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 186 | try { |
| 187 | return utils.verifyPassword(password, challenge, userId); |
| 188 | } catch (RequestThrottledException ex) { |
| 189 | mThrottleTimeout = ex.getTimeoutMs(); |
| 190 | return null; |
| 191 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | protected void onPostExecute(byte[] result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 196 | callback.onVerified(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 197 | } |
| 198 | }; |
| 199 | task.execute(); |
| 200 | return task; |
| 201 | } |
| 202 | |
| 203 | /** |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 204 | * 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 Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 213 | final byte[] password, |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 214 | 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 Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 241 | * 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 Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 247 | * @deprecated Pass passwords as byte[] |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 248 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame^] | 249 | @UnsupportedAppUsage |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 250 | @Deprecated |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 251 | public static AsyncTask<?, ?, ?> checkPassword(final LockPatternUtils utils, |
| 252 | final String password, |
| 253 | final int userId, |
| 254 | final OnCheckCallback callback) { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 255 | 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 Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 271 | AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 272 | private int mThrottleTimeout; |
| 273 | |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 274 | @Override |
| 275 | protected Boolean doInBackground(Void... args) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 276 | try { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 277 | return utils.checkPassword(passwordBytes, userId, callback::onEarlyMatched); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 278 | } catch (RequestThrottledException ex) { |
| 279 | mThrottleTimeout = ex.getTimeoutMs(); |
| 280 | return false; |
| 281 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | @Override |
| 285 | protected void onPostExecute(Boolean result) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 286 | callback.onChecked(result, mThrottleTimeout); |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 287 | } |
Jorim Jaggi | ed3032b | 2016-10-07 16:49:34 +0200 | [diff] [blame] | 288 | |
| 289 | @Override |
| 290 | protected void onCancelled() { |
| 291 | callback.onCancelled(); |
| 292 | } |
Xiyuan Xia | 6e38058 | 2015-05-05 15:16:08 -0700 | [diff] [blame] | 293 | }; |
| 294 | task.execute(); |
| 295 | return task; |
| 296 | } |
| 297 | } |