From 6a2c29de5e89e6e000f5bc3b39785f655068b80a Mon Sep 17 00:00:00 2001 From: Cameron Date: Fri, 23 Jan 2015 21:14:28 -0800 Subject: [PATCH] Corrected filename --- .../hybridauth/Hybrid/Providers/Github.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 e107_handlers/hybridauth/Hybrid/Providers/Github.php diff --git a/e107_handlers/hybridauth/Hybrid/Providers/Github.php b/e107_handlers/hybridauth/Hybrid/Providers/Github.php new file mode 100644 index 000000000..00b35f2a9 --- /dev/null +++ b/e107_handlers/hybridauth/Hybrid/Providers/Github.php @@ -0,0 +1,80 @@ + public read-only access (includes public user profile info, public repo info, and gists). + public $scope = ""; + + /** + * IDp wrappers initializer + */ + function initialize() + { + parent::initialize(); + + // Provider api end-points + $this->api->api_base_url = "https://api.github.com/"; + $this->api->authorize_url = "https://github.com/login/oauth/authorize"; + $this->api->token_url = "https://github.com/login/oauth/access_token"; + } + + /** + * load the user profile from the IDp api client + */ + function getUserProfile() + { + $data = $this->api->api( "user" ); + + if ( ! isset( $data->id ) ){ + throw new Exception( "User profile request failed! {$this->providerId} returned an invalid response.", 6 ); + } + + $this->user->profile->identifier = @ $data->id; + $this->user->profile->displayName = @ $data->name; + $this->user->profile->description = @ $data->bio; + $this->user->profile->photoURL = @ $data->avatar_url; + $this->user->profile->profileURL = @ $data->html_url; + $this->user->profile->email = @ $data->email; + $this->user->profile->webSiteURL = @ $data->blog; + $this->user->profile->region = @ $data->location; + + if( empty($this->user->profile->displayName) ){ + $this->user->profile->displayName = @ $data->login; + } + + // request user emails from github api + if( empty($data->email) ){ + try{ + $emails = $this->api->api("user/emails"); + + // fail gracefully, and let apps collect the email if not present + if (is_array($emails)) { + foreach ($emails as $email) { + if ($email instanceof stdClass + && property_exists('primary', $email) + && true === $email->primary + && property_exists('email', $email) + ) { + $this->user->profile->email = $email->email; + break; + } + } + } + } + catch( GithubApiException $e ){ + throw new Exception( "User email request failed! {$this->providerId} returned an error: $e", 6 ); + } + } + + return $this->user->profile; + } +}