| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 | 6 * @fileoverview |
| 7 * Script to be injected into SAML provider pages, serving three main purposes: | 7 * Script to be injected into SAML provider pages, serving three main purposes: |
| 8 * 1. Signal hosting extension that an external page is loaded so that the | 8 * 1. Signal hosting extension that an external page is loaded so that the |
| 9 * UI around it should be changed accordingly; | 9 * UI around it should be changed accordingly; |
| 10 * 2. Provide an API via which the SAML provider can pass user credentials to | 10 * 2. Provide an API via which the SAML provider can pass user credentials to |
| 11 * Chrome OS, allowing the password to be used for encrypting user data and | 11 * Chrome OS, allowing the password to be used for encrypting user data and |
| 12 * offline login. | 12 * offline login. |
| 13 * 3. Scrape password fields, making the password available to Chrome OS even if | 13 * 3. Scrape password fields, making the password available to Chrome OS even if |
| 14 * the SAML provider does not support the credential passing API. | 14 * the SAML provider does not support the credential passing API. |
| 15 */ | 15 */ |
| 16 | 16 |
| 17 (function() { | 17 (function() { |
| 18 function APICallForwarder() { | 18 function APICallForwarder() {} |
| 19 } | |
| 20 | 19 |
| 21 /** | 20 /** |
| 22 * The credential passing API is used by sending messages to the SAML page's | 21 * The credential passing API is used by sending messages to the SAML page's |
| 23 * |window| object. This class forwards API calls from the SAML page to a | 22 * |window| object. This class forwards API calls from the SAML page to a |
| 24 * background script and API responses from the background script to the SAML | 23 * background script and API responses from the background script to the SAML |
| 25 * page. Communication with the background script occurs via a |Channel|. | 24 * page. Communication with the background script occurs via a |Channel|. |
| 26 */ | 25 */ |
| 27 APICallForwarder.prototype = { | 26 APICallForwarder.prototype = { |
| 28 // Channel to which API calls are forwarded. | 27 // Channel to which API calls are forwarded. |
| 29 channel_: null, | 28 channel_: null, |
| 30 | 29 |
| 31 /** | 30 /** |
| 32 * Initialize the API call forwarder. | 31 * Initialize the API call forwarder. |
| 33 * @param {!Object} channel Channel to which API calls should be forwarded. | 32 * @param {!Object} channel Channel to which API calls should be forwarded. |
| 34 */ | 33 */ |
| 35 init: function(channel) { | 34 init: function(channel) { |
| 36 this.channel_ = channel; | 35 this.channel_ = channel; |
| 37 this.channel_.registerMessage('apiResponse', | 36 this.channel_.registerMessage( |
| 38 this.onAPIResponse_.bind(this)); | 37 'apiResponse', this.onAPIResponse_.bind(this)); |
| 39 | 38 |
| 40 window.addEventListener('message', this.onMessage_.bind(this)); | 39 window.addEventListener('message', this.onMessage_.bind(this)); |
| 41 }, | 40 }, |
| 42 | 41 |
| 43 onMessage_: function(event) { | 42 onMessage_: function(event) { |
| 44 if (event.source != window || | 43 if (event.source != window || typeof event.data != 'object' || |
| 45 typeof event.data != 'object' || | |
| 46 !event.data.hasOwnProperty('type') || | 44 !event.data.hasOwnProperty('type') || |
| 47 event.data.type != 'gaia_saml_api') { | 45 event.data.type != 'gaia_saml_api') { |
| 48 return; | 46 return; |
| 49 } | 47 } |
| 50 // Forward API calls to the background script. | 48 // Forward API calls to the background script. |
| 51 this.channel_.send({name: 'apiCall', call: event.data.call}); | 49 this.channel_.send({name: 'apiCall', call: event.data.call}); |
| 52 }, | 50 }, |
| 53 | 51 |
| 54 onAPIResponse_: function(msg) { | 52 onAPIResponse_: function(msg) { |
| 55 // Forward API responses to the SAML page. | 53 // Forward API responses to the SAML page. |
| 56 window.postMessage({type: 'gaia_saml_api_reply', response: msg.response}, | 54 window.postMessage( |
| 57 '/'); | 55 {type: 'gaia_saml_api_reply', response: msg.response}, '/'); |
| 58 } | 56 } |
| 59 }; | 57 }; |
| 60 | 58 |
| 61 /** | 59 /** |
| 62 * A class to scrape password from type=password input elements under a given | 60 * A class to scrape password from type=password input elements under a given |
| 63 * docRoot and send them back via a Channel. | 61 * docRoot and send them back via a Channel. |
| 64 */ | 62 */ |
| 65 function PasswordInputScraper() { | 63 function PasswordInputScraper() {} |
| 66 } | |
| 67 | 64 |
| 68 PasswordInputScraper.prototype = { | 65 PasswordInputScraper.prototype = { |
| 69 // URL of the page. | 66 // URL of the page. |
| 70 pageURL_: null, | 67 pageURL_: null, |
| 71 | 68 |
| 72 // Channel to send back changed password. | 69 // Channel to send back changed password. |
| 73 channel_: null, | 70 channel_: null, |
| 74 | 71 |
| 75 // An array to hold password fields. | 72 // An array to hold password fields. |
| 76 passwordFields_: null, | 73 passwordFields_: null, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 95 this.channel_ = channel; | 92 this.channel_ = channel; |
| 96 | 93 |
| 97 this.passwordFields_ = []; | 94 this.passwordFields_ = []; |
| 98 this.passwordValues_ = []; | 95 this.passwordValues_ = []; |
| 99 | 96 |
| 100 this.findAndTrackChildren(docRoot); | 97 this.findAndTrackChildren(docRoot); |
| 101 | 98 |
| 102 this.passwordFieldsObserver = new MutationObserver(function(mutations) { | 99 this.passwordFieldsObserver = new MutationObserver(function(mutations) { |
| 103 mutations.forEach(function(mutation) { | 100 mutations.forEach(function(mutation) { |
| 104 Array.prototype.forEach.call( | 101 Array.prototype.forEach.call( |
| 105 mutation.addedNodes, | 102 mutation.addedNodes, function(addedNode) { |
| 106 function(addedNode) { | 103 if (addedNode.nodeType != Node.ELEMENT_NODE) |
| 107 if (addedNode.nodeType != Node.ELEMENT_NODE) | 104 return; |
| 108 return; | |
| 109 | 105 |
| 110 if (addedNode.matches('input[type=password]')) { | 106 if (addedNode.matches('input[type=password]')) { |
| 111 this.trackPasswordField(addedNode); | 107 this.trackPasswordField(addedNode); |
| 112 } else { | 108 } else { |
| 113 this.findAndTrackChildren(addedNode); | 109 this.findAndTrackChildren(addedNode); |
| 114 } | 110 } |
| 115 }.bind(this)); | 111 }.bind(this)); |
| 116 }.bind(this)); | 112 }.bind(this)); |
| 117 }.bind(this)); | 113 }.bind(this)); |
| 118 this.passwordFieldsObserver.observe(docRoot, | 114 this.passwordFieldsObserver.observe( |
| 119 {subtree: true, childList: true}); | 115 docRoot, {subtree: true, childList: true}); |
| 120 }, | 116 }, |
| 121 | 117 |
| 122 /** | 118 /** |
| 123 * Find and track password fields that are descendants of the given element. | 119 * Find and track password fields that are descendants of the given element. |
| 124 * @param {!HTMLElement} element The parent element to search from. | 120 * @param {!HTMLElement} element The parent element to search from. |
| 125 */ | 121 */ |
| 126 findAndTrackChildren: function(element) { | 122 findAndTrackChildren: function(element) { |
| 127 Array.prototype.forEach.call( | 123 Array.prototype.forEach.call( |
| 128 element.querySelectorAll('input[type=password]'), function(field) { | 124 element.querySelectorAll('input[type=password]'), function(field) { |
| 129 this.trackPasswordField(field); | 125 this.trackPasswordField(field); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 157 maybeSendUpdatedPassword: function(index, fieldId) { | 153 maybeSendUpdatedPassword: function(index, fieldId) { |
| 158 var newValue = this.passwordFields_[index].value; | 154 var newValue = this.passwordFields_[index].value; |
| 159 if (newValue == this.passwordValues_[index]) | 155 if (newValue == this.passwordValues_[index]) |
| 160 return; | 156 return; |
| 161 | 157 |
| 162 this.passwordValues_[index] = newValue; | 158 this.passwordValues_[index] = newValue; |
| 163 | 159 |
| 164 // Use an invalid char for URL as delimiter to concatenate page url, | 160 // Use an invalid char for URL as delimiter to concatenate page url, |
| 165 // password field index and id to construct a unique ID for the password | 161 // password field index and id to construct a unique ID for the password |
| 166 // field. | 162 // field. |
| 167 var passwordId = this.pageURL_.split('#')[0].split('?')[0] + | 163 var passwordId = this.pageURL_.split('#')[0].split('?')[0] + '|' + index + |
| 168 '|' + index + '|' + fieldId; | 164 '|' + fieldId; |
| 169 this.channel_.send({ | 165 this.channel_.send( |
| 170 name: 'updatePassword', | 166 {name: 'updatePassword', id: passwordId, password: newValue}); |
| 171 id: passwordId, | |
| 172 password: newValue | |
| 173 }); | |
| 174 }, | 167 }, |
| 175 | 168 |
| 176 /** | 169 /** |
| 177 * Handles 'change' event in the scraped password fields. | 170 * Handles 'change' event in the scraped password fields. |
| 178 * @param {number} index The index of the password fields in | 171 * @param {number} index The index of the password fields in |
| 179 * |passwordFields_|. | 172 * |passwordFields_|. |
| 180 * @param {string} fieldId The id or name of the password field or blank. | 173 * @param {string} fieldId The id or name of the password field or blank. |
| 181 */ | 174 */ |
| 182 onPasswordChanged_: function(index, fieldId) { | 175 onPasswordChanged_: function(index, fieldId) { |
| 183 this.maybeSendUpdatedPassword(index, fieldId); | 176 this.maybeSendUpdatedPassword(index, fieldId); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 203 initPasswordScraper(); | 196 initPasswordScraper(); |
| 204 window.removeEventListener(event.type, listener, true); | 197 window.removeEventListener(event.type, listener, true); |
| 205 }, true); | 198 }, true); |
| 206 } else { | 199 } else { |
| 207 initPasswordScraper(); | 200 initPasswordScraper(); |
| 208 } | 201 } |
| 209 } | 202 } |
| 210 | 203 |
| 211 var channel = Channel.create(); | 204 var channel = Channel.create(); |
| 212 channel.connect('injected'); | 205 channel.connect('injected'); |
| 213 channel.sendWithCallback({name: 'getSAMLFlag'}, | 206 channel.sendWithCallback( |
| 214 onGetSAMLFlag.bind(undefined, channel)); | 207 {name: 'getSAMLFlag'}, onGetSAMLFlag.bind(undefined, channel)); |
| 215 | 208 |
| 216 var apiCallForwarder = new APICallForwarder(); | 209 var apiCallForwarder = new APICallForwarder(); |
| 217 apiCallForwarder.init(channel); | 210 apiCallForwarder.init(channel); |
| 218 })(); | 211 })(); |
| OLD | NEW |