Skip to content

Commit 0e99155

Browse files
committed
Replace People API user lookup with OIDC userinfo endpoint
Joe says: OK I'm going to count this as a win for Claude. It has always bothered me that we added a whole OAuth scope just so we could say “programming as joe.politz” in the bottom left, when *we just logged the user in with that email address*. I just never found the API to do it. Turns out this does come for free with OpenID Connect. This change removes an entire scope *and* makes it so we can disable an API in the cloud console. Claude says: The four call sites that fetched the user's email via People API (people/v1, api.people.get) only ever used the email address — the display name was always overwritten. Switch to the OIDC userinfo endpoint, which returns the email with just the `email` scope. - Drop `profile` from DEFAULT_OAUTH_SCOPES and FULL_OAUTH_SCOPES - Replace gwrap.load({name: 'people'}) + api.people.get with a fetch to https://openidconnect.googleapis.com/v1/userinfo - Update StudentDashboard to read `.email` instead of `.emailAddresses[0].value` Consent screen no longer says "Email and profile photo", and the People API no longer needs to be enabled in the Cloud project.
1 parent 0f43b2c commit 0e99155

5 files changed

Lines changed: 19 additions & 29 deletions

File tree

src/google-auth.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ var OAuth2 = gapi.auth.OAuth2;
44

55
var DEFAULT_OAUTH_SCOPES = [
66
"email",
7-
"profile",
87
"https://www.googleapis.com/auth/drive.file",
98
"https://www.googleapis.com/auth/drive.install",
109
];
1110

1211
var FULL_OAUTH_SCOPES = [
1312
"email",
14-
"profile",
1513
"https://www.googleapis.com/auth/spreadsheets",
1614
"https://www.googleapis.com/auth/drive.file",
1715
"https://www.googleapis.com/auth/drive",

src/web/js/beforeBlocks.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,13 @@ $(function() {
305305
};
306306

307307
function setUsername(target) {
308-
return gwrap.load({name: 'people',
309-
version: 'v1',
310-
}).then((api) => {
311-
api.people.get({ resourceName: "people/me", personFields: "names,emailAddresses" }).then(function(user) {
312-
var name = user.names && user.names[0] ? user.names[0].displayName : undefined;
313-
if (user.emailAddresses && user.emailAddresses[0] && user.emailAddresses[0].value) {
314-
name = user.emailAddresses[0].value;
315-
}
316-
target.text(name);
317-
});
308+
var token = window.gapi.auth.getToken().access_token;
309+
return fetch('https://openidconnect.googleapis.com/v1/userinfo', {
310+
headers: { Authorization: 'Bearer ' + token }
311+
}).then(function(resp) {
312+
return resp.json();
313+
}).then(function(info) {
314+
target.text(info.email);
318315
});
319316
}
320317

src/web/js/beforePyret.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,16 +457,13 @@ $(function() {
457457
};
458458

459459
function setUsername(target) {
460-
return gwrap.load({name: 'people',
461-
version: 'v1',
462-
}).then((api) => {
463-
api.people.get({ resourceName: "people/me", personFields: "names,emailAddresses" }).then(function(user) {
464-
var name = user.names && user.names[0] ? user.names[0].displayName : undefined;
465-
if (user.emailAddresses && user.emailAddresses[0] && user.emailAddresses[0].value) {
466-
name = user.emailAddresses[0].value;
467-
}
468-
target.text(name);
469-
});
460+
var token = window.gapi.auth.getToken().access_token;
461+
return fetch('https://openidconnect.googleapis.com/v1/userinfo', {
462+
headers: { Authorization: 'Bearer ' + token }
463+
}).then(function(resp) {
464+
return resp.json();
465+
}).then(function(info) {
466+
target.text(info.email);
470467
});
471468
}
472469

src/web/js/dashboard/GoogleAPI.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,10 @@ class GoogleAPI {
151151
}
152152

153153
getUsername = () => {
154-
return gwrap.load({name: 'people',
155-
version: 'v1',
156-
}).then((api) => {
157-
console.log("Api: ", api);
158-
return api.people.get({ resourceName: "people/me", personFields: "names,emailAddresses" });
159-
});
154+
var token = window.gapi.auth.getToken().access_token;
155+
return fetch('https://openidconnect.googleapis.com/v1/userinfo', {
156+
headers: { Authorization: 'Bearer ' + token }
157+
}).then((resp) => resp.json());
160158
}
161159
}
162160

src/web/js/dashboard/StudentDashboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class StudentDashboard extends Component {
3131
this.setState({signedIn: SIGNED_IN});
3232
this.updateRecentFiles();
3333
this.api.getUsername().then((userInfo) => {
34-
this.setState({ userName: userInfo.emailAddresses[0].value });
34+
this.setState({ userName: userInfo.email });
3535
});
3636
}
3737
else {

0 commit comments

Comments
 (0)