Module::initAuthtentication()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 41
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 41
rs 8.439
cc 6
eloc 17
nc 8
nop 1
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
namespace LearnZF2Authentication;
20
21
use Zend\EventManager\EventInterface;
22
use Zend\Loader\StandardAutoloader;
23
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
24
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
25
use Zend\ModuleManager\Feature\ConfigProviderInterface;
26
use Zend\Mvc\MvcEvent;
27
28
/**
29
 * Class Module.
30
 *
31
 * @author Stanimir Dimitrov <[email protected]>
32
 */
33
class Module implements
34
    ConfigProviderInterface,
35
    AutoloaderProviderInterface,
36
    BootstrapListenerInterface
37
{
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getConfig()
42
    {
43
        return include __DIR__.'/config/module.config.php';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getAutoloaderConfig()
50
    {
51
        return [
52
            'Zend\Loader\StandardAutoloader' => [
53
                StandardAutoloader::LOAD_NS => [
54
                    __NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
55
                ],
56
            ],
57
        ];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function onBootstrap(EventInterface $e)
64
    {
65
        /* @var MvcEvent $e */
66
        $sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
67
        $sharedManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, [$this, 'initAuthtentication'], 100);
68
    }
69
70
    public function initAuthtentication(MvcEvent $e)
71
    {
72
        /*
73
         * @var MvcEvent $e
74
         */
75
        $request = $e->getRequest();
76
        $response = $e->getResponse();
77
        $view = $e->getApplication()->getMvcEvent()->getViewModel();
78
        $sm = $e->getApplication()->getServiceManager();
79
        $authAdapter = $sm->get('LearnZF2Authentication\BasicAuthenticationAdapter');
80
81
        /*
82
         * Call the factory class and try to authenticate
83
         */
84
        if ($e->getRouteMatch()->getParam('action') === 'digest') {
85
            $authAdapter = $sm->get('LearnZF2Authentication\DigestAuthenticationAdapter');
86
        }
87
        $authAdapter->setRequest($request);
88
        $authAdapter->setResponse($response);
89
90
        if ($e->getRouteMatch()->getParam('action') === 'basic' || $e->getRouteMatch()->getParam('action') === 'digest') {
91
            $result = $authAdapter->authenticate();
92
93
            /*
94
             * Pass the information to the view and see what we got
95
             */
96
            if (!$result->isValid()) {
97
                /*
98
                 * Create a log function or just use the one from LearnZF2.
99
                 * Also make sure to redirect to another page, 404 for example
100
                 */
101
                foreach ($result->getMessages() as $msg) {
102
                    $view->authProblem = $msg;
103
                }
104
105
                return $view->authProblem;
106
            }
107
108
            return $view->identity = $result->getIdentity();
109
        }
110
    }
111
}
112