- ã¯ããã«
- ä½æãã OAuth ã¯ã©ã¤ã¢ã³ã
- ã¤ã³ã¹ãã¼ã«ããã³è¨å®
- 使ãæ¹
- ãµã³ãã«ããã°ã©ã
- ã«ã¹ã¿ã ã® OAuth ãµã¼ãã¹ã®ä½¿ãæ¹
- åè
ã¯ããã«
ä¸è¨ã®æ¡ä»¶ã§ Lumen
ã§ä½¿ãã OAuth ã¯ã©ã¤ã¢ã³ããæ¢ãã¦æé ãªãã®ãè¦ã¤ãããªãã£ãã®ã§ãéå»ã« Laravel
ã使ã£ã¦ããæã«ãã使ã£ã OAuth ã¯ã©ã¤ã¢ã³ãã移æ¤ãã¦ã¿ã¾ãã
- Facade ã使ããªã
- ã«ã¹ã¿ã ã® OAuth ãµã¼ãã¹ãç»é²å¯è½
ä½æãã OAuth ã¯ã©ã¤ã¢ã³ã
ãã¡ããä»åä½æããããã±ã¼ã¸ã§ããoriceon/oauth-5-laravel ã fork ã㦠Lumen
åãã«æ¸ãæãã¦ãã¾ã
Lumen 6
以éã§åä½ãã¾ã
ã¤ã³ã¹ãã¼ã«ããã³è¨å®
ã¤ã³ã¹ãã¼ã«
Lumen
ã®ããã¸ã§ã¯ãã® composer.json
ã«ä»¥ä¸ã®å
容ã追å
"repositories": [ { "type": "vcs", "url": "https://github.com/takaya030/oauth-lumen" } ], "require": { "takaya030/oauth-lumen": "dev-master" }
ãã®å¾ã composer update
ã§ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«
$ composer update
ããã±ã¼ã¸ç»é²
ä¸è¨å 容㧠OAuth ãµã¼ãã¹ãããã¤ããä½æ
app/Providers/OAuthServiveProvider.php
<?php namespace App\Providers; /** * @author Dariusz Prz?da <[email protected]> * @copyright Copyright (c) 2013 * @license http://www.opensource.org/licenses/mit-license.html MIT License */ use Illuminate\Support\ServiceProvider; use Takaya030\OAuth\OAuth; class OAuthServiceProvider extends ServiceProvider { /** * Indicates if loading of the provider is deferred. * * @var bool */ protected $defer = false; /** * Bootstrap the application events. * * @return void */ public function boot() { } /** * Register the service provider. * * @return void */ public function register() { // Register 'oauth' $this->app->singleton('oauth', function ($app) { // create oAuth instance $oauth = new OAuth(); // return oAuth instance return $oauth; }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return []; } }
boostrap/app.php
ã« OAuth ãµã¼ãã¹ãããã¤ããç»é²
/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
$app->register(App\Providers\OAuthServiceProvider::class);
è¨å®ãã¡ã¤ã«
ããã¸ã§ã¯ãã®ã«ã¼ãã« config
ãã©ã«ããä½æããã®ä¸ã«ä¸è¨å
容㧠oauth-lumen.php
ãé
ç½®ãã
config/oauth-lumen.php
<?php return [ /* |-------------------------------------------------------------------------- | oAuth Config |-------------------------------------------------------------------------- */ /** * Storage */ 'storage' => '\\OAuth\\Common\\Storage\\Session', //'storage' => '\\Takaya030\\OAuth\\OAuthLumenSession', /** * Consumers */ 'consumers' => [ 'Google' => [ 'client_id' => '', 'client_secret' => '', 'scope' => [], ], ] ];
boostrap/app.php
ã®ä¸ã§ config/oauth-lumen.php
ãèªã¿è¾¼ãããã«ãã
/*
|--------------------------------------------------------------------------
| Load Custom Config Files
|--------------------------------------------------------------------------
*/
$app->configure('oauth-lumen');
èªè¨¼æ å ±
config/oauth-lumen.php
ã® consumers
ã«è¿½å ãã
/** * Consumers */ 'consumers' => [ 'Google' => [ 'client_id' => 'Your Google client ID', 'client_secret' => 'Your Google Client Secret', 'scope' => ['OAuth2 scope'], ], ]
Session Storage ã®è¨å®
ã¢ã¯ã»ã¹ãã¼ã¯ã³ãã»ãã·ã§ã³ã«ä¿åããããã®è¨å®
Lumen
ã¯ããã©ã«ãã§ã¯ã»ãã·ã§ã³ãæå¹ã«ãªã£ã¦ããªããããä¸è¨ãµã¤ãã®æé ã§æå¹åãã
config/oauth-lumen.php
ã® storage
ã®è¨å®ãä¸è¨ã«å¤æ´
/**
* Storage
*/
'storage' => '\\Takaya030\\OAuth\\OAuthLumenSession',
使ãæ¹
åºæ¬çãªä½¿ãæ¹
ä¸è¨ã®ã³ã¼ã㧠OAuth ãµã¼ãã¹ã¯ã©ã¹ã®ãªãã¸ã§ã¯ã ãåå¾å¯è½
$service = app('oauth')->consumer('Google');
ãªãã·ã§ã³ã§ç¬¬2å¼æ°ã«èªè¨¼æã«ãªãã¤ã¬ã¯ãå URL ãæå®ã§ãã
$service = app('oauth')->consumer('Google', 'http://url.to.redirect.to');
ãµã³ãã«ããã°ã©ã
èªè¨¼æ
å ±ã config/oauth-lumen.php
ã«è¿½å
'Google' => [ 'client_id' => 'Your Google client ID', 'client_secret' => 'Your Google Client Secret', 'scope' => ['userinfo_email', 'userinfo_profile'], ],
ã³ã³ããã¼ã©ã®ãµã³ãã«ã³ã¼ã
<?php public function loginWithGoogle(Request $request) { // get data from request $code = $request->get('code'); // get google service $googleService = app('oauth')->consumer('Google'); // check if code is valid // if code is provided get user data and sign in if ( ! is_null($code)) { // This was a callback request from google, get the token $token = $googleService->requestAccessToken($code); // Send a request with it $result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true); $message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name']; echo $message. "<br/>"; //Var_dump //display whole array. dd($result); } // if not ask for permission first else { // get googleService authorization $url = $googleService->getAuthorizationUri(); // return to google login url return redirect((string)$url); } }
ã«ã¹ã¿ã ã® OAuth ãµã¼ãã¹ã®ä½¿ãæ¹
ä¾ã¨ã㦠Google ãµã¼ãã¹ã Datastore API
ã使ããããæ¡å¼µãã¦ã¿ã¾ã
OAuth ãµã¼ãã¹ã¯ã©ã¹ã®å®ç¾©
Google ãµã¼ã㹠㯠Datastore API
ã使ãã¨ã¨ã©ã¼ã«ãªãã¾ãããåå ã¯ãµã¼ãã¹ã¯ã©ã¹ã«ã¹ã³ã¼ããå®ç¾©ããã¦ããªããããªã®ã§ Datastore
ã®ã¹ã³ã¼ããå®ç¾©ãããµã¼ãã¹ã¯ã©ã¹ãä½æãã¾ã
app/OAuth/Service/MyGoogle.php
<?php namespace App\OAuth\Service; use \OAuth\OAuth2\Service\Google; class MyGoogle extends Google { const SCOPE_DATASTORE = 'https://www.googleapis.com/auth/datastore'; }
OAuth ãµã¼ãã¹ã¯ã©ã¹ã®ç»é²
ä½æãããµã¼ãã¹ã¯ã©ã¹ããµã¼ãã¹ãããã¤ãã§ç»é²ãã¾ã
app/Providers/OAuthServiveProvider.php
<?php namespace App\Providers; /** * @author Dariusz Prz?da <[email protected]> * @copyright Copyright (c) 2013 * @license http://www.opensource.org/licenses/mit-license.html MIT License */ use Illuminate\Support\ServiceProvider; use Takaya030\OAuth\OAuth; class OAuthServiceProvider extends ServiceProvider { /** * Indicates if loading of the provider is deferred. * * @var bool */ protected $defer = false; /** * Bootstrap the application events. * * @return void */ public function boot() { } /** * Register the service provider. * * @return void */ public function register() { // Register 'oauth' $this->app->singleton('oauth', function ($app) { // create oAuth instance $oauth = new OAuth(); // register custom service $oauth->registerService('MyGoogle', \App\OAuth\Service\MyGoogle::class); // return oAuth instance return $oauth; }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return []; } }
OAuth ãµã¼ãã¹ã®è¨å®è¿½å
config/oauth-lumen.php
<?php return [ /* |-------------------------------------------------------------------------- | oAuth Config |-------------------------------------------------------------------------- */ /** * Storage */ //'storage' => '\\OAuth\\Common\\Storage\\Session', 'storage' => '\\Takaya030\\OAuth\\OAuthLumenSession', /** * Consumers */ 'consumers' => [ 'MyGoogle' => [ 'client_id' => 'Your Google client ID', 'client_secret' => 'Your Google Client Secret', 'scope' => ['https://www.googleapis.com/auth/datastore'], ], ] ];
OAuth ãµã¼ãã¹ãªãã¸ã§ã¯ãã®åå¾
ä¸è¨ã®ã³ã¼ã㧠Datastore API
ã使ç¨å¯è½ãª MyGoogle
ãµã¼ãã¹ãªãã¸ã§ã¯ããåå¾å¯è½
$service = app('oauth')->consumer('MyGoogle');