Improve GitHub oauth email discovery
This commit is contained in:
parent
e84372aae2
commit
70b5281a0b
2 changed files with 21 additions and 3 deletions
|
@ -566,6 +566,10 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
|
|||
$name = $oauth2->getUserName($accessToken);
|
||||
$email = $oauth2->getUserEmail($accessToken);
|
||||
|
||||
if(empty($email)) {
|
||||
throw new Exception(Exception::USER_UNAUTHORIZED, 'OAuth provider failed to return email.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Is verified is not used yet, since we don't know after an accout is created anymore if it was verified or not.
|
||||
*/
|
||||
|
|
|
@ -183,13 +183,27 @@ class Github extends OAuth2
|
|||
$emails = $this->request('GET', 'https://api.github.com/user/emails', ['Authorization: token ' . \urlencode($accessToken)]);
|
||||
|
||||
$emails = \json_decode($emails, true);
|
||||
|
||||
$verifiedEmail = null;
|
||||
$primaryEmail = null;
|
||||
|
||||
foreach ($emails as $email) {
|
||||
if (isset($email['verified']) && $email['verified'] === true) {
|
||||
$this->user['email'] = $email['email'];
|
||||
$this->user['verified'] = $email['verified'];
|
||||
break;
|
||||
$verifiedEmail = $email;
|
||||
|
||||
if (isset($email['primary']) && $email['primary'] === true) {
|
||||
$primaryEmail = $email;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($primaryEmail)) {
|
||||
$this->user['email'] = $primaryEmail['email'];
|
||||
$this->user['verified'] = $primaryEmail['verified'];
|
||||
} else if(!empty($verifiedEmail)) {
|
||||
$this->user['email'] = $verifiedEmail['email'];
|
||||
$this->user['verified'] = $verifiedEmail['verified'];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->user;
|
||||
|
|
Loading…
Reference in a new issue