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 DateTime; |
22
|
|
|
use PDO; |
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
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function store($userId, $accessTokenKey, $accessToken, $clientId, $scope, DateTime $expiresAt) |
36
|
|
|
{ |
37
|
|
|
$stmt = $this->db->prepare( |
38
|
|
|
'INSERT INTO tokens ( |
39
|
|
|
user_id, |
40
|
|
|
access_token_key, |
41
|
|
|
access_token, |
42
|
|
|
client_id, |
43
|
|
|
scope, |
44
|
|
|
expires_at |
45
|
|
|
) |
46
|
|
|
VALUES( |
47
|
|
|
:user_id, |
48
|
|
|
:access_token_key, |
49
|
|
|
:access_token, |
50
|
|
|
:client_id, |
51
|
|
|
:scope, |
52
|
|
|
:expires_at |
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
|
|
|
$stmt->bindValue(':expires_at', $expiresAt->format('Y-m-d H:i:s'), PDO::PARAM_STR); |
62
|
|
|
$stmt->execute(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getExistingToken($userId, $clientId, $scope) |
66
|
|
|
{ |
67
|
|
|
$stmt = $this->db->prepare( |
68
|
|
|
'SELECT |
69
|
|
|
access_token_key, |
70
|
|
|
access_token, |
71
|
|
|
expires_at |
72
|
|
|
FROM tokens |
73
|
|
|
WHERE |
74
|
|
|
user_id = :user_id AND |
75
|
|
|
client_id = :client_id AND |
76
|
|
|
scope = :scope' |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
80
|
|
|
$stmt->bindValue(':client_id', $clientId, PDO::PARAM_STR); |
81
|
|
|
$stmt->bindValue(':scope', $scope, PDO::PARAM_STR); |
82
|
|
|
$stmt->execute(); |
83
|
|
|
|
84
|
|
|
return $stmt->fetch(PDO::FETCH_ASSOC); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function get($accessTokenKey) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$stmt = $this->db->prepare( |
90
|
|
|
'SELECT |
91
|
|
|
user_id, |
92
|
|
|
access_token, |
93
|
|
|
client_id, |
94
|
|
|
scope, |
95
|
|
|
expires_at |
96
|
|
|
FROM tokens |
97
|
|
|
WHERE |
98
|
|
|
access_token_key = :access_token_key' |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$stmt->bindValue(':access_token_key', $accessTokenKey, PDO::PARAM_STR); |
102
|
|
|
$stmt->execute(); |
103
|
|
|
|
104
|
|
|
return $stmt->fetch(PDO::FETCH_ASSOC); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function getAuthorizedClients($userId) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$stmt = $this->db->prepare( |
110
|
|
|
'SELECT |
111
|
|
|
client_id, |
112
|
|
|
scope |
113
|
|
|
FROM tokens |
114
|
|
|
WHERE |
115
|
|
|
user_id = :user_id' |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
119
|
|
|
$stmt->execute(); |
120
|
|
|
|
121
|
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function removeClientTokens($userId, $clientId) |
125
|
|
|
{ |
126
|
|
|
$stmt = $this->db->prepare( |
127
|
|
|
'DELETE FROM tokens |
128
|
|
|
WHERE user_id = :user_id AND client_id = :client_id' |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
132
|
|
|
$stmt->bindValue(':client_id', $clientId, PDO::PARAM_STR); |
133
|
|
|
$stmt->execute(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function init() |
137
|
|
|
{ |
138
|
|
|
$queryList = [ |
139
|
|
|
'CREATE TABLE IF NOT EXISTS tokens ( |
140
|
|
|
user_id VARCHAR(255) NOT NULL, |
141
|
|
|
access_token_key VARCHAR(255) NOT NULL, |
142
|
|
|
access_token VARCHAR(255) NOT NULL, |
143
|
|
|
client_id VARCHAR(255) NOT NULL, |
144
|
|
|
scope VARCHAR(255) NOT NULL, |
145
|
|
|
expires_at VARCHAR(255) NOT NULL, |
146
|
|
|
UNIQUE(access_token_key) |
147
|
|
|
)', |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
foreach ($queryList as $query) { |
151
|
|
|
$this->db->query($query); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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.