Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!-- | 1 <!-- |
| 2 Copyright 2017 The LUCI Authors. All rights reserved. | 2 Copyright 2017 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 28 matching lines...) Expand all Loading... | |
| 39 .center { vertical-align: middle; } | 39 .center { vertical-align: middle; } |
| 40 | 40 |
| 41 .typeface { | 41 .typeface { |
| 42 font-size: 75%; | 42 font-size: 75%; |
| 43 color: white; | 43 color: white; |
| 44 } | 44 } |
| 45 </style> | 45 </style> |
| 46 | 46 |
| 47 <google-signin-aware id="aware" | 47 <google-signin-aware id="aware" |
| 48 client-id="[[client_id]]" | 48 client-id="[[client_id]]" |
| 49 initialized="{{initialized}}" | |
| 49 scopes="email" | 50 scopes="email" |
| 50 on-google-signin-aware-success="_onSignin" | 51 on-google-signin-aware-success="_onSignin" |
| 51 on-google-signin-aware-signed-out="_onSignout"> | 52 on-google-signin-aware-signed-out="_onSignout"> |
| 52 </google-signin-aware> | 53 </google-signin-aware> |
| 53 | 54 |
| 54 <template is="dom-if" if="[[!signed_in]]"> | 55 <template is="dom-if" if="[[!signed_in]]"> |
| 55 <div id="signinContainer"> | 56 <div id="signinContainer"> |
| 56 <!-- TODO(cwpayton): Before official deployment, change href to "/#/" -- > | 57 <!-- TODO(cwpayton): Before official deployment, change href to "/#/" -- > |
| 57 <a class="typeface" on-tap="signIn" href="/newui#/">Sign in</a> | 58 <a class="typeface" on-tap="signIn" href="/newui#/">Sign in</a> |
| 58 </div> | 59 </div> |
| 59 </template> | 60 </template> |
| 60 | 61 |
| 61 <template is="dom-if" if="[[signed_in]]"> | 62 <template is="dom-if" if="[[signed_in]]"> |
| 62 <img class="center" id="avatar" src="[[profile.imageUrl]]" width="30" heig ht="30"> | 63 <img class="center" id="avatar" src="[[profile.imageUrl]]" width="30" heig ht="30"> |
| 63 <span class="center typeface">[[profile.email]]</span> | 64 <span class="center typeface">[[profile.email]]</span> |
| 64 <span class="center typeface">|</span> | 65 <span class="center typeface">|</span> |
| 65 <!-- TODO(cwpayton): Before official deployment, change href to "/#/" --> | 66 <!-- TODO(cwpayton): Before official deployment, change href to "/#/" --> |
| 66 <a class="center typeface" on-tap="signOut" href="/newui#/">Sign out</a> | 67 <a class="center typeface" on-tap="signOut" href="/newui#/">Sign out</a> |
| 67 </template> | 68 </template> |
| 68 </template> | 69 </template> |
| 69 <script> | 70 <script> |
| 70 'use strict'; | 71 'use strict'; |
| 71 Polymer({ | 72 Polymer({ |
| 72 is: 'auth-signin', | 73 is: 'auth-signin', |
| 73 properties: { | 74 properties: { |
| 74 auth_headers: { | 75 auth_headers: { |
| 75 type: Object, | 76 type: Object, |
| 76 value: () => {}, | 77 value: () => null, |
| 77 notify: true | 78 notify: true |
| 78 }, | 79 }, |
| 79 | 80 |
| 80 auth_response: { | 81 auth_response: { |
| 81 type: Object, | 82 type: Object, |
| 82 notify: true, | 83 notify: true, |
| 83 observer: '_makeHeader' | 84 observer: '_makeHeader' |
| 84 }, | 85 }, |
| 85 | 86 |
| 86 client_id: { | 87 client_id: { |
| 87 type: String | 88 type: String |
| 88 }, | 89 }, |
| 89 | 90 |
| 91 initialized: { | |
| 92 type: Boolean, | |
| 93 value: false, | |
| 94 notify: true | |
| 95 }, | |
| 96 | |
| 90 profile: { | 97 profile: { |
| 91 type: Object, | 98 type: Object, |
| 92 notify: true | 99 notify: true |
| 93 }, | 100 }, |
| 94 | 101 |
| 95 signed_in: { | 102 signed_in: { |
| 96 type: Boolean, | 103 type: Boolean, |
| 97 value: false, | 104 value: false, |
| 98 notify: true | 105 notify: true |
| 106 }, | |
| 107 | |
| 108 user: { | |
| 109 type: Object, | |
| 110 value: () => null, | |
| 111 notify: true, | |
| 99 } | 112 } |
| 100 }, | 113 }, |
| 101 | 114 |
| 102 ready: function() { | 115 ready: function() { |
| 103 if (!this.client_id) { | 116 if (!this.client_id) { |
| 104 return; | 117 return; |
| 105 } | 118 } |
| 106 // If a page is opened in a new tab, we are (likely) already logged in | 119 // 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 | 120 // so we wait for the gapi and auth2 to be loaded and re-extract our |
| 108 // access_token. | 121 // access_token. |
| 109 window.setTimeout(function(){ | 122 window.setTimeout(function(){ |
|
Ryan Tseng
2017/07/14 20:43:05
Actually you just have to attach this function to
| |
| 110 // The 'gapi' checks are the same that signin-aware does. We do them | 123 // The 'gapi' checks are the same that signin-aware does. We do them |
| 111 // to avoid extraneous errors in the console. | 124 // to avoid extraneous errors in the console. |
| 112 if (!this.signed_in && !this._signingIn){ | 125 if (!this.signed_in && !this._signingIn){ |
| 113 if (('gapi' in window) && ('auth2' in window.gapi)) { | 126 if (('gapi' in window) && ('auth2' in window.gapi)) { |
| 114 var user = gapi.auth2.getAuthInstance().currentUser.get(); | 127 this.set('user', null); |
| 115 if (user && user.getAuthResponse().access_token) { | 128 this.set('user', gapi.auth2.getAuthInstance().currentUser.get()); |
| 129 if (this.user && this.user.getAuthResponse().access_token) { | |
| 116 // User is already logged in, can use the access_token. | 130 // User is already logged in, can use the access_token. |
| 117 this._onSignin(); | 131 this._onSignin(); |
| 118 } else { | 132 } else if (!this.initialized) { |
| 119 window.setTimeout(this.ready.bind(this), 50); | 133 window.setTimeout(this.ready.bind(this), 50); |
| 120 } | 134 } |
| 121 } else { | 135 } else { |
| 122 window.setTimeout(this.ready.bind(this), 50); | 136 window.setTimeout(this.ready.bind(this), 50); |
| 123 } | 137 } |
| 124 } | 138 } |
| 125 }.bind(this), 50); | 139 }.bind(this), 50); |
| 126 }, | 140 }, |
| 127 | 141 |
| 128 _onSignin: function() { | 142 _onSignin: function() { |
| 129 this._signingIn = true; | 143 this._signingIn = true; |
| 130 var user = gapi.auth2.getAuthInstance().currentUser.get(); | 144 this.set('user', null); |
| 131 var profile = { email: user.getBasicProfile().getEmail(), | 145 this.set('user', gapi.auth2.getAuthInstance().currentUser.get()); |
| 132 imageUrl: user.getBasicProfile().getImageUrl() }; | 146 var profile = { email: this.user.getBasicProfile().getEmail(), |
| 133 this.set('profile', {}); | 147 imageUrl: this.user.getBasicProfile().getImageUrl() }; |
| 148 this.set('profile', null); | |
| 134 this.set('profile', profile); | 149 this.set('profile', profile); |
| 135 this.set('auth_response', user.getAuthResponse()); | 150 this.set('auth_response', this.user.getAuthResponse()); |
| 136 this.signed_in = true; | 151 this.signed_in = true; |
| 137 // The credential will expire after a while (usually an hour) | 152 // The credential will expire after a while (usually an hour) |
| 138 // so we need to reload it. | 153 // so we need to reload it. |
| 139 this.async(function(){ | 154 this.async(function(){ |
| 140 console.log("reloading credentials"); | 155 console.log("reloading credentials"); |
| 141 user.reloadAuthResponse(); | 156 this.user.reloadAuthResponse(); |
| 142 this._onSignin(); | 157 this._onSignin(); |
| 143 }, this.auth_response.expires_in * 1000); // convert seconds to ms | 158 }, this.auth_response.expires_in * 1000); // convert seconds to ms |
| 144 this._signingIn = false; | 159 this._signingIn = false; |
| 145 }, | 160 }, |
| 146 | 161 |
| 147 _onSignout: function(e) { | 162 _onSignout: function(e) { |
| 148 this.signed_in = false; | 163 this.signed_in = false; |
| 149 this.set('profile', null); | 164 this.set('profile', null); |
| 150 }, | 165 }, |
| 151 | 166 |
| 152 _makeHeader: function() { | 167 _makeHeader: function() { |
| 153 if (!this.auth_response) { | 168 if (!this.auth_response) { |
| 154 this.set('auth_headers', {}); | 169 this.set('auth_headers', null); |
| 155 } | 170 } |
| 156 this.set('auth_headers', | 171 this.set('auth_headers', |
| 157 { | 172 { |
| 158 "authorization": this.auth_response.token_type + " " + | 173 "authorization": this.auth_response.token_type + " " + |
| 159 this.auth_response.access_token | 174 this.auth_response.access_token |
| 160 }); | 175 }); |
| 161 }, | 176 }, |
| 162 | 177 |
| 163 signIn: function() { | 178 signIn: function() { |
| 164 this.$.aware.signIn(); | 179 this.$.aware.signIn(); |
| 165 }, | 180 }, |
| 166 | 181 |
| 167 signOut: function() { | 182 signOut: function() { |
| 168 this.$.aware.signOut(); | 183 this.$.aware.signOut(); |
| 169 window.location.reload(); | 184 window.location.reload(); |
| 170 } | 185 } |
| 171 }); | 186 }); |
| 172 </script> | 187 </script> |
| 173 </dom-module> | 188 </dom-module> |
| OLD | NEW |