1 // Copyright (c) 2012 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.
5 login.createScreen('OAuthEnrollmentScreen', 'oauth-enrollment', function() {
6 /** @const */ var STEP_SIGNIN = 'signin';
7 /** @const */ var STEP_WORKING = 'working';
8 /** @const */ var STEP_ERROR = 'error';
9 /** @const */ var STEP_EXPLAIN = 'explain';
10 /** @const */ var STEP_SUCCESS = 'success';
14 'setIsAutoEnrollment',
18 'setAuthenticatedUserEmail',
22 * URL to load in the sign in frame.
27 * Whether this is a manual or auto enrollment.
29 isAutoEnrollment_: false,
32 * True if enrollment cancellation should be prevented.
34 preventCancellation_: false,
37 * Enrollment steps with names and buttons to show.
42 * Dialog to confirm that auto-enrollment should really be cancelled.
43 * This is only created the first time it's used.
48 * The current step. This is the last value passed to showStep().
53 * Opaque token used to correlate request and response while retrieving the
54 * authenticated user's e-mail address from GAIA.
59 decorate: function() {
60 window.addEventListener('message',
61 this.onMessage_.bind(this), false);
62 $('oauth-enroll-error-retry').addEventListener('click',
63 this.doRetry_.bind(this));
64 var links = document.querySelectorAll('.oauth-enroll-explain-link');
65 for (var i = 0; i < links.length; i++) {
66 links[i].addEventListener('click',
67 this.showStep.bind(this, STEP_EXPLAIN));
72 * Header text of the screen.
76 return loadTimeData.getString('oauthEnrollScreenTitle');
80 * Buttons in oobe wizard's button strip.
81 * @type {array} Array of Buttons.
86 var cancelButton = this.ownerDocument.createElement('button');
87 cancelButton.id = 'oauth-enroll-cancel-button';
88 cancelButton.textContent = loadTimeData.getString('oauthEnrollCancel');
90 cancelButton.addEventListener('click', function(e) {
91 chrome.send('oauthEnrollClose', ['cancel']);
93 buttons.push(cancelButton);
95 var tryAgainButton = this.ownerDocument.createElement('button');
96 tryAgainButton.id = 'oauth-enroll-try-again-button';
97 tryAgainButton.hidden = true;
98 tryAgainButton.textContent =
99 loadTimeData.getString('oauthEnrollRetry');
100 tryAgainButton.addEventListener('click', this.doRetry_.bind(this));
101 buttons.push(tryAgainButton);
103 var explainButton = this.ownerDocument.createElement('button');
104 explainButton.id = 'oauth-enroll-explain-button';
105 explainButton.hidden = true;
106 explainButton.textContent =
107 loadTimeData.getString('oauthEnrollExplainButton');
108 explainButton.addEventListener('click', this.doRetry_.bind(this));
109 buttons.push(explainButton);
111 var doneButton = this.ownerDocument.createElement('button');
112 doneButton.id = 'oauth-enroll-done-button';
113 doneButton.hidden = true;
114 doneButton.textContent =
115 loadTimeData.getString('oauthEnrollDone');
116 doneButton.addEventListener('click', function(e) {
117 chrome.send('oauthEnrollClose', ['done']);
119 buttons.push(doneButton);
125 * Sets the |isAutoEnrollment| flag of the OAuthEnrollmentScreen class and
127 * @param {boolean} is_auto_enrollment the new value of the flag.
129 setIsAutoEnrollment: function(is_auto_enrollment) {
130 this.isAutoEnrollment_ = is_auto_enrollment;
131 // The cancel button is not available during auto-enrollment.
132 var cancel = this.isAutoEnrollment_ ? null : 'cancel';
133 // During auto-enrollment the user must try again from the error screen.
134 var errorCancel = this.isAutoEnrollment_ ? 'try-again' : 'cancel';
147 focusButton: this.isAutoEnrollment_
161 var links = document.querySelectorAll('.oauth-enroll-explain-link');
162 for (var i = 0; i < links.length; i++) {
163 links[i].hidden = !this.isAutoEnrollment_;
168 * Event handler that is invoked just before the frame is shown.
169 * @param {Object} data Screen init payload, contains the signin frame
172 onBeforeShow: function(data) {
173 var url = data.signin_url;
174 url += '?gaiaUrl=' + encodeURIComponent(data.gaiaUrl);
175 this.signInUrl_ = url;
176 this.setIsAutoEnrollment(data.is_auto_enrollment);
177 this.preventCancellation_ = data.prevent_cancellation;
178 $('oauth-enroll-signin-frame').contentWindow.location.href =
180 if (this.preventCancellation_) {
181 $('oauth-enroll-cancel-button').textContent =
182 loadTimeData.getString('oauthEnrollCancelAutoEnrollmentGoBack');
184 this.classList.toggle('saml', false);
186 this.showStep(STEP_SIGNIN);
190 * Cancels enrollment and drops the user back to the login screen.
193 if (this.isAutoEnrollment_)
196 chrome.send('oauthEnrollClose', ['cancel']);
200 * Switches between the different steps in the enrollment flow.
201 * @param {string} step the steps to show, one of "signin", "working",
202 * "error", "success".
204 showStep: function(step) {
205 this.currentStep_ = step;
206 $('oauth-enroll-cancel-button').hidden = true;
207 $('oauth-enroll-try-again-button').hidden = true;
208 $('oauth-enroll-explain-button').hidden = true;
209 $('oauth-enroll-done-button').hidden = true;
210 for (var i = 0; i < this.steps_.length; i++) {
211 var theStep = this.steps_[i];
212 var active = (theStep.name == step);
213 $('oauth-enroll-step-' + theStep.name).hidden = !active;
214 if (active && theStep.button) {
215 var button = $('oauth-enroll-' + theStep.button + '-button');
216 button.hidden = false;
217 if (theStep.focusButton)
224 * Sets an error message and switches to the error screen.
225 * @param {string} message the error message.
226 * @param {boolean} retry whether the retry link should be shown.
228 showError: function(message, retry) {
229 $('oauth-enroll-error-message').textContent = message;
230 $('oauth-enroll-error-retry').hidden = !retry || this.isAutoEnrollment_;
231 this.showStep(STEP_ERROR);
235 * Sets a progressing message and switches to the working screen.
236 * @param {string} message the progress message.
238 showWorking: function(message) {
239 $('oauth-enroll-working-message').textContent = message;
240 this.showStep(STEP_WORKING);
244 * Invoked when the authenticated user's e-mail address has been retrieved.
245 * This completes SAML authentication.
246 * @param {number} attemptToken An opaque token used to correlate this
247 * method invocation with the corresponding request to retrieve the
248 * user's e-mail address.
249 * @param {string} email The authenticated user's e-mail address.
251 setAuthenticatedUserEmail: function(attemptToken, email) {
252 if (this.attemptToken_ == attemptToken)
253 chrome.send('oauthEnrollCompleteLogin', [email]);
257 * Handler for cancellations of an enforced auto-enrollment.
259 cancelAutoEnrollment: function() {
260 // Check if this is forced enrollment flow for a kiosk app.
261 if (this.preventCancellation_)
264 // The dialog to confirm cancellation of auto-enrollment is only shown
265 // if this is an auto-enrollment, and if the user is currently in the
267 if (!this.isAutoEnrollment_ || this.currentStep_ !== STEP_EXPLAIN)
269 if (!this.confirmDialog_) {
270 this.confirmDialog_ = new cr.ui.dialogs.ConfirmDialog(document.body);
271 this.confirmDialog_.setOkLabel(
272 loadTimeData.getString('oauthEnrollCancelAutoEnrollmentConfirm'));
273 this.confirmDialog_.setCancelLabel(
274 loadTimeData.getString('oauthEnrollCancelAutoEnrollmentGoBack'));
275 this.confirmDialog_.setInitialFocusOnCancel();
277 this.confirmDialog_.show(
278 loadTimeData.getString('oauthEnrollCancelAutoEnrollmentReally'),
279 this.onConfirmCancelAutoEnrollment_.bind(this));
283 * Retries the enrollment process after an error occurred in a previous
284 * attempt. This goes to the C++ side through |chrome| first to clean up the
285 * profile, so that the next attempt is performed with a clean state.
287 doRetry_: function() {
288 chrome.send('oauthEnrollRetry');
292 * Handler for confirmation of cancellation of auto-enrollment.
294 onConfirmCancelAutoEnrollment_: function() {
295 chrome.send('oauthEnrollClose', ['autocancel']);
299 * Checks if a given HTML5 message comes from the URL loaded into the signin
301 * @param {Object} m HTML5 message.
302 * @type {boolean} whether the message comes from the signin frame.
304 isSigninMessage_: function(m) {
305 return this.signInUrl_ != null &&
306 this.signInUrl_.indexOf(m.origin) == 0 &&
307 m.source == $('oauth-enroll-signin-frame').contentWindow;
311 * Event handler for HTML5 messages.
312 * @param {Object} m HTML5 message.
314 onMessage_: function(m) {
315 if (!this.isSigninMessage_(m))
320 if (msg.method == 'completeLogin') {
321 // A user has successfully authenticated via regular GAIA.
322 chrome.send('oauthEnrollCompleteLogin', [msg.email]);
325 if (msg.method == 'retrieveAuthenticatedUserEmail') {
326 // A user has successfully authenticated via SAML. However, the user's
327 // identity is not known. Instead of reporting success immediately,
328 // retrieve the user's e-mail address first.
329 this.attemptToken_ = msg.attemptToken;
330 this.showWorking(null);
331 chrome.send('oauthEnrollRetrieveAuthenticatedUserEmail',
335 if (msg.method == 'authPageLoaded') {
337 $('oauth-saml-notice-message').textContent = loadTimeData.getStringF(
341 this.classList.toggle('saml', msg.isSAML);