|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\OAuth\Storage; |
|
20
|
|
|
|
|
21
|
|
|
use fkooman\IO\IO; |
|
22
|
|
|
use fkooman\RemoteStorage\OAuth\AccessToken; |
|
23
|
|
|
use fkooman\RemoteStorage\OAuth\AccessTokenStorageInterface; |
|
24
|
|
|
use PDO; |
|
25
|
|
|
|
|
26
|
|
|
class PdoAccessTokenStorage extends PdoBaseStorage implements AccessTokenStorageInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var \fkooman\IO\IO */ |
|
29
|
|
|
private $io; |
|
30
|
|
|
|
|
31
|
|
View Code Duplication |
public function __construct(PDO $db, $dbPrefix = '', IO $io = null) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct($db, $dbPrefix); |
|
34
|
|
|
if (null === $io) { |
|
35
|
|
|
$io = new IO(); |
|
36
|
|
|
} |
|
37
|
|
|
$this->io = $io; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function storeAccessToken(AccessToken $accessToken) |
|
41
|
|
|
{ |
|
42
|
|
|
$generatedToken = $this->io->getRandom(); |
|
43
|
|
|
|
|
44
|
|
|
$stmt = $this->db->prepare( |
|
45
|
|
|
sprintf( |
|
46
|
|
|
'INSERT INTO %s (token, client_id, user_id, issued_at, scope) VALUES(:token, :client_id, :user_id, :issued_at, :scope)', |
|
47
|
|
|
$this->dbPrefix.'access_token' |
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
$stmt->bindValue(':token', $generatedToken, PDO::PARAM_STR); |
|
51
|
|
|
$stmt->bindValue(':client_id', $accessToken->getClientId(), PDO::PARAM_STR); |
|
52
|
|
|
$stmt->bindValue(':user_id', $accessToken->getUserId(), PDO::PARAM_STR); |
|
53
|
|
|
$stmt->bindValue(':issued_at', $accessToken->getIssuedAt(), PDO::PARAM_INT); |
|
54
|
|
|
$stmt->bindValue(':scope', $accessToken->getScope(), PDO::PARAM_STR); |
|
55
|
|
|
$stmt->execute(); |
|
56
|
|
|
|
|
57
|
|
|
return $generatedToken; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
public function retrieveAccessToken($accessToken) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$stmt = $this->db->prepare( |
|
63
|
|
|
sprintf( |
|
64
|
|
|
'SELECT client_id, user_id, issued_at, scope FROM %s WHERE token = :token', |
|
65
|
|
|
$this->dbPrefix.'access_token' |
|
66
|
|
|
) |
|
67
|
|
|
); |
|
68
|
|
|
$stmt->bindValue(':token', $accessToken, PDO::PARAM_STR); |
|
69
|
|
|
$stmt->execute(); |
|
70
|
|
|
$result = $stmt->fetch(PDO::FETCH_ASSOC); |
|
71
|
|
|
if (false === $result) { |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return new AccessToken( |
|
76
|
|
|
$result['client_id'], |
|
77
|
|
|
$result['user_id'], |
|
78
|
|
|
$result['issued_at'], |
|
79
|
|
|
$result['scope'] |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function createTableQueries($dbPrefix) |
|
84
|
|
|
{ |
|
85
|
|
|
return [ |
|
86
|
|
|
sprintf( |
|
87
|
|
|
'CREATE TABLE IF NOT EXISTS %s ( |
|
88
|
|
|
token VARCHAR(255) NOT NULL, |
|
89
|
|
|
client_id VARCHAR(255) NOT NULL, |
|
90
|
|
|
user_id VARCHAR(255) NOT NULL, |
|
91
|
|
|
issued_at INT NOT NULL, |
|
92
|
|
|
scope VARCHAR(255) NOT NULL, |
|
93
|
|
|
PRIMARY KEY (token) |
|
94
|
|
|
)', |
|
95
|
|
|
$dbPrefix.'access_token' |
|
96
|
|
|
), |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
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.