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