Skip to content

Commit db93196

Browse files
committed
Completely untested code to pull the username that issued an access token
1 parent d3c4f6f commit db93196

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

bin/git-credential-atom.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,29 @@ async function fromKeytar(query) {
198198
log(`reading service "atom-github" and account "${githubHost}"`);
199199
const githubPassword = await strategy.getPassword('atom-github', githubHost);
200200
if (githubPassword !== UNAUTHENTICATED) {
201-
if (!query.username) {
202-
// TODO find username from GitHub API
201+
try {
202+
if (!query.username) {
203+
const apiUrl = githubHost === 'https://api.github.com' ? `${githubHost}/graphql` : `${githubHost}/api/v3/graphql`;
204+
const response = await fetch(apiUrl, {
205+
method: 'POST',
206+
headers: {
207+
'content-type': 'application/json',
208+
'Authorization': `bearer ${githubPassword}`,
209+
'Accept': 'application/vnd.github.graphql-profiling+json',
210+
},
211+
body: JSON.stringify({
212+
query: 'query { viewer { login } }',
213+
}),
214+
});
215+
216+
query.username = response.json().data.viewer.login;
217+
}
218+
} catch (e) {
219+
log(`unable to acquire username from token: ${e.stack}`);
203220
throw new Error('token found in keychain, but no username');
204-
} else {
205-
password = githubPassword;
206221
}
222+
223+
password = githubPassword;
207224
}
208225

209226
if (password !== UNAUTHENTICATED) {

0 commit comments

Comments
 (0)