Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware authentication in the connector #12

Merged
merged 9 commits into from
May 15, 2019
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,35 @@ Authentication for CKFinder is not configured yet, so you will see an error resp

## Configuring Authentication

CKFinder connector authentication is managed by the `authentication` option in the connector configuration file (`config/ckfinder.php`).
It expects a [PHP callable](http://php.net/manual/pl/language.types.callable.php) value that after calling would return a Boolean value to decide if the user should have access to CKFinder.
As you can see, the default service implementation is not complete and simply returns `false`.
CKFinder connector authentication is handled by [middleware](https://laravel.com/docs/5.8/middleware) class or alias. To create the custom middleware class, use the artisan command:

A basic implementation that returns `true` from the `authentication` callable (which is obviously **not secure**) can look like below:
```bash
php artisan make:middleware CustomCKFinderAuth
```

The new middleware class will appear in `app/Http/Middleware/CustomCKFinderAuth.php`. Change the `authentication` option in `config/ckfinder.php`:

```php
// config/ckfinder.php
$config['authentication'] = '\App\Http\Middleware\CustomCKFinderAuth';
```

The `handle` method in `CustomCKFinderAuth` class allows to authenticate CKFinder users. A basic implementation that returns `true` from the `authentication` callable (which is obviously **not secure**) can look like below:

$config['authentication'] = function () {
return true;
};
```php
public function handle($request, Closure $next)
{
config(['ckfinder.authentication' => function() {
return true;
}]);
return $next($request);
}
```

Please have a look at the [CKFinder for PHP connector documentation](https://ckeditor.com/docs/ckfinder/ckfinder3-php/configuration.html#configuration_options_authentication) to find out
more about this option.



## Configuration Options

The CKFinder connector configuration is taken from the `config/ckfinder.php` file.
Expand Down
18 changes: 18 additions & 0 deletions src/CKFinderMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace CKSource\CKFinderBridge;

use Closure;

class CKFinderMiddleware
{
public function handle($request, Closure $next)
{
config(['ckfinder.authentication' => function() use ($request) {

return false;
}] );

return $next($request);
}
}
16 changes: 16 additions & 0 deletions src/Controller/CKFinderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
*/
class CKFinderController extends Controller
{
/**
* Use custom middleware to handle custom authentication and redirects.
*/
public function __construct()
{
$authenticationMiddleware = config('ckfinder.authentication');

if(!is_callable($authenticationMiddleware)) {
if(isset($authenticationMiddleware) && is_string($authenticationMiddleware)) {
$this->middleware($authenticationMiddleware);
} else {
$this->middleware(\CKSource\CKFinderBridge\CKFinderMiddleware::class);
}
}
}

/**
* Action that handles all CKFinder requests.
*
Expand Down
4 changes: 1 addition & 3 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

$config = array();

$config['authentication'] = function () {
return false;
};
$config['authentication'] = '\CKSource\CKFinderBridge\CKFinderMiddleware';

/*============================ License Key ============================================*/
// http://docs.cksource.com/ckfinder3-php/configuration.html#configuration_options_licenseKey
Expand Down