summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/settings/controls/password_prompt_dialog.js
blob: 574dd209c16467b184290412ec8935fb8056d91f (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview
 *
 * 'settings-password-prompt-dialog' shows a dialog which asks for the user to
 * enter their password. It validates the password is correct. Once the user has
 * entered their account password, the page fires an 'authenticated' event and
 * updates the authToken binding.
 *
 * Example:
 *
 * <settings-password-prompt-dialog
 *   id="passwordPrompt"
 *   password-prompt-text="{{passwordPromptText}}"
 *   auth-token="{{authToken}}">
 * </settings-password-prompt-dialog>
 */

(function() {
'use strict';

Polymer({
  is: 'settings-password-prompt-dialog',

  properties: {
    /**
     * The subtext to be displayed above the password input field. Embedders
     * may choose to change this value for their specific use case.
     * @type {string}
     */
    passwordPromptText: {
      type: String,
      notify: true,
      value: '',
    },

    /**
     * Authentication token returned by quickUnlockPrivate.getAuthToken().
     * Should be passed to API calls which require authentication.
     * @type {string}
     */
    authToken: {
      type: String,
      notify: true,
    },

    /**
     * @private {string}
     */
    inputValue_: {
      type: String,
      value: '',
      observer: 'onInputValueChange_',
    },

    /**
     * Helper property which marks password as valid/invalid.
     * @private {boolean}
     */
    passwordInvalid_: {
      type: Boolean,
      value: false,
    },

    /**
     * Interface for chrome.quickUnlockPrivate calls. May be overridden by
     * tests.
     * @type {QuickUnlockPrivate}
     */
    quickUnlockPrivate: {type: Object, value: chrome.quickUnlockPrivate},
  },

  /** @return {!CrInputElement} */
  get passwordInput() {
    return this.$.passwordInput;
  },

  /** @override */
  attached: function() {
    this.$.dialog.showModal();
    // This needs to occur at the next paint otherwise the password input will
    // not receive focus.
    this.async(() => {
      // TODO(crbug.com/876377): This is unusual; the 'autofocus' attribute on
      // the cr-input element should work. Investigate.
      this.passwordInput.focus();
    }, 1 /* waitTime */);
  },

  /** @private */
  onCancelTap_: function() {
    if (this.$.dialog.open) {
      this.$.dialog.close();
    }
  },

  /**
   * The timeout ID to pass to clearTimeout() to cancel auth token
   * invalidation.
   * @private {number|undefined}
   */
  clearAccountPasswordTimeoutId_: undefined,

  /**
   * Run the account password check.
   * @private
   */
  submitPassword_: function() {
    clearTimeout(this.clearAccountPasswordTimeoutId_);

    const password = this.passwordInput.value;
    // The user might have started entering a password and then deleted it all.
    // Do not submit/show an error in this case.
    if (!password) {
      this.passwordInvalid_ = false;
      return;
    }

    this.quickUnlockPrivate.getAuthToken(password, (tokenInfo) => {
      if (chrome.runtime.lastError) {
        this.passwordInvalid_ = true;
        // Select the whole password if user entered an incorrect password.
        this.passwordInput.select();
        return;
      }

      this.authToken = tokenInfo.token;
      this.passwordInvalid_ = false;

      // Clear |this.authToken| after tokenInfo.lifetimeSeconds.
      // Subtract time from the expiration time to account for IPC delays.
      // Treat values less than the minimum as 0 for testing.
      const IPC_SECONDS = 2;
      const lifetimeMs = tokenInfo.lifetimeSeconds > IPC_SECONDS ?
          (tokenInfo.lifetimeSeconds - IPC_SECONDS) * 1000 :
          0;
      this.clearAccountPasswordTimeoutId_ = setTimeout(() => {
        this.authToken = '';
      }, lifetimeMs);

      if (this.$.dialog.open) {
        this.$.dialog.close();
      }
    });
  },

  /** @private */
  onInputValueChange_: function() {
    this.passwordInvalid_ = false;
  },

  /** @private */
  isConfirmEnabled_: function() {
    return !this.passwordInvalid_ && this.inputValue_;
  },
});
})();