Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/includes/GithubProvider.php
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2020-02-01 09:05:48 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2020-02-01 09:05:48 +0100
commit6854cb3f4d8219cf1829e32122eb2502a916eae9 (patch)
tree350feb504587d932e02837a1442b059759927646 /includes/GithubProvider.php
initial checkin
Diffstat (limited to 'includes/GithubProvider.php')
-rw-r--r--includes/GithubProvider.php60
1 files changed, 60 insertions, 0 deletions
diff --git a/includes/GithubProvider.php b/includes/GithubProvider.php
new file mode 100644
index 0000000..5639383
--- /dev/null
+++ b/includes/GithubProvider.php
@@ -0,0 +1,60 @@
+<?php
+
+use League\OAuth2\Client\Provider\Github;
+use League\OAuth2\Client\Token\AccessToken as AccessToken;
+
+/**
+ * A workaround for fetching the users email address if the user does not have a
+ * public email address.
+ */
+class GithubProvider extends Github
+{
+ public function userDetails($response, AccessToken $token)
+ {
+ $user = parent::userDetails($response, $token);
+
+ // Fetch the primary email address
+ if (!$user->email) {
+ $emails = $this->fetchUserEmails($token);
+ $emails = json_decode($emails);
+ $email = null;
+
+ foreach ($emails as $email) {
+ if ($email->primary) {
+ $email = $email->email;
+ break;
+ }
+ }
+
+ $user->email = $email;
+ }
+
+ return $user;
+ }
+
+ protected function fetchUserEmails(AccessToken $token)
+ {
+ $url = "https://api.github.com/user/emails?access_token={$token}";
+
+ try {
+
+ $client = $this->getHttpClient();
+ $client->setBaseUrl($url);
+
+ if ($this->headers) {
+ $client->setDefaultOption('headers', $this->headers);
+ }
+
+ $request = $client->get()->send();
+ $response = $request->getBody();
+
+ } catch (BadResponseException $e) {
+ // @codeCoverageIgnoreStart
+ $raw_response = explode("\n", $e->getResponse());
+ throw new IDPException(end($raw_response));
+ // @codeCoverageIgnoreEnd
+ }
+
+ return $response;
+ }
+} \ No newline at end of file