 Chromium Code Reviews
 Chromium Code Reviews Issue 2344883002:
  Let auth credentials autorefresh and fix new tab  (Closed) 
  Base URL: git@github.com:luci/luci-py@master
    
  
    Issue 2344883002:
  Let auth credentials autorefresh and fix new tab  (Closed) 
  Base URL: git@github.com:luci/luci-py@master| OLD | NEW | 
|---|---|
| 1 <!-- | 1 <!-- | 
| 2 Copyright 2016 The LUCI Authors. All rights reserved. | 2 Copyright 2016 The LUCI Authors. All rights reserved. | 
| 3 Use of this source code is governed under the Apache License, Version 2.0 | 3 Use of this source code is governed under the Apache License, Version 2.0 | 
| 4 that can be found in the LICENSE file. | 4 that can be found in the LICENSE file. | 
| 5 | 5 | 
| 6 The `auth-signin` element displays sign-in/sign-out button, user email and | 6 The `auth-signin` element displays sign-in/sign-out button, user email and | 
| 7 avatar. | 7 avatar. | 
| 8 It has a google-signin/google-signin-aware element under the hood that handles | 8 It has a google-signin/google-signin-aware element under the hood that handles | 
| 9 the actual OAuth logic. | 9 the actual OAuth logic. | 
| 10 | 10 | 
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 }, | 84 }, | 
| 85 profile: { | 85 profile: { | 
| 86 type: Object, | 86 type: Object, | 
| 87 readOnly: true | 87 readOnly: true | 
| 88 }, | 88 }, | 
| 89 signed_in: { | 89 signed_in: { | 
| 90 type: Boolean, | 90 type: Boolean, | 
| 91 readOnly: true, | 91 readOnly: true, | 
| 92 value: false, | 92 value: false, | 
| 93 notify: true, | 93 notify: true, | 
| 94 }, | |
| 95 | |
| 96 _signingIn: { | |
| 
jcgregorio
2016/09/20 12:58:18
_signingIn seems to be needed because you are usin
 
kjlubick
2016/09/20 13:24:02
Excellent suggestion.  Done.
 | |
| 97 type: Boolean, | |
| 98 value: false, | |
| 94 } | 99 } | 
| 95 }, | 100 }, | 
| 96 | 101 | 
| 97 _onSignin: function(e) { | 102 attached: function() { | 
| 98 this._setSigned_in(true); | 103 if (!this.client_id) { | 
| 104 return; | |
| 105 } | |
| 106 // If a page is opened in a new tab, we are (likely) already logged in | |
| 107 // so we wait for the gapi and auth2 to be loaded and re-extract our | |
| 108 // access_token. | |
| 109 var trySignin = window.setInterval(function(){ | |
| 110 if (this.signed_in) { | |
| 111 window.clearInterval(trySignin); | |
| 112 return; | |
| 113 } | |
| 114 // The 'gapi' checks are the same that signin-aware does. We do them | |
| 115 // to avoid extraneous errors in the console. | |
| 116 if (('gapi' in window) && ('auth2' in window.gapi) && | |
| 117 !this.signed_in && !this._signingIn) { | |
| 118 this._signingIn = true; | |
| 119 var user = gapi.auth2.getAuthInstance().currentUser.get(); | |
| 120 if (user && user.getAuthResponse().access_token) { | |
| 121 // User is already logged in, can use the access_token. | |
| 122 this._onSignin(); | |
| 123 } else { | |
| 124 this.$.aware.signIn(); | |
| 125 } | |
| 126 } | |
| 127 }.bind(this), | |
| 128 // 300 ms is chosen because this seems to be long enough for the signin | |
| 129 // to happen normally on a "visible" page and avoid the popup that | |
| 130 // always happens when signIn() is manually called. It is also short | |
| 131 // enough such that when the page is opened in a new tab, it seems to | |
| 132 // happen automatically. | |
| 133 300); | |
| 134 | |
| 
jcgregorio
2016/09/20 12:58:18
No blank line here.
 
kjlubick
2016/09/20 13:24:02
Done.
 | |
| 135 }, | |
| 136 | |
| 137 _onSignin: function() { | |
| 138 this._signingIn = true; | |
| 99 var user = gapi.auth2.getAuthInstance().currentUser.get(); | 139 var user = gapi.auth2.getAuthInstance().currentUser.get(); | 
| 100 var profile = user.getBasicProfile(); | 140 var profile = user.getBasicProfile(); | 
| 101 this._setProfile({ | 141 this._setProfile({ | 
| 102 email: profile.getEmail(), | 142 email: profile.getEmail(), | 
| 103 imageUrl: profile.getImageUrl() | 143 imageUrl: profile.getImageUrl() | 
| 104 }); | 144 }); | 
| 105 this.set("auth_response", user.getAuthResponse()); | 145 this.set("auth_response", user.getAuthResponse()); | 
| 106 this._setSigned_in(true); | 146 this._setSigned_in(true); | 
| 107 this.fire("auth-signin"); | 147 this.fire("auth-signin"); | 
| 148 // The credential will expire after a while (usually an hour) | |
| 149 // so we need to reload it. | |
| 150 this.async(function(){ | |
| 151 console.log("reloading credentials"); | |
| 
jcgregorio
2016/09/20 12:58:18
Is the console.log needed?
 
kjlubick
2016/09/20 13:24:02
I would like to keep it in, so if it doesn't work
 | |
| 152 user.reloadAuthResponse(); | |
| 153 this._onSignin(); | |
| 154 }, this.auth_response.expires_in * 1000); // convert seconds to ms | |
| 155 this._signingIn = false; | |
| 108 }, | 156 }, | 
| 109 | 157 | 
| 110 _onSignout: function(e) { | 158 _onSignout: function(e) { | 
| 111 this._setSigned_in(false); | 159 this._setSigned_in(false); | 
| 112 this._setProfile(null); | 160 this._setProfile(null); | 
| 113 }, | 161 }, | 
| 114 | 162 | 
| 115 _makeHeader: function(auth_response) { | 163 _makeHeader: function(auth_response) { | 
| 116 if (!auth_response) { | 164 if (!auth_response) { | 
| 117 return {}; | 165 return {}; | 
| 118 } | 166 } | 
| 119 return { | 167 return { | 
| 120 "authorization": auth_response.token_type + " " + auth_response.access _token | 168 "authorization": auth_response.token_type + " " + auth_response.access _token | 
| 121 }; | 169 }; | 
| 122 }, | 170 }, | 
| 123 | 171 | 
| 124 signIn: function() { | 172 signIn: function() { | 
| 125 this.$.aware.signIn(); | 173 this.$.aware.signIn(); | 
| 126 }, | 174 }, | 
| 127 | 175 | 
| 128 signOut: function() { | 176 signOut: function() { | 
| 129 this.$.aware.signOut(); | 177 this.$.aware.signOut(); | 
| 130 } | 178 } | 
| 131 }); | 179 }); | 
| 132 </script> | 180 </script> | 
| 133 </dom-module> | 181 </dom-module> | 
| OLD | NEW |