1<?php
2
3namespace OAuth\Plugin;
4
5use OAuth\Common\Storage\Exception\TokenNotFoundException;
6use OAuth\Common\Storage\TokenStorageInterface;
7use OAuth\Common\Token\TokenInterface;
8use OAuth\OAuth2\Token\StdOAuth2Token;
9
10/**
11 * Class oAuthStorage
12 */
13class oAuthStorage implements TokenStorageInterface {
14
15    /**
16     * The path to the file where tokens for this service are stored
17     *
18     * @param string $service
19     * @return string
20     */
21    protected function getStateFile() {
22        return getCacheName(session_id(), '.oauth');
23    }
24
25    /**
26     * Load the data from disk
27     *
28     * @param string $service
29     * @return array
30     */
31    protected function loadStateFile() {
32        $file = $this->getStateFile();
33        if(file_exists($file)) {
34            return unserialize(io_readFile($file, false));
35        } else {
36            return array();
37        }
38    }
39
40    /**
41     * Load the data from cookie
42     *
43     * @param string $service
44     * @return array
45     */
46    protected function getLoadToken($service) {
47        if (isset($_SESSION[DOKU_COOKIE]['evesso-storage']['token'])) {
48            return unserialize($_SESSION[DOKU_COOKIE]['evesso-storage']['token']);
49        } else {
50            return null;
51        }
52    }
53
54    /**
55     * @param string $service
56     *
57     * @return TokenInterface
58     *
59     * @throws TokenNotFoundException
60     */
61    public function retrieveAccessToken($service) {
62        $token = $this->getLoadToken($service);
63        if(!isset($token)) {
64            $this->clearAuthorizationState($service);
65            throw new TokenNotFoundException('No token found in storage');
66        }
67        return $token;
68    }
69
70    /**
71     * @param string         $service
72     * @param TokenInterface $token
73     *
74     * @return TokenStorageInterface
75     */
76    public function storeAccessToken($service, TokenInterface $token) {
77         $_SESSION[DOKU_COOKIE]['evesso-storage']['token'] = serialize($token);
78    }
79
80    /**
81     * @param string $service
82     *
83     * @return bool
84     */
85    public function hasAccessToken($service) {
86        $token = $this->getLoadToken($service);
87        return isset($token);
88    }
89
90    /**
91     * Delete the users token. Aka, log out.
92     *
93     * @param string $service
94     *
95     * @return TokenStorageInterface
96     */
97    public function clearToken($service) {
98        if (isset($_SESSION[DOKU_COOKIE]['evesso-storage'])) {
99            unset($_SESSION[DOKU_COOKIE]['evesso-storage']); //Purge everything
100        }
101    }
102
103    /**
104     * Delete *ALL* user tokens. Use with care. Most of the time you will likely
105     * want to use clearToken() instead.
106     *
107     * @return TokenStorageInterface
108     */
109    public function clearAllTokens() {
110        // TODO: Implement clearAllTokens() method.
111    }
112
113    /**
114     * Store the authorization state related to a given service
115     *
116     * @param string $service
117     * @param string $state
118     *
119     * @return TokenStorageInterface
120     */
121    public function storeAuthorizationState($service, $state) {
122        $data = array();
123        $data['state'] = $state;
124        $file = $this->getStateFile();
125        io_saveFile($file, serialize($data));
126    }
127
128    /**
129     * Check if an authorization state for a given service exists
130     *
131     * @param string $service
132     *
133     * @return bool
134     */
135    public function hasAuthorizationState($service) {
136        $data = $this->loadStateFile();
137        return isset($data['state']);
138    }
139
140    /**
141     * Retrieve the authorization state for a given service
142     *
143     * @param string $service
144     *
145     * @throws \OAuth\Common\Storage\Exception\TokenNotFoundException
146     * @return string
147     */
148    public function retrieveAuthorizationState($service) {
149        $data = $this->loadStateFile();
150        if(!isset($data['state'])) {
151            throw new TokenNotFoundException('No state found in storage');
152        }
153        return $data['state'];
154    }
155
156    /**
157     * Clear the authorization state of a given service
158     *
159     * @param string $service
160     *
161     * @return TokenStorageInterface
162     */
163    public function clearAuthorizationState($service) {
164        $file = $this->getStateFile();
165        @unlink($file);
166        $file = getCacheName('oauth', '.purged');
167        //Only do this once
168        if(file_exists($file)) {
169           return;
170        }
171        $this->clearAllAuthorizationStates();
172        io_saveFile($file, 'oauth purged');
173    }
174
175    /**
176     * Delete *ALL* user authorization states. Use with care. Most of the time you will likely
177     * want to use clearAuthorization() instead.
178     *
179     * @return TokenStorageInterface
180     */
181    public function clearAllAuthorizationStates() {
182        global $conf;
183        $directory = $conf['cachedir'];
184        $this->removeRecursive($directory);
185    }
186
187    function removeRecursive($directory) {
188        array_map('unlink', glob("$directory/*.oauth"));
189        foreach (glob("$directory/*", GLOB_ONLYDIR) as $dir) {
190            $this->removeRecursive($dir);
191        }
192        return true;
193    }
194}