diff --git a/src/AdamWathan/EloquentOAuth/OAuthIdentity.php b/src/AdamWathan/EloquentOAuth/OAuthIdentity.php index ea628ae..d4924fc 100644 --- a/src/AdamWathan/EloquentOAuth/OAuthIdentity.php +++ b/src/AdamWathan/EloquentOAuth/OAuthIdentity.php @@ -5,15 +5,15 @@ class OAuthIdentity extends Eloquent { - protected static $configuredTable = 'oauth_identities'; + protected static $configuredTable = 'oauth_identities'; - public static function configureTable($table) - { - static::$configuredTable = $table; - } + public static function configureTable($table) + { + static::$configuredTable = $table; + } - public function getTable() - { - return static::$configuredTable; - } + public function getTable() + { + return static::$configuredTable; + } } diff --git a/src/AdamWathan/EloquentOAuth/ProviderUserDetails.php b/src/AdamWathan/EloquentOAuth/ProviderUserDetails.php index 7317e30..5f13a12 100644 --- a/src/AdamWathan/EloquentOAuth/ProviderUserDetails.php +++ b/src/AdamWathan/EloquentOAuth/ProviderUserDetails.php @@ -11,30 +11,30 @@ */ class ProviderUserDetails { - protected $details = array( - 'accessToken' => null, - 'userId' => null, - 'nickname' => null, - 'firstName' => null, - 'lastName' => null, - 'email' => null, - 'imageUrl' => null, - ); + protected $details = array( + 'accessToken' => null, + 'userId' => null, + 'nickname' => null, + 'firstName' => null, + 'lastName' => null, + 'email' => null, + 'imageUrl' => null, + ); - public function __construct($details) - { - $this->addDetails($details); - } + public function __construct($details) + { + $this->addDetails($details); + } - protected function addDetails($details = array()) - { - foreach ($details as $key => $value) { - $this->details[$key] = $value; - } - } + protected function addDetails($details = array()) + { + foreach ($details as $key => $value) { + $this->details[$key] = $value; + } + } - public function __get($key) - { - return isset($this->details[$key]) ? $this->details[$key] : null; - } + public function __get($key) + { + return isset($this->details[$key]) ? $this->details[$key] : null; + } } diff --git a/src/AdamWathan/EloquentOAuth/Providers/FacebookProvider.php b/src/AdamWathan/EloquentOAuth/Providers/FacebookProvider.php index 5a65478..98c1bc5 100644 --- a/src/AdamWathan/EloquentOAuth/Providers/FacebookProvider.php +++ b/src/AdamWathan/EloquentOAuth/Providers/FacebookProvider.php @@ -4,69 +4,69 @@ class FacebookProvider extends Provider { - protected $authorizeUrl = "https://www.facebook.com/dialog/oauth"; - protected $accessTokenUrl = "https://graph.facebook.com/oauth/access_token"; - protected $userDataUrl = "https://graph.facebook.com/me"; - protected $scope = array( + protected $authorizeUrl = "https://www.facebook.com/dialog/oauth"; + protected $accessTokenUrl = "https://graph.facebook.com/oauth/access_token"; + protected $userDataUrl = "https://graph.facebook.com/me"; + protected $scope = array( 'email', - ); + ); - protected function getAuthorizeUrl() - { - return $this->authorizeUrl; - } + protected function getAuthorizeUrl() + { + return $this->authorizeUrl; + } - protected function getAccessTokenBaseUrl() - { - return $this->accessTokenUrl; - } + protected function getAccessTokenBaseUrl() + { + return $this->accessTokenUrl; + } - protected function getUserDataUrl() - { - return $this->userDataUrl; - } + protected function getUserDataUrl() + { + return $this->userDataUrl; + } - protected function parseTokenResponse($response) - { - parse_str($response); - if (! isset($access_token)) { - throw new InvalidAuthorizationCodeException; - } - return $access_token; - } + protected function parseTokenResponse($response) + { + parse_str($response); + if (! isset($access_token)) { + throw new InvalidAuthorizationCodeException; + } + return $access_token; + } - protected function parseUserDataResponse($response) - { - return json_decode($response, true); - } + protected function parseUserDataResponse($response) + { + return json_decode($response, true); + } - protected function userId() - { - return $this->getProviderUserData('id'); - } + protected function userId() + { + return $this->getProviderUserData('id'); + } - protected function nickname() - { - return $this->getProviderUserData('username'); - } + protected function nickname() + { + return $this->getProviderUserData('username'); + } - protected function firstName() - { - return $this->getProviderUserData('first_name'); - } + protected function firstName() + { + return $this->getProviderUserData('first_name'); + } - protected function lastName() - { - return $this->getProviderUserData('last_name'); - } + protected function lastName() + { + return $this->getProviderUserData('last_name'); + } - protected function imageUrl() - { - return 'https://graph.facebook.com/'.$this->userId().'/picture'; - } + protected function imageUrl() + { + return 'https://graph.facebook.com/'.$this->userId().'/picture'; + } - protected function email() - { - return $this->getProviderUserData('email'); - } + protected function email() + { + return $this->getProviderUserData('email'); + } } diff --git a/src/AdamWathan/EloquentOAuth/Providers/GitHubProvider.php b/src/AdamWathan/EloquentOAuth/Providers/GitHubProvider.php index 6da40d5..6e00d92 100644 --- a/src/AdamWathan/EloquentOAuth/Providers/GitHubProvider.php +++ b/src/AdamWathan/EloquentOAuth/Providers/GitHubProvider.php @@ -4,114 +4,114 @@ class GitHubProvider extends Provider { - protected $authorizeUrl = "https://github.com/login/oauth/authorize"; - protected $accessTokenUrl = "https://github.com/login/oauth/access_token"; - protected $userDataUrl = "https://api.github.com/user"; - protected $scope = array( + protected $authorizeUrl = "https://github.com/login/oauth/authorize"; + protected $accessTokenUrl = "https://github.com/login/oauth/access_token"; + protected $userDataUrl = "https://api.github.com/user"; + protected $scope = array( 'user:email', - ); - - protected $headers = array( - 'authorize' => array(), - 'access_token' => array( - 'Accept' => 'application/json' - ), - 'user_details' => array( - 'Accept' => 'application/vnd.github.v3' - ), - ); - - protected function getAuthorizeUrl() - { - return $this->authorizeUrl; - } - - protected function getAccessTokenBaseUrl() - { - return $this->accessTokenUrl; - } - - protected function getUserDataUrl() - { - return $this->userDataUrl; - } - - protected function parseTokenResponse($response) - { - return $this->parseJsonTokenResponse($response); - } - - protected function requestUserData() - { - $userData = parent::requestUserData(); - $userData['email'] = $this->requestEmail(); - return $userData; - } - - protected function requestEmail() - { - $url = $this->getEmailUrl(); - $emails = $this->getJson($url, $this->headers['user_details']); - return $this->getPrimaryEmail($emails); - } - - protected function getEmailUrl() - { - $url = $this->getUserDataUrl() .'/emails'; - $url .= "?access_token=".$this->accessToken; - return $url; - } - - public function getJson($url, $headers) - { - $request = $this->httpClient->get($url, $headers); - $response = $request->send(); - return $response->json(); - } - - protected function getPrimaryEmail($emails) - { - foreach ($emails as $email) { - if ($email['primary']) { - return $email['email']; - } - } - return $emails[0]['email']; - } - - protected function parseUserDataResponse($response) - { - $data = json_decode($response, true); - return $data; - } - - protected function userId() - { - return $this->getProviderUserData('id'); - } - - protected function nickname() - { - return $this->getProviderUserData('login'); - } - - protected function firstName() - { - return strstr($this->getProviderUserData('name'), ' ', true); - } - - protected function lastName() - { - return substr(strstr($this->getProviderUserData('name'), ' '), 1); - } - - protected function imageUrl() - { - return $this->getProviderUserData('avatar_url'); - } - - protected function email() - { - return $this->getProviderUserData('email'); - } + ); + + protected $headers = array( + 'authorize' => array(), + 'access_token' => array( + 'Accept' => 'application/json' + ), + 'user_details' => array( + 'Accept' => 'application/vnd.github.v3' + ), + ); + + protected function getAuthorizeUrl() + { + return $this->authorizeUrl; + } + + protected function getAccessTokenBaseUrl() + { + return $this->accessTokenUrl; + } + + protected function getUserDataUrl() + { + return $this->userDataUrl; + } + + protected function parseTokenResponse($response) + { + return $this->parseJsonTokenResponse($response); + } + + protected function requestUserData() + { + $userData = parent::requestUserData(); + $userData['email'] = $this->requestEmail(); + return $userData; + } + + protected function requestEmail() + { + $url = $this->getEmailUrl(); + $emails = $this->getJson($url, $this->headers['user_details']); + return $this->getPrimaryEmail($emails); + } + + protected function getEmailUrl() + { + $url = $this->getUserDataUrl() .'/emails'; + $url .= "?access_token=".$this->accessToken; + return $url; + } + + public function getJson($url, $headers) + { + $request = $this->httpClient->get($url, $headers); + $response = $request->send(); + return $response->json(); + } + + protected function getPrimaryEmail($emails) + { + foreach ($emails as $email) { + if ($email['primary']) { + return $email['email']; + } + } + return $emails[0]['email']; + } + + protected function parseUserDataResponse($response) + { + $data = json_decode($response, true); + return $data; + } + + protected function userId() + { + return $this->getProviderUserData('id'); + } + + protected function nickname() + { + return $this->getProviderUserData('login'); + } + + protected function firstName() + { + return strstr($this->getProviderUserData('name'), ' ', true); + } + + protected function lastName() + { + return substr(strstr($this->getProviderUserData('name'), ' '), 1); + } + + protected function imageUrl() + { + return $this->getProviderUserData('avatar_url'); + } + + protected function email() + { + return $this->getProviderUserData('email'); + } } diff --git a/src/AdamWathan/EloquentOAuth/Providers/GoogleProvider.php b/src/AdamWathan/EloquentOAuth/Providers/GoogleProvider.php index 172a229..49b6261 100644 --- a/src/AdamWathan/EloquentOAuth/Providers/GoogleProvider.php +++ b/src/AdamWathan/EloquentOAuth/Providers/GoogleProvider.php @@ -4,79 +4,79 @@ class GoogleProvider extends Provider { - protected $authorizeUrl = "https://accounts.google.com/o/oauth2/auth"; - protected $accessTokenUrl = "https://accounts.google.com/o/oauth2/token"; - protected $userDataUrl = "https://www.googleapis.com/userinfo/v2/me"; - protected $scope = array( + protected $authorizeUrl = "https://accounts.google.com/o/oauth2/auth"; + protected $accessTokenUrl = "https://accounts.google.com/o/oauth2/token"; + protected $userDataUrl = "https://www.googleapis.com/userinfo/v2/me"; + protected $scope = array( 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email', ); - protected $headers = array( - 'authorize' => array(), - 'access_token' => array( - 'Content-Type' => 'application/x-www-form-urlencoded' - ), - 'user_details' => array(), - ); + protected $headers = array( + 'authorize' => array(), + 'access_token' => array( + 'Content-Type' => 'application/x-www-form-urlencoded' + ), + 'user_details' => array(), + ); - protected function compileScopes() - { - return implode(' ', $this->scope); - } + protected function compileScopes() + { + return implode(' ', $this->scope); + } - protected function getAuthorizeUrl() - { - return $this->authorizeUrl; - } + protected function getAuthorizeUrl() + { + return $this->authorizeUrl; + } - protected function getAccessTokenBaseUrl() - { - return $this->accessTokenUrl; - } + protected function getAccessTokenBaseUrl() + { + return $this->accessTokenUrl; + } - protected function getUserDataUrl() - { - return $this->userDataUrl; - } + protected function getUserDataUrl() + { + return $this->userDataUrl; + } - protected function parseTokenResponse($response) - { - return $this->parseJsonTokenResponse($response); - } + protected function parseTokenResponse($response) + { + return $this->parseJsonTokenResponse($response); + } - protected function parseUserDataResponse($response) - { - return json_decode($response, true); - } + protected function parseUserDataResponse($response) + { + return json_decode($response, true); + } - protected function userId() - { - return $this->getProviderUserData('id'); - } + protected function userId() + { + return $this->getProviderUserData('id'); + } - protected function nickname() - { - return $this->getProviderUserData('email'); - } + protected function nickname() + { + return $this->getProviderUserData('email'); + } - protected function firstName() - { - return $this->getProviderUserData('given_name'); - } + protected function firstName() + { + return $this->getProviderUserData('given_name'); + } - protected function lastName() - { - return $this->getProviderUserData('family_name'); - } + protected function lastName() + { + return $this->getProviderUserData('family_name'); + } - protected function imageUrl() - { - return $this->getProviderUserData('picture'); - } + protected function imageUrl() + { + return $this->getProviderUserData('picture'); + } - protected function email() - { - return $this->getProviderUserData('email'); - } + protected function email() + { + return $this->getProviderUserData('email'); + } } diff --git a/src/AdamWathan/EloquentOAuth/Providers/InstagramProvider.php b/src/AdamWathan/EloquentOAuth/Providers/InstagramProvider.php index 575f8ea..372f4e8 100644 --- a/src/AdamWathan/EloquentOAuth/Providers/InstagramProvider.php +++ b/src/AdamWathan/EloquentOAuth/Providers/InstagramProvider.php @@ -4,77 +4,77 @@ class InstagramProvider extends Provider { - protected $authorizeUrl = "https://api.instagram.com/oauth/authorize"; - protected $accessTokenUrl = "https://api.instagram.com/oauth/access_token"; - protected $userDataUrl = "https://api.instagram.com/v1/users/self"; - protected $scope = array( + protected $authorizeUrl = "https://api.instagram.com/oauth/authorize"; + protected $accessTokenUrl = "https://api.instagram.com/oauth/access_token"; + protected $userDataUrl = "https://api.instagram.com/v1/users/self"; + protected $scope = array( 'basic', - ); + ); - protected function getAuthorizeUrl() - { - return $this->authorizeUrl; - } + protected function getAuthorizeUrl() + { + return $this->authorizeUrl; + } - protected function getAccessTokenBaseUrl() - { - return $this->accessTokenUrl; - } + protected function getAccessTokenBaseUrl() + { + return $this->accessTokenUrl; + } - protected function getUserDataUrl() - { - return $this->userDataUrl; - } + protected function getUserDataUrl() + { + return $this->userDataUrl; + } - protected function compileScopes() - { - return implode('+', $this->scope); - } + protected function compileScopes() + { + return implode('+', $this->scope); + } - protected function parseTokenResponse($response) - { - return $this->parseJsonTokenResponse($response); - } + protected function parseTokenResponse($response) + { + return $this->parseJsonTokenResponse($response); + } - protected function requestUserData() - { - $userData = parent::requestUserData(); - return $userData; - } + protected function requestUserData() + { + $userData = parent::requestUserData(); + return $userData; + } - protected function parseUserDataResponse($response) - { - $data = json_decode($response, true); - return $data['data']; - } + protected function parseUserDataResponse($response) + { + $data = json_decode($response, true); + return $data['data']; + } - protected function userId() - { - return $this->getProviderUserData('id'); - } + protected function userId() + { + return $this->getProviderUserData('id'); + } - protected function nickname() - { - return $this->getProviderUserData('username'); - } + protected function nickname() + { + return $this->getProviderUserData('username'); + } - protected function firstName() - { - return strstr($this->getProviderUserData('full_name'), ' ', true); - } + protected function firstName() + { + return strstr($this->getProviderUserData('full_name'), ' ', true); + } - protected function lastName() - { - return substr(strstr($this->getProviderUserData('full_name'), ' '), 1); - } + protected function lastName() + { + return substr(strstr($this->getProviderUserData('full_name'), ' '), 1); + } - protected function imageUrl() - { - return $this->getProviderUserData('profile_picture'); - } + protected function imageUrl() + { + return $this->getProviderUserData('profile_picture'); + } - protected function email() - { - return null; // Impossible to get email from Instagram - } + protected function email() + { + return null; // Impossible to get email from Instagram + } } diff --git a/src/AdamWathan/EloquentOAuth/Providers/LinkedInProvider.php b/src/AdamWathan/EloquentOAuth/Providers/LinkedInProvider.php index 2d1a303..76b699c 100644 --- a/src/AdamWathan/EloquentOAuth/Providers/LinkedInProvider.php +++ b/src/AdamWathan/EloquentOAuth/Providers/LinkedInProvider.php @@ -4,97 +4,97 @@ class LinkedInProvider extends Provider { - protected $authorizeUrl = "https://www.linkedin.com/uas/oauth2/authorization"; - protected $accessTokenUrl = "https://www.linkedin.com/uas/oauth2/accessToken"; - protected $userDataUrl = "https://api.linkedin.com/v1/people/~"; - protected $scope = array( + protected $authorizeUrl = "https://www.linkedin.com/uas/oauth2/authorization"; + protected $accessTokenUrl = "https://www.linkedin.com/uas/oauth2/accessToken"; + protected $userDataUrl = "https://api.linkedin.com/v1/people/~"; + protected $scope = array( 'r_basicprofile', 'r_emailaddress', ); - protected $profileFields = array( - 'id', - 'first-name', - 'last-name', - 'email-address', - 'picture-url', - ); - - protected function compileScopes() - { - return implode(' ', $this->scope); - } - - protected function getAuthorizeUrl() - { - return $this->authorizeUrl; - } - - protected function getAccessTokenBaseUrl() - { - $queryString = "code=".$this->getAuthorizationCode(); - $queryString .= "&client_id=".$this->clientId; - $queryString .= "&client_secret=".$this->clientSecret; - $queryString .= "&redirect_uri=".$this->redirectUri(); - $queryString .= "&grant_type=authorization_code"; - return $this->accessTokenUrl ."?" . $queryString; - } - - protected function getUserDataUrl() - { - return $this->userDataUrl; - } - - protected function parseTokenResponse($response) - { - return $this->parseJsonTokenResponse($response); - } - - protected function buildUserDataUrl() - { - $url = $this->getUserDataUrl(); - $url .= ':('.$this->compileProfileFields().')'; - $url .= '?format=json'; - $url .= "&oauth2_access_token=".$this->accessToken; - return $url; - } - - protected function compileProfileFields() - { - return implode(',', $this->profileFields); - } - - protected function parseUserDataResponse($response) - { - return json_decode($response, true); - } - - protected function userId() - { - return $this->getProviderUserData('id'); - } - - protected function nickname() - { - return $this->getProviderUserData('emailAddress'); - } - - protected function firstName() - { - return $this->getProviderUserData('firstName'); - } - - protected function lastName() - { - return $this->getProviderUserData('lastName'); - } - - protected function imageUrl() - { - return $this->getProviderUserData('pictureUrl'); - } - - protected function email() - { - return $this->getProviderUserData('emailAddress'); - } + protected $profileFields = array( + 'id', + 'first-name', + 'last-name', + 'email-address', + 'picture-url', + ); + + protected function compileScopes() + { + return implode(' ', $this->scope); + } + + protected function getAuthorizeUrl() + { + return $this->authorizeUrl; + } + + protected function getAccessTokenBaseUrl() + { + $queryString = "code=".$this->getAuthorizationCode(); + $queryString .= "&client_id=".$this->clientId; + $queryString .= "&client_secret=".$this->clientSecret; + $queryString .= "&redirect_uri=".$this->redirectUri(); + $queryString .= "&grant_type=authorization_code"; + return $this->accessTokenUrl ."?" . $queryString; + } + + protected function getUserDataUrl() + { + return $this->userDataUrl; + } + + protected function parseTokenResponse($response) + { + return $this->parseJsonTokenResponse($response); + } + + protected function buildUserDataUrl() + { + $url = $this->getUserDataUrl(); + $url .= ':('.$this->compileProfileFields().')'; + $url .= '?format=json'; + $url .= "&oauth2_access_token=".$this->accessToken; + return $url; + } + + protected function compileProfileFields() + { + return implode(',', $this->profileFields); + } + + protected function parseUserDataResponse($response) + { + return json_decode($response, true); + } + + protected function userId() + { + return $this->getProviderUserData('id'); + } + + protected function nickname() + { + return $this->getProviderUserData('emailAddress'); + } + + protected function firstName() + { + return $this->getProviderUserData('firstName'); + } + + protected function lastName() + { + return $this->getProviderUserData('lastName'); + } + + protected function imageUrl() + { + return $this->getProviderUserData('pictureUrl'); + } + + protected function email() + { + return $this->getProviderUserData('emailAddress'); + } } diff --git a/src/AdamWathan/EloquentOAuth/Providers/Provider.php b/src/AdamWathan/EloquentOAuth/Providers/Provider.php index 44f91af..ec66fc5 100644 --- a/src/AdamWathan/EloquentOAuth/Providers/Provider.php +++ b/src/AdamWathan/EloquentOAuth/Providers/Provider.php @@ -9,149 +9,149 @@ abstract class Provider implements ProviderInterface { - protected $httpClient; - protected $input; - protected $clientId; - protected $clientSecret; - protected $redirectUri; - protected $scope = array(); - - protected $headers = array( - 'authorize' => array(), - 'access_token' => array(), - 'user_details' => array(), - ); - - protected $accessToken; - protected $providerUserData; - - public function __construct($config, HttpClient $httpClient, Input $input) - { - $this->httpClient = $httpClient; - $this->input = $input; - $this->clientId = $config['id']; - $this->clientSecret = $config['secret']; - $this->redirectUri = $config['redirect']; - if (isset($config['scope'])) { - $this->scope = array_merge($this->scope, $config['scope']); - } - } - - public function redirectUri() - { - return $this->redirectUri; - } - - public function authorizeUrl($state) - { - $url = $this->getAuthorizeUrl(); - $url .= '?' . $this->buildAuthorizeQueryString($state); - return $url; - } - - protected function buildAuthorizeQueryString($state) - { - $queryString = "client_id=".$this->clientId; - $queryString .= "&scope=".urlencode($this->compileScopes()); - $queryString .= "&redirect_uri=".$this->redirectUri; - $queryString .= "&response_type=code"; - $queryString .= "&state=".$state; - return $queryString; - } - - protected function compileScopes() - { - return implode(',', $this->scope); - } - - public function getUserDetails() - { - $this->accessToken = $this->requestAccessToken(); - $this->providerUserData = $this->requestUserData(); - return new UserDetails(array( - 'accessToken' => $this->accessToken, - 'userId' => $this->userId(), - 'nickname' => $this->nickname(), - 'firstName' => $this->firstName(), - 'lastName' => $this->lastName(), - 'email' => $this->email(), - 'imageUrl' => $this->imageUrl(), - )); - } - - protected function getProviderUserData($key) - { - if (! isset($this->providerUserData[$key])) { - return null; - } - return $this->providerUserData[$key]; - } - - protected function requestAccessToken() - { - $url = $this->getAccessTokenBaseUrl(); - $request = $this->httpClient->post($url, $this->headers['access_token'], $this->buildAccessTokenPostBody()); - try { - $response = $request->send(); - } catch (BadResponseException $e) { - throw new InvalidAuthorizationCodeException((string) $e->getResponse()); - } - return $this->parseTokenResponse((string) $response->getBody()); - } - - protected function requestUserData() - { - $url = $this->buildUserDataUrl(); - $request = $this->httpClient->get($url, $this->headers['user_details']); - $response = $request->send(); - return $this->parseUserDataResponse((string) $response->getBody()); - } - - protected function buildAccessTokenPostBody() - { - $body = "code=".$this->getAuthorizationCode(); - $body .= "&client_id=".$this->clientId; - $body .= "&client_secret=".$this->clientSecret; - $body .= "&redirect_uri=".$this->redirectUri(); - $body .= "&grant_type=authorization_code"; - return $body; - } - - protected function buildUserDataUrl() - { - $url = $this->getUserDataUrl(); - $url .= "?access_token=".$this->accessToken; - return $url; - } - - protected function getAuthorizationCode() - { - if (! $this->input->has('code')) { - throw new ApplicationRejectedException; - } - return $this->input->get('code'); - } - - protected function parseJsonTokenResponse($response) - { - $response = json_decode($response); - if (! isset($response->access_token)) { - throw new InvalidAuthorizationCodeException; - } - return $response->access_token; - } - - abstract protected function getAuthorizeUrl(); - abstract protected function getAccessTokenBaseUrl(); - abstract protected function getUserDataUrl(); - - abstract protected function parseTokenResponse($response); - abstract protected function parseUserDataResponse($response); - - abstract protected function userId(); - abstract protected function nickname(); - abstract protected function firstName(); - abstract protected function lastName(); - abstract protected function email(); - abstract protected function imageUrl(); + protected $httpClient; + protected $input; + protected $clientId; + protected $clientSecret; + protected $redirectUri; + protected $scope = array(); + + protected $headers = array( + 'authorize' => array(), + 'access_token' => array(), + 'user_details' => array(), + ); + + protected $accessToken; + protected $providerUserData; + + public function __construct($config, HttpClient $httpClient, Input $input) + { + $this->httpClient = $httpClient; + $this->input = $input; + $this->clientId = $config['id']; + $this->clientSecret = $config['secret']; + $this->redirectUri = $config['redirect']; + if (isset($config['scope'])) { + $this->scope = array_merge($this->scope, $config['scope']); + } + } + + public function redirectUri() + { + return $this->redirectUri; + } + + public function authorizeUrl($state) + { + $url = $this->getAuthorizeUrl(); + $url .= '?' . $this->buildAuthorizeQueryString($state); + return $url; + } + + protected function buildAuthorizeQueryString($state) + { + $queryString = "client_id=".$this->clientId; + $queryString .= "&scope=".urlencode($this->compileScopes()); + $queryString .= "&redirect_uri=".$this->redirectUri; + $queryString .= "&response_type=code"; + $queryString .= "&state=".$state; + return $queryString; + } + + protected function compileScopes() + { + return implode(',', $this->scope); + } + + public function getUserDetails() + { + $this->accessToken = $this->requestAccessToken(); + $this->providerUserData = $this->requestUserData(); + return new UserDetails(array( + 'accessToken' => $this->accessToken, + 'userId' => $this->userId(), + 'nickname' => $this->nickname(), + 'firstName' => $this->firstName(), + 'lastName' => $this->lastName(), + 'email' => $this->email(), + 'imageUrl' => $this->imageUrl(), + )); + } + + protected function getProviderUserData($key) + { + if (! isset($this->providerUserData[$key])) { + return null; + } + return $this->providerUserData[$key]; + } + + protected function requestAccessToken() + { + $url = $this->getAccessTokenBaseUrl(); + $request = $this->httpClient->post($url, $this->headers['access_token'], $this->buildAccessTokenPostBody()); + try { + $response = $request->send(); + } catch (BadResponseException $e) { + throw new InvalidAuthorizationCodeException((string) $e->getResponse()); + } + return $this->parseTokenResponse((string) $response->getBody()); + } + + protected function requestUserData() + { + $url = $this->buildUserDataUrl(); + $request = $this->httpClient->get($url, $this->headers['user_details']); + $response = $request->send(); + return $this->parseUserDataResponse((string) $response->getBody()); + } + + protected function buildAccessTokenPostBody() + { + $body = "code=".$this->getAuthorizationCode(); + $body .= "&client_id=".$this->clientId; + $body .= "&client_secret=".$this->clientSecret; + $body .= "&redirect_uri=".$this->redirectUri(); + $body .= "&grant_type=authorization_code"; + return $body; + } + + protected function buildUserDataUrl() + { + $url = $this->getUserDataUrl(); + $url .= "?access_token=".$this->accessToken; + return $url; + } + + protected function getAuthorizationCode() + { + if (! $this->input->has('code')) { + throw new ApplicationRejectedException; + } + return $this->input->get('code'); + } + + protected function parseJsonTokenResponse($response) + { + $response = json_decode($response); + if (! isset($response->access_token)) { + throw new InvalidAuthorizationCodeException; + } + return $response->access_token; + } + + abstract protected function getAuthorizeUrl(); + abstract protected function getAccessTokenBaseUrl(); + abstract protected function getUserDataUrl(); + + abstract protected function parseTokenResponse($response); + abstract protected function parseUserDataResponse($response); + + abstract protected function userId(); + abstract protected function nickname(); + abstract protected function firstName(); + abstract protected function lastName(); + abstract protected function email(); + abstract protected function imageUrl(); } diff --git a/src/AdamWathan/EloquentOAuth/Providers/ProviderInterface.php b/src/AdamWathan/EloquentOAuth/Providers/ProviderInterface.php index 4b63ad9..e21a554 100644 --- a/src/AdamWathan/EloquentOAuth/Providers/ProviderInterface.php +++ b/src/AdamWathan/EloquentOAuth/Providers/ProviderInterface.php @@ -2,6 +2,6 @@ interface ProviderInterface { - public function authorizeUrl($state); - public function getUserDetails(); + public function authorizeUrl($state); + public function getUserDetails(); }