getPostAuthenticationProvider()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Foundation\Security;
13
14
use Jitamin\Bus\Event\AuthFailureEvent;
15
use Jitamin\Bus\Event\AuthSuccessEvent;
16
use Jitamin\Foundation\Base;
17
use LogicException;
18
19
/**
20
 * Authentication Manager.
21
 */
22
class AuthenticationManager extends Base
23
{
24
    /**
25
     * Event names.
26
     *
27
     * @var string
28
     */
29
    const EVENT_SUCCESS = 'auth.success';
30
    const EVENT_FAILURE = 'auth.failure';
31
32
    /**
33
     * List of authentication providers.
34
     *
35
     * @var array
36
     */
37
    private $providers = [];
38
39
    /**
40
     * Register a new authentication provider.
41
     *
42
     * @param AuthenticationProviderInterface $provider
43
     *
44
     * @return AuthenticationManager
45
     */
46
    public function register(AuthenticationProviderInterface $provider)
47
    {
48
        $this->providers[$provider->getName()] = $provider;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Register a new authentication provider.
55
     *
56
     * @param string $name
57
     *
58
     * @return AuthenticationProviderInterface|OAuthAuthenticationProviderInterface|PasswordAuthenticationProviderInterface|PreAuthenticationProviderInterface|OAuthAuthenticationProviderInterface
59
     */
60
    public function getProvider($name)
61
    {
62
        if (!isset($this->providers[$name])) {
63
            throw new LogicException('Authentication provider not found: '.$name);
64
        }
65
66
        return $this->providers[$name];
67
    }
68
69
    /**
70
     * Execute providers that are able to validate the current session.
71
     *
72
     * @return bool
73
     */
74
    public function checkCurrentSession()
75
    {
76
        if ($this->userSession->isLogged()) {
0 ignored issues
show
Documentation introduced by
The property userSession does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
77
            foreach ($this->filterProviders('SessionCheckProviderInterface') as $provider) {
78
                if (!$provider->isValidSession()) {
79
                    $this->logger->debug('Invalidate session for '.$this->userSession->getUsername());
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property userSession does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
                    $this->sessionStorage->flush();
0 ignored issues
show
Documentation introduced by
The property sessionStorage does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
                    $this->preAuthentication();
82
83
                    return false;
84
                }
85
            }
86
        }
87
88
        return true;
89
    }
90
91
    /**
92
     * Execute pre-authentication providers.
93
     *
94
     * @return bool
95
     */
96
    public function preAuthentication()
97
    {
98
        foreach ($this->filterProviders('PreAuthenticationProviderInterface') as $provider) {
99 View Code Duplication
            if ($provider->authenticate() && $this->userProfile->initialize($provider->getUser())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Documentation introduced by
The property userProfile does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
                $this->dispatcher->dispatch(self::EVENT_SUCCESS, new AuthSuccessEvent($provider->getName()));
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
102
                return true;
103
            }
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Execute username/password authentication providers.
111
     *
112
     * @param string $username
113
     * @param string $password
114
     * @param bool   $fireEvent
115
     *
116
     * @return bool
117
     */
118
    public function passwordAuthentication($username, $password, $fireEvent = true)
119
    {
120
        foreach ($this->filterProviders('PasswordAuthenticationProviderInterface') as $provider) {
121
            $provider->setUsername($username);
122
            $provider->setPassword($password);
123
124 View Code Duplication
            if ($provider->authenticate() && $this->userProfile->initialize($provider->getUser())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Documentation introduced by
The property userProfile does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
125
                if ($fireEvent) {
126
                    $this->dispatcher->dispatch(self::EVENT_SUCCESS, new AuthSuccessEvent($provider->getName()));
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
127
                }
128
129
                return true;
130
            }
131
        }
132
133
        if ($fireEvent) {
134
            $this->dispatcher->dispatch(self::EVENT_FAILURE, new AuthFailureEvent($username));
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
        }
136
137
        return false;
138
    }
139
140
    /**
141
     * Perform OAuth2 authentication.
142
     *
143
     * @param string $name
144
     *
145
     * @return bool
146
     */
147
    public function oauthAuthentication($name)
148
    {
149
        $provider = $this->getProvider($name);
150
151 View Code Duplication
        if ($provider->authenticate() && $this->userProfile->initialize($provider->getUser())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Documentation introduced by
The property userProfile does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
152
            $this->dispatcher->dispatch(self::EVENT_SUCCESS, new AuthSuccessEvent($provider->getName()));
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
154
            return true;
155
        }
156
157
        $this->dispatcher->dispatch(self::EVENT_FAILURE, new AuthFailureEvent());
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Jitamin\Foundatio...\AuthenticationManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
158
159
        return false;
160
    }
161
162
    /**
163
     * Get the last Post-Authentication provider.
164
     *
165
     * @return PostAuthenticationProviderInterface
166
     */
167
    public function getPostAuthenticationProvider()
168
    {
169
        $providers = $this->filterProviders('PostAuthenticationProviderInterface');
170
171
        if (empty($providers)) {
172
            throw new LogicException('You must have at least one Post-Authentication Provider configured');
173
        }
174
175
        return array_pop($providers);
176
    }
177
178
    /**
179
     * Filter registered providers by interface type.
180
     *
181
     * @param string $interface
182
     *
183
     * @return array
184
     */
185
    private function filterProviders($interface)
186
    {
187
        $interface = '\Jitamin\Foundation\Security\\'.$interface;
188
189
        return array_filter($this->providers, function (AuthenticationProviderInterface $provider) use ($interface) {
190
            return is_a($provider, $interface);
191
        });
192
    }
193
}
194