From 6854cb3f4d8219cf1829e32122eb2502a916eae9 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 1 Feb 2020 09:05:48 +0100 Subject: initial checkin --- .../src/Provider/AbstractProvider.php | 399 +++++++++++++++++++++ .../oauth2-client/src/Provider/Eventbrite.php | 51 +++ .../league/oauth2-client/src/Provider/Facebook.php | 103 ++++++ .../league/oauth2-client/src/Provider/Github.php | 99 +++++ .../league/oauth2-client/src/Provider/Google.php | 124 +++++++ .../oauth2-client/src/Provider/Instagram.php | 59 +++ .../league/oauth2-client/src/Provider/LinkedIn.php | 76 ++++ .../oauth2-client/src/Provider/Microsoft.php | 69 ++++ .../src/Provider/ProviderInterface.php | 36 ++ .../oauth2-client/src/Provider/Vkontakte.php | 99 +++++ 10 files changed, 1115 insertions(+) create mode 100644 vendor/league/oauth2-client/src/Provider/AbstractProvider.php create mode 100644 vendor/league/oauth2-client/src/Provider/Eventbrite.php create mode 100644 vendor/league/oauth2-client/src/Provider/Facebook.php create mode 100644 vendor/league/oauth2-client/src/Provider/Github.php create mode 100644 vendor/league/oauth2-client/src/Provider/Google.php create mode 100644 vendor/league/oauth2-client/src/Provider/Instagram.php create mode 100644 vendor/league/oauth2-client/src/Provider/LinkedIn.php create mode 100644 vendor/league/oauth2-client/src/Provider/Microsoft.php create mode 100644 vendor/league/oauth2-client/src/Provider/ProviderInterface.php create mode 100644 vendor/league/oauth2-client/src/Provider/Vkontakte.php (limited to 'vendor/league/oauth2-client/src/Provider') diff --git a/vendor/league/oauth2-client/src/Provider/AbstractProvider.php b/vendor/league/oauth2-client/src/Provider/AbstractProvider.php new file mode 100644 index 0000000..a200013 --- /dev/null +++ b/vendor/league/oauth2-client/src/Provider/AbstractProvider.php @@ -0,0 +1,399 @@ + $value) { + if (property_exists($this, $option)) { + $this->{$option} = $value; + } + } + + $this->setHttpClient(new GuzzleClient()); + } + + public function setHttpClient(GuzzleClient $client) + { + $this->httpClient = $client; + + return $this; + } + + public function getHttpClient() + { + $client = clone $this->httpClient; + + return $client; + } + + /** + * Get the URL that this provider uses to begin authorization. + * + * @return string + */ + abstract public function urlAuthorize(); + + /** + * Get the URL that this provider uses to request an access token. + * + * @return string + */ + abstract public function urlAccessToken(); + + /** + * Get the URL that this provider uses to request user details. + * + * Since this URL is typically an authorized route, most providers will require you to pass the access_token as + * a parameter to the request. For example, the google url is: + * + * 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token='.$token + * + * @param AccessToken $token + * @return string + */ + abstract public function urlUserDetails(AccessToken $token); + + /** + * Given an object response from the server, process the user details into a format expected by the user + * of the client. + * + * @param object $response + * @param AccessToken $token + * @return mixed + */ + abstract public function userDetails($response, AccessToken $token); + + public function getScopes() + { + return $this->scopes; + } + + public function setScopes(array $scopes) + { + $this->scopes = $scopes; + } + + public function getAuthorizationUrl($options = []) + { + $this->state = isset($options['state']) ? $options['state'] : md5(uniqid(rand(), true)); + + $params = [ + 'client_id' => $this->clientId, + 'redirect_uri' => $this->redirectUri, + 'state' => $this->state, + 'scope' => is_array($this->scopes) ? implode($this->scopeSeparator, $this->scopes) : $this->scopes, + 'response_type' => isset($options['response_type']) ? $options['response_type'] : 'code', + 'approval_prompt' => isset($options['approval_prompt']) ? $options['approval_prompt'] : 'auto', + ]; + + return $this->urlAuthorize().'?'.$this->httpBuildQuery($params, '', '&'); + } + + // @codeCoverageIgnoreStart + public function authorize($options = []) + { + $url = $this->getAuthorizationUrl($options); + if ($this->redirectHandler) { + $handler = $this->redirectHandler; + return $handler($url); + } + // @codeCoverageIgnoreStart + header('Location: ' . $url); + exit; + // @codeCoverageIgnoreEnd + } + + public function getAccessToken($grant = 'authorization_code', $params = []) + { + if (is_string($grant)) { + // PascalCase the grant. E.g: 'authorization_code' becomes 'AuthorizationCode' + $className = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $grant))); + $grant = 'League\\OAuth2\\Client\\Grant\\'.$className; + if (! class_exists($grant)) { + throw new \InvalidArgumentException('Unknown grant "'.$grant.'"'); + } + $grant = new $grant(); + } elseif (! $grant instanceof GrantInterface) { + $message = get_class($grant).' is not an instance of League\OAuth2\Client\Grant\GrantInterface'; + throw new \InvalidArgumentException($message); + } + + $defaultParams = [ + 'client_id' => $this->clientId, + 'client_secret' => $this->clientSecret, + 'redirect_uri' => $this->redirectUri, + 'grant_type' => $grant, + ]; + + $requestParams = $grant->prepRequestParams($defaultParams, $params); + + try { + switch (strtoupper($this->method)) { + case 'GET': + // @codeCoverageIgnoreStart + // No providers included with this library use get but 3rd parties may + $client = $this->getHttpClient(); + $client->setBaseUrl($this->urlAccessToken() . '?' . $this->httpBuildQuery($requestParams, '', '&')); + $request = $client->get(null, $this->getHeaders(), $requestParams)->send(); + $response = $request->getBody(); + break; + // @codeCoverageIgnoreEnd + case 'POST': + $client = $this->getHttpClient(); + $client->setBaseUrl($this->urlAccessToken()); + $request = $client->post(null, $this->getHeaders(), $requestParams)->send(); + $response = $request->getBody(); + break; + // @codeCoverageIgnoreStart + default: + throw new \InvalidArgumentException('Neither GET nor POST is specified for request'); + // @codeCoverageIgnoreEnd + } + } catch (BadResponseException $e) { + // @codeCoverageIgnoreStart + $response = $e->getResponse()->getBody(); + // @codeCoverageIgnoreEnd + } + + $result = $this->prepareResponse($response); + + if (isset($result['error']) && ! empty($result['error'])) { + // @codeCoverageIgnoreStart + throw new IDPException($result); + // @codeCoverageIgnoreEnd + } + + $result = $this->prepareAccessTokenResult($result); + + return $grant->handleResponse($result); + } + + /** + * Prepare the response, parsing according to configuration and returning + * the response as an array. + * + * @param string $response + * @return array + */ + protected function prepareResponse($response) + { + $result = []; + + switch ($this->responseType) { + case 'json': + $json = json_decode($response, true); + + if (JSON_ERROR_NONE === json_last_error()) { + $result = $json; + } + + break; + case 'string': + parse_str($response, $result); + break; + } + + return $result; + } + + /** + * Prepare the access token response for the grant. Custom mapping of + * expirations, etc should be done here. + * + * @param array $result + * @return array + */ + protected function prepareAccessTokenResult(array $result) + { + $this->setResultUid($result); + return $result; + } + + /** + * Sets any result keys we've received matching our provider-defined uidKey to the key "uid". + * + * @param array $result + */ + protected function setResultUid(array &$result) + { + // If we're operating with the default uidKey there's nothing to do. + if ($this->uidKey === "uid") { + return; + } + + if (isset($result[$this->uidKey])) { + // The AccessToken expects a "uid" to have the key "uid". + $result['uid'] = $result[$this->uidKey]; + } + } + + public function getUserDetails(AccessToken $token) + { + $response = $this->fetchUserDetails($token); + + return $this->userDetails(json_decode($response), $token); + } + + public function getUserUid(AccessToken $token) + { + $response = $this->fetchUserDetails($token, true); + + return $this->userUid(json_decode($response), $token); + } + + public function getUserEmail(AccessToken $token) + { + $response = $this->fetchUserDetails($token, true); + + return $this->userEmail(json_decode($response), $token); + } + + public function getUserScreenName(AccessToken $token) + { + $response = $this->fetchUserDetails($token, true); + + return $this->userScreenName(json_decode($response), $token); + } + + public function userUid($response, AccessToken $token) + { + return isset($response->id) && $response->id ? $response->id : null; + } + + public function userEmail($response, AccessToken $token) + { + return isset($response->email) && $response->email ? $response->email : null; + } + + public function userScreenName($response, AccessToken $token) + { + return isset($response->name) && $response->name ? $response->name : null; + } + + /** + * Build HTTP the HTTP query, handling PHP version control options + * + * @param array $params + * @param integer $numeric_prefix + * @param string $arg_separator + * @param null|integer $enc_type + * + * @return string + * @codeCoverageIgnoreStart + */ + protected function httpBuildQuery($params, $numeric_prefix = 0, $arg_separator = '&', $enc_type = null) + { + if (version_compare(PHP_VERSION, '5.4.0', '>=') && !defined('HHVM_VERSION')) { + if ($enc_type === null) { + $enc_type = $this->httpBuildEncType; + } + $url = http_build_query($params, $numeric_prefix, $arg_separator, $enc_type); + } else { + $url = http_build_query($params, $numeric_prefix, $arg_separator); + } + + return $url; + } + + protected function fetchUserDetails(AccessToken $token) + { + $url = $this->urlUserDetails($token); + + $headers = $this->getHeaders($token); + + return $this->fetchProviderData($url, $headers); + } + + protected function fetchProviderData($url, array $headers = []) + { + try { + $client = $this->getHttpClient(); + $client->setBaseUrl($url); + + if ($headers) { + $client->setDefaultOption('headers', $headers); + } + + $request = $client->get()->send(); + $response = $request->getBody(); + } catch (BadResponseException $e) { + // @codeCoverageIgnoreStart + $response = $e->getResponse()->getBody(); + $result = $this->prepareResponse($response); + throw new IDPException($result); + // @codeCoverageIgnoreEnd + } + + return $response; + } + + protected function getAuthorizationHeaders($token) + { + $headers = []; + if ($this->authorizationHeader) { + $headers['Authorization'] = $this->authorizationHeader . ' ' . $token; + } + return $headers; + } + + public function getHeaders($token = null) + { + $headers = $this->headers; + if ($token) { + $headers = array_merge($headers, $this->getAuthorizationHeaders($token)); + } + return $headers; + } + + public function setRedirectHandler(Closure $handler) + { + $this->redirectHandler = $handler; + } +} diff --git a/vendor/league/oauth2-client/src/Provider/Eventbrite.php b/vendor/league/oauth2-client/src/Provider/Eventbrite.php new file mode 100644 index 0000000..116051f --- /dev/null +++ b/vendor/league/oauth2-client/src/Provider/Eventbrite.php @@ -0,0 +1,51 @@ +exchangeArray([ + 'uid' => $response->user->user_id, + 'email' => $response->user->email, + ]); + + return $user; + } + + public function userUid($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return $response->user->user_id; + } + + public function userEmail($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return isset($response->user->email) && $response->user->email ? $response->user->email : null; + } + + public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return $response->user->user_id; + } +} diff --git a/vendor/league/oauth2-client/src/Provider/Facebook.php b/vendor/league/oauth2-client/src/Provider/Facebook.php new file mode 100644 index 0000000..9155836 --- /dev/null +++ b/vendor/league/oauth2-client/src/Provider/Facebook.php @@ -0,0 +1,103 @@ +graphApiVersion = (isset($options['graphApiVersion'])) + ? $options['graphApiVersion'] + : static::DEFAULT_GRAPH_VERSION; + } + + public function urlAuthorize() + { + return 'https://www.facebook.com/'.$this->graphApiVersion.'/dialog/oauth'; + } + + public function urlAccessToken() + { + return 'https://graph.facebook.com/'.$this->graphApiVersion.'/oauth/access_token'; + } + + public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token) + { + $fields = implode(',', [ + 'id', + 'name', + 'first_name', + 'last_name', + 'email', + 'hometown', + 'bio', + 'picture.type(large){url}', + 'gender', + 'locale', + 'link', + ]); + + return 'https://graph.facebook.com/'.$this->graphApiVersion.'/me?fields='.$fields.'&access_token='.$token; + } + + public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token) + { + $user = new User(); + + $email = (isset($response->email)) ? $response->email : null; + // The "hometown" field will only be returned if you ask for the `user_hometown` permission. + $location = (isset($response->hometown->name)) ? $response->hometown->name : null; + $description = (isset($response->bio)) ? $response->bio : null; + $imageUrl = (isset($response->picture->data->url)) ? $response->picture->data->url : null; + $gender = (isset($response->gender)) ? $response->gender : null; + $locale = (isset($response->locale)) ? $response->locale : null; + + $user->exchangeArray([ + 'uid' => $response->id, + 'name' => $response->name, + 'firstname' => $response->first_name, + 'lastname' => $response->last_name, + 'email' => $email, + 'location' => $location, + 'description' => $description, + 'imageurl' => $imageUrl, + 'gender' => $gender, + 'locale' => $locale, + 'urls' => [ 'Facebook' => $response->link ], + ]); + + return $user; + } + + public function userUid($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return $response->id; + } + + public function userEmail($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return isset($response->email) && $response->email ? $response->email : null; + } + + public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return [$response->first_name, $response->last_name]; + } +} diff --git a/vendor/league/oauth2-client/src/Provider/Github.php b/vendor/league/oauth2-client/src/Provider/Github.php new file mode 100644 index 0000000..8182a4e --- /dev/null +++ b/vendor/league/oauth2-client/src/Provider/Github.php @@ -0,0 +1,99 @@ +domain.'/login/oauth/authorize'; + } + + public function urlAccessToken() + { + return $this->domain.'/login/oauth/access_token'; + } + + public function urlUserDetails(AccessToken $token) + { + if ($this->domain === 'https://github.com') { + return $this->apiDomain.'/user'; + } + return $this->domain.'/api/v3/user'; + } + + public function urlUserEmails(AccessToken $token) + { + if ($this->domain === 'https://github.com') { + return $this->apiDomain.'/user/emails'; + } + return $this->domain.'/api/v3/user/emails'; + } + + public function userDetails($response, AccessToken $token) + { + $user = new User(); + + $name = (isset($response->name)) ? $response->name : null; + $email = (isset($response->email)) ? $response->email : null; + + $user->exchangeArray([ + 'uid' => $response->id, + 'nickname' => $response->login, + 'name' => $name, + 'email' => $email, + 'urls' => [ + 'GitHub' => $this->domain.'/'.$response->login, + ], + ]); + + return $user; + } + + public function userUid($response, AccessToken $token) + { + return $response->id; + } + + public function getUserEmails(AccessToken $token) + { + $response = $this->fetchUserEmails($token); + + return $this->userEmails(json_decode($response), $token); + } + + public function userEmail($response, AccessToken $token) + { + return isset($response->email) && $response->email ? $response->email : null; + } + + public function userEmails($response, AccessToken $token) + { + return $response; + } + + public function userScreenName($response, AccessToken $token) + { + return $response->name; + } + + protected function fetchUserEmails(AccessToken $token) + { + $url = $this->urlUserEmails($token); + + $headers = $this->getHeaders($token); + + return $this->fetchProviderData($url, $headers); + } +} diff --git a/vendor/league/oauth2-client/src/Provider/Google.php b/vendor/league/oauth2-client/src/Provider/Google.php new file mode 100644 index 0000000..87393ee --- /dev/null +++ b/vendor/league/oauth2-client/src/Provider/Google.php @@ -0,0 +1,124 @@ +hostedDomain = $hd; + } + + public function getHostedDomain() + { + return $this->hostedDomain; + } + + /** + * @var string If set, this will be sent to google as the "access_type" parameter. + * @link https://developers.google.com/accounts/docs/OAuth2WebServer#offline + */ + public $accessType = ''; + + public function setAccessType($accessType) + { + $this->accessType = $accessType; + } + + public function getAccessType() + { + return $this->accessType; + } + + public function urlAuthorize() + { + return 'https://accounts.google.com/o/oauth2/auth'; + } + + public function urlAccessToken() + { + return 'https://accounts.google.com/o/oauth2/token'; + } + + public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token) + { + return + 'https://www.googleapis.com/plus/v1/people/me?'. + 'fields=id%2Cname(familyName%2CgivenName)%2CdisplayName%2C'. + 'emails%2Fvalue%2Cimage%2Furl&alt=json'; + } + + public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token) + { + $response = (array) $response; + + $user = new User(); + + $imageUrl = (isset($response['image']) && + $response['image']->url) ? $response['image']->url : null; + $email = + (isset($response['emails']) && + count($response['emails']) && + $response['emails'][0]->value)? $response['emails'][0]->value : null; + + $user->exchangeArray([ + 'uid' => $response['id'], + 'name' => $response['displayName'], + 'firstname' => $response['name']->givenName, + 'lastName' => $response['name']->familyName, + 'email' => $email, + 'imageUrl' => $imageUrl, + ]); + + return $user; + } + + public function userUid($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return $response->id; + } + + public function userEmail($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return ($response->emails && + count($response->emails) && + $response->emails[0]->value) ? $response->emails[0]->value : null; + } + + public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return [$response->name->givenName, $response->name->familyName]; + } + + public function getAuthorizationUrl($options = array()) + { + $url = parent::getAuthorizationUrl($options); + + if (!empty($this->hostedDomain)) { + $url .= '&' . $this->httpBuildQuery(['hd' => $this->hostedDomain]); + } + + if (!empty($this->accessType)) { + $url .= '&' . $this->httpBuildQuery(['access_type'=> $this->accessType]); + } + + return $url; + } +} diff --git a/vendor/league/oauth2-client/src/Provider/Instagram.php b/vendor/league/oauth2-client/src/Provider/Instagram.php new file mode 100644 index 0000000..0377c8a --- /dev/null +++ b/vendor/league/oauth2-client/src/Provider/Instagram.php @@ -0,0 +1,59 @@ +data->bio)) ? $response->data->bio : null; + + $user->exchangeArray([ + 'uid' => $response->data->id, + 'nickname' => $response->data->username, + 'name' => $response->data->full_name, + 'description' => $description, + 'imageUrl' => $response->data->profile_picture, + ]); + + return $user; + } + + public function userUid($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return $response->data->id; + } + + public function userEmail($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return; + } + + public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token) + { + return $response->data->full_name; + } +} diff --git a/vendor/league/oauth2-client/src/Provider/LinkedIn.php b/vendor/league/oauth2-client/src/Provider/LinkedIn.php new file mode 100644 index 0000000..c790ddf --- /dev/null +++ b/vendor/league/oauth2-client/src/Provider/LinkedIn.php @@ -0,0 +1,76 @@ +fields); + return 'https://api.linkedin.com/v1/people/~:(' . $fields . ')?format=json'; + } + + public function userDetails($response, AccessToken $token) + { + $user = new User(); + + $email = (isset($response->emailAddress)) ? $response->emailAddress : null; + $location = (isset($response->location->name)) ? $response->location->name : null; + $description = (isset($response->headline)) ? $response->headline : null; + $pictureUrl = (isset($response->pictureUrl)) ? $response->pictureUrl : null; + $publicProfileUrl = (isset($response->publicProfileUrl)) ? $response->publicProfileUrl : null; + + $user->exchangeArray([ + 'uid' => $response->id, + 'name' => $response->firstName.' '.$response->lastName, + 'firstname' => $response->firstName, + 'lastname' => $response->lastName, + 'email' => $email, + 'location' => $location, + 'description' => $description, + 'imageurl' => $pictureUrl, + 'urls' => $publicProfileUrl, + ]); + + return $user; + } + + public function userUid($response, AccessToken $token) + { + return $response->id; + } + + public function userEmail($response, AccessToken $token) + { + return isset($response->emailAddress) && $response->emailAddress + ? $response->emailAddress + : null; + } + + public function userScreenName($response, AccessToken $token) + { + return [$response->firstName, $response->lastName]; + } +} diff --git a/vendor/league/oauth2-client/src/Provider/Microsoft.php b/vendor/league/oauth2-client/src/Provider/Microsoft.php new file mode 100644 index 0000000..d0ed12e --- /dev/null +++ b/vendor/league/oauth2-client/src/Provider/Microsoft.php @@ -0,0 +1,69 @@ +getHttpClient(); + $client->setBaseUrl('https://apis.live.net/v5.0/'.$response->id.'/picture'); + $request = $client->get()->send(); + $info = $request->getInfo(); + $imageUrl = $info['url']; + + $user = new User(); + + $email = (isset($response->emails->preferred)) ? $response->emails->preferred : null; + + $user->exchangeArray([ + 'uid' => $response->id, + 'name' => $response->name, + 'firstname' => $response->first_name, + 'lastname' => $response->last_name, + 'email' => $email, + 'imageurl' => $imageUrl, + 'urls' => $response->link.'/cid-'.$response->id, + ]); + + return $user; + } + + public function userUid($response, AccessToken $token) + { + return $response->id; + } + + public function userEmail($response, AccessToken $token) + { + return isset($response->emails->preferred) && $response->emails->preferred + ? $response->emails->preferred + : null; + } + + public function userScreenName($response, AccessToken $token) + { + return [$response->first_name, $response->last_name]; + } +} diff --git a/vendor/league/oauth2-client/src/Provider/ProviderInterface.php b/vendor/league/oauth2-client/src/Provider/ProviderInterface.php new file mode 100644 index 0000000..efe6087 --- /dev/null +++ b/vendor/league/oauth2-client/src/Provider/ProviderInterface.php @@ -0,0 +1,36 @@ +uid}&fields=" + .implode(",", $fields)."&access_token={$token}"; + } + + public function userDetails($response, AccessToken $token) + { + $response = $response->response[0]; + + $user = new User(); + + $email = (isset($response->email)) ? $response->email : null; + $location = (isset($response->country)) ? $response->country : null; + $description = (isset($response->status)) ? $response->status : null; + + $user->exchangeArray([ + 'uid' => $response->uid, + 'nickname' => $response->nickname, + 'name' => $response->screen_name, + 'firstname' => $response->first_name, + 'lastname' => $response->last_name, + 'email' => $email, + 'location' => $location, + 'description' => $description, + 'imageUrl' => $response->photo_200_orig, + ]); + + return $user; + } + + public function userUid($response, AccessToken $token) + { + $response = $response->response[0]; + + return $response->uid; + } + + public function userEmail($response, AccessToken $token) + { + $response = $response->response[0]; + + return isset($response->email) && $response->email ? $response->email : null; + } + + public function userScreenName($response, AccessToken $token) + { + $response = $response->response[0]; + + return [$response->first_name, $response->last_name]; + } +} -- cgit v1.2.3-70-g09d2