Skip to content

Commit

Permalink
Merge pull request #16 from italia/certs-in-files
Browse files Browse the repository at this point in the history
Allow private key and certificate to be stored in files
  • Loading branch information
pdavide authored Oct 3, 2019
2 parents 4aee0eb + fc50ee9 commit 6c9ccee
Show file tree
Hide file tree
Showing 30 changed files with 745 additions and 374 deletions.
40 changes: 40 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# http://editorconfig.org

# A special property that should be specified at the top of the file outside of
# any sections. Set to true to stop .editor config file search on current file
root = true

[*]
# Indentation style
# Possible values - tab, space
indent_style = space

# Line ending file format
# Possible values - lf, crlf, cr
end_of_line = lf

# File character encoding
# Possible values - latin1, utf-8, utf-16be, utf-16le
charset = utf-8

# Denotes whether to trim whitespace at the end of lines
# Possible values - true, false
trim_trailing_whitespace = true

# Denotes whether file should end with a newline
# Possible values - true, false
insert_final_newline = true

# Indentation size in single-spaced characters
# Possible values - an integer, tab
indent_size = 4

[*.{css,scss,sass}]
# Indentation size in single-spaced characters
# Possible values - an integer, tab
indent_size = 2

[*.{yml,yaml}]
# Indentation size in single-spaced characters
# Possible values - an integer, tab
indent_size = 2
41 changes: 41 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
use PhpCsFixer\Finder;
use PhpCsFixer\Config;
use PhpCsFixer\FixerInterface;

$excluded_folders = [
'config',
'bootstrap/cache',
'node_modules',
'public',
'resources',
'storage',
'vendor',
];

$rules = [
'@PSR2' => true,
'@Symfony' => true,
'ordered_class_elements' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'linebreak_after_opening_tag' => true,
'no_empty_comment' => false,
'ordered_imports' => true,
'phpdoc_align' => ['align' => 'left'],
'phpdoc_order' => true,
];

$finder = Finder::create()
->exclude($excluded_folders)
->notPath('server.php')
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true)
->in(__DIR__);

return Config::create()
->setRules($rules)
->setFinder($finder)
->setUsingCache(true);
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ before_install:
install:
- travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest

script:
script:
- vendor/bin/pds-skeleton validate
- vendor/bin/php-cs-fixer fix --diff --dry-run --verbose
- vendor/bin/phpunit

after_success:
Expand Down
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ applications based on [Laravel 5](https://www.laravel.com).

1. Before installing this package patching must be enabled in `composer.json`.
This is necessary because
[this patch](https://rawgit.com/italia/spid-laravel/master/patches/php-saml-3-spid.patch)
[this patch](https://rawgit.com/italia/spid-laravel/master/patches/php-saml-3.3.0-spid.patch)
has to be applied to [onelogin/php-saml](https://github.com/onelogin/php-saml)
for SPID compatibility.

Expand All @@ -45,7 +45,7 @@ for SPID compatibility.
composer config prefer-stable true
```
**This installation step will be removed before the first stable release of
**This installation step will be removed before the first stable release of
this package.**
2. Require this package with composer.
Expand Down Expand Up @@ -77,6 +77,7 @@ can't know what CSRF token include in their POST requests sent to your routes.
];
}
```

## Configuration

Publish the configuration with:
Expand Down Expand Up @@ -145,6 +146,29 @@ system of your application. If you plan to integrate your authentication system
with SPID, you can listen to the `LoginEvent` and `LogoutEvent` (see
[Events](#events) and [Example](#example)).

### SPIDAuth Service Provider

If you need more customization in the authentication logic of your application,
you can use the methods available in the `SPIDAuth` Service Provider.

First you need an instance of the Service Provider from the Service Container:
```php
$SPIDAuth = app('SPIDAuth');
```

The following public methods can be used in your application.

| Method | Description |
|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| login | Show the configured `login_view` with a SPID button, if authenticated redirect to `after_login_url`. |
| doLogin | Attempt login with the SPID Identity Provider in the current request and redirect to the intended or configured `after_login_url` if authenticated. |
| acs | Process the POST response from Identity Providers, set session variables and redirect to the intended or configured `after_login_url`. |
| logout | Attempt logout with the SPID Identity Provider stored in the current session. |
| isAuthenticated | Check if the current session is authenticated with SPID. |
| metadata | Show metadata for this SPID Service Provider. |
| providers | Identity Providers list in JSON format used by the SPID smart button. |
| getSPIDUser | Return the current authenticated SPIDUser or `null` if not authenticated. |

### Button

You can display a simple SPID access button by including the
Expand Down Expand Up @@ -366,6 +390,10 @@ You can set your own X.509 certificate and private key in the
`config/spid-auth.php` file of your application (which overrides the one in the
package).

The X.509 certificate and the private key can be configured as strings or as
paths to files. If both are specified in your `config/spid-auth.php` then the
ones specified as strings will take precedence.

**Change the values and keep the private key secret**.

### SPID smart button
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
"Compatibility with SPID": "https://rawgit.com/italia/spid-laravel/master/patches/php-saml-3.3.0-spid.patch"
}
}
},
"archive": {
"exclude": ["/tests", ".travis.yml", "/certs", ".editorconfig", "php_cs.dist"]
}
}
2 changes: 2 additions & 0 deletions config/spid-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#'digitalAddress',
#'expirationDate'
],
'sp_certificate_file' => null,
'sp_private_key_file' => null,
'sp_certificate' => 'MIIEKjCCAxKgAwIBAgIJAOlEQZ6zeWkPMA0GCSqGSIb3DQEBBQUAMGsxCzAJBgNVBAYTAklUMQswCQYDVQQIEwJSTTENMAsGA1UEBxMEUm9tZTEmMCQGA1UEChMdQWdlbnppYSBwZXIgbCdJdGFsaWEgRGlnaXRhbGUxGDAWBgNVBAMTD3d3dy5hZ2lkLmdvdi5pdDAeFw0xNzEyMTExMTE2NTJaFw0yNzEyMTExMTE2NTJaMGsxCzAJBgNVBAYTAklUMQswCQYDVQQIEwJSTTENMAsGA1UEBxMEUm9tZTEmMCQGA1UEChMdQWdlbnppYSBwZXIgbCdJdGFsaWEgRGlnaXRhbGUxGDAWBgNVBAMTD3d3dy5hZ2lkLmdvdi5pdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALooizGSq3EdEzr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dcw3GSVYh0hV89dd4oGKmB8wTfEZWdq73g14xK3Q8Hn95+ZsQAXAVKECgYAvKLy9RVg12LJeYOUUIjKunf/F/EC8lvqHnLH5il83h10nhjKVmyXsR6FuvAz5T1XBXrlotdV2hfqUa4OH4aB9ewbDI0IjhlXPUeC20roenqUzwpIIUriNLeCPyb7g5nVUX9CXn8MuPxgYf5sZNw7aLXVrWFU/hdoeSCi+QB8rowKBgQDHaCH7NAXpo3yDtdN+PPuZPnXnTMHQctLbXytuzqqDNbC5z4EVPv8PEOVl8h6CEk7IhdQBnpxd8s6IODRNy7csqoQduvaLhNJiahVSbsZFsjLs4IaO3kRfaUL7I8BaW+1I4pfa2JLSZFfEv4BguqW3QEOH7BuxaSi78GvNwnvaag==',

Expand Down
55 changes: 27 additions & 28 deletions routes/spid-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@
/**
* This file contains the routes needed for SPIDAuth Package.
*
* @package Italia\SPIDAuth
* @license BSD-3-clause
*/

Route::group([
'prefix' => config('spid-auth.routes_prefix'),
'middleware' => config('spid-auth.middleware_group')
'middleware' => config('spid-auth.middleware_group'),
], function () {
Route::get('login', [
'as' => 'spid-auth_login',
'uses' => 'SPIDAuth@login'
]);
Route::post('login', [
'as' => 'spid-auth_do-login',
'uses' => 'SPIDAuth@doLogin'
]);
Route::match(['get', 'post'], 'logout', [
'as' => 'spid-auth_logout',
'uses' => 'SPIDAuth@logout'
]);
Route::post('acs', [
'as' => 'spid-auth_acs',
'uses' => 'SPIDAuth@acs'
]);
Route::get('metadata', [
'as' => 'spid-auth_metadata',
'uses' => 'SPIDAuth@metadata'
]);
Route::get('providers', [
'as' => 'spid-auth_providers',
'uses' => 'SPIDAuth@providers'
]);
});
Route::get('login', [
'as' => 'spid-auth_login',
'uses' => 'SPIDAuth@login',
]);
Route::post('login', [
'as' => 'spid-auth_do-login',
'uses' => 'SPIDAuth@doLogin',
]);
Route::match(['get', 'post'], 'logout', [
'as' => 'spid-auth_logout',
'uses' => 'SPIDAuth@logout',
]);
Route::post('acs', [
'as' => 'spid-auth_acs',
'uses' => 'SPIDAuth@acs',
]);
Route::get('metadata', [
'as' => 'spid-auth_metadata',
'uses' => 'SPIDAuth@metadata',
]);
Route::get('providers', [
'as' => 'spid-auth_providers',
'uses' => 'SPIDAuth@providers',
]);
}
);
24 changes: 9 additions & 15 deletions src/Console/CommandExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Illuminate\Auth\Console\AuthMakeCommand class which is part of the Laravel
* Framework project and licensed under the MIT license.
*
* @package Italia\SPIDAuth
* @license BSD-3-clause
* @license MIT
*/
Expand Down Expand Up @@ -47,8 +46,6 @@ class CommandExample extends Command

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
Expand All @@ -73,7 +70,7 @@ public function handle()

file_put_contents(
base_path('routes/web.php'),
file_get_contents(__DIR__.'/stubs/example/routes.stub'),
file_get_contents(__DIR__ . '/stubs/example/routes.stub'),
FILE_APPEND
);

Expand All @@ -82,36 +79,32 @@ public function handle()

/**
* Create the directory for the files.
*
* @return void
*/
protected function createDirectories()
{
if (! is_dir($directory = resource_path('views/layouts'))) {
if (!is_dir($directory = resource_path('views/layouts'))) {
mkdir($directory, 0755, true);
}

if (! is_dir($directory = app_path('Listeners'))) {
if (!is_dir($directory = app_path('Listeners'))) {
mkdir($directory, 0755, true);
}
}

/**
* Export the example views.
*
* @return void
*/
protected function exportViews()
{
foreach ($this->views as $key => $value) {
if (file_exists($view = resource_path('views/'.$value))) {
if (! $this->confirm("The [{$value}] view already exists. Do you want to replace it?")) {
if (file_exists($view = resource_path('views/' . $value))) {
if (!$this->confirm("The [{$value}] view already exists. Do you want to replace it?")) {
continue;
}
}

copy(
__DIR__.'/stubs/example/views/'.$key,
__DIR__ . '/stubs/example/views/' . $key,
$view
);
}
Expand All @@ -121,14 +114,15 @@ protected function exportViews()
* Compiles the stubs.
*
* @param string The stub to compile
*
* @return string The compiled stub
*/
protected function compileStub($stubName)
protected function compileStub(string $stubName): string
{
return str_replace(
'{{namespace}}',
$this->getAppNamespace(),
file_get_contents(__DIR__.'/stubs/example/'.$stubName.'.stub')
file_get_contents(__DIR__ . '/stubs/example/' . $stubName . '.stub')
);
}
}
6 changes: 3 additions & 3 deletions src/Console/stubs/example/controllers/HomeController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace {{namespace}}Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\View\View;

class HomeController extends Controller
{
/**
* Show the application home.
*
* @return \Illuminate\Http\Response
* @return View
*/
public function index()
public function index(): View
{
return view('home');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Console/stubs/example/controllers/PrivateController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace {{namespace}}Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\View\View;

class PrivateController extends Controller
{
Expand All @@ -19,9 +19,9 @@ class PrivateController extends Controller
/**
* Show the application private area.
*
* @return \Illuminate\Http\Response
* @return View
*/
public function index()
public function index(): View
{
return view('private')->with('spid_user', resolve('SPIDAuth')->getSPIDUser());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ class SPIDEventSubscriber
'{{namespace}}Listeners\SPIDEventSubscriber@onSPIDLogout'
);
}

}
Loading

0 comments on commit 6c9ccee

Please sign in to comment.