| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview |
| 7 * |
| 8 * 'settings-quick-unlock-authenticate' shows a password input prompt to the |
| 9 * user. It validates the password is correct. Once the user has entered their |
| 10 * account password, the page navigates to the quick unlock setup methods page. |
| 11 * |
| 12 * This element provides a wrapper around chrome.quickUnlockPrivate.setModes |
| 13 * which has a prebound account password (the |set-modes| property). The account |
| 14 * password by itself is not available for other elements to access. |
| 15 * |
| 16 * Example: |
| 17 * |
| 18 * <settings-quick-unlock-authenticate |
| 19 * set-modes="[[setModes]]" |
| 20 * current-route="{{currentRoute}}" |
| 21 * profile-name="[[profileName_]]"> |
| 22 * </settings-quick-unlock-authenticate> |
| 23 */ |
| 24 |
| 25 (function() { |
| 26 'use strict'; |
| 27 |
| 28 /** @const */ var PASSWORD_ACTIVE_DURATION_MS = 10 * 60 * 1000; // Ten minutes. |
| 29 /** @const */ var AUTOSUBMIT_DELAY_MS = 500; // .5 seconds |
| 30 |
| 31 /** |
| 32 * Helper method that checks if |password| is valid. |
| 33 * @param {string} password |
| 34 * @param {function(boolean):void} onCheck |
| 35 */ |
| 36 function checkAccountPassword_(password, onCheck) { |
| 37 // We check the account password by trying to update the active set of quick |
| 38 // unlock modes without changing any credentials. |
| 39 chrome.quickUnlockPrivate.getActiveModes(function(modes) { |
| 40 var credentials = |
| 41 /** @type {!Array<string>} */ (Array(modes.length).fill('')); |
| 42 chrome.quickUnlockPrivate.setModes(password, modes, credentials, onCheck); |
| 43 }); |
| 44 } |
| 45 |
| 46 Polymer({ |
| 47 is: 'settings-quick-unlock-authenticate', |
| 48 |
| 49 behaviors: [ |
| 50 QuickUnlockRoutingBehavior, |
| 51 ], |
| 52 |
| 53 properties: { |
| 54 /** |
| 55 * A wrapper around chrome.quickUnlockPrivate.setModes with the account |
| 56 * password already supplied. If this is null, the authentication screen |
| 57 * needs to be redisplayed. This property will be cleared after |
| 58 * PASSWORD_ACTIVE_DURATION_MS milliseconds. |
| 59 */ |
| 60 setModes: { |
| 61 type: Object, |
| 62 notify: true |
| 63 }, |
| 64 |
| 65 /** |
| 66 * Name of the profile. |
| 67 */ |
| 68 profileName: String, |
| 69 |
| 70 /** |
| 71 * The actual value of the password field. This is cleared whenever the |
| 72 * authentication screen is not displayed so that the user's password is not |
| 73 * easily available to an attacker. The actual password is stored as an |
| 74 * captured closure variable inside of setModes. |
| 75 * @private |
| 76 */ |
| 77 password_: String, |
| 78 |
| 79 /** |
| 80 * Helper property which marks password as valid/invalid. |
| 81 * @private |
| 82 */ |
| 83 passwordInvalid_: Boolean |
| 84 }, |
| 85 |
| 86 observers: [ |
| 87 'onRouteChanged_(currentRoute)' |
| 88 ], |
| 89 |
| 90 /** @private */ |
| 91 onRouteChanged_: function(currentRoute) { |
| 92 // Clear local state if this screen is not active so if this screen shows |
| 93 // up again the user will get a fresh UI. |
| 94 if (!this.isScreenActive(QuickUnlockScreen.AUTHENTICATE)) { |
| 95 this.password_ = ''; |
| 96 this.passwordInvalid_ = false; |
| 97 } |
| 98 }, |
| 99 |
| 100 /** |
| 101 * Start or restart a timer to check the account password and move past the |
| 102 * authentication screen. |
| 103 * @private |
| 104 */ |
| 105 startDelayedPasswordCheck_: function() { |
| 106 clearTimeout(this.delayedPasswordCheckTimeout_); |
| 107 this.delayedPasswordCheckTimeout_ = |
| 108 setTimeout(this.checkPasswordNow_.bind(this), AUTOSUBMIT_DELAY_MS); |
| 109 }, |
| 110 |
| 111 /** |
| 112 * Run the account password check right now. This will cancel any delayed |
| 113 * check. |
| 114 * @private |
| 115 */ |
| 116 checkPasswordNow_: function() { |
| 117 clearTimeout(this.delayedPasswordCheckTimeout_); |
| 118 clearTimeout(this.clearAccountPasswordTimeout_); |
| 119 |
| 120 // The user might have started entering a password and then deleted it all. |
| 121 // Do not submit/show an error in this case. |
| 122 if (!this.password_) { |
| 123 this.passwordInvalid_ = false; |
| 124 return; |
| 125 } |
| 126 |
| 127 function onPasswordChecked(valid) { |
| 128 // The password might have been cleared during the duration of the |
| 129 // getActiveModes call. |
| 130 this.passwordInvalid_ = !valid && !!this.password_; |
| 131 |
| 132 if (valid) { |
| 133 // Create the |this.setModes| closure and automatically clear it after |
| 134 // |PASSWORD_ACTIVE_DURATION_MS|. |
| 135 var password = this.password_; |
| 136 this.password_ = ''; |
| 137 |
| 138 this.setModes = function(modes, credentials, onComplete) { |
| 139 chrome.quickUnlockPrivate.setModes( |
| 140 password, modes, credentials, onComplete); |
| 141 }; |
| 142 |
| 143 function clearSetModes() { |
| 144 // Reset the password so that any cached references to this.setModes |
| 145 // will fail. |
| 146 password = ''; |
| 147 this.setModes = null; |
| 148 } |
| 149 |
| 150 this.clearAccountPasswordTimeout_ = setTimeout( |
| 151 clearSetModes.bind(this), PASSWORD_ACTIVE_DURATION_MS); |
| 152 |
| 153 this.currentRoute = { |
| 154 page: 'basic', |
| 155 section: 'people', |
| 156 subpage: [QuickUnlockScreen.CHOOSE_METHOD] |
| 157 }; |
| 158 } |
| 159 } |
| 160 |
| 161 checkAccountPassword_(this.password_, onPasswordChecked.bind(this)); |
| 162 } |
| 163 }); |
| 164 |
| 165 })(); |
| OLD | NEW |