| 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 } | 94 }, |
| 95 }, | 95 }, |
| 96 | 96 |
| 97 _onSignin: function(e) { | 97 attached: function() { |
| 98 this._setSigned_in(true); | 98 if (!this.client_id) { |
| 99 return; |
| 100 } |
| 101 // If a page is opened in a new tab, we are (likely) already logged in |
| 102 // so we wait for the gapi and auth2 to be loaded and re-extract our |
| 103 // access_token. |
| 104 var trySignin = window.setTimeout(function(){ |
| 105 // The 'gapi' checks are the same that signin-aware does. We do them |
| 106 // to avoid extraneous errors in the console. |
| 107 if (('gapi' in window) && ('auth2' in window.gapi) && |
| 108 !this.signed_in && !this._signingIn) { |
| 109 var user = gapi.auth2.getAuthInstance().currentUser.get(); |
| 110 if (user && user.getAuthResponse().access_token) { |
| 111 // User is already logged in, can use the access_token. |
| 112 this._onSignin(); |
| 113 } else { |
| 114 this.$.aware.signIn(); |
| 115 } |
| 116 } else { |
| 117 window.setTimeout(this.attached.bind(this)); |
| 118 } |
| 119 }.bind(this), |
| 120 // 300 ms is chosen because this seems to be long enough for the signin |
| 121 // to happen normally on a "visible" page and avoid the popup that |
| 122 // always happens when signIn() is manually called. It is also short |
| 123 // enough such that when the page is opened in a new tab, it seems to |
| 124 // happen automatically. |
| 125 300); |
| 126 }, |
| 127 |
| 128 _onSignin: function() { |
| 129 this._signingIn = true; |
| 99 var user = gapi.auth2.getAuthInstance().currentUser.get(); | 130 var user = gapi.auth2.getAuthInstance().currentUser.get(); |
| 100 var profile = user.getBasicProfile(); | 131 var profile = user.getBasicProfile(); |
| 101 this._setProfile({ | 132 this._setProfile({ |
| 102 email: profile.getEmail(), | 133 email: profile.getEmail(), |
| 103 imageUrl: profile.getImageUrl() | 134 imageUrl: profile.getImageUrl() |
| 104 }); | 135 }); |
| 105 this.set("auth_response", user.getAuthResponse()); | 136 this.set("auth_response", user.getAuthResponse()); |
| 106 this._setSigned_in(true); | 137 this._setSigned_in(true); |
| 107 this.fire("auth-signin"); | 138 this.fire("auth-signin"); |
| 139 // The credential will expire after a while (usually an hour) |
| 140 // so we need to reload it. |
| 141 this.async(function(){ |
| 142 console.log("reloading credentials"); |
| 143 user.reloadAuthResponse(); |
| 144 this._onSignin(); |
| 145 }, this.auth_response.expires_in * 1000); // convert seconds to ms |
| 146 this._signingIn = false; |
| 108 }, | 147 }, |
| 109 | 148 |
| 110 _onSignout: function(e) { | 149 _onSignout: function(e) { |
| 111 this._setSigned_in(false); | 150 this._setSigned_in(false); |
| 112 this._setProfile(null); | 151 this._setProfile(null); |
| 113 }, | 152 }, |
| 114 | 153 |
| 115 _makeHeader: function(auth_response) { | 154 _makeHeader: function(auth_response) { |
| 116 if (!auth_response) { | 155 if (!auth_response) { |
| 117 return {}; | 156 return {}; |
| 118 } | 157 } |
| 119 return { | 158 return { |
| 120 "authorization": auth_response.token_type + " " + auth_response.access
_token | 159 "authorization": auth_response.token_type + " " + auth_response.access
_token |
| 121 }; | 160 }; |
| 122 }, | 161 }, |
| 123 | 162 |
| 124 signIn: function() { | 163 signIn: function() { |
| 125 this.$.aware.signIn(); | 164 this.$.aware.signIn(); |
| 126 }, | 165 }, |
| 127 | 166 |
| 128 signOut: function() { | 167 signOut: function() { |
| 129 this.$.aware.signOut(); | 168 this.$.aware.signOut(); |
| 130 } | 169 } |
| 131 }); | 170 }); |
| 132 </script> | 171 </script> |
| 133 </dom-module> | 172 </dom-module> |
| OLD | NEW |