Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!-- | |
| 2 Copyright 2014 Google Inc | |
|
Sergey Berezin
2017/06/28 19:53:02
As discussed offline, let's commit the original ve
cwpayton
2017/06/29 22:28:15
Done.
| |
| 3 | |
| 4 Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 you may not use this file except in compliance with the License. | |
| 6 You may obtain a copy of the License at | |
| 7 | |
| 8 https://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | |
| 10 Unless required by applicable law or agreed to in writing, software | |
| 11 distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 See the License for the specific language governing permissions and | |
| 14 limitations under the License. | |
| 15 --> | |
| 16 | |
| 17 <link rel="import" href="../../../bower_components/polymer/polymer.html"> | |
| 18 <link rel="import" href="../../../bower_components/google-apis/google-js-api.htm l"> | |
|
Sergey Berezin
2017/06/28 19:53:02
This is probably fine for the our vendored copy, b
cwpayton
2017/06/29 22:28:15
Acknowledged.
| |
| 19 | |
| 20 <script> | |
| 21 (function() { | |
| 22 | |
| 23 /** | |
| 24 * Enum of attributes to be passed through to the login API call. | |
| 25 * @readonly | |
| 26 * @enum {string} | |
| 27 */ | |
| 28 var ProxyLoginAttributes = { | |
| 29 'appPackageName': 'apppackagename', | |
| 30 'clientId': 'clientid', | |
| 31 'cookiePolicy': 'cookiepolicy', | |
| 32 'hostedDomain': 'hostedDomain', | |
| 33 'openidPrompt': 'prompt', | |
| 34 'requestVisibleActions': 'requestvisibleactions' | |
| 35 }; | |
| 36 | |
| 37 /** | |
| 38 * AuthEngine does all interactions with gapi.auth2 | |
| 39 * | |
| 40 * It is tightly coupled with <google-signin-aware> element | |
| 41 * The elements configure AuthEngine. | |
| 42 * AuthEngine propagates all authentication events to all google-signin-awar e elements | |
| 43 * | |
| 44 * API used: https://developers.google.com/identity/sign-in/web/reference | |
| 45 * | |
| 46 */ | |
| 47 var AuthEngine = { | |
| 48 | |
| 49 /** | |
| 50 * oauth2 argument, set by google-signin-aware | |
| 51 */ | |
| 52 _clientId: null, | |
| 53 | |
| 54 get clientId() { | |
| 55 return this._clientId; | |
| 56 }, | |
| 57 | |
| 58 set clientId(val) { | |
| 59 if (this._clientId && val && val != this._clientId) { | |
| 60 throw new Error('clientId cannot change. Values do not match. New: ' + val + ' Old:' + this._clientId); | |
| 61 } | |
| 62 if (val && val != this._clientId) { | |
| 63 this._clientId = val; | |
| 64 this.initAuth2(); | |
| 65 } | |
| 66 }, | |
| 67 | |
| 68 /** | |
| 69 * oauth2 argument, set by google-signin-aware | |
| 70 */ | |
| 71 _cookiePolicy: 'single_host_origin', | |
| 72 | |
| 73 get cookiePolicy() { | |
| 74 return this._cookiePolicy; | |
| 75 }, | |
| 76 | |
| 77 set cookiePolicy(val) { | |
| 78 if (val) { | |
| 79 this._cookiePolicy = val; | |
| 80 } | |
| 81 }, | |
| 82 | |
| 83 /** | |
| 84 * oauth2 argument, set by google-signin-aware | |
| 85 */ | |
| 86 _appPackageName: '', | |
| 87 | |
| 88 get appPackageName() { | |
| 89 return this._appPackageName; | |
| 90 }, | |
| 91 | |
| 92 set appPackageName(val) { | |
| 93 if (this._appPackageName && val && val != this._appPackageName) { | |
| 94 throw new Error('appPackageName cannot change. Values do not match. Ne w: ' + val + ' Old: ' + this._appPackageName); | |
| 95 } | |
| 96 if (val) { | |
| 97 this._appPackageName = val; | |
| 98 } | |
| 99 }, | |
| 100 | |
| 101 /** | |
| 102 * oauth2 argument, set by google-signin-aware | |
| 103 */ | |
| 104 _requestVisibleActions: '', | |
| 105 | |
| 106 get requestVisibleactions() { | |
| 107 return this._requestVisibleActions; | |
| 108 }, | |
| 109 | |
| 110 set requestVisibleactions(val) { | |
| 111 if (this._requestVisibleActions && val && val != this._requestVisibleAct ions) { | |
| 112 throw new Error('requestVisibleactions cannot change. Values do not ma tch. New: ' + val + ' Old: ' + this._requestVisibleActions); | |
| 113 } | |
| 114 if (val) | |
| 115 this._requestVisibleActions = val; | |
| 116 }, | |
| 117 | |
| 118 /** | |
| 119 * oauth2 argument, set by google-signin-aware | |
| 120 */ | |
| 121 _hostedDomain: '', | |
| 122 | |
| 123 get hostedDomain() { | |
| 124 return this._hostedDomain; | |
| 125 }, | |
| 126 | |
| 127 set hostedDomain(val) { | |
| 128 if (this._hostedDomain && val && val != this._hostedDomain) { | |
| 129 throw new Error('hostedDomain cannot change. Values do not match. New: ' + val + ' Old: ' + this._hostedDomain); | |
| 130 } | |
| 131 if (val) | |
| 132 this._hostedDomain = val; | |
| 133 }, | |
| 134 | |
| 135 /** | |
| 136 * oauth2 argument, set by google-signin-aware | |
| 137 */ | |
| 138 _openidPrompt: '', | |
| 139 | |
| 140 get openidPrompt() { | |
| 141 return this._openidPrompt; | |
| 142 }, | |
| 143 | |
| 144 set openidPrompt(val) { | |
| 145 if (typeof val !== 'string') { | |
| 146 throw new Error( | |
| 147 'openidPrompt must be a string. Received ' + typeof val); | |
| 148 } | |
| 149 if (val) { | |
| 150 var values = val.split(' '); | |
| 151 values = values.map(function(v) { | |
| 152 return v.trim(); | |
| 153 }); | |
| 154 values = values.filter(function(v) { | |
| 155 return v; | |
| 156 }); | |
| 157 var validValues = {none: 0, login: 0, consent: 0, select_account: 0}; | |
| 158 values.forEach(function(v) { | |
| 159 if (v == 'none' && values.length > 1) { | |
| 160 throw new Error( | |
| 161 'none cannot be combined with other openidPrompt values'); | |
| 162 } | |
| 163 if (!(v in validValues)) { | |
| 164 throw new Error( | |
| 165 'invalid openidPrompt value ' + v + | |
| 166 '. Valid values: ' + Object.keys(validValues).join(', ')); | |
| 167 } | |
| 168 }); | |
| 169 } | |
| 170 this._openidPrompt = val; | |
| 171 }, | |
| 172 | |
| 173 /** Is offline access currently enabled in the google-signin-aware element ? */ | |
| 174 _offline: false, | |
| 175 | |
| 176 get offline() { | |
| 177 return this._offline; | |
| 178 }, | |
| 179 | |
| 180 set offline(val) { | |
| 181 this._offline = val; | |
| 182 this.updateAdditionalAuth(); | |
| 183 }, | |
| 184 | |
| 185 /** Should we force a re-prompt for offline access? */ | |
| 186 _offlineAlwaysPrompt: false, | |
| 187 | |
| 188 get offlineAlwaysPrompt() { | |
| 189 return this._offlineAlwaysPrompt; | |
| 190 }, | |
| 191 | |
| 192 set offlineAlwaysPrompt(val) { | |
| 193 this._offlineAlwaysPrompt = val; | |
| 194 this.updateAdditionalAuth(); | |
| 195 }, | |
| 196 | |
| 197 /** Have we already gotten offline access from Google during this session? */ | |
| 198 offlineGranted: false, | |
| 199 | |
| 200 /** <google-js-api> */ | |
| 201 _apiLoader: null, | |
| 202 | |
| 203 /** an array of wanted scopes. oauth2 argument */ | |
| 204 _requestedScopeArray: [], | |
| 205 | |
| 206 /** _requestedScopeArray as string */ | |
| 207 get requestedScopes() { | |
| 208 return this._requestedScopeArray.join(' '); | |
| 209 }, | |
| 210 | |
| 211 /** Is auth library initalized? */ | |
| 212 _initialized: false, | |
| 213 | |
| 214 /** Is user signed in? */ | |
| 215 _signedIn: false, | |
| 216 | |
| 217 /** Currently granted scopes */ | |
| 218 _grantedScopeArray: [], | |
| 219 | |
| 220 /** True if additional authorization is required */ | |
| 221 _needAdditionalAuth: true, | |
| 222 | |
| 223 /** True if have google+ scopes */ | |
| 224 _hasPlusScopes: false, | |
| 225 | |
| 226 /** | |
| 227 * array of <google-signin-aware> | |
| 228 * state changes are broadcast to them | |
| 229 */ | |
| 230 signinAwares: [], | |
| 231 | |
| 232 init: function() { | |
| 233 var that = this; | |
| 234 document.addEventListener('DOMContentLoaded', function() { | |
| 235 that._apiLoader = document.createElement('google-js-api'); | |
| 236 that._apiLoader.addEventListener('js-api-load', that.loadAuth2.bind(th at)); | |
| 237 if (Polymer.Element) { | |
| 238 document.body.appendChild(that._apiLoader); | |
| 239 } | |
| 240 }) | |
| 241 }, | |
| 242 | |
| 243 loadAuth2: function() { | |
| 244 var that = this; | |
|
Sergey Berezin
2017/06/28 19:53:02
Is this renaming necessary? There is no function c
Sergey Berezin
2017/06/29 23:52:53
What about this line?
cwpayton
2017/06/30 00:12:48
This line is in here because of the way the "this"
| |
| 245 gapi.load('auth2', that.initAuth2.bind(that)); | |
| 246 }, | |
| 247 | |
| 248 initAuth2: function() { | |
| 249 if (!('gapi' in window) || !('auth2' in window.gapi) || !this.clientId) { | |
| 250 return; | |
| 251 } | |
| 252 var auth = gapi.auth2.init({ | |
| 253 'client_id': this.clientId, | |
| 254 'cookie_policy': this.cookiePolicy, | |
| 255 'scope': this.requestedScopes, | |
| 256 'hosted_domain': this.hostedDomain | |
| 257 }); | |
| 258 | |
| 259 auth['currentUser'].listen(this.handleUserUpdate.bind(this)); | |
| 260 | |
| 261 auth.then( | |
| 262 function onFulfilled() { | |
| 263 // Let the current user listener trigger the changes. | |
| 264 }, | |
| 265 function onRejected(error) { | |
| 266 console.error(error); | |
| 267 } | |
| 268 ); | |
| 269 }, | |
| 270 | |
| 271 handleUserUpdate: function(newPrimaryUser) { | |
| 272 // update and broadcast currentUser | |
| 273 var isSignedIn = newPrimaryUser.isSignedIn(); | |
| 274 if (isSignedIn != this._signedIn) { | |
| 275 this._signedIn = isSignedIn; | |
| 276 for (var i=0; i<this.signinAwares.length; i++) { | |
| 277 this.signinAwares[i]._setSignedIn(isSignedIn); | |
| 278 } | |
| 279 } | |
| 280 // update and broadcast initialized property the first time the isSigned In property is set. | |
| 281 if(!this._initialized) { | |
| 282 for (var i=0; i<this.signinAwares.length; i++) { | |
| 283 this.signinAwares[i]._setInitialized(true); | |
| 284 } | |
| 285 this._initialized = true; | |
| 286 } | |
| 287 | |
| 288 | |
| 289 // update granted scopes | |
| 290 this._grantedScopeArray = this.strToScopeArray( | |
| 291 newPrimaryUser.getGrantedScopes()); | |
| 292 // console.log(this._grantedScopeArray); | |
| 293 this.updateAdditionalAuth(); | |
| 294 | |
| 295 var response = newPrimaryUser.getAuthResponse(); | |
| 296 for (var i=0; i<this.signinAwares.length; i++) { | |
| 297 this.signinAwares[i]._updateScopeStatus(response); | |
| 298 } | |
| 299 }, | |
| 300 | |
| 301 setOfflineCode: function(code) { | |
| 302 for (var i=0; i<this.signinAwares.length; i++) { | |
| 303 this.signinAwares[i]._updateOfflineCode(code); | |
| 304 } | |
| 305 }, | |
| 306 | |
| 307 /** convert scope string to scope array */ | |
| 308 strToScopeArray: function(str) { | |
| 309 if (!str) { | |
| 310 return []; | |
| 311 } | |
| 312 // remove extra spaces, then split | |
| 313 var scopes = str.replace(/\ +/g, ' ').trim().split(' '); | |
| 314 for (var i=0; i<scopes.length; i++) { | |
| 315 scopes[i] = scopes[i].toLowerCase(); | |
| 316 // Handle scopes that will be deprecated but are still returned with their old value | |
| 317 if (scopes[i] === 'https://www.googleapis.com/auth/userinfo.profile') { | |
| 318 scopes[i] = 'profile'; | |
| 319 } | |
| 320 if (scopes[i] === 'https://www.googleapis.com/auth/userinfo.email') { | |
| 321 scopes[i] = 'email'; | |
| 322 } | |
| 323 } | |
| 324 // return with duplicates filtered out | |
| 325 return scopes.filter( function(value, index, self) { | |
| 326 return self.indexOf(value) === index; | |
| 327 }); | |
| 328 }, | |
| 329 | |
| 330 /** true if scopes have google+ scopes */ | |
| 331 isPlusScope: function(scope) { | |
| 332 return (scope.indexOf('/auth/games') > -1) | |
| 333 || (scope.indexOf('auth/plus.') > -1 && scope.indexOf('auth/plus.me' ) < 0); | |
| 334 }, | |
| 335 | |
| 336 /** true if scopes have been granted */ | |
| 337 hasGrantedScopes: function(scopeStr) { | |
| 338 var scopes = this.strToScopeArray(scopeStr); | |
| 339 for (var i=0; i< scopes.length; i++) { | |
| 340 if (this._grantedScopeArray.indexOf(scopes[i]) === -1) | |
| 341 return false; | |
| 342 } | |
| 343 return true; | |
| 344 }, | |
| 345 | |
| 346 /** request additional scopes */ | |
| 347 requestScopes: function(newScopeStr) { | |
| 348 var newScopes = this.strToScopeArray(newScopeStr); | |
| 349 var scopesUpdated = false; | |
| 350 for (var i=0; i<newScopes.length; i++) { | |
| 351 if (this._requestedScopeArray.indexOf(newScopes[i]) === -1) { | |
| 352 this._requestedScopeArray.push(newScopes[i]); | |
| 353 scopesUpdated = true; | |
| 354 } | |
| 355 } | |
| 356 if (scopesUpdated) { | |
| 357 this.updateAdditionalAuth(); | |
| 358 this.updatePlusScopes(); | |
| 359 } | |
| 360 }, | |
| 361 | |
| 362 /** update status of _needAdditionalAuth */ | |
| 363 updateAdditionalAuth: function() { | |
| 364 var needMoreAuth = false; | |
| 365 if ((this.offlineAlwaysPrompt || this.offline ) && !this.offlineGranted) { | |
| 366 needMoreAuth = true; | |
| 367 } else { | |
| 368 for (var i=0; i<this._requestedScopeArray.length; i++) { | |
| 369 if (this._grantedScopeArray.indexOf(this._requestedScopeArray[i]) == = -1) { | |
| 370 needMoreAuth = true; | |
| 371 break; | |
| 372 } | |
| 373 } | |
| 374 } | |
| 375 if (this._needAdditionalAuth != needMoreAuth) { | |
| 376 this._needAdditionalAuth = needMoreAuth; | |
| 377 // broadcast new value | |
| 378 for (var i=0; i<this.signinAwares.length; i++) { | |
| 379 this.signinAwares[i]._setNeedAdditionalAuth(needMoreAuth); | |
| 380 } | |
| 381 } | |
| 382 }, | |
| 383 | |
| 384 updatePlusScopes: function() { | |
| 385 var hasPlusScopes = false; | |
| 386 for (var i = 0; i < this._requestedScopeArray.length; i++) { | |
| 387 if (this.isPlusScope(this._requestedScopeArray[i])) { | |
| 388 hasPlusScopes = true; | |
| 389 break; | |
| 390 } | |
| 391 } | |
| 392 if (this._hasPlusScopes != hasPlusScopes) { | |
| 393 this._hasPlusScopes = hasPlusScopes; | |
| 394 for (var i=0; i<this.signinAwares.length; i++) { | |
| 395 this.signinAwares[i]._setHasPlusScopes(hasPlusScopes); | |
| 396 } | |
| 397 } | |
| 398 }, | |
| 399 /** | |
| 400 * attached <google-signin-aware> | |
| 401 * @param {!GoogleSigninAwareElement} aware element to add | |
| 402 */ | |
| 403 attachSigninAware: function(aware) { | |
| 404 if (this.signinAwares.indexOf(aware) == -1) { | |
| 405 this.signinAwares.push(aware); | |
| 406 // Initialize aware properties | |
| 407 aware._setNeedAdditionalAuth(this._needAdditionalAuth); | |
| 408 aware._setInitialized(this._initialized); | |
| 409 aware._setSignedIn(this._signedIn); | |
| 410 aware._setHasPlusScopes(this._hasPlusScopes); | |
| 411 } else { | |
| 412 console.warn('signinAware attached more than once', aware); | |
| 413 } | |
| 414 }, | |
| 415 | |
| 416 detachSigninAware: function(aware) { | |
| 417 var index = this.signinAwares.indexOf(aware); | |
| 418 if (index != -1) { | |
| 419 this.signinAwares.splice(index, 1); | |
| 420 } else { | |
| 421 console.warn('Trying to detach unattached signin-aware'); | |
| 422 } | |
| 423 }, | |
| 424 | |
| 425 /** returns scopes not granted */ | |
| 426 getMissingScopes: function() { | |
| 427 return this._requestedScopeArray.filter( function(scope) { | |
| 428 return this._grantedScopeArray.indexOf(scope) === -1; | |
| 429 }.bind(this)).join(' '); | |
| 430 }, | |
| 431 | |
| 432 assertAuthInitialized: function() { | |
| 433 if (!this.clientId) { | |
| 434 throw new Error("AuthEngine not initialized. clientId has not been con figured."); | |
| 435 } | |
| 436 if (!('gapi' in window)) { | |
| 437 throw new Error("AuthEngine not initialized. gapi has not loaded."); | |
| 438 } | |
| 439 if (!('auth2' in window.gapi)) { | |
| 440 throw new Error("AuthEngine not initialized. auth2 not loaded."); | |
| 441 } | |
| 442 }, | |
| 443 | |
| 444 /** pops up sign-in dialog */ | |
| 445 signIn: function() { | |
| 446 this.assertAuthInitialized(); | |
| 447 var params = { | |
| 448 'scope': this.getMissingScopes() | |
| 449 }; | |
| 450 | |
| 451 // Proxy specific attributes through to the signIn options. | |
| 452 Object.keys(ProxyLoginAttributes).forEach(function(key) { | |
| 453 if (this[key] && this[key] !== '') { | |
| 454 params[ProxyLoginAttributes[key]] = this[key]; | |
| 455 } | |
| 456 }, this); | |
| 457 | |
| 458 var promise; | |
| 459 var user = gapi.auth2.getAuthInstance()['currentUser'].get(); | |
| 460 if (!(this.offline || this.offlineAlwaysPrompt)) { | |
| 461 if (user.getGrantedScopes()) { | |
| 462 // additional auth, skip multiple account dialog | |
| 463 promise = user.grant(params); | |
| 464 } else { | |
| 465 // initial signin | |
| 466 promise = gapi.auth2.getAuthInstance().signIn(params); | |
| 467 } | |
| 468 } else { | |
| 469 params.redirect_uri = 'postmessage'; | |
| 470 if (this.offlineAlwaysPrompt) { | |
| 471 params.approval_prompt = 'force'; | |
| 472 } | |
| 473 | |
| 474 // Despite being documented at https://goo.gl/tiO0Bk | |
| 475 // It doesn't seem like user.grantOfflineAccess() actually exists in | |
| 476 // the current version of the Google Sign-In JS client we're using | |
| 477 // through GoogleWebComponents. So in the offline case, we will not | |
| 478 // distinguish between a first auth and an additional one. | |
| 479 promise = gapi.auth2.getAuthInstance().grantOfflineAccess(params); | |
| 480 } | |
| 481 promise.then( | |
| 482 function onFulfilled(response) { | |
| 483 // If login was offline, response contains one string "code" | |
| 484 // Otherwise it contains the user object already | |
| 485 var newUser; | |
| 486 if (response.code) { | |
| 487 AuthEngine.offlineGranted = true; | |
| 488 newUser = gapi.auth2.getAuthInstance()['currentUser'].get(); | |
| 489 AuthEngine.setOfflineCode(response.code); | |
| 490 } else { | |
| 491 newUser = response; | |
| 492 } | |
| 493 | |
| 494 var authResponse = newUser.getAuthResponse(); | |
| 495 // Let the current user listener trigger the changes. | |
| 496 }, | |
| 497 function onRejected(error) { | |
| 498 // Access denied is not an error, user hit cancel | |
| 499 if ("Access denied." !== error.reason) { | |
| 500 this.signinAwares.forEach(function(awareInstance) { | |
| 501 awareInstance.errorNotify(error); | |
| 502 }); | |
| 503 } | |
| 504 }.bind(this) | |
| 505 ); | |
| 506 }, | |
| 507 | |
| 508 /** signs user out */ | |
| 509 signOut: function() { | |
| 510 this.assertAuthInitialized(); | |
| 511 gapi.auth2.getAuthInstance().signOut().then( | |
| 512 function onFulfilled() { | |
| 513 // Let the current user listener trigger the changes. | |
| 514 }, | |
| 515 function onRejected(error) { | |
| 516 console.error(error); | |
| 517 } | |
| 518 ); | |
| 519 } | |
| 520 }; | |
| 521 | |
| 522 AuthEngine.init(); | |
| 523 | |
| 524 /** | |
| 525 `google-signin-aware` is used to enable authentication in custom elements by | |
| 526 interacting with a google-signin element that needs to be present somewhere | |
| 527 on the page. | |
| 528 | |
| 529 The `scopes` attribute allows you to specify which scope permissions are require d | |
| 530 (e.g do you want to allow interaction with the Google Drive API). | |
| 531 | |
| 532 The `google-signin-aware-success` event is triggered when a user successfully | |
| 533 authenticates. If either `offline` or `offlineAlwaysPrompt` is set to true, succ essful | |
| 534 authentication will also trigger the `google-signin-offline-success`event. | |
| 535 The `google-signin-aware-signed-out` event is triggered when a user explicitly | |
| 536 signs out via the google-signin element. | |
| 537 | |
| 538 You can bind to `isAuthorized` property to monitor authorization state. | |
| 539 ##### Example | |
| 540 | |
| 541 <google-signin-aware scopes="https://www.googleapis.com/auth/drive"></google -signin-aware> | |
| 542 | |
| 543 | |
| 544 ##### Example with offline | |
| 545 <template id="awareness" is="dom-bind"> | |
| 546 <google-signin-aware | |
| 547 scopes="https://www.googleapis.com/auth/drive" | |
| 548 offline | |
| 549 on-google-signin-aware-success="handleSignin" | |
| 550 on-google-signin-offline-success="handleOffline"></google-signin-aware > | |
| 551 <\/template> | |
| 552 <script> | |
| 553 var aware = document.querySelector('#awareness'); | |
| 554 aware.handleSignin = function(response) { | |
| 555 var user = gapi.auth2.getAuthInstance()['currentUser'].get(); | |
| 556 console.log('User name: ' + user.getBasicProfile().getName()); | |
| 557 }; | |
| 558 aware.handleOffline = function(response) { | |
| 559 console.log('Offline code received: ' + response.detail.code); | |
| 560 // Here you would POST response.detail.code to your webserver, which can | |
| 561 // exchange the authorization code for an access token. More info at: | |
| 562 // https://developers.google.com/identity/protocols/OAuth2WebServer | |
| 563 }; | |
| 564 <\/script> | |
| 565 */ | |
| 566 Polymer({ | |
| 567 | |
| 568 is: 'google-signin-aware', | |
| 569 | |
| 570 /** | |
| 571 * Fired when this scope has been authorized | |
| 572 * @param {Object} result Authorization result. | |
| 573 * @event google-signin-aware-success | |
| 574 */ | |
| 575 | |
| 576 /** | |
| 577 * Fired when an offline authorization is successful. | |
| 578 * @param {{code: string}} detail - | |
| 579 * code: The one-time authorization code from Google. | |
| 580 * Your application can exchange this for an `access_token` and `r efresh_token` | |
| 581 * @event google-signin-offline-success | |
| 582 */ | |
| 583 | |
| 584 /** | |
| 585 * Fired when this scope is not authorized | |
| 586 * @event google-signin-aware-signed-out | |
| 587 */ | |
| 588 | |
| 589 /** | |
| 590 * Fired when there is an error during the signin flow. | |
| 591 * @param {Object} detail The error object returned from the OAuth 2 flow. | |
| 592 * @event google-signin-aware-error | |
| 593 */ | |
| 594 | |
| 595 /** | |
| 596 * This block is needed so the previous @param is not assigned to the next property. | |
| 597 */ | |
| 598 | |
| 599 properties: { | |
| 600 /** | |
| 601 * App package name for android over-the-air installs. | |
| 602 * See the relevant [docs](https://developers.google.com/+/web/signin/an droid-app-installs) | |
| 603 */ | |
| 604 appPackageName: { | |
| 605 type: String, | |
| 606 observer: '_appPackageNameChanged' | |
| 607 }, | |
| 608 | |
| 609 /** | |
| 610 * a Google Developers clientId reference | |
| 611 */ | |
| 612 clientId: { | |
| 613 type: String, | |
| 614 observer: '_clientIdChanged' | |
| 615 }, | |
| 616 | |
| 617 /** | |
| 618 * The cookie policy defines what URIs have access to the session cookie | |
| 619 * remembering the user's sign-in state. | |
| 620 * See the relevant [docs](https://developers.google.com/+/web/signin/re ference#determining_a_value_for_cookie_policy) for more information. | |
| 621 * @default 'single_host_origin' | |
| 622 */ | |
| 623 cookiePolicy: { | |
| 624 type: String, | |
| 625 observer: '_cookiePolicyChanged' | |
| 626 }, | |
| 627 | |
| 628 /** | |
| 629 * The app activity types you want to write on behalf of the user | |
| 630 * (e.g http://schemas.google.com/AddActivity) | |
| 631 * | |
| 632 */ | |
| 633 requestVisibleActions: { | |
| 634 type: String, | |
| 635 observer: '_requestVisibleActionsChanged' | |
| 636 }, | |
| 637 | |
| 638 /** | |
| 639 * The Google Apps domain to which users must belong to sign in. | |
| 640 * See the relevant [docs](https://developers.google.com/identity/sign-i n/web/reference) for more information. | |
| 641 */ | |
| 642 hostedDomain: { | |
| 643 type: String, | |
| 644 observer: '_hostedDomainChanged' | |
| 645 }, | |
| 646 | |
| 647 /** | |
| 648 * Allows for offline `access_token` retrieval during the signin process . | |
| 649 * See also `offlineAlwaysPrompt`. You only need to set one of the two; if both | |
| 650 * are set, the behavior of `offlineAlwaysPrompt` will override `offline `. | |
| 651 */ | |
| 652 offline: { | |
| 653 type: Boolean, | |
| 654 value: false, | |
| 655 observer: '_offlineChanged' | |
| 656 }, | |
| 657 | |
| 658 /** | |
| 659 * Works the same as `offline` with the addition that it will always | |
| 660 * force a re-prompt to the user, guaranteeing that you will get a | |
| 661 * refresh_token even if the user has already granted offline access to | |
| 662 * this application. You only need to set one of `offline` or | |
| 663 * `offlineAlwaysPrompt`, not both. | |
| 664 */ | |
| 665 offlineAlwaysPrompt: { | |
| 666 type: Boolean, | |
| 667 value: false, | |
| 668 observer: '_offlineAlwaysPromptChanged' | |
| 669 }, | |
| 670 | |
| 671 /** | |
| 672 * The scopes to provide access to (e.g https://www.googleapis.com/auth/ drive) | |
| 673 * and should be space-delimited. | |
| 674 */ | |
| 675 scopes: { | |
| 676 type: String, | |
| 677 value: 'profile', | |
| 678 observer: '_scopesChanged' | |
| 679 }, | |
| 680 | |
| 681 /** | |
| 682 * Space-delimited, case-sensitive list of strings that | |
| 683 * specifies whether the the user is prompted for reauthentication | |
| 684 * and/or consent. The defined values are: | |
| 685 * none: do not display authentication or consent pages. | |
| 686 * This value is mutually exclusive with the rest. | |
| 687 * login: always prompt the user for reauthentication. | |
| 688 * consent: always show consent screen. | |
| 689 * select_account: always show account selection page. | |
| 690 * This enables a user who has multiple accounts to select amongst | |
| 691 * the multiple accounts that they might have current sessions for. | |
| 692 * For more information, see "prompt" parameter description in | |
| 693 * https://openid.net/specs/openid-connect-basic-1_0.html#RequestParamet ers | |
| 694 */ | |
| 695 openidPrompt: { | |
| 696 type: String, | |
| 697 value: '', | |
| 698 observer: '_openidPromptChanged' | |
| 699 }, | |
| 700 | |
| 701 /** | |
| 702 * True when the auth library has been initialized, and signedIn propert y value is set from the first api response. | |
| 703 */ | |
| 704 initialized: { | |
| 705 type: Boolean, | |
| 706 notify: true, | |
| 707 readOnly: true | |
| 708 }, | |
| 709 | |
| 710 /** | |
| 711 * True if user is signed in | |
| 712 */ | |
| 713 signedIn: { | |
| 714 type: Boolean, | |
| 715 notify: true, | |
| 716 readOnly: true | |
| 717 }, | |
| 718 | |
| 719 /** | |
| 720 * True if authorizations for *this* element have been granted | |
| 721 */ | |
| 722 isAuthorized: { | |
| 723 type: Boolean, | |
| 724 notify: true, | |
| 725 readOnly: true, | |
| 726 value: false | |
| 727 }, | |
| 728 | |
| 729 /** | |
| 730 * True if additional authorizations for *any* element are required | |
| 731 */ | |
| 732 needAdditionalAuth: { | |
| 733 type: Boolean, | |
| 734 notify: true, | |
| 735 readOnly: true | |
| 736 }, | |
| 737 | |
| 738 /** | |
| 739 * True if *any* element has google+ scopes | |
| 740 */ | |
| 741 hasPlusScopes: { | |
| 742 type: Boolean, | |
| 743 value: false, | |
| 744 notify: true, | |
| 745 readOnly: true | |
| 746 } | |
| 747 }, | |
| 748 | |
| 749 attached: function() { | |
| 750 AuthEngine.attachSigninAware(this); | |
| 751 }, | |
| 752 | |
| 753 detached: function() { | |
| 754 AuthEngine.detachSigninAware(this); | |
| 755 }, | |
| 756 | |
| 757 /** pops up the authorization dialog */ | |
| 758 signIn: function() { | |
| 759 AuthEngine.signIn(); | |
| 760 }, | |
| 761 | |
| 762 /** signs user out */ | |
| 763 signOut: function() { | |
| 764 AuthEngine.signOut(); | |
| 765 }, | |
| 766 | |
| 767 errorNotify: function(error) { | |
| 768 this.fire('google-signin-aware-error', error); | |
| 769 }, | |
| 770 | |
| 771 _appPackageNameChanged: function(newName, oldName) { | |
| 772 AuthEngine.appPackageName = newName; | |
| 773 }, | |
| 774 | |
| 775 _clientIdChanged: function(newId, oldId) { | |
| 776 AuthEngine.clientId = newId; | |
| 777 }, | |
| 778 | |
| 779 _cookiePolicyChanged: function(newPolicy, oldPolicy) { | |
| 780 AuthEngine.cookiePolicy = newPolicy; | |
| 781 }, | |
| 782 | |
| 783 _requestVisibleActionsChanged: function(newVal, oldVal) { | |
| 784 AuthEngine.requestVisibleActions = newVal; | |
| 785 }, | |
| 786 | |
| 787 _hostedDomainChanged: function(newVal, oldVal) { | |
| 788 AuthEngine.hostedDomain = newVal; | |
| 789 }, | |
| 790 | |
| 791 _offlineChanged: function(newVal, oldVal) { | |
| 792 AuthEngine.offline = newVal; | |
| 793 }, | |
| 794 | |
| 795 _offlineAlwaysPromptChanged: function(newVal, oldVal) { | |
| 796 AuthEngine.offlineAlwaysPrompt = newVal; | |
| 797 }, | |
| 798 | |
| 799 _scopesChanged: function(newVal, oldVal) { | |
| 800 AuthEngine.requestScopes(newVal); | |
| 801 this._updateScopeStatus(undefined); | |
| 802 }, | |
| 803 | |
| 804 _openidPromptChanged: function(newVal, oldVal) { | |
| 805 AuthEngine.openidPrompt = newVal; | |
| 806 }, | |
| 807 | |
| 808 _updateScopeStatus: function(user) { | |
| 809 var newAuthorized = this.signedIn && AuthEngine.hasGrantedScopes(this.sc opes); | |
| 810 if (newAuthorized !== this.isAuthorized) { | |
| 811 this._setIsAuthorized(newAuthorized); | |
| 812 if (newAuthorized) { | |
| 813 this.fire('google-signin-aware-success', user); | |
| 814 } | |
| 815 else { | |
| 816 this.fire('google-signin-aware-signed-out', user); | |
| 817 } | |
| 818 } | |
| 819 }, | |
| 820 | |
| 821 _updateOfflineCode: function(code) { | |
| 822 if (code) { | |
| 823 this.fire('google-signin-offline-success', {code: code}); | |
| 824 } | |
| 825 } | |
| 826 }); | |
| 827 })(); | |
| 828 </script> | |
| OLD | NEW |