Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] Simplify module #12

Merged
merged 16 commits into from
Mar 7, 2015
122 changes: 90 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
ray/oauth-module
Ray.OAuthModule
================

[![Build Status](https://travis-ci.org/Ray-Di/Ray.OAuthModule.svg?branch=master)](https://travis-ci.org/Ray-Di/Ray.OAuthModule)

[OAuth](https://github.com/Lusitanian/PHPoAuthLib) Module for [Ray.Di](https://github.com/koriym/Ray.Di)
[OAuth](https://github.com/kawanamiyuu/Maye.OAuthClient) Module for [Ray.Di](https://github.com/koriym/Ray.Di)

## Installation

Expand All @@ -15,59 +15,117 @@ $ composer require ray/oauth-module

### Module install

**e.g. TwitterModule**

```php
use Ray\Di\AbstractModule;
use Ray\OAuthModule\Twitter\TwitterModule;
use Ray\OAuthModule\OAuth1Module;
use Ray\OAuthModule\OAuth1Service;

class AppModule extends AbstractModule
{
protected function configure()
{
// Callback URL Path of your application
$callbackUrlPath = '/oauth/twitter/callback';
$this->install(new TwitterModule('{YOUR_CONSUMER_KEY}', '{YOUR_CONSUMER_SECRET}', $callbackUrlPath);
$this->install(new OAuth1Module(OAuth1Service::TWITTER, $_ENV['CONSUMER_KEY'], $_ENV['CONSUMER_SECRET'], '/oauth/callback'));
}
}

```
### DI trait

**e.g. TwitterInject**
### Usage

Redirects to the authorization page.

```php
use Ray\OAuthModule\Inject\TwitterOAuthInject;

class OAuthController
{
use TwitterOAuthInject;

public function redirectAction()
{
$this->twitterOAuth->authorize();
}
}
```

use Ray\OAuthModule\Twitter\TwitterInject;
Requests the AccessToken.
This is callback process after authorization finished.

```php
use Ray\OAuthModule\Inject\TwitterOAuthInject;

class AuthController extends AbstractController
class OAuthController
{
use TwitterInject;
public function indexAction()
use TwitterOAuthInject;

public function callbackAction()
{
$requestToken = $this->twitterOAuthClient->requestRequestToken()->getRequestToken();
$url = $this->twitterOAuthClient->getAuthorizationUri([
'oauth_token' => $requestToken,
'force_login' => 'true'
]);
// redirect to Twitter authorize URL
header('Location:' . $url);
exit(0);
if ($_GET['denied']) {
// should be handled as error
return 'ERROR';
}

// requests AccessToken
$token = $this->twitterOAuth->requestAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']);
/** @var OAuth\OAuth1\Token\TokenInterface $token */

// $accessToken = $token->getAccessToken();
// $accessTokenSecret = $token->getAccessTokenSecret();
$userId = $token->getExtraParams()['user_id'];
$screenName = $token->getExtraParams()['screen_name'];

// gets authorized user info
$user = $this->twitterOAuth->api('get', 'users/show.json', ['user_id' => $userId]);
$user = json_decode($user);

$result = 'user_id : ' . $userId .'<br />';
$result.= 'screen_name : @' . $screenName . '<br />';
$result.= 'name: ' . $user->name;

return $result;
}
}
```

## Demo

#### OAuth1 (Twitter)

See ```docs/demo/www/oauth1_twitter.php``` for detail.

```php
# 1. Create and configure the Twitter App on Developer Website

# 2. Set Consumer Key and Secret in docs/demo/www/oauth1_twitter.php

# 3. Start the PHP built-in Web-Server
$ php -S localhost:8080 -t docs/demo/www

# 4. Access http://localhost:8080/oauth1_twitter.php
<< output >>
user_id: {Your User ID}
screen_name: @{your_screen_name}
name: {Your Name}
```

### Requiuments
#### OAuth2 (Facebook)

* PHP 5.5+
* hhvm

## Other Services?
See ```docs/demo/www/oauth2_facebook.php``` for detail.

```php
# 1. Create and configure the Facebook App on Developer Website

# 2. Set App ID and Secret in docs/demo/www/oauth2_facebook.php

If you need other [OAuth1](https://github.com/Lusitanian/PHPoAuthLib/tree/master/src/OAuth/OAuth1/Service)/[OAuth2](https://github.com/Lusitanian/PHPoAuthLib/tree/master/src/OAuth/OAuth2/Service) service module, for example "Tumblr" (OAuth1),
# 3. Start the PHP built-in Web-Server
$ php -S localhost:8080 -t docs/demo/www

# 4. Access http://localhost:8080/oauth2_facebook.php
<< output >>
id: {Your ID}
name: {Your Name}
```

1. Add TumblrModle class and TumblrInject trait.
### Requirements

1. Send a Pull Request.
* PHP 5.5+
* hhvm
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"license": "MIT",
"require": {
"php": ">=5.5.0",
"ray/di": "~2.0@dev",
"lusitanian/oauth": "~0.3"
"ray/di": "~2.0",
"maye/oauth-client": "~1.1"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
Expand All @@ -17,5 +17,11 @@
"psr-4": {
"Ray\\OAuthModule\\": "src/"
}
},
"autoload-dev":{
"psr-4":{
"Ray\\OAuthModule\\": "tests/",
"Ray\\OAuthModule\\": "tests/Fake"
}
}
}
34 changes: 34 additions & 0 deletions docs/demo/run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

require dirname(dirname(__DIR__)) . '/vendor/autoload.php';

use Maye\OAuthClient\OAuth1ClientInterface;
use Ray\Di\Injector;
use Ray\OAuthModule\Inject\TwitterOAuthInject;
use Ray\OAuthModule\OAuth1Module;
use Ray\OAuthModule\OAuth1Service;

class Fake
{
use TwitterOAuthInject;

public function getTwitterOAuth()
{
return $this->twitterOAuth;
}
}

/**
* (Emulates $_SERVER and $_ENV variables for CLI)
*/
$_SERVER['REQUEST_URI'] = 'http://127.0.0.1:8080/';
$_ENV['CONSUMER_KEY'] = 'Consumer Key of your twitter app';
$_ENV['CONSUMER_SECRET'] = 'Consumer Secret of your twitter app';


$module = new OAuth1Module(OAuth1Service::TWITTER, $_ENV['CONSUMER_KEY'], $_ENV['CONSUMER_SECRET'], '/callback/twitter');
/** @var Fake $fake */
$fake = (new Injector($module))->getInstance(Fake::class);
$works = ($fake->getTwitterOAuth() instanceof OAuth1ClientInterface);

echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL;
44 changes: 44 additions & 0 deletions docs/demo/run_by_app_module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

require dirname(dirname(__DIR__)) . '/vendor/autoload.php';

use Maye\OAuthClient\OAuth1ClientInterface;
use Ray\Di\AbstractModule;
use Ray\Di\Injector;
use Ray\OAuthModule\Inject\TwitterOAuthInject;
use Ray\OAuthModule\OAuth1Module;
use Ray\OAuthModule\OAuth1Service;

class Fake
{
use TwitterOAuthInject;

public function getTwitterOAuth()
{
return $this->twitterOAuth;
}
}


class AppModule extends AbstractModule
{
protected function configure()
{
$module = new OAuth1Module(OAuth1Service::TWITTER, $_ENV['CONSUMER_KEY'], $_ENV['CONSUMER_SECRET'], '/callback/twitter');
$this->install($module);
}
}

/**
* (Emulates $_SERVER and $_ENV variables for CLI)
*/
$_SERVER['REQUEST_URI'] = 'http://127.0.0.1:8080/auth/';
$_ENV['CONSUMER_KEY'] = 'Consumer Key of your twitter app';
$_ENV['CONSUMER_SECRET'] = 'Consumer Secret of your twitter app';


/** @var Fake $fake */
$fake = (new Injector(new AppModule))->getInstance(Fake::class);
$works = ($fake->getTwitterOAuth() instanceof OAuth1ClientInterface);

echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL;
108 changes: 108 additions & 0 deletions docs/demo/www/oauth1_twitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* OAuth1 Demo
*
* 1) Create and configure the App on Twitter Developer Site.
* https://apps.twitter.com/
*
* [Settings > Application Details > Website]
* => http://127.0.0.1:8080/oauth1_twitter.php
*
* [Settings > Application Details > Callback URL]
* => http://127.0.0.1:8080/oauth1_twitter.php
*
* 2) Set Consumer Key and Secret of your Twitter App.
*/
define('CONSUMER_KEY', 'xxxxxx');
define('CONSUMER_SECRET', 'yyyyyy');
/*
* 3) Start the PHP built-in Web-Server.
*
* $ php -S localhost:8080 -t docs/demo/www
*
* 4) Access 'http://localhost:8080/oauth1_twitter.php' in your browser.
*/

error_reporting(E_ALL ^ E_NOTICE);

require dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php';

use Ray\Di\AbstractModule;
use Ray\Di\Injector;
use Ray\OAuthModule\Inject\TwitterOAuthInject;
use Ray\OAuthModule\OAuth1Module;
use Ray\OAuthModule\OAuth1Service;

/**
* AppModule
*/
class AppModule extends AbstractModule
{
protected function configure()
{
$module = new OAuth1Module(
OAuth1Service::TWITTER,
CONSUMER_KEY,
CONSUMER_SECRET,
'/oauth1_twitter.php',
['force_login' => 'true']
);

$this->install($module);
}
}

/**
* OAuthController
*/
class OAuthController
{
use TwitterOAuthInject;

public function redirectAction()
{
// redirects to Twitter authorization page
$this->twitterOAuth->authorize();
}

public function callbackAction($oauth_token, $oauth_verifier, $denied)
{
if ($denied) {
// should be handled as error
return 'ERROR';
}

// requests AccessToken
$token = $this->twitterOAuth->requestAccessToken($oauth_token, $oauth_verifier);
/** @var OAuth\OAuth1\Token\TokenInterface $token */

// $accessToken = $token->getAccessToken();
// $accessTokenSecret = $token->getAccessTokenSecret();
$userId = $token->getExtraParams()['user_id'];
$screenName = $token->getExtraParams()['screen_name'];

// gets authorized user info
$user = $this->twitterOAuth->api('get', 'users/show.json', ['user_id' => $userId]);
$user = json_decode($user);

$result = 'user_id : ' . $userId .'<br />';
$result.= 'screen_name : @' . $screenName . '<br />';
$result.= 'name: ' . $user->name;

return $result;
}
}

// - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
// - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -

/** @var OAuthController $controller */
$controller = (new Injector(new AppModule))->getInstance(OAuthController::class);

if (empty($_GET)) {
$controller->redirectAction();

} else {
$result = $controller->callbackAction($_GET['oauth_token'], $_GET['oauth_verifier'], $_GET['denied']);
echo $result;
}
Loading