From 6ce0f1d8cddb182efbecde03b3d4c40160fda936 Mon Sep 17 00:00:00 2001 From: Troy Pavlek Date: Mon, 2 Feb 2015 11:50:41 -0700 Subject: [PATCH] Commit for the first release of the oauth2-twitch package. --- .gitignore | 2 + README.md | 45 ++++++++ composer.json | 18 +++ composer.lock | 227 ++++++++++++++++++++++++++++++++++++++ index.php | 22 ++++ src/Entity/TwitchUser.php | 78 +++++++++++++ src/Provider/Twitch.php | 64 +++++++++++ 7 files changed, 456 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 index.php create mode 100644 src/Entity/TwitchUser.php create mode 100644 src/Provider/Twitch.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c36e38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +vendor/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..f3e47d6 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +Twitch provider for league/oauth2-client +========================================= + +This is a package to integrate Battle.net authentication with the [OAuth2 client library](https://github.com/thephpleague/oauth2-client) by +[The League of Extraordinary Packages](http://thephpleague.com). + +To install, use composer: + +```bash +composer require depotwarehouse/oauth2-twitch +``` + +Usage is the same as the league's OAuth client, using `\Depotwarehouse\OAuth2\Client\Twitch\Provider\Twitch` as the provider. +For example: + +```php +$provider = new \Depotwarehouse\OAuth2\Client\Twitch\Provider\Twitch([ + 'clientId' => "YOUR_CLIENT_ID", + 'clientSecret' => "YOUR_CLIENT_SECRET", + 'redirectUri' => "http://your-redirect-uri" +]); +``` + +You can also optionally add a `scopes` key to the array passed to the constructor. The available scopes are documented +on the [Twitch API Documentation](https://github.com/justintv/Twitch-API/blob/master/authentication.md). + +> Note: The provider uses the "user_read" scope by default. If you pass other scopes, and want the ->getUserDetails() method +to work, you will need to ensure the "user_read" scope is used. + +```php +if (isset($_GET['code']) && $_GET['code']) { + $token = $this->provider->getAccessToken("authorizaton_code", [ + 'code' => $_GET['code'] + ]); + + // Returns an instance of Depotwarehouse\OAuth2\Client\Twitch\Entity\TwitchUser + $user = $this->provider->getUserDetails($token); + + $user->getDisplayName(); + $user->getId() + $user->getType(); + $user->getBio(); + $user->getEmail(); + $user->getPartnered(); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..171c0c6 --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "depotwarehouse/oauth2-twitch", + "description": "A Twitch provider for league/oauth2-client", + "authors": [ + { + "name": "Troy Pavlek", + "email": "troy@depotwarehouse.net" + } + ], + "require": { + "league/oauth2-client": "0.7.*" + }, + "autoload": { + "psr-4": { + "Depotwarehouse\\OAuth2\\Client\\Twitch\\": "src/" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..21451c2 --- /dev/null +++ b/composer.lock @@ -0,0 +1,227 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "7ee63b0fd363373422f0802e64c9dc1c", + "packages": [ + { + "name": "guzzle/guzzle", + "version": "v3.9.2", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle3.git", + "reference": "54991459675c1a2924122afbb0e5609ade581155" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle3/zipball/54991459675c1a2924122afbb0e5609ade581155", + "reference": "54991459675c1a2924122afbb0e5609ade581155", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "php": ">=5.3.3", + "symfony/event-dispatcher": "~2.1" + }, + "replace": { + "guzzle/batch": "self.version", + "guzzle/cache": "self.version", + "guzzle/common": "self.version", + "guzzle/http": "self.version", + "guzzle/inflection": "self.version", + "guzzle/iterator": "self.version", + "guzzle/log": "self.version", + "guzzle/parser": "self.version", + "guzzle/plugin": "self.version", + "guzzle/plugin-async": "self.version", + "guzzle/plugin-backoff": "self.version", + "guzzle/plugin-cache": "self.version", + "guzzle/plugin-cookie": "self.version", + "guzzle/plugin-curlauth": "self.version", + "guzzle/plugin-error-response": "self.version", + "guzzle/plugin-history": "self.version", + "guzzle/plugin-log": "self.version", + "guzzle/plugin-md5": "self.version", + "guzzle/plugin-mock": "self.version", + "guzzle/plugin-oauth": "self.version", + "guzzle/service": "self.version", + "guzzle/stream": "self.version" + }, + "require-dev": { + "doctrine/cache": "~1.3", + "monolog/monolog": "~1.0", + "phpunit/phpunit": "3.7.*", + "psr/log": "~1.0", + "symfony/class-loader": "~2.1", + "zendframework/zend-cache": "2.*,<2.3", + "zendframework/zend-log": "2.*,<2.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.9-dev" + } + }, + "autoload": { + "psr-0": { + "Guzzle": "src/", + "Guzzle\\Tests": "tests/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Guzzle Community", + "homepage": "https://github.com/guzzle/guzzle/contributors" + } + ], + "description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2014-08-11 04:32:36" + }, + { + "name": "league/oauth2-client", + "version": "0.7.1", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/oauth2-client.git", + "reference": "0a8e12a4f86302227991895a4166dfe22c28de03" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/oauth2-client/zipball/0a8e12a4f86302227991895a4166dfe22c28de03", + "reference": "0a8e12a4f86302227991895a4166dfe22c28de03", + "shasum": "" + }, + "require": { + "guzzle/guzzle": "~3.7", + "php": ">=5.4.0" + }, + "require-dev": { + "mockery/mockery": "~0.9", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.7.x-dev" + } + }, + "autoload": { + "psr-4": { + "League\\OAuth2\\Client\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Alex Bilbie", + "email": "hello@alexbilbie.com", + "homepage": "http://www.alexbilbie.com", + "role": "Developer" + } + ], + "description": "OAuth 2.0 Client Library", + "keywords": [ + "Authentication", + "SSO", + "authorization", + "identity", + "idp", + "oauth", + "oauth2", + "single sign on" + ], + "time": "2015-01-06 15:58:40" + }, + { + "name": "symfony/event-dispatcher", + "version": "v2.6.3", + "target-dir": "Symfony/Component/EventDispatcher", + "source": { + "type": "git", + "url": "https://github.com/symfony/EventDispatcher.git", + "reference": "40ff70cadea3785d83cac1c8309514b36113064e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/40ff70cadea3785d83cac1c8309514b36113064e", + "reference": "40ff70cadea3785d83cac1c8309514b36113064e", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "~2.0,>=2.0.5", + "symfony/dependency-injection": "~2.6", + "symfony/expression-language": "~2.6", + "symfony/stopwatch": "~2.3" + }, + "suggest": { + "symfony/dependency-injection": "", + "symfony/http-kernel": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.6-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\EventDispatcher\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony EventDispatcher Component", + "homepage": "http://symfony.com", + "time": "2015-01-05 14:28:40" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..85bb914 --- /dev/null +++ b/index.php @@ -0,0 +1,22 @@ + "bqzmhb7560sdxx7ixll05hmo53taxwp", +'clientSecret' => "l8z2b6c0ov025fhg1s14v3eosfdut4r", +'redirectUri' => "http://localhost:8000" +]); + +if (isset($_GET['code']) && $_GET['code']) { + $token = $provider->getAccessToken("authorization_code", [ + 'code' => $_GET['code'] + ]); + + $user = $provider->getUserDetails($token); + var_dump($user); + + +} else { + header('Location: ' . $provider->getAuthorizationUrl()); +} diff --git a/src/Entity/TwitchUser.php b/src/Entity/TwitchUser.php new file mode 100644 index 0000000..df5416a --- /dev/null +++ b/src/Entity/TwitchUser.php @@ -0,0 +1,78 @@ +id = $attributes['_id']; + $this->display_name = $attributes['display_name']; + $this->type = $attributes['type']; + $this->bio = $attributes['bio']; + $this->email = $attributes['email']; + $this->partnered = $attributes['partnered']; + } + + /** + * @return string + */ + public function getDisplayName() + { + return $this->display_name; + } + + /** + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return string + */ + public function getBio() + { + return $this->bio; + } + + /** + * @return string + */ + public function getEmail() + { + return $this->email; + } + + /** + * @return boolean + */ + public function isPartnered() + { + return $this->partnered; + } + +} diff --git a/src/Provider/Twitch.php b/src/Provider/Twitch.php new file mode 100644 index 0000000..a6735cb --- /dev/null +++ b/src/Provider/Twitch.php @@ -0,0 +1,64 @@ +