Skip to content

Commit bd41360

Browse files
committed
adds tests for JwtBearer storage
1 parent 4929e32 commit bd41360

File tree

6 files changed

+101
-10
lines changed

6 files changed

+101
-10
lines changed

src/OAuth2/Storage/Cassandra.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,26 @@ public function setScope($scope, $client_id = null, $type = 'supported')
316316
/*JWTBearerInterface */
317317
public function getClientKey($client_id, $subject)
318318
{
319-
$jwt = $this->getValue($this->config['jwt_key'] . $client_id);
319+
if (!$jwt = $this->getValue($this->config['jwt_key'] . $client_id)) {
320+
return false;
321+
}
322+
320323
if (isset($jwt['subject']) && $jwt['subject'] == $subject ) {
321324
return $jwt['key'];
322325
}
323326

324327
return null;
325328
}
326329

330+
public function setClientKey($client_id, $key, $subject = null)
331+
{
332+
return $this->setValue($this->config['jwt_key'] . $client_id, array(
333+
'key' => $key,
334+
'subject' => $subject
335+
));
336+
}
337+
338+
/*ScopeInterface */
327339
public function getClientScope($client_id)
328340
{
329341
if (!$clientDetails = $this->getClientDetails($client_id)) {

src/OAuth2/Storage/Mongo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public function getClientKey($client_id, $subject)
304304
'subject' => $subject
305305
));
306306

307-
return $result;
307+
return is_null($result) ? false : $result['key'];
308308
}
309309

310310
public function getClientScope($client_id)

src/OAuth2/Storage/Redis.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,25 @@ public function setScope($scope, $client_id = null, $type = 'supported')
263263
/*JWTBearerInterface */
264264
public function getClientKey($client_id, $subject)
265265
{
266-
$jwt = $this->getValue($this->config['jwt_key'] . $client_id);
267-
if ( isset($jwt['subject']) && $jwt['subject'] == $subject ) {
266+
if (!$jwt = $this->getValue($this->config['jwt_key'] . $client_id)) {
267+
return false;
268+
}
269+
270+
if (isset($jwt['subject']) && $jwt['subject'] == $subject) {
268271
return $jwt['key'];
269272
}
270273

271274
return null;
272275
}
273276

277+
public function setClientKey($client_id, $key, $subject = null)
278+
{
279+
return $this->setValue($this->config['jwt_key'] . $client_id, array(
280+
'key' => $key,
281+
'subject' => $subject
282+
));
283+
}
284+
274285
public function getClientScope($client_id)
275286
{
276287
if (!$clientDetails = $this->getClientDetails($client_id)) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace OAuth2\Storage;
4+
5+
class JwtBearerTest extends BaseTest
6+
{
7+
/** @dataProvider provideStorage */
8+
public function testGetClientKey(JwtBearerInterface $storage)
9+
{
10+
if ($storage instanceof NullStorage) {
11+
$this->markTestSkipped('Skipped Storage: ' . $storage->getMessage());
12+
13+
return;
14+
}
15+
16+
// nonexistant client_id
17+
$key = $storage->getClientKey('this-is-not-real', 'nor-is-this');
18+
$this->assertFalse($key);
19+
20+
// valid client_id and subject
21+
$key = $storage->getClientKey('oauth_test_client', 'test_subject');
22+
$this->assertNotNull($key);
23+
$this->assertEquals($key, Bootstrap::getInstance()->getTestPublicKey());
24+
}
25+
}

test/config/storage.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@
123123
"Missing Key Client PHP-5.2": {
124124
"key": null,
125125
"subject": "[email protected]"
126+
},
127+
"oauth_test_client": {
128+
"key": "-----BEGIN CERTIFICATE-----\nMIICiDCCAfGgAwIBAgIBADANBgkqhkiG9w0BAQQFADA9MQswCQYDVQQGEwJVUzEL\nMAkGA1UECBMCVVQxITAfBgNVBAoTGFZpZ25ldHRlIENvcnBvcmF0aW9uIFNCWDAe\nFw0xMTEwMTUwMzE4MjdaFw0zMTEwMTAwMzE4MjdaMD0xCzAJBgNVBAYTAlVTMQsw\nCQYDVQQIEwJVVDEhMB8GA1UEChMYVmlnbmV0dGUgQ29ycG9yYXRpb24gU0JYMIGf\nMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8fpi06NfVYHAOAnxNMVnTXr/ptsLs\nNjP+uAt2eO0cc5J9H5XV8lFVujOrRu/JWi1TDmAvOaf/6A3BphIA1Pwp0AAqlZdw\nizIum8j0KzpsGYH5qReNQDwF3oUSKMsQCCGCDHrDYifG/pRi9bN1ZVjEXPr35HJu\nBe+FQpZTs8DewwIDAQABo4GXMIGUMB0GA1UdDgQWBBRe8hrEXm+Yim4YlD5Nx+1K\nvCYs9DBlBgNVHSMEXjBcgBRe8hrEXm+Yim4YlD5Nx+1KvCYs9KFBpD8wPTELMAkG\nA1UEBhMCVVMxCzAJBgNVBAgTAlVUMSEwHwYDVQQKExhWaWduZXR0ZSBDb3Jwb3Jh\ndGlvbiBTQliCAQAwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQQFAAOBgQBjhyRD\nlM7vnLn6drgQVftW5V9nDFAyPAuiGvMIKFSbiAf1PxXCRn5sfJquwWKsJUi4ZGNl\naViXdFmN6/F13PSM+yg63tpKy0fYqMbTM+Oe5WuSHkSW1VuYNHV+24adgNk/FRDL\nFRrlM1f6s9VTLWvwGItjfrof0Ba8Uq7ZDSb9Xg==\n-----END CERTIFICATE-----",
129+
"subject": "test_subject"
126130
}
127131
},
128132
"jti": [

test/lib/OAuth2/Storage/Bootstrap.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ private function createCassandraDb(Cassandra $storage)
199199

200200
$storage->setScope('clientscope1 clientscope2 clientscope3', 'Test Default Scope Client ID 2');
201201
$storage->setScope('clientscope3', 'Test Default Scope Client ID 2', 'default');
202+
203+
$storage->setClientKey('oauth_test_client', $this->getTestPublicKey(), 'test_subject');
202204
}
203205

204206
private function createSqliteDb(\PDO $pdo)
@@ -222,13 +224,14 @@ private function createMysqlDb(\PDO $pdo)
222224

223225
public function runPdoSql(\PDO $pdo)
224226
{
225-
$pdo->exec('CREATE TABLE oauth_clients (client_id TEXT, client_secret TEXT, redirect_uri TEXT, grant_types TEXT, scope TEXT, user_id TEXT)');
227+
$pdo->exec('CREATE TABLE oauth_clients (client_id TEXT, client_secret TEXT, redirect_uri TEXT, grant_types TEXT, scope TEXT, user_id TEXT, public_key TEXT)');
226228
$pdo->exec('CREATE TABLE oauth_access_tokens (access_token TEXT, client_id TEXT, user_id TEXT, expires DATETIME, scope TEXT)');
227229
$pdo->exec('CREATE TABLE oauth_authorization_codes (authorization_code TEXT, client_id TEXT, user_id TEXT, redirect_uri TEXT, expires DATETIME, scope TEXT)');
228230
$pdo->exec('CREATE TABLE oauth_users (username TEXT, password TEXT, first_name TEXT, last_name TEXT, scope TEXT)');
229231
$pdo->exec('CREATE TABLE oauth_refresh_tokens (refresh_token TEXT, client_id TEXT, user_id TEXT, expires DATETIME, scope TEXT)');
230232
$pdo->exec('CREATE TABLE oauth_scopes (scope TEXT, is_default BOOLEAN)');
231233
$pdo->exec('CREATE TABLE oauth_public_keys (client_id TEXT, public_key TEXT, private_key TEXT, encryption_algorithm VARCHAR(100) DEFAULT "RS256")');
234+
$pdo->exec('CREATE TABLE oauth_jwt (client_id VARCHAR(80), subject VARCHAR(80), public_key VARCHAR(2000))');
232235

233236
// set up scopes
234237
foreach (explode(' ', 'supportedscope1 supportedscope2 supportedscope3 supportedscope4 clientscope1 clientscope2 clientscope3') as $supportedScope) {
@@ -251,7 +254,8 @@ public function runPdoSql(\PDO $pdo)
251254
$pdo->exec('INSERT INTO oauth_users (username, password) VALUES ("testuser", "password")');
252255
$pdo->exec('INSERT INTO oauth_public_keys (client_id, public_key, private_key, encryption_algorithm) VALUES ("ClientID_One", "client_1_public", "client_1_private", "RS256")');
253256
$pdo->exec('INSERT INTO oauth_public_keys (client_id, public_key, private_key, encryption_algorithm) VALUES ("ClientID_Two", "client_2_public", "client_2_private", "RS256")');
254-
$pdo->exec(sprintf('INSERT INTO oauth_public_keys (client_id, public_key, private_key, encryption_algorithm) VALUES (NULL, "%s", "%s", "RS256")', file_get_contents($this->configDir.'/keys/id_rsa.pub'), file_get_contents($this->configDir.'/keys/id_rsa')));
257+
$pdo->exec(sprintf('INSERT INTO oauth_public_keys (client_id, public_key, private_key, encryption_algorithm) VALUES (NULL, "%s", "%s", "RS256")', $this->getTestPublicKey(), $this->getTestPrivateKey()));
258+
$pdo->exec(sprintf('INSERT INTO oauth_jwt (client_id, subject, public_key) VALUES ("oauth_test_client", "test_subject", "%s")', $this->getTestPublicKey()));
255259
}
256260

257261
public function removeMysqlDb(\PDO $pdo)
@@ -271,10 +275,33 @@ public function getConfigDir()
271275

272276
private function createMongoDb(\MongoDB $db)
273277
{
274-
$db->oauth_clients->insert(array('client_id' => "oauth_test_client", 'client_secret' => "testpass", 'redirect_uri' => "http://example.com", 'grant_types' => 'implicit password'));
275-
$db->oauth_access_tokens->insert(array('access_token' => "testtoken", 'client_id' => "Some Client"));
276-
$db->oauth_authorization_codes->insert(array('authorization_code' => "testcode", 'client_id' => "Some Client"));
277-
$db->oauth_users->insert(array('username' => "testuser", 'password' => "password"));
278+
$db->oauth_clients->insert(array(
279+
'client_id' => "oauth_test_client",
280+
'client_secret' => "testpass",
281+
'redirect_uri' => "http://example.com",
282+
'grant_types' => 'implicit password'
283+
));
284+
285+
$db->oauth_access_tokens->insert(array(
286+
'access_token' => "testtoken",
287+
'client_id' => "Some Client"
288+
));
289+
290+
$db->oauth_authorization_codes->insert(array(
291+
'authorization_code' => "testcode",
292+
'client_id' => "Some Client"
293+
));
294+
295+
$db->oauth_users->insert(array(
296+
'username' => "testuser",
297+
'password' => "password"
298+
));
299+
300+
$db->oauth_jwt->insert(array(
301+
'client_id' => 'oauth_test_client',
302+
'key' => $this->getTestPublicKey(),
303+
'subject' => 'test_subject',
304+
));
278305
}
279306

280307
private function createRedisDb(Redis $storage)
@@ -298,10 +325,22 @@ private function createRedisDb(Redis $storage)
298325

299326
$storage->setScope('clientscope1 clientscope2 clientscope3', 'Test Default Scope Client ID 2');
300327
$storage->setScope('clientscope3', 'Test Default Scope Client ID 2', 'default');
328+
329+
$storage->setClientKey('oauth_test_client', $this->getTestPublicKey(), 'test_subject');
301330
}
302331

303332
public function removeMongoDb(\MongoDB $db)
304333
{
305334
$db->drop();
306335
}
336+
337+
public function getTestPublicKey()
338+
{
339+
return file_get_contents(__DIR__.'/../../../config/keys/id_rsa.pub');
340+
}
341+
342+
private function getTestPrivateKey()
343+
{
344+
return file_get_contents(__DIR__.'/../../../config/keys/id_rsa');
345+
}
307346
}

0 commit comments

Comments
 (0)