|
1 | 1 | # Oauth2Server plugin for CakePHP
|
2 | 2 |
|
3 |
| -## Installation |
| 3 | +**!!Attention!!** |
| 4 | +This plugin does not support refresh token repository yet. Access tokens are usable without any |
| 5 | +expiration date. **use at your own risk!** |
| 6 | + |
| 7 | +**PRs are more than welcome** |
| 8 | + |
| 9 | +## How to use? |
4 | 10 |
|
5 | 11 | You can install this plugin into your CakePHP application using [composer](http://getcomposer.org).
|
6 | 12 |
|
7 |
| -The recommended way to install composer packages is: |
| 13 | +#### 1. Use composer to install |
| 14 | + |
| 15 | +``` |
| 16 | +composer require suhaboncukcu/Oauth2Server |
| 17 | +``` |
| 18 | + |
| 19 | +#### 2. Load the plugin |
8 | 20 |
|
9 | 21 | ```
|
10 |
| -composer require your-name-here/Oauth2Server |
| 22 | +Plugin::load('Oauth2Server', ['bootstrap' => true, 'routes' => false]); |
11 | 23 | ```
|
| 24 | + |
| 25 | +#### 3. Create your validators |
| 26 | + |
| 27 | +**!!Attention!!** |
| 28 | + |
| 29 | +You can find example validator classes under `vendors\suhaboncukcu\Oauth2Server\src\OauthLogic\Validators`. |
| 30 | +You should copy and paste them to your desired location. |
| 31 | + |
| 32 | +#### 4. Create & Update the config file |
| 33 | + |
| 34 | +Copy & paste `vendors\suhaboncukcu\Oauth2Server\config\oauth2.php` to your config folder and update it. |
| 35 | + |
| 36 | +#### 5. Implement end points. |
| 37 | + |
| 38 | +``` |
| 39 | +// in one of your controllers |
| 40 | +
|
| 41 | + // Auth endpoint |
| 42 | + public function authorize() |
| 43 | + { |
| 44 | + $this->autoRender = false; |
| 45 | +
|
| 46 | +
|
| 47 | + $this->loadComponent('Oauth2Server.Oauth2'); |
| 48 | +
|
| 49 | + $response = $this->Oauth2->authorize($this->request, $this->response); |
| 50 | + $response = $response->withHeader('Content-Type', 'application/json'); |
| 51 | +
|
| 52 | + return $response; |
| 53 | + } |
| 54 | + |
| 55 | + // callback endpoint |
| 56 | + public function code() |
| 57 | + { |
| 58 | + $this->autoRender = false; |
| 59 | + $response = $this->response |
| 60 | + ->withHeader('Content-Type', 'application/json') |
| 61 | + ->withStringBody(json_encode([ |
| 62 | + 'code' => urldecode($this->request->getQuery('code')) |
| 63 | + ])); |
| 64 | +
|
| 65 | + return $response; |
| 66 | + } |
| 67 | + |
| 68 | + // access token endpoint |
| 69 | + public function accessToken() |
| 70 | + { |
| 71 | + $this->autoRender = false; |
| 72 | +
|
| 73 | + $this->loadComponent('Oauth2Server.Oauth2'); |
| 74 | +
|
| 75 | +
|
| 76 | + $response = $this->Oauth2->accessToken($this->request, $this->response); |
| 77 | + $response = $response->withHeader('Content-Type', 'application/json'); |
| 78 | +
|
| 79 | + return $response; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +``` |
| 84 | + |
| 85 | +#### 6. Use middleware to secure your routes. |
| 86 | + |
| 87 | +``` |
| 88 | +// assuming you have a plugin named Api |
| 89 | +
|
| 90 | +//\Api\config\routes |
| 91 | +use Cake\Routing\RouteBuilder; |
| 92 | +use Cake\Routing\Router; |
| 93 | +use Cake\Routing\Route\DashedRoute; |
| 94 | +
|
| 95 | +use League\OAuth2\Server\Middleware\ResourceServerMiddleware; |
| 96 | +use Oauth2Server\OauthLogic\ServerUtility; |
| 97 | +
|
| 98 | +$serverUtility = new ServerUtility(); |
| 99 | +$server = $serverUtility->getPublicServer(); |
| 100 | +
|
| 101 | +
|
| 102 | +Router::plugin( |
| 103 | + 'Api', |
| 104 | + ['path' => '/api'], |
| 105 | + function (RouteBuilder $routes) use ($server) { |
| 106 | +
|
| 107 | + $routes->registerMiddleware('resourceServer', new ResourceServerMiddleware($server)); |
| 108 | + $routes->middlewareGroup('Oauth2Stack', ['resourceServer']); |
| 109 | +
|
| 110 | + $routes->applyMiddleware('Oauth2Stack'); |
| 111 | +
|
| 112 | +
|
| 113 | + $routes->scope('/v1', function ($routes) { |
| 114 | + $routes->fallbacks(DashedRoute::class); |
| 115 | + }); |
| 116 | +
|
| 117 | + } |
| 118 | +); |
| 119 | +
|
| 120 | +``` |
| 121 | + |
| 122 | +### 7. Use attributes to get total control in your actions if Validators are not enough |
| 123 | +`$this->request->getAttributes()` |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments