PHP code example of aquaturtlium / cakephp-oauth-server
1. Go to this page and download the library: Download aquaturtlium/cakephp-oauth-server library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
aquaturtlium / cakephp-oauth-server example snippets
$this->addPlugin('OAuthServer');
'OAuthServer' => [
'privateKey' => CONFIG . 'oauth.pem',
'publicKey' => CONFIG . 'oauth.pub',
'encryptionKey' => 'def0000060c80a6856e8...', // <- SET encryption key FROM `vendor/bin/generate-defuse-key`
],
class Application extends BaseApplication
{
public function middleware($middleware)
{
$middleware
->add(ErrorHandlerMiddleware::class)
->add(AssetMiddleware::class)
// ADD THIS: bypass Authorization environment to request header
->add(\OAuthServer\Middleware\AuthorizationEnvironmentMiddleware::class)
->add(RoutingMiddleware::class);
return $middleware;
}
}
public function login()
{
$result = $this->Authentication->getResult();
// If the user is logged in send them away.
if ($result->isValid()) {
$target = $this->Authentication->getLoginRedirect() ?? '/home';
if ($this->request->getQuery('redir') === 'oauth') {
$target = [
'plugin' => 'OAuthServer',
'controller' => 'OAuth',
'action' => 'authorize',
'?' => $this->request->getQueryParams(),
];
}
return $this->redirect($target);
}
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error('Invalid username or password');
}
}
'login' => [
'className' => 'OAuthServer.Login'
]
namespace App\Controller;
use Crud\Controller\ControllerTrait;
/**
* OauthClients Controller
*
* @property \OAuthServer\Model\Table\OauthClientsTable $Clients
*/
class ClientsController extends AppController
{
use ControllerTrait;
public $modelClass = 'OAuthServer.Clients';
/**
* @return void
*/
public function initialize()
{
parent::initialize();
$this->viewClass = 'CrudView\View\CrudView';
$tables = [
'Clients',
'Scopes'
];
$this->loadComponent('Crud.Crud', [
'actions' => [
'index' => [
'className' => 'Crud.Index',
'scaffold' => [
'tables' => $tables
]
],
'view' => [
'className' => 'Crud.View',
'scaffold' => [
'tables' => $tables
]
],
'edit' => [
'className' => 'Crud.Edit',
'scaffold' => [
'tables' => $tables,
'fields' => [
'name',
'redirect_uri',
'parent_model',
'parent_id' => [
'label' => 'Parent ID',
'type' => 'text'
]
]
]
],
'add' => [
'className' => 'Crud.Add',
'scaffold' => [
'tables' => $tables,
'fields' => [
'name',
'redirect_uri',
'parent_model',
'parent_id' => [
'label' => 'Parent ID',
'type' => 'text'
]
]
]
],
'delete' => [
'className' => 'Crud.Delete',
'scaffold' => [
'tables' => $tables
]
],
],
'listeners' => [
'CrudView.View',
'Crud.RelatedModels',
'Crud.Redirect',
'Crud.Api'
],
]);
}
}
namespace App\Controller;
use Crud\Controller\ControllerTrait;
/**
* Scopes Controller
*
* @property \OAuthServer\Model\Table\OauthScopesTable $Scopes
*/
class ScopesController extends AppController
{
use ControllerTrait;
public $modelClass = 'OAuthServer.Scopes';
/**
* @return void
*/
public function initialize()
{
parent::initialize();
$this->viewClass = 'CrudView\View\CrudView';
$tables = [
'Clients',
'Scopes'
];
$this->loadComponent('Crud.Crud', [
'actions' => [
'index' => [
'className' => 'Crud.Index',
'scaffold' => [
'tables' => $tables
]
],
'view' => [
'className' => 'Crud.View',
'scaffold' => [
'tables' => $tables
]
],
'edit' => [
'className' => 'Crud.Edit',
'scaffold' => [
'tables' => $tables,
'fields' => [
'id' => [
'label' => 'ID',
'type' => 'text'
],
'description',
]
]
],
'add' => [
'className' => 'Crud.Add',
'scaffold' => [
'tables' => $tables,
'fields' => [
'id' => [
'label' => 'ID',
'type' => 'text'
],
'description',
]
]
],
'delete' => [
'className' => 'Crud.Delete',
'scaffold' => [
'tables' => $tables
]
],
],
'listeners' => [
'CrudView.View',
'Crud.RelatedModels',
'Crud.Redirect',
],
]);
}
}