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