# Consumer service for AuthStack SAML 2.0 assertions

## Installation


### Step #1
Add Buckhill's satis to your `composer.json`

```
"repositories": [
  { "type": "composer", "url": "https://satis.buckhill.com.hr" }
]
```


### Step #2
Add `"buckhill/laravel-saml-sp": "*"` to your `composer.json` or `composer require "buckhill/laravel-saml-sp: *"`


### Step #3

Make sure you have `sso` middleware defined in your `App\Http\Kernel.php`:

```
'sso' => [
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
]
```

### Step #4

Make sure you have `account` middleware defined in your `App\Http\Kernel.php`.

`account` middleware is for routes that require authenticated pepople before allowing access.

```
'account' => [
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Buckhill\LaravelSAML\Http\Middleware\Authenticated::class
]
```


### Step #5

Create `config/sso.php` file. Use the file located in `samples/sp_config.php` and adjust it accordingly.


### Step #6

Open `config/app.php` and add the following to `providers` array:

```
'providers' => [
    Buckhill\LaravelSAML\Providers\AuthenticatedUserProvider::class,
    Buckhill\LaravelSAML\Providers\SAMLRoutesProvider::class,
]
```


### Step #7 

Modifying behavior after assertion consumer service does its job:

You can specify a different route / controller for ACS, you do it like this:

- open `config/sso.php` 
- find `SP.routes` key
- look for `SP.routes.acs` and change the path to controller. Default one is `Buckhill\LaravelSAML\Http\Controllers\SSO`
- Create your controller in `App\Http\Controllers` and `extend` the default controller

Example on how to communicate to Buckhill's Customer Service with extended ACS:

```php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use function AuthStack\SP\authn;
use Illuminate\Support\Facades\Log;
use AuthStack\SP\AttributeCollection;
use Buckhill\CustomerServerClient\Facade\CustomerServer;
use Buckhill\LaravelSAML\Http\Controllers\SSO as SSOController;

class SSO extends SSOController
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function acs(Request $request)
    {
        return parent::acs($request);
    }

    /**
     * @param Request $request
     * @param AttributeCollection $collection
     */
    protected function afterACS(Request $request, AttributeCollection $collection)
    {
        $data = [
            'type_id'           => 3,
            'signup_id'         => 1,
            'social_id'         => $collection->getNameIdValue(),
            'email'             => $collection->getNameIdValue(),
            'full_name'         => sprintf("%s %s", $collection->get('givenname')->getFirstAttributeValue(), $collection->get('sn')->getFirstAttributeValue()),
            'additional'        => [
                'organization'      => $collection->get('o')->getFirstAttributeValue(),
                'country'           => $collection->get('st')->getFirstAttributeValue(),
                'city'              => $collection->get('l')->getFirstAttributeValue(),
                'postalCode'        => $collection->get('postalcode')->getFirstAttributeValue(),
                'postaladdress'     => $collection->get('postaladdress')->getFirstAttributeValue(),
                'tel'               => $collection->get('telephonenumber')->getFirstAttributeValue()
            ]
        ];

        Log::info("SAML response returned", ['request' => $request->all()]);

        /** @var  $customer \Buckhill\CustomerServerClient\Model\Customer */
        $customer = CustomerServer::retrieve($data);

        $request->session()->put('profile', $customer->toArray());

        session()->put(config('sso.SP.sessionKeyName'), $customer->toArray());
    }
}

```




