|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\OAuth; |
|
20
|
|
|
|
|
21
|
|
|
use PDO; |
|
22
|
|
|
use PDOException; |
|
23
|
|
|
|
|
24
|
|
|
class TokenStorage |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var PDO */ |
|
27
|
|
|
private $db; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(PDO $db) |
|
30
|
|
|
{ |
|
31
|
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
32
|
|
|
$this->db = $db; |
|
33
|
|
|
// enable foreign keys, only for SQLite! |
|
34
|
|
|
$this->db->query('PRAGMA foreign_keys = ON'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function store($userId, $accessTokenKey, $accessToken, $clientId, $scope) |
|
38
|
|
|
{ |
|
39
|
|
|
$stmt = $this->db->prepare( |
|
40
|
|
|
'INSERT INTO tokens ( |
|
41
|
|
|
user_id, |
|
42
|
|
|
access_token_key, |
|
43
|
|
|
access_token, |
|
44
|
|
|
client_id, |
|
45
|
|
|
scope |
|
46
|
|
|
) |
|
47
|
|
|
VALUES( |
|
48
|
|
|
:user_id, |
|
49
|
|
|
:access_token_key, |
|
50
|
|
|
:access_token, |
|
51
|
|
|
:client_id, |
|
52
|
|
|
:scope |
|
53
|
|
|
)' |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
|
57
|
|
|
$stmt->bindValue(':access_token_key', $accessTokenKey, PDO::PARAM_STR); |
|
58
|
|
|
$stmt->bindValue(':access_token', $accessToken, PDO::PARAM_STR); |
|
59
|
|
|
$stmt->bindValue(':client_id', $clientId, PDO::PARAM_STR); |
|
60
|
|
|
$stmt->bindValue(':scope', $scope, PDO::PARAM_STR); |
|
61
|
|
|
try { |
|
62
|
|
|
$stmt->execute(); |
|
63
|
|
|
} catch (PDOException $e) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getExistingToken($userId, $clientId, $scope) |
|
71
|
|
|
{ |
|
72
|
|
|
$stmt = $this->db->prepare( |
|
73
|
|
|
'SELECT |
|
74
|
|
|
access_token_key, |
|
75
|
|
|
access_token |
|
76
|
|
|
FROM tokens |
|
77
|
|
|
WHERE |
|
78
|
|
|
user_id = :user_id AND |
|
79
|
|
|
client_id = :client_id AND |
|
80
|
|
|
scope = :scope' |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
|
84
|
|
|
$stmt->bindValue(':client_id', $clientId, PDO::PARAM_STR); |
|
85
|
|
|
$stmt->bindValue(':scope', $scope, PDO::PARAM_STR); |
|
86
|
|
|
|
|
87
|
|
|
$stmt->execute(); |
|
88
|
|
|
|
|
89
|
|
|
return $stmt->fetch(PDO::FETCH_ASSOC); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
public function get($accessTokenKey) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$stmt = $this->db->prepare( |
|
95
|
|
|
'SELECT |
|
96
|
|
|
user_id, |
|
97
|
|
|
access_token, |
|
98
|
|
|
client_id, |
|
99
|
|
|
scope |
|
100
|
|
|
FROM tokens |
|
101
|
|
|
WHERE |
|
102
|
|
|
access_token_key = :access_token_key' |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$stmt->bindValue(':access_token_key', $accessTokenKey, PDO::PARAM_STR); |
|
106
|
|
|
$stmt->execute(); |
|
107
|
|
|
|
|
108
|
|
|
return $stmt->fetch(PDO::FETCH_ASSOC); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
View Code Duplication |
public function getAuthorizedClients($userId) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$stmt = $this->db->prepare( |
|
114
|
|
|
'SELECT |
|
115
|
|
|
client_id, |
|
116
|
|
|
scope |
|
117
|
|
|
FROM tokens |
|
118
|
|
|
WHERE |
|
119
|
|
|
user_id = :user_id' |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
|
123
|
|
|
$stmt->execute(); |
|
124
|
|
|
|
|
125
|
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function removeClientTokens($userId, $clientId) |
|
129
|
|
|
{ |
|
130
|
|
|
$stmt = $this->db->prepare( |
|
131
|
|
|
'DELETE FROM tokens |
|
132
|
|
|
WHERE user_id = :user_id AND client_id = :client_id' |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
|
136
|
|
|
$stmt->bindValue(':client_id', $clientId, PDO::PARAM_STR); |
|
137
|
|
|
try { |
|
138
|
|
|
$stmt->execute(); |
|
139
|
|
|
} catch (PDOException $e) { |
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return true; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function init() |
|
147
|
|
|
{ |
|
148
|
|
|
$queryList = [ |
|
149
|
|
|
'CREATE TABLE IF NOT EXISTS tokens ( |
|
150
|
|
|
user_id VARCHAR(255) NOT NULL, |
|
151
|
|
|
access_token_key VARCHAR(255) NOT NULL, |
|
152
|
|
|
access_token VARCHAR(255) NOT NULL, |
|
153
|
|
|
client_id VARCHAR(255) NOT NULL, |
|
154
|
|
|
scope VARCHAR(255) NOT NULL, |
|
155
|
|
|
UNIQUE(access_token_key) |
|
156
|
|
|
)', |
|
157
|
|
|
]; |
|
158
|
|
|
|
|
159
|
|
|
foreach ($queryList as $query) { |
|
160
|
|
|
$this->db->query($query); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.