| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Handles web page requests for gnubby enrollment. | 6 * @fileoverview Handles web page requests for gnubby enrollment. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 'use strict'; | 9 'use strict'; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * Handles an enroll request. | 12 * Handles an enroll request. |
| 13 * @param {!EnrollHelperFactory} factory Factory to create an enroll helper. | 13 * @param {!EnrollHelperFactory} factory Factory to create an enroll helper. |
| 14 * @param {MessageSender} sender The sender of the message. | 14 * @param {MessageSender} sender The sender of the message. |
| 15 * @param {Object} request The web page's enroll request. | 15 * @param {Object} request The web page's enroll request. |
| 16 * @param {boolean} enforceAppIdValid Whether to enforce that the appId in the | |
| 17 * request matches the sender's origin. | |
| 18 * @param {Function} sendResponse Called back with the result of the enroll. | 16 * @param {Function} sendResponse Called back with the result of the enroll. |
| 19 * @param {boolean} toleratesMultipleResponses Whether the sendResponse | 17 * @param {boolean} toleratesMultipleResponses Whether the sendResponse |
| 20 * callback can be called more than once, e.g. for progress updates. | 18 * callback can be called more than once, e.g. for progress updates. |
| 21 * @return {Closeable} A handler object to be closed when the browser channel | 19 * @return {Closeable} A handler object to be closed when the browser channel |
| 22 * closes. | 20 * closes. |
| 23 */ | 21 */ |
| 24 function handleEnrollRequest(factory, sender, request, enforceAppIdValid, | 22 function handleEnrollRequest(factory, sender, request, sendResponse, |
| 25 sendResponse, toleratesMultipleResponses) { | 23 toleratesMultipleResponses) { |
| 26 var sentResponse = false; | 24 var sentResponse = false; |
| 27 function sendResponseOnce(r) { | 25 function sendResponseOnce(r) { |
| 28 if (enroller) { | 26 if (enroller) { |
| 29 enroller.close(); | 27 enroller.close(); |
| 30 enroller = null; | 28 enroller = null; |
| 31 } | 29 } |
| 32 if (!sentResponse) { | 30 if (!sentResponse) { |
| 33 sentResponse = true; | 31 sentResponse = true; |
| 34 try { | 32 try { |
| 35 // If the page has gone away or the connection has otherwise gone, | 33 // If the page has gone away or the connection has otherwise gone, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 if (request['requestId']) { | 110 if (request['requestId']) { |
| 113 response['requestId'] = request['requestId']; | 111 response['requestId'] = request['requestId']; |
| 114 } | 112 } |
| 115 sendResponse(response); | 113 sendResponse(response); |
| 116 } | 114 } |
| 117 } | 115 } |
| 118 | 116 |
| 119 var timer = new CountdownTimer(timeoutMillis); | 117 var timer = new CountdownTimer(timeoutMillis); |
| 120 var enroller = new Enroller(factory, timer, origin, sendErrorResponse, | 118 var enroller = new Enroller(factory, timer, origin, sendErrorResponse, |
| 121 sendSuccessResponse, sendNotification, sender.tlsChannelId, logMsgUrl); | 119 sendSuccessResponse, sendNotification, sender.tlsChannelId, logMsgUrl); |
| 122 enroller.doEnroll(enrollChallenges, signData, enforceAppIdValid); | 120 enroller.doEnroll(enrollChallenges, signData); |
| 123 return /** @type {Closeable} */ (enroller); | 121 return /** @type {Closeable} */ (enroller); |
| 124 } | 122 } |
| 125 | 123 |
| 126 /** | 124 /** |
| 127 * Returns whether the request appears to be a valid enroll request. | 125 * Returns whether the request appears to be a valid enroll request. |
| 128 * @param {Object} request the request. | 126 * @param {Object} request the request. |
| 129 * @return {boolean} whether the request appears valid. | 127 * @return {boolean} whether the request appears valid. |
| 130 */ | 128 */ |
| 131 function isValidEnrollRequest(request) { | 129 function isValidEnrollRequest(request) { |
| 132 if (!request.hasOwnProperty('enrollChallenges')) | 130 if (!request.hasOwnProperty('enrollChallenges')) |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 /** | 223 /** |
| 226 * Default timeout value in case the caller never provides a valid timeout. | 224 * Default timeout value in case the caller never provides a valid timeout. |
| 227 */ | 225 */ |
| 228 Enroller.DEFAULT_TIMEOUT_MILLIS = 30 * 1000; | 226 Enroller.DEFAULT_TIMEOUT_MILLIS = 30 * 1000; |
| 229 | 227 |
| 230 /** | 228 /** |
| 231 * Performs an enroll request with the given enroll and sign challenges. | 229 * Performs an enroll request with the given enroll and sign challenges. |
| 232 * @param {Array.<Object>} enrollChallenges A set of enroll challenges | 230 * @param {Array.<Object>} enrollChallenges A set of enroll challenges |
| 233 * @param {Array.<Object>} signChallenges A set of sign challenges for existing | 231 * @param {Array.<Object>} signChallenges A set of sign challenges for existing |
| 234 * enrollments for this user and appId | 232 * enrollments for this user and appId |
| 235 * @param {boolean} enforceAppIdValid Whether to enforce that appId is valid | |
| 236 */ | 233 */ |
| 237 Enroller.prototype.doEnroll = | 234 Enroller.prototype.doEnroll = function(enrollChallenges, signChallenges) { |
| 238 function(enrollChallenges, signChallenges, enforceAppIdValid) { | |
| 239 this.setEnrollChallenges_(enrollChallenges); | 235 this.setEnrollChallenges_(enrollChallenges); |
| 240 this.setSignChallenges_(signChallenges); | 236 this.setSignChallenges_(signChallenges); |
| 241 | 237 |
| 242 if (!enforceAppIdValid) { | 238 // Begin fetching/checking the app ids. |
| 243 // If not enforcing app id validity, begin enrolling right away. | |
| 244 this.helper_.doEnroll(this.encodedEnrollChallenges_, | |
| 245 this.encodedSignChallenges_); | |
| 246 } | |
| 247 // Whether or not enforcing app id validity, begin fetching/checking the | |
| 248 // app ids. | |
| 249 var enrollAppIds = []; | 239 var enrollAppIds = []; |
| 250 for (var i = 0; i < enrollChallenges.length; i++) { | 240 for (var i = 0; i < enrollChallenges.length; i++) { |
| 251 enrollAppIds.push(enrollChallenges[i]['appId']); | 241 enrollAppIds.push(enrollChallenges[i]['appId']); |
| 252 } | 242 } |
| 253 var self = this; | 243 var self = this; |
| 254 this.checkAppIds_(enrollAppIds, signChallenges, function(result) { | 244 this.checkAppIds_(enrollAppIds, signChallenges, function(result) { |
| 255 if (!enforceAppIdValid) { | |
| 256 // Nothing to do, move along. | |
| 257 return; | |
| 258 } | |
| 259 if (result) { | 245 if (result) { |
| 260 self.helper_.doEnroll(self.encodedEnrollChallenges_, | 246 self.helper_.doEnroll(self.encodedEnrollChallenges_, |
| 261 self.encodedSignChallenges_); | 247 self.encodedSignChallenges_); |
| 262 } else { | 248 } else { |
| 263 self.notifyError_(GnubbyCodeTypes.BAD_APP_ID); | 249 self.notifyError_(GnubbyCodeTypes.BAD_APP_ID); |
| 264 } | 250 } |
| 265 }); | 251 }); |
| 266 }; | 252 }; |
| 267 | 253 |
| 268 /** | 254 /** |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 * @param {number} code Status code | 548 * @param {number} code Status code |
| 563 * @param {boolean} anyGnubbies If any gnubbies were found | 549 * @param {boolean} anyGnubbies If any gnubbies were found |
| 564 * @private | 550 * @private |
| 565 */ | 551 */ |
| 566 Enroller.prototype.helperProgress_ = function(code, anyGnubbies) { | 552 Enroller.prototype.helperProgress_ = function(code, anyGnubbies) { |
| 567 var reportedError = Enroller.mapError_(code, anyGnubbies); | 553 var reportedError = Enroller.mapError_(code, anyGnubbies); |
| 568 console.log(UTIL_fmt('helper notified ' + code.toString(16) + | 554 console.log(UTIL_fmt('helper notified ' + code.toString(16) + |
| 569 ', returning ' + reportedError)); | 555 ', returning ' + reportedError)); |
| 570 this.notifyProgress_(reportedError); | 556 this.notifyProgress_(reportedError); |
| 571 }; | 557 }; |
| OLD | NEW |