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\AuthorizationCode; |
23
|
|
|
use fkooman\RemoteStorage\OAuth\AuthorizationCodeStorageInterface; |
24
|
|
|
use PDO; |
25
|
|
|
|
26
|
|
|
class PdoAuthorizationCodeStorage extends PdoBaseStorage implements AuthorizationCodeStorageInterface |
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 storeAuthorizationCode(AuthorizationCode $authorizationCode) |
41
|
|
|
{ |
42
|
|
|
$generatedCode = $this->io->getRandom(); |
43
|
|
|
|
44
|
|
|
$stmt = $this->db->prepare( |
45
|
|
|
sprintf( |
46
|
|
|
'INSERT INTO %s (code, client_id, user_id, issued_at, redirect_uri, scope) VALUES(:code, :client_id, :user_id, :issued_at, :redirect_uri, :scope)', |
47
|
|
|
$this->dbPrefix.'authorization_code' |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
$stmt->bindValue(':code', $generatedCode, PDO::PARAM_STR); |
51
|
|
|
$stmt->bindValue(':client_id', $authorizationCode->getClientId(), PDO::PARAM_STR); |
52
|
|
|
$stmt->bindValue(':user_id', $authorizationCode->getUserId(), PDO::PARAM_STR); |
53
|
|
|
$stmt->bindValue(':issued_at', $authorizationCode->getIssuedAt(), PDO::PARAM_INT); |
54
|
|
|
$stmt->bindValue(':redirect_uri', $authorizationCode->getRedirectUri(), PDO::PARAM_STR); |
55
|
|
|
$stmt->bindValue(':scope', $authorizationCode->getScope(), PDO::PARAM_STR); |
56
|
|
|
$stmt->execute(); |
57
|
|
|
|
58
|
|
|
return $generatedCode; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function retrieveAuthorizationCode($authorizationCode) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$stmt = $this->db->prepare( |
64
|
|
|
sprintf( |
65
|
|
|
'SELECT client_id, user_id, issued_at, redirect_uri, scope FROM %s WHERE code = :code', |
66
|
|
|
$this->dbPrefix.'authorization_code' |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
$stmt->bindValue(':code', $authorizationCode, PDO::PARAM_STR); |
70
|
|
|
$stmt->execute(); |
71
|
|
|
$result = $stmt->fetch(PDO::FETCH_ASSOC); |
72
|
|
|
if (false === $result) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new AuthorizationCode( |
77
|
|
|
$result['client_id'], |
78
|
|
|
$result['user_id'], |
79
|
|
|
$result['issued_at'], |
80
|
|
|
$result['redirect_uri'], |
81
|
|
|
$result['scope'] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function isFreshAuthorizationCode($authorizationCode) |
86
|
|
|
{ |
87
|
|
|
$stmt = $this->db->prepare( |
88
|
|
|
sprintf( |
89
|
|
|
'SELECT code FROM %s WHERE code = :code', |
90
|
|
|
$this->dbPrefix.'authorization_code_log' |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
$stmt->bindValue(':code', $authorizationCode, PDO::PARAM_STR); |
94
|
|
|
$stmt->execute(); |
95
|
|
|
$result = $stmt->fetch(PDO::FETCH_ASSOC); |
96
|
|
|
|
97
|
|
|
if (false !== $result) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$stmt = $this->db->prepare( |
102
|
|
|
sprintf( |
103
|
|
|
'INSERT INTO %s (code) VALUES(:code)', |
104
|
|
|
$this->dbPrefix.'authorization_code_log' |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
$stmt->bindValue(':code', $authorizationCode, PDO::PARAM_STR); |
108
|
|
|
$stmt->execute(); |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function createTableQueries($dbPrefix) |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
sprintf( |
117
|
|
|
'CREATE TABLE IF NOT EXISTS %s ( |
118
|
|
|
code VARCHAR(255) NOT NULL, |
119
|
|
|
client_id VARCHAR(255) NOT NULL, |
120
|
|
|
user_id VARCHAR(255) NOT NULL, |
121
|
|
|
issued_at INT NOT NULL, |
122
|
|
|
redirect_uri VARCHAR(255) NOT NULL, |
123
|
|
|
scope VARCHAR(255) NOT NULL, |
124
|
|
|
PRIMARY KEY (code) |
125
|
|
|
)', |
126
|
|
|
$dbPrefix.'authorization_code' |
127
|
|
|
), |
128
|
|
|
sprintf( |
129
|
|
|
'CREATE TABLE IF NOT EXISTS %s ( |
130
|
|
|
code VARCHAR(255) NOT NULL, |
131
|
|
|
UNIQUE (code) |
132
|
|
|
)', |
133
|
|
|
$dbPrefix.'authorization_code_log' |
134
|
|
|
), |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.