1<?php
2
3namespace dokuwiki\plugin\oauth;
4
5use OAuth\Common\Storage\Exception\TokenNotFoundException;
6use OAuth\Common\Storage\TokenStorageInterface;
7use OAuth\Common\Token\TokenInterface;
8
9/**
10 * Implements custom handling for storing tokens
11 */
12class Storage implements TokenStorageInterface
13{
14    /** @var string */
15    protected $storageId;
16
17    /**
18     * @param string $storageId The ID identifying the user
19     */
20    public function __construct($storageId)
21    {
22        $this->storageId = $storageId;
23    }
24
25    /**
26     * The path to the file where tokens for this service and user are stored
27     *
28     * @param string $service
29     * @return string
30     */
31    protected function getServiceFile($service)
32    {
33        return getCacheName($this->storageId . $service, '.oauth');
34    }
35
36    /**
37     * Load the data from disk
38     *
39     * @param string $service
40     * @return array
41     */
42    protected function loadServiceFile($service)
43    {
44        $file = $this->getServiceFile($service);
45        if (file_exists($file)) {
46            return unserialize(io_readFile($file, false));
47        } else {
48            return [];
49        }
50    }
51
52    /**
53     * Store the data to disk
54     *
55     * @param string $service
56     * @param array $data
57     */
58    protected function saveServiceFile($service, $data)
59    {
60        $file = $this->getServiceFile($service);
61        io_saveFile($file, serialize($data));
62    }
63
64    /** @inheritDoc */
65    public function retrieveAccessToken($service)
66    {
67        $data = $this->loadServiceFile($service);
68        if (!isset($data['token'])) {
69            throw new TokenNotFoundException('No token found in storage');
70        }
71        return $data['token'];
72    }
73
74    /** @inheritDoc */
75    public function storeAccessToken($service, TokenInterface $token)
76    {
77        $data = $this->loadServiceFile($service);
78        $data['token'] = $token;
79        $this->saveServiceFile($service, $data);
80    }
81
82    /** @inheritDoc */
83    public function hasAccessToken($service)
84    {
85        $data = $this->loadServiceFile($service);
86        return isset($data['token']);
87    }
88
89    /** @inheritDoc */
90    public function clearToken($service)
91    {
92        $data = $this->loadServiceFile($service);
93        if (isset($data['token'])) unset($data['token']);
94        $this->saveServiceFile($service, $data);
95
96        return $this;
97    }
98
99    /** @inheritDoc */
100    public function clearAllTokens()
101    {
102        // TODO: Implement clearAllTokens() method.
103        return $this;
104    }
105
106    /** @inheritDoc */
107    public function storeAuthorizationState($service, $state)
108    {
109        $data = $this->loadServiceFile($service);
110        $data['state'] = $state;
111        $this->saveServiceFile($service, $data);
112        return $this;
113    }
114
115    /** @inheritDoc */
116    public function hasAuthorizationState($service)
117    {
118        $data = $this->loadServiceFile($service);
119        return isset($data['state']);
120    }
121
122    /**
123     * @inheritDoc
124     * @throws TokenNotFoundException
125     */
126    public function retrieveAuthorizationState($service)
127    {
128        $data = $this->loadServiceFile($service);
129        if (!isset($data['state'])) {
130            throw new TokenNotFoundException('No state found in storage');
131        }
132        return $data['state'];
133    }
134
135    /** @inheritDoc */
136    public function clearAuthorizationState($service)
137    {
138        $data = $this->loadServiceFile($service);
139        if (isset($data['state'])) unset($data['state']);
140        $this->saveServiceFile($service, $data);
141
142        return $this;
143    }
144
145    /** @inheritDoc */
146    public function clearAllAuthorizationStates()
147    {
148        // TODO: Implement clearAllAuthorizationStates() method.
149
150        return $this;
151    }
152}
153