Skip to content

Commit

Permalink
fix(connector): fix GitHub connector GET /emails forbidden error
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed May 28, 2024
1 parent 07fdd85 commit f3a47a8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-deers-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@logto/connector-github": patch
---

fix GET /users/emails forbidden error
27 changes: 27 additions & 0 deletions packages/connectors/connector-github/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,33 @@ describe('getUserInfo', () => {
});
});

it('should fallback to empty array when can not access to GET /users/emails endpoint', async () => {
nock(userInfoEndpoint).get('').reply(200, {
id: 1,
avatar_url: 'https://github.com/images/error/octocat_happy.gif',
name: 'monalisa octocat',
foo: 'bar',
});
nock(userEmailsEndpoint).get('').reply(403, []);
const connector = await createConnector({ getConfig });
const socialUserInfo = await connector.getUserInfo({ code: 'code' }, vi.fn());
expect(socialUserInfo).toStrictEqual({
id: '1',
avatar: 'https://github.com/images/error/octocat_happy.gif',
name: 'monalisa octocat',
email: undefined,
rawData: {
userInfo: {
id: 1,
avatar_url: 'https://github.com/images/error/octocat_happy.gif',
name: 'monalisa octocat',
foo: 'bar',
},
userEmails: [],
},
});
});

it('should convert `null` to `undefined` in SocialUserInfo', async () => {
nock(userInfoEndpoint).get('').reply(200, {
id: 1,
Expand Down
13 changes: 10 additions & 3 deletions packages/connectors/connector-github/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, conditional } from '@silverhand/essentials';
import { assert, conditional, trySafe } from '@silverhand/essentials';

import {
ConnectorError,
Expand Down Expand Up @@ -124,9 +124,16 @@ const getUserInfo =
});

try {
const [userInfo, userEmails] = await Promise.all([
/**
* If user(s) is using GitHub Apps (instead of OAuth Apps), they can customize
* "Account permissions" and restrict the "email addresses" visibility, and GitHub
* hence throws error instead of returning an empty array.
*
* We try catch the error and return an empty array instead.
*/
const [userInfo, userEmails = []] = await Promise.all([
authedApi.get(userInfoEndpoint).json(),
authedApi.get(userEmailsEndpoint).json(),
trySafe(authedApi.get(userEmailsEndpoint).json()),
]);

const userInfoResult = userInfoResponseGuard.safeParse(userInfo);
Expand Down

0 comments on commit f3a47a8

Please sign in to comment.