| 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 } | 19 |
| 20 /** |
| 21 * The credential passing API is used by sending messages to the SAML page's |
| 22 * |window| object. This class forwards API calls from the SAML page to a |
| 23 * background script and API responses from the background script to the SAML |
| 24 * page. Communication with the background script occurs via a |Channel|. |
| 25 */ |
| 26 APICallForwarder.prototype = { |
| 27 // Channel to which API calls are forwarded. |
| 28 channel_: null, |
| 20 | 29 |
| 21 /** | 30 /** |
| 22 * The credential passing API is used by sending messages to the SAML page's | 31 * Initialize the API call forwarder. |
| 23 * |window| object. This class forwards API calls from the SAML page to a | 32 * @param {!Object} channel Channel to which API calls should be forwarded. |
| 24 * background script and API responses from the background script to the SAML | |
| 25 * page. Communication with the background script occurs via a |Channel|. | |
| 26 */ | 33 */ |
| 27 APICallForwarder.prototype = { | 34 init: function(channel) { |
| 28 // Channel to which API calls are forwarded. | 35 this.channel_ = channel; |
| 29 channel_: null, | 36 this.channel_.registerMessage( |
| 37 'apiResponse', this.onAPIResponse_.bind(this)); |
| 30 | 38 |
| 31 /** | 39 window.addEventListener('message', this.onMessage_.bind(this)); |
| 32 * Initialize the API call forwarder. | 40 }, |
| 33 * @param {!Object} channel Channel to which API calls should be forwarded. | |
| 34 */ | |
| 35 init: function(channel) { | |
| 36 this.channel_ = channel; | |
| 37 this.channel_.registerMessage('apiResponse', | |
| 38 this.onAPIResponse_.bind(this)); | |
| 39 | 41 |
| 40 window.addEventListener('message', this.onMessage_.bind(this)); | 42 onMessage_: function(event) { |
| 41 }, | 43 if (event.source != window || typeof event.data != 'object' || |
| 44 !event.data.hasOwnProperty('type') || |
| 45 event.data.type != 'gaia_saml_api') { |
| 46 return; |
| 47 } |
| 48 // Forward API calls to the background script. |
| 49 this.channel_.send({name: 'apiCall', call: event.data.call}); |
| 50 }, |
| 42 | 51 |
| 43 onMessage_: function(event) { | 52 onAPIResponse_: function(msg) { |
| 44 if (event.source != window || | 53 // Forward API responses to the SAML page. |
| 45 typeof event.data != 'object' || | 54 window.postMessage( |
| 46 !event.data.hasOwnProperty('type') || | 55 {type: 'gaia_saml_api_reply', response: msg.response}, '/'); |
| 47 event.data.type != 'gaia_saml_api') { | 56 } |
| 48 return; | 57 }; |
| 49 } | |
| 50 // Forward API calls to the background script. | |
| 51 this.channel_.send({name: 'apiCall', call: event.data.call}); | |
| 52 }, | |
| 53 | 58 |
| 54 onAPIResponse_: function(msg) { | 59 /** |
| 55 // Forward API responses to the SAML page. | 60 * A class to scrape password from type=password input elements under a given |
| 56 window.postMessage({type: 'gaia_saml_api_reply', response: msg.response}, | 61 * docRoot and send them back via a Channel. |
| 57 '/'); | 62 */ |
| 58 } | 63 function PasswordInputScraper() {} |
| 64 |
| 65 PasswordInputScraper.prototype = { |
| 66 // URL of the page. |
| 67 pageURL_: null, |
| 68 |
| 69 // Channel to send back changed password. |
| 70 channel_: null, |
| 71 |
| 72 // An array to hold password fields. |
| 73 passwordFields_: null, |
| 74 |
| 75 // An array to hold cached password values. |
| 76 passwordValues_: null, |
| 77 |
| 78 // A MutationObserver to watch for dynamic password field creation. |
| 79 passwordFieldsObserver: null, |
| 80 |
| 81 /** |
| 82 * Initialize the scraper with given channel and docRoot. Note that the |
| 83 * scanning for password fields happens inside the function and does not |
| 84 * handle DOM tree changes after the call returns. |
| 85 * @param {!Object} channel The channel to send back password. |
| 86 * @param {!string} pageURL URL of the page. |
| 87 * @param {!HTMLElement} docRoot The root element of the DOM tree that |
| 88 * contains the password fields of interest. |
| 89 */ |
| 90 init: function(channel, pageURL, docRoot) { |
| 91 this.pageURL_ = pageURL; |
| 92 this.channel_ = channel; |
| 93 |
| 94 this.passwordFields_ = []; |
| 95 this.passwordValues_ = []; |
| 96 |
| 97 this.findAndTrackChildren(docRoot); |
| 98 |
| 99 this.passwordFieldsObserver = new MutationObserver(function(mutations) { |
| 100 mutations.forEach(function(mutation) { |
| 101 Array.prototype.forEach.call(mutation.addedNodes, function(addedNode) { |
| 102 if (addedNode.nodeType != Node.ELEMENT_NODE) |
| 103 return; |
| 104 |
| 105 if (addedNode.matches('input[type=password]')) { |
| 106 this.trackPasswordField(addedNode); |
| 107 } else { |
| 108 this.findAndTrackChildren(addedNode); |
| 109 } |
| 110 }.bind(this)); |
| 111 }.bind(this)); |
| 112 }.bind(this)); |
| 113 this.passwordFieldsObserver.observe( |
| 114 docRoot, {subtree: true, childList: true}); |
| 115 }, |
| 116 |
| 117 /** |
| 118 * Find and track password fields that are descendants of the given element. |
| 119 * @param {!HTMLElement} element The parent element to search from. |
| 120 */ |
| 121 findAndTrackChildren: function(element) { |
| 122 Array.prototype.forEach.call( |
| 123 element.querySelectorAll('input[type=password]'), function(field) { |
| 124 this.trackPasswordField(field); |
| 125 }.bind(this)); |
| 126 }, |
| 127 |
| 128 /** |
| 129 * Start tracking value changes of the given password field if it is |
| 130 * not being tracked yet. |
| 131 * @param {!HTMLInputElement} passworField The password field to track. |
| 132 */ |
| 133 trackPasswordField: function(passwordField) { |
| 134 var existing = this.passwordFields_.filter(function(element) { |
| 135 return element === passwordField; |
| 136 }); |
| 137 if (existing.length != 0) |
| 138 return; |
| 139 |
| 140 var index = this.passwordFields_.length; |
| 141 var fieldId = passwordField.id || passwordField.name || ''; |
| 142 passwordField.addEventListener( |
| 143 'input', this.onPasswordChanged_.bind(this, index, fieldId)); |
| 144 this.passwordFields_.push(passwordField); |
| 145 this.passwordValues_.push(passwordField.value); |
| 146 }, |
| 147 |
| 148 /** |
| 149 * Check if the password field at |index| has changed. If so, sends back |
| 150 * the updated value. |
| 151 */ |
| 152 maybeSendUpdatedPassword: function(index, fieldId) { |
| 153 var newValue = this.passwordFields_[index].value; |
| 154 if (newValue == this.passwordValues_[index]) |
| 155 return; |
| 156 |
| 157 this.passwordValues_[index] = newValue; |
| 158 |
| 159 // Use an invalid char for URL as delimiter to concatenate page url, |
| 160 // password field index and id to construct a unique ID for the password |
| 161 // field. |
| 162 var passwordId = |
| 163 this.pageURL_.split('#')[0].split('?')[0] + '|' + index + '|' + fieldId; |
| 164 this.channel_.send( |
| 165 {name: 'updatePassword', id: passwordId, password: newValue}); |
| 166 }, |
| 167 |
| 168 /** |
| 169 * Handles 'change' event in the scraped password fields. |
| 170 * @param {number} index The index of the password fields in |
| 171 * |passwordFields_|. |
| 172 * @param {string} fieldId The id or name of the password field or blank. |
| 173 */ |
| 174 onPasswordChanged_: function(index, fieldId) { |
| 175 this.maybeSendUpdatedPassword(index, fieldId); |
| 176 } |
| 177 }; |
| 178 |
| 179 function onGetSAMLFlag(channel, isSAMLPage) { |
| 180 if (!isSAMLPage) |
| 181 return; |
| 182 var pageURL = window.location.href; |
| 183 |
| 184 channel.send({name: 'pageLoaded', url: pageURL}); |
| 185 |
| 186 var initPasswordScraper = function() { |
| 187 var passwordScraper = new PasswordInputScraper(); |
| 188 passwordScraper.init(channel, pageURL, document.documentElement); |
| 59 }; | 189 }; |
| 60 | 190 |
| 61 /** | 191 if (document.readyState == 'loading') { |
| 62 * A class to scrape password from type=password input elements under a given | 192 window.addEventListener('readystatechange', function listener(event) { |
| 63 * docRoot and send them back via a Channel. | 193 if (document.readyState == 'loading') |
| 64 */ | 194 return; |
| 65 function PasswordInputScraper() { | 195 initPasswordScraper(); |
| 196 window.removeEventListener(event.type, listener, true); |
| 197 }, true); |
| 198 } else { |
| 199 initPasswordScraper(); |
| 66 } | 200 } |
| 201 } |
| 67 | 202 |
| 68 PasswordInputScraper.prototype = { | 203 var channel = Channel.create(); |
| 69 // URL of the page. | 204 channel.connect('injected'); |
| 70 pageURL_: null, | 205 channel.sendWithCallback( |
| 206 {name: 'getSAMLFlag'}, onGetSAMLFlag.bind(undefined, channel)); |
| 71 | 207 |
| 72 // Channel to send back changed password. | 208 var apiCallForwarder = new APICallForwarder(); |
| 73 channel_: null, | 209 apiCallForwarder.init(channel); |
| 74 | |
| 75 // An array to hold password fields. | |
| 76 passwordFields_: null, | |
| 77 | |
| 78 // An array to hold cached password values. | |
| 79 passwordValues_: null, | |
| 80 | |
| 81 // A MutationObserver to watch for dynamic password field creation. | |
| 82 passwordFieldsObserver: null, | |
| 83 | |
| 84 /** | |
| 85 * Initialize the scraper with given channel and docRoot. Note that the | |
| 86 * scanning for password fields happens inside the function and does not | |
| 87 * handle DOM tree changes after the call returns. | |
| 88 * @param {!Object} channel The channel to send back password. | |
| 89 * @param {!string} pageURL URL of the page. | |
| 90 * @param {!HTMLElement} docRoot The root element of the DOM tree that | |
| 91 * contains the password fields of interest. | |
| 92 */ | |
| 93 init: function(channel, pageURL, docRoot) { | |
| 94 this.pageURL_ = pageURL; | |
| 95 this.channel_ = channel; | |
| 96 | |
| 97 this.passwordFields_ = []; | |
| 98 this.passwordValues_ = []; | |
| 99 | |
| 100 this.findAndTrackChildren(docRoot); | |
| 101 | |
| 102 this.passwordFieldsObserver = new MutationObserver(function(mutations) { | |
| 103 mutations.forEach(function(mutation) { | |
| 104 Array.prototype.forEach.call( | |
| 105 mutation.addedNodes, | |
| 106 function(addedNode) { | |
| 107 if (addedNode.nodeType != Node.ELEMENT_NODE) | |
| 108 return; | |
| 109 | |
| 110 if (addedNode.matches('input[type=password]')) { | |
| 111 this.trackPasswordField(addedNode); | |
| 112 } else { | |
| 113 this.findAndTrackChildren(addedNode); | |
| 114 } | |
| 115 }.bind(this)); | |
| 116 }.bind(this)); | |
| 117 }.bind(this)); | |
| 118 this.passwordFieldsObserver.observe(docRoot, | |
| 119 {subtree: true, childList: true}); | |
| 120 }, | |
| 121 | |
| 122 /** | |
| 123 * Find and track password fields that are descendants of the given element. | |
| 124 * @param {!HTMLElement} element The parent element to search from. | |
| 125 */ | |
| 126 findAndTrackChildren: function(element) { | |
| 127 Array.prototype.forEach.call( | |
| 128 element.querySelectorAll('input[type=password]'), function(field) { | |
| 129 this.trackPasswordField(field); | |
| 130 }.bind(this)); | |
| 131 }, | |
| 132 | |
| 133 /** | |
| 134 * Start tracking value changes of the given password field if it is | |
| 135 * not being tracked yet. | |
| 136 * @param {!HTMLInputElement} passworField The password field to track. | |
| 137 */ | |
| 138 trackPasswordField: function(passwordField) { | |
| 139 var existing = this.passwordFields_.filter(function(element) { | |
| 140 return element === passwordField; | |
| 141 }); | |
| 142 if (existing.length != 0) | |
| 143 return; | |
| 144 | |
| 145 var index = this.passwordFields_.length; | |
| 146 var fieldId = passwordField.id || passwordField.name || ''; | |
| 147 passwordField.addEventListener( | |
| 148 'input', this.onPasswordChanged_.bind(this, index, fieldId)); | |
| 149 this.passwordFields_.push(passwordField); | |
| 150 this.passwordValues_.push(passwordField.value); | |
| 151 }, | |
| 152 | |
| 153 /** | |
| 154 * Check if the password field at |index| has changed. If so, sends back | |
| 155 * the updated value. | |
| 156 */ | |
| 157 maybeSendUpdatedPassword: function(index, fieldId) { | |
| 158 var newValue = this.passwordFields_[index].value; | |
| 159 if (newValue == this.passwordValues_[index]) | |
| 160 return; | |
| 161 | |
| 162 this.passwordValues_[index] = newValue; | |
| 163 | |
| 164 // 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 | |
| 166 // field. | |
| 167 var passwordId = this.pageURL_.split('#')[0].split('?')[0] + | |
| 168 '|' + index + '|' + fieldId; | |
| 169 this.channel_.send({ | |
| 170 name: 'updatePassword', | |
| 171 id: passwordId, | |
| 172 password: newValue | |
| 173 }); | |
| 174 }, | |
| 175 | |
| 176 /** | |
| 177 * Handles 'change' event in the scraped password fields. | |
| 178 * @param {number} index The index of the password fields in | |
| 179 * |passwordFields_|. | |
| 180 * @param {string} fieldId The id or name of the password field or blank. | |
| 181 */ | |
| 182 onPasswordChanged_: function(index, fieldId) { | |
| 183 this.maybeSendUpdatedPassword(index, fieldId); | |
| 184 } | |
| 185 }; | |
| 186 | |
| 187 function onGetSAMLFlag(channel, isSAMLPage) { | |
| 188 if (!isSAMLPage) | |
| 189 return; | |
| 190 var pageURL = window.location.href; | |
| 191 | |
| 192 channel.send({name: 'pageLoaded', url: pageURL}); | |
| 193 | |
| 194 var initPasswordScraper = function() { | |
| 195 var passwordScraper = new PasswordInputScraper(); | |
| 196 passwordScraper.init(channel, pageURL, document.documentElement); | |
| 197 }; | |
| 198 | |
| 199 if (document.readyState == 'loading') { | |
| 200 window.addEventListener('readystatechange', function listener(event) { | |
| 201 if (document.readyState == 'loading') | |
| 202 return; | |
| 203 initPasswordScraper(); | |
| 204 window.removeEventListener(event.type, listener, true); | |
| 205 }, true); | |
| 206 } else { | |
| 207 initPasswordScraper(); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 var channel = Channel.create(); | |
| 212 channel.connect('injected'); | |
| 213 channel.sendWithCallback({name: 'getSAMLFlag'}, | |
| 214 onGetSAMLFlag.bind(undefined, channel)); | |
| 215 | |
| 216 var apiCallForwarder = new APICallForwarder(); | |
| 217 apiCallForwarder.init(channel); | |
| 218 })(); | 210 })(); |
| OLD | NEW |