PHPackages                             elstc/cakephp-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. elstc/cakephp-oauth-server

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

elstc/cakephp-oauth-server
==========================

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

v0.8.7(2y ago)56023[1 issues](https://github.com/nojimage/cakephp-oauth-server/issues)[1 PRs](https://github.com/nojimage/cakephp-oauth-server/pulls)MITPHPPHP &gt;= 7.1

Since May 22Pushed 2y agoCompare

[ Source](https://github.com/nojimage/cakephp-oauth-server)[ Packagist](https://packagist.org/packages/elstc/cakephp-oauth-server)[ RSS](/packages/elstc-cakephp-oauth-server/feed)WikiDiscussions 0.8.x Synced 6d ago

READMEChangelog (8)Dependencies (5)Versions (18)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/f2ccc71d45bbefad79cdece7937c4758ded994eacb2dce0990a0fc0ae0222ccf/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6e6f6a696d6167652f63616b657068702d6f617574682d7365727665722f302e382e782e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/nojimage/cakephp-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.

This repository is a fork of [uafrica/oauth-server](https://github.com/uafrica/oauth-server).

Requirements
------------

[](#requirements)

- PHP &gt;= 7.1 with openssl extension
- CakePHP &gt;= 3.5
- Database (MySQL, SQLite tested)

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

[](#installation)

You can install this plugin into your CakePHP application using. Run:

```
composer require elstc/cakephp-oauth-server
```

### Load plugin

[](#load-plugin)

(CakePHP &gt;= 3.6.0) Load the plugin by adding the following statement in your project's `src/Application.php`:

```
$this->addPlugin('OAuthServer');
```

(CakePHP &lt;= 3.5.x) Load the plugin by adding the following statement in your project's `config/bootstrap.php` file:

```
Plugin::load('OAuthServer', ['bootstrap' => true, 'route' => true]);
```

### Run database migration

[](#run-database-migration)

The database migrations need to be run.

```
bin/cake migrations migrate -p OAuthServer
```

### Generating and setup keys

[](#generating-and-setup-keys)

Generating `private and public keys` (see also ):

```
openssl genrsa -out config/oauth.pem 2048
openssl rsa -in config/oauth.pem -pubout -out config/oauth.pub
```

Generating `encryption key` :

```
vendor/bin/generate-defuse-key
(COPY result hash)
```

Change your app.php, Add `OAuthServer` configuration :

```
    'OAuthServer' => [
        'privateKey' => CONFIG . 'oauth.pem',
        'publicKey' => CONFIG . 'oauth.pub',
        'encryptionKey' => 'def0000060c80a6856e8...', // add(ErrorHandlerMiddleware::class)

            ->add(AssetMiddleware::class)

            // ADD THIS: bypass Authorization environment to request header
            ->add(\OAuthServer\Middleware\AuthorizationEnvironmentMiddleware::class)

            ->add(RoutingMiddleware::class);

        return $middleware;
    }
}
```

It is recommended to insert between AssetMiddleware and RoutingMiddleware.

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->getQuery('redir') === 'oauth') {
                $redirectUri = [
                    'plugin' => 'OAuthServer',
                    'controller' => 'OAuth',
                    'action' => 'authorize',
                    '?' => $this->request->getQueryParams(),
                ];
            }

            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:

```
