thephpleague / oauth2-github

GitHub Provider for the OAuth 2.0 Client
MIT License
106 stars 29 forks source link

Unable to fetch email user #19

Closed Yoann-TYT closed 1 year ago

Yoann-TYT commented 2 years ago

Hi everybody :)

I found a bug. If we add user:email scop, we get null for email attribute.

I checked on HWIOAuthBundle, and email is fetched using /user/emails endpoint.

I don't know how to fix this issue because the request is done in "oauth-client".

https://github.com/hwi/HWIOAuthBundle/blob/3dfd2c80835b8c3fe0d1dd666bc1bff6391017bc/src/OAuth/ResourceOwner/GitHubResourceOwner.php#L48

Thanks !

Yoann-TYT commented 2 years ago

We can simply avoid this issue by fetching emails in our authenticator.

$githubUser = $client->fetchUserFromToken($accessToken)->toArray();
$emails = $this->client->request(
    'GET',
    'https://api.github.com/user/emails',
    ['auth_bearer' => $accessToken->getToken()]
)->toArray();
$githubUser['email'] =
    array_values(
        array_filter($emails, fn ($email) => true === $email['primary'])
    )[0]['email'];
shadowhand commented 1 year ago

Fixed by #20.