Created
August 29, 2019 09:00
-
-
Save alexdd55/ecb5537a38a0871a07e19cee6a67f48e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | |
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | |
* | |
* Licensed under The MIT License | |
* For full copyright and license information, please see the LICENSE.txt | |
* Redistributions of files must retain the above copyright notice. | |
* | |
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | |
* @link https://cakephp.org CakePHP(tm) Project | |
* @since 3.3.0 | |
* @license https://opensource.org/licenses/mit-license.php MIT License | |
*/ | |
namespace App; | |
use Authentication\AuthenticationServiceInterface; | |
use Cake\Core\Configure; | |
use Cake\Core\Exception\MissingPluginException; | |
use Cake\Error\Middleware\ErrorHandlerMiddleware; | |
use Cake\Http\BaseApplication; | |
use Cake\Http\MiddlewareQueue; | |
use Cake\Routing\Middleware\AssetMiddleware; | |
use Cake\Routing\Middleware\RoutingMiddleware; | |
use Authentication\AuthenticationService; | |
use Cake\Routing\Router; | |
use Authentication\Middleware\AuthenticationMiddleware; | |
use Psr\Http\Message\ResponseInterface; | |
use Psr\Http\Message\ServerRequestInterface; | |
/** | |
* Application setup class. | |
* | |
* This defines the bootstrapping logic and middleware layers you | |
* want to use in your application. | |
*/ | |
class Application extends BaseApplication | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function bootstrap(): void { | |
// Call parent to load bootstrap from files. | |
parent::bootstrap(); | |
if (PHP_SAPI === 'cli') { | |
$this->bootstrapCli(); | |
} | |
/* | |
* Only try to load DebugKit in development mode | |
* Debug Kit should not be installed on a production system | |
*/ | |
if (Configure::read('debug')) { | |
Configure::write('DebugKit.forceEnable', true); | |
$this->addPlugin('DebugKit'); | |
} | |
// $this->addPlugin('Authentication'); | |
// Load more plugins here | |
} | |
/** | |
* @return void | |
*/ | |
protected function bootstrapCli(): void { | |
try { | |
$this->addPlugin('Bake'); | |
} catch (MissingPluginException $e) { | |
// Do not halt if the plugin is missing | |
} | |
$this->addPlugin('Migrations'); | |
// Load more plugins here | |
} | |
/** | |
* Returns a service provider instance. | |
* | |
* @return AuthenticationService | |
*/ | |
public function getAuthenticationService(): AuthenticationService { | |
$service = new AuthenticationService([ | |
'authenticators' => [ | |
'Authentication.LDAP' | |
], | |
'identifiers' => [ | |
'Authentication.LDAP' | |
], | |
]); | |
$service->loadIdentifier('Authentication.LDAP', [ | |
'fields' => [ | |
'username' => 'username', | |
'password' => 'password' | |
], | |
'host' => 'ldaps://some.stuff.com', | |
'port' => 636, | |
'bindDN' => function ($username) { | |
return 'AD\\' . $username; | |
}, | |
'options' => [ | |
'LDAP_OPT_PROTOCOL_VERSION' => 3 | |
] | |
]); | |
$service->loadAuthenticator('Authentication.Session'); | |
$service->loadAuthenticator('Authentication.LDAP'); | |
// Load the authenticators, you want session first | |
return $service; | |
} | |
/** | |
* Setup the middleware queue your application will use. | |
* | |
* @param MiddlewareQueue $middlewareQueue The middleware queue to setup. | |
* @return MiddlewareQueue The updated middleware queue. | |
*/ | |
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue { | |
$authentication = new AuthenticationMiddleware($this->getAuthenticationService()); | |
$middlewareQueue | |
// Catch any exceptions in the lower layers, | |
// and make an error page/response | |
->add(new ErrorHandlerMiddleware(null, Configure::read('Error'))) | |
// Handle plugin/theme assets like CakePHP normally does. | |
->add(new AssetMiddleware([ | |
'cacheTime' => Configure::read('Asset.cacheTime'), | |
])) | |
->add($authentication) | |
// Add routing middleware. | |
// If you have a large number of routes connected, turning on routes | |
// caching in production could improve performance. For that when | |
// creating the middleware instance specify the cache config name by | |
// using it's second constructor argument: | |
// `new RoutingMiddleware($this, '_cake_routes_')` | |
->add(new RoutingMiddleware($this)); | |
return $middlewareQueue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment