1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
<?php
/**
*
* @package auth
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
use OAuth\Common\Token\TokenInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Storage\Exception\StorageException;
use OAuth\Common\Storage\Exception\TokenNotFoundException;
/**
* OAuth storage wrapper for phpbb's cache
*
* @package auth
*/
class phpbb_auth_oauth_token_storage implements TokenStorageInterface
{
/**
* Cache driver.
*
* @var phpbb_db_driver
*/
protected $db;
/**
* phpBB user
*
* @var phpbb_user
*/
protected $user;
/**
* Name of the OAuth provider
*
* @var string
*/
protected $service_name;
/**
* OAuth token table
*
* @var string
*/
protected $auth_provider_oauth_table;
/**
* @var object|TokenInterface
*/
protected $cachedToken;
/**
* Creates token storage for phpBB.
*
* @param phpbb_db_driver $db
* @param phpbb_user $user
* @param string $service_name
* @param string $auth_provider_oauth_table
*/
public function __construct(phpbb_db_driver $db, phpbb_user $user, $service_name, $auth_provider_oauth_table)
{
$this->db = $db;
$this->user = $user;
$this->service_name = $service_name;
$this->auth_provider_oauth_table = $auth_provider_oauth_table;
}
/**
* {@inheritdoc}
*/
public function retrieveAccessToken()
{
if( $this->cachedToken instanceOf TokenInterface ) {
return $this->token;
}
$sql = 'SELECT oauth_token FROM ' . $this->auth_provider_oauth_table .
$this->db->sql_build_array('SELECT', array(
'user_id' => $this->user->data['user_id'],
'oauth_provider' => $this->service_name,
));
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$row)
{
throw new TokenNotFoundException('Token not stored');
}
$token = unserialize($row['oauth_token']);
// Ensure that the token was serialized/unserialized correctly
if (!($token instanceof TokenInterface))
{
$this->clearToken();
throw new TokenNotFoundException('Token not stored correctly');
}
$this->cachedToken = $token;
return $token;
}
/**
* {@inheritdoc}
*/
public function storeAccessToken(TokenInterface $token)
{
$this->cachedToken = $token;
$sql = 'INSERT INTO ' . $this->auth_provider_oauth_table . ' ' .
$this->db->sql_build_array('INSERT', array(
'user_id' => $this->user->data['user_id'],
'oauth_provider' => $this->service_name,
'oauth_token' => serialize($token),
));
$this->db->sql_query($sql);
}
/**
* {@inheritdoc}
*/
public function hasAccessToken()
{
if( $this->cachedToken ) {
return true;
}
$sql = 'SELECT oauth_token FROM ' . $this->auth_provider_oauth_table .
$this->db->sql_build_array('SELECT', array(
'user_id' => $this->user->data['user_id'],
'oauth_provider' => $this->service_name,
));
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$row)
{
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function clearToken()
{
$this->cachedToken = null;
$sql = 'DELETE FROM ' . $this->auth_provider_oauth_table . 'WHERE user_id = ' . $this->user->data['user_id'] .
' AND oauth_provider = ' . $this->db->sql_escape($this->oauth_provider);
$this->db->sql_query($sql);
}
}
|