Shib.php 2.64 KB
Newer Older
1
2
3
4
5
6
7
8
<?php

namespace UnicaenAuth\Authentication\Storage;

use UnicaenAuth\Entity\Shibboleth\ShibUser;
use UnicaenAuth\Service\ShibService;
use Zend\Authentication\Storage\Session;
use Zend\Authentication\Storage\StorageInterface;
9
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\ServiceManager\ServiceManager;

/**
 * Shibboleth authentication storage.
 *
 * @author Unicaen
 */
class Shib implements ChainableStorage, ServiceLocatorAwareInterface
{
    use ServiceLocatorAwareTrait;

    /**
     * @var StorageInterface
     */
    protected $storage;

    /**
     * @var ShibUser
     */
    protected $resolvedIdentity;

    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
     * Returns the contents of storage
     *
     * Behavior is undefined when storage is empty.
     *
     * @param ChainEvent $e
     * @return ShibUser
     * @throws \Zend\Authentication\Exception\ExceptionInterface
     */
    public function read(ChainEvent $e)
    {
48
        $shibUser = $this->getAuthenticatedUser();
49
50
51
52
53
54

        $e->addContents('shib', $shibUser);
        
        return $shibUser;
    }

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    /**
     * @return null|ShibUser
     */
    private function getAuthenticatedUser()
    {
        if (null !== $this->resolvedIdentity) {
            return $this->resolvedIdentity;
        }

        /** @var ShibService $shib */
        $shib = $this->getServiceLocator()->get(ShibService::class);

        $this->resolvedIdentity = $shib->getAuthenticatedUser();

        return $this->resolvedIdentity;
    }

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    /**
     * Writes $contents to storage
     *
     * @param ChainEvent $e
     * @throws \Zend\Authentication\Exception\ExceptionInterface
     */
    public function write(ChainEvent $e)
    {
        $contents = $e->getParam('contents');
        $this->resolvedIdentity = null;
        $this->getStorage()->write($contents);
    }

    /**
     * Clears contents from storage
     *
     * @param ChainEvent $e
     * @throws \Zend\Authentication\Exception\ExceptionInterface
     */
    public function clear(ChainEvent $e)
    {
        $this->resolvedIdentity = null;
        $this->getStorage()->clear();
    }

    /**
     * getStorage
     *
     * @return StorageInterface
     */
    public function getStorage()
    {
        if (null === $this->storage) {
            $this->setStorage(new Session());
        }

        return $this->storage;
    }

    /**
     * setStorage
     *
     * @param StorageInterface $storage
     * @return self
     */
    public function setStorage(StorageInterface $storage)
    {
        $this->storage = $storage;

        return $this;
    }
}