-
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connector): can access all user email even if no public email is…
… set (#5737)
- Loading branch information
Showing
7 changed files
with
179 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@logto/connector-github": minor | ||
--- | ||
|
||
fetch GitHub account's private email address list and pick the verified primary email as a fallback | ||
|
||
- Add `user:email` as part of default scope to fetch GitHub account's private email address list | ||
- Pick the verified primary email among private email address list as a fallback if the user does not set a public email for GitHub account |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import nock from 'nock'; | ||
|
||
import { ConnectorError, ConnectorErrorCodes } from '@logto/connector-kit'; | ||
import qs from 'query-string'; | ||
|
||
import { accessTokenEndpoint, authorizationEndpoint, userInfoEndpoint } from './constant.js'; | ||
import { | ||
accessTokenEndpoint, | ||
authorizationEndpoint, | ||
userEmailsEndpoint, | ||
userInfoEndpoint, | ||
} from './constant.js'; | ||
import createConnector, { getAccessToken } from './index.js'; | ||
import { mockedConfig } from './mock.js'; | ||
|
||
|
@@ -28,7 +32,7 @@ describe('getAuthorizationUri', () => { | |
vi.fn() | ||
); | ||
expect(authorizationUri).toEqual( | ||
`${authorizationEndpoint}?client_id=%3Cclient-id%3E&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&state=some_state&scope=read%3Auser` | ||
`${authorizationEndpoint}?client_id=%3Cclient-id%3E&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&state=some_state&scope=read%3Auser+user%3Aemail` | ||
); | ||
}); | ||
}); | ||
|
@@ -40,24 +44,19 @@ describe('getAccessToken', () => { | |
}); | ||
|
||
it('should get an accessToken by exchanging with code', async () => { | ||
nock(accessTokenEndpoint) | ||
.post('') | ||
.reply( | ||
200, | ||
qs.stringify({ | ||
access_token: 'access_token', | ||
scope: 'scope', | ||
token_type: 'token_type', | ||
}) | ||
); | ||
nock(accessTokenEndpoint).post('').reply(200, { | ||
access_token: 'access_token', | ||
scope: 'scope', | ||
token_type: 'token_type', | ||
}); | ||
const { accessToken } = await getAccessToken(mockedConfig, { code: 'code' }); | ||
expect(accessToken).toEqual('access_token'); | ||
}); | ||
|
||
it('throws SocialAuthCodeInvalid error if accessToken not found in response', async () => { | ||
nock(accessTokenEndpoint) | ||
.post('') | ||
.reply(200, qs.stringify({ access_token: '', scope: 'scope', token_type: 'token_type' })); | ||
.reply(200, { access_token: '', scope: 'scope', token_type: 'token_type' }); | ||
await expect(getAccessToken(mockedConfig, { code: 'code' })).rejects.toStrictEqual( | ||
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid) | ||
); | ||
|
@@ -66,16 +65,11 @@ describe('getAccessToken', () => { | |
|
||
describe('getUserInfo', () => { | ||
beforeEach(() => { | ||
nock(accessTokenEndpoint) | ||
.post('') | ||
.reply( | ||
200, | ||
qs.stringify({ | ||
access_token: 'access_token', | ||
scope: 'scope', | ||
token_type: 'token_type', | ||
}) | ||
); | ||
nock(accessTokenEndpoint).post('').query(true).reply(200, { | ||
access_token: 'access_token', | ||
scope: 'scope', | ||
token_type: 'token_type', | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
|
@@ -91,6 +85,7 @@ describe('getUserInfo', () => { | |
email: '[email protected]', | ||
foo: 'bar', | ||
}); | ||
nock(userEmailsEndpoint).get('').reply(200, []); | ||
const connector = await createConnector({ getConfig }); | ||
const socialUserInfo = await connector.getUserInfo({ code: 'code' }, vi.fn()); | ||
expect(socialUserInfo).toStrictEqual({ | ||
|
@@ -99,11 +94,70 @@ describe('getUserInfo', () => { | |
name: 'monalisa octocat', | ||
email: '[email protected]', | ||
rawData: { | ||
id: 1, | ||
avatar_url: 'https://github.com/images/error/octocat_happy.gif', | ||
name: 'monalisa octocat', | ||
email: '[email protected]', | ||
foo: 'bar', | ||
userInfo: { | ||
id: 1, | ||
avatar_url: 'https://github.com/images/error/octocat_happy.gif', | ||
name: 'monalisa octocat', | ||
email: '[email protected]', | ||
foo: 'bar', | ||
}, | ||
userEmails: [], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should fallback to verified primary email if not public is available', async () => { | ||
nock(userInfoEndpoint).get('').reply(200, { | ||
id: 1, | ||
avatar_url: 'https://github.com/images/error/octocat_happy.gif', | ||
name: 'monalisa octocat', | ||
email: undefined, | ||
foo: 'bar', | ||
}); | ||
nock(userEmailsEndpoint) | ||
.get('') | ||
.reply(200, [ | ||
{ | ||
email: '[email protected]', | ||
verified: true, | ||
primary: true, | ||
visibility: 'public', | ||
}, | ||
{ | ||
email: '[email protected]', | ||
verified: true, | ||
primary: false, | ||
visibility: null, | ||
}, | ||
]); | ||
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: '[email protected]', | ||
rawData: { | ||
userInfo: { | ||
id: 1, | ||
avatar_url: 'https://github.com/images/error/octocat_happy.gif', | ||
name: 'monalisa octocat', | ||
foo: 'bar', | ||
}, | ||
userEmails: [ | ||
{ | ||
email: '[email protected]', | ||
verified: true, | ||
primary: true, | ||
visibility: 'public', | ||
}, | ||
{ | ||
email: '[email protected]', | ||
verified: true, | ||
primary: false, | ||
visibility: null, | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
@@ -115,15 +169,14 @@ describe('getUserInfo', () => { | |
name: null, | ||
email: null, | ||
}); | ||
nock(userEmailsEndpoint).get('').reply(200, []); | ||
const connector = await createConnector({ getConfig }); | ||
const socialUserInfo = await connector.getUserInfo({ code: 'code' }, vi.fn()); | ||
expect(socialUserInfo).toMatchObject({ | ||
id: '1', | ||
rawData: { | ||
id: 1, | ||
avatar_url: null, | ||
name: null, | ||
email: null, | ||
userInfo: { id: 1, avatar_url: null, name: null, email: null }, | ||
userEmails: [], | ||
}, | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.