Test Failed
Pull Request — master (#108)
by
unknown
06:09
created

ResolveIdp::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Slides\Saml2\Http\Middleware;
4
5
use Illuminate\Support\Facades\Log;
6
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
7
use Slides\Saml2\OneLoginBuilder;
8
9
/**
10
 * Class ResolveIdp
11
 *
12
 * @package Slides\Saml2\Http\Middleware
13
 */
14
class ResolveIdp
15
{
16
17
    /**
18
     * @var OneLoginBuilder
19
     */
20
    protected $builder;
21
22
    /**
23
     * ResolveIdp constructor.
24
     *
25
     * @param OneLoginBuilder $builder
26
     */
27
    public function __construct(OneLoginBuilder $builder)
28
    {
29
        $this->builder = $builder;
30
    }
31
32
    /**
33
     * Handle an incoming request.
34
     *
35
     * @param  \Illuminate\Http\Request  $request
36
     * @param  \Closure  $next
37
     *
38
     * @throws NotFoundHttpException
39
     *
40
     * @return mixed
41
     */
42
    public function handle($request, \Closure $next)
43
    {
44
        if(!$idp = $this->resolveIdp($request)) {
45
            throw new NotFoundHttpException();
46
        }
47
48
        if (config('saml2.debug')) {
49
            Log::debug('[Saml2] IdP resolved', [
50
                'key' => $idp['key'],
51
            ]);
52
        }
53
54
        session()->flash('saml2.idp.key', $idp['key']);
55
56
        $this->builder
57
            ->withIdp($idp)
58
            ->bootstrap();
59
60
        return $next($request);
61
    }
62
63
    /**
64
     * Resolve an IdP by a request.
65
     *
66
     * @param  \Illuminate\Http\Request  $request
67
     *
68
     * @return array|null
69
     */
70
    protected function resolveIdp($request)
71
    {
72
        if(!$key = $request->route('key')) {
73
            if (config('saml2.debug')) {
74
                Log::debug('[Saml2] IdP key is not present in the URL so cannot be resolved', [
75
                    'url' => $request->fullUrl()
76
                ]);
77
            }
78
79
            return null;
80
        }
81
82
        if(!$idp = config("saml2.idps.$key")) {
83
            if (config('saml2.debug')) {
84
                Log::debug('[Saml2] Unknown IdP requested', [
85
                  'key' => $key
86
                ]);
87
            }
88
89
            return null;
90
        }
91
92
        $idp['key'] = $key;
93
94
        return $idp;
95
    }
96
}
97