Passed
Pull Request — master (#38)
by Derek
10:37
created

Session::logout()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 4
nop 0
dl 0
loc 31
ccs 0
cts 20
cp 0
crap 20
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace Slides\Saml2;
4
5
use Illuminate\Http\RedirectResponse;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Facades\Cookie;
7
use Slides\Saml2\Models\Tenant;
8
use Slides\Saml2\Facades\Auth;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Slides\Saml2\Auth. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Slides\Saml2\Repositories\TenantRepository;
10
use Slides\Saml2\OneLoginBuilder;
11
use OneLogin\Saml2\Error as OneLoginError;
12
13
/**
14
 * Class Session
15
 *
16
 * @package Slides\Saml2
17
 */
18
class Session
19
{
20
    /**
21
     * @var TenantRepository
22
     */
23
    protected $tenants;
24
25
    /**
26
     * @var OneLoginBuilder
27
     */
28
    protected $builder;
29
30
    public function __construct(TenantRepository $tenants, OneLoginBuilder $builder)
31
    {
32
        $this->tenants = $tenants;
33
        $this->builder = $builder;
34
    }
35
36
    public function exists(): bool
37
    {
38
        return Cookie::has('saml_tenant_id');
39
    }
40
41
    public function store(Tenant $tenant, Saml2User $samlUser): void
42
    {
43
        Cookie::queue(cookie()->make('saml_tenant_id', $tenant->id, config('session.lifetime')));
0 ignored issues
show
Bug introduced by
The function cookie was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        Cookie::queue(/** @scrutinizer ignore-call */ cookie()->make('saml_tenant_id', $tenant->id, config('session.lifetime')));
Loading history...
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        Cookie::queue(cookie()->make('saml_tenant_id', $tenant->id, /** @scrutinizer ignore-call */ config('session.lifetime')));
Loading history...
44
        Cookie::queue(cookie()->make('saml_session_id', $samlUser->getSessionIndex(), config('session.lifetime')));
45
        Cookie::queue(cookie()->make('saml_name_id', $samlUser->getNameId(), config('session.lifetime')));
46
    }
47
48
    public function clear(): void
49
    {
50
        Cookie::queue(cookie()->forget('saml_tenant_id'));
0 ignored issues
show
Bug introduced by
The function cookie was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        Cookie::queue(/** @scrutinizer ignore-call */ cookie()->forget('saml_tenant_id'));
Loading history...
51
        Cookie::queue(cookie()->forget('saml_session_id'));
52
        Cookie::queue(cookie()->forget('saml_name_id'));
53
    }
54
55
    /**
56
     * Generates the redirect url to initiate a global session
57
     * sign out for a user with the IdP.
58
     */
59
    public function logout(): ?RedirectResponse
60
    {
61
        if (!$this->exists()) {
62
            return null;
63
        }
64
65
        $tenant = $this->resolveTenant();
66
        if (empty($tenant)) {
67
            return null;
68
        }
69
70
        $this->builder
71
            ->withTenant($tenant)
72
            ->bootstrap();
73
74
        try {
75
            $sloUrl = Auth::logout(
76
                config('saml2.logoutRoute'),
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
                /** @scrutinizer ignore-call */ 
77
                config('saml2.logoutRoute'),
Loading history...
77
                Cookie::get('saml_name_id'),
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facad...ie::get('saml_name_id') can also be of type array; however, parameter $nameId of Slides\Saml2\Auth::logout() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
                /** @scrutinizer ignore-type */ Cookie::get('saml_name_id'),
Loading history...
78
                Cookie::get('saml_session_id'),
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facad...:get('saml_session_id') can also be of type array; however, parameter $sessionIndex of Slides\Saml2\Auth::logout() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
                /** @scrutinizer ignore-type */ Cookie::get('saml_session_id'),
Loading history...
79
                null,
80
                true
81
            );
82
        } catch (OneLoginError $e) {
83
            report($e);
0 ignored issues
show
Bug introduced by
The function report was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            /** @scrutinizer ignore-call */ 
84
            report($e);
Loading history...
84
            return null;
85
        }
86
87
        return redirect($sloUrl)->withHeaders([
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        return /** @scrutinizer ignore-call */ redirect($sloUrl)->withHeaders([
Loading history...
88
            'Pragma' => 'no-cache',
89
            'Cache-Control' => 'no-cache, must-revalidate',
90
        ]);
91
    }
92
93
    /**
94
     * Resolve a tenant from the session.
95
     */
96
    protected function resolveTenant(): ?Tenant
97
    {
98
        $id = Cookie::get('saml_tenant_id');
99
        if (empty($id)) {
100
            return null;
101
        }
102
103
        return $this->tenants->findById($id);
0 ignored issues
show
Bug introduced by
$id of type array|string is incompatible with the type integer expected by parameter $id of Slides\Saml2\Repositorie...tRepository::findById(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        return $this->tenants->findById(/** @scrutinizer ignore-type */ $id);
Loading history...
104
    }
105
}
106