|
1 |
| -# oauth-server |
2 |
| -OAuth Server |
| 1 | +# OAuth2 Server for CakePHP 3 |
| 2 | + |
| 3 | +A plugin for implementing an OAuth2 server in CakePHP 3. Built on top of the [PHP League's OAuth2 Server](http://oauth2.thephpleague.com/). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Installation is done using composer. Run: |
| 8 | + |
| 9 | +```bash |
| 10 | +$ composer require uafrica/oauth-server |
| 11 | +``` |
| 12 | + |
| 13 | +Once composer has installed the package, the plugin needs to be activated by running: |
| 14 | + |
| 15 | +```bash |
| 16 | +$ bin/cake plugin load OAuthServer --routes |
| 17 | +$ bin/cake plugin load Crud |
| 18 | +$ bin/cake plugin load CrudView |
| 19 | +$ bin/cake plugin load BootstrapUI |
| 20 | +``` |
| 21 | + |
| 22 | +Finally the database migrations need to be run. |
| 23 | + |
| 24 | +```bash |
| 25 | +$ bin/cake migrations migrate --plugin OAuthServer |
| 26 | +``` |
| 27 | + |
| 28 | +## Configuration |
| 29 | + |
| 30 | +It is assumed that you already have working Form based authentication using the built in CakePHP 3 authentication component. |
| 31 | +If you do not, please read [the authentication chapter](http://book.cakephp.org/3.0/en/controllers/components/authentication.html). |
| 32 | + |
| 33 | +Set OAuthServer as an authentication adaptor. |
| 34 | + |
| 35 | +In your `AppController` `beforeFilter` method, add (or modify) |
| 36 | + |
| 37 | +```php |
| 38 | +$this->Auth->config('authenticate', [ |
| 39 | + 'Form', |
| 40 | + 'OAuthServer' |
| 41 | +]); |
| 42 | +``` |
| 43 | + |
| 44 | +Change your login method to look as follows: |
| 45 | + |
| 46 | +```php |
| 47 | +public function login() |
| 48 | +{ |
| 49 | + if ($this->request->is('post')) { |
| 50 | + $user = $this->Auth->identify(); |
| 51 | + if ($user) { |
| 52 | + $this->Auth->setUser($user); |
| 53 | + $redirect_uri = $this->Auth->redirectUrl(); |
| 54 | + if ($this->request->query['redir'] === 'oauth') { |
| 55 | + $redirect_uri = [ |
| 56 | + 'plugin' => 'OAuthServer', |
| 57 | + 'controller' => 'OAuth', |
| 58 | + 'action' => 'authorize', |
| 59 | + '?' => $this->request->query |
| 60 | + ]; |
| 61 | + } |
| 62 | + return $this->redirect($redirect_uri); |
| 63 | + } else { |
| 64 | + $this->Flash->error( |
| 65 | + __('Username or password is incorrect'), |
| 66 | + 'default', |
| 67 | + [], |
| 68 | + 'auth' |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +Alternatively, if you are using the [Friends Of Cake CRUD plugin](https://github.com/friendsofcake/crud), add |
| 76 | + |
| 77 | +```php |
| 78 | +'login' => [ |
| 79 | + 'className' => 'OAuthServer.Login' |
| 80 | +] |
| 81 | +``` |
| 82 | + |
| 83 | +to your CRUD actions config. |
| 84 | + |
| 85 | +## Usage |
| 86 | + |
| 87 | +Visit `example.com/oauth/clients` to create OAuth clients, and `example.com/oauth/scopes` to create OAuth scopes. |
| 88 | + |
| 89 | +The base OAuth2 path with `example.com/oauth` |
| 90 | + |
| 91 | +## Customisation |
| 92 | + |
| 93 | +The OAuth2 Server can be customised, the look for the various pages can be changed by creating templates in `Template/Plugin/OAuthServer/OAuth` |
| 94 | + |
| 95 | +The server also fires a number of events that can be used to inject values into the process. The current events fired are: |
| 96 | + |
| 97 | +* `OAuthServer.beforeAuthorize` - On rendering of the approval page for the user. |
| 98 | +* `OAuthServer.afterAuthorize` - On the user authorising the client |
| 99 | +* `OAuthServer.afterDeny` - On the user denying the client |
| 100 | +* `OAuthServer.getUser` - On loading user details for authentication requests. |
0 commit comments