PHPackages                             doonot/oauth-server - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. doonot/oauth-server

ActiveCakephp-plugin[Authentication &amp; Authorization](/categories/authentication)

doonot/oauth-server
===================

OAuth Server for CakePHP 3 using the PHP League's OAuth2 Server

v0.2.0(10y ago)03MITPHPPHP &gt;= 5.5

Since May 22Pushed 6y agoCompare

[ Source](https://github.com/doonot/oauth-server)[ Packagist](https://packagist.org/packages/doonot/oauth-server)[ RSS](/packages/doonot-oauth-server/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (4)Versions (8)Used By (0)

OAuth2 Server for CakePHP 3
===========================

[](#oauth2-server-for-cakephp-3)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.txt)[![Build Status](https://camo.githubusercontent.com/2c3f0824b49058ea81aad29b236f2d9a565ba6b820751e6fe1511c852874367b/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f756166726963612f6f617574682d7365727665722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/uafrica/oauth-server)

A plugin for implementing an OAuth2 server in CakePHP 3. Built on top of the [PHP League's OAuth2 Server](http://oauth2.thephpleague.com/). Currently we support the following grant types: AuthCode, RefreshToken, ClientCredentials.

Installation
------------

[](#installation)

Installation is done using composer. Run:

```
$ composer require uafrica/oauth-server
```

Once composer has installed the package, the plugin needs to be activated by running:

```
$ bin/cake plugin load -r OAuthServer
```

Finally the database migrations need to be run.

```
$ bin/cake migrations migrate --plugin OAuthServer
```

Configuration
-------------

[](#configuration)

It is assumed that you already have working Form based authentication using the built in CakePHP 3 authentication component. If you do not, please read [the authentication chapter](http://book.cakephp.org/3.0/en/controllers/components/authentication.html).

Set OAuthServer as an authentication adaptor.

In your `AppController::beforeFilter()` method, add (or modify)

```
$this->Auth->config('authenticate', [
    'Form',
    'OAuthServer.OAuth'
]);
```

Change your login method to look as follows:

```
public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            $redirectUri = $this->Auth->redirectUrl();
            if ($this->request->query['redir'] === 'oauth') {
                $redirectUri = [
                    'plugin' => 'OAuthServer',
                    'controller' => 'OAuth',
                    'action' => 'authorize',
                    '?' => $this->request->query
                ];
            }
            return $this->redirect($redirectUri);
        } else {
            $this->Flash->error(
                __('Username or password is incorrect'),
                'default',
                [],
                'auth'
            );
        }
    }
}
```

Alternatively, if you are using the [Friends Of Cake CRUD plugin](https://github.com/friendsofcake/crud), add

```
'login' => [
    'className' => 'OAuthServer.Login'
]
```

to your CRUD actions config.

Usage
-----

[](#usage)

The base OAuth2 path is `example.com/oauth`.

In order to add clients and OAuth scopes you need to create a `ClientsController` and a `ScopesController` (Which is not part of this plugin)

The simplest way is to make use of the [Friends Of Cake CRUD-View plugin](https://github.com/friendsofcake/crud-view).

Install it by running

```
$ composer require friendsofcake/bootstrap-ui:dev-master
$ composer require friendsofcake/crud:dev-master
$ composer require friendsofcake/crud-view:dev-master
```

Then create a `ClientsController` that looks like:

```
