diff options
| -rw-r--r-- | src/OAuth2/Storage/Pdo.php | 38 | ||||
| -rw-r--r-- | src/OAuth2/Storage/Redis.php | 15 | ||||
| -rw-r--r-- | src/OAuth2/Storage/UserCredentialsInterface.php | 6 | ||||
| -rw-r--r-- | test/lib/OAuth2/Storage/Bootstrap.php | 10 |
4 files changed, 42 insertions, 27 deletions
diff --git a/src/OAuth2/Storage/Pdo.php b/src/OAuth2/Storage/Pdo.php index bf8c9c3..49f977a 100644 --- a/src/OAuth2/Storage/Pdo.php +++ b/src/OAuth2/Storage/Pdo.php @@ -35,10 +35,10 @@ class Pdo implements AuthorizationCodeInterface, } // merge optional parameters $connection = array_merge(array( - 'user_id' => null, + 'username' => null, 'password' => null, ), $connection); - $connection = new \PDO($connection['dsn'], $connection['user_id'], $connection['password']); + $connection = new \PDO($connection['dsn'], $connection['username'], $connection['password']); } $this->db = $connection; @@ -151,17 +151,17 @@ class Pdo implements AuthorizationCodeInterface, } /* OAuth2_Storage_UserCredentialsInterface */ - public function checkUserCredentials($user_id, $password) + public function checkUserCredentials($username, $password) { - if ($user = $this->getUser($user_id)) { + if ($user = $this->getUser($username)) { return $this->checkPassword($user, $password); } return false; } - public function getUserDetails($user_id) + public function getUserDetails($username) { - return $this->getUser($user_id); + return $this->getUser($username); } /* OAuth2_Storage_RefreshTokenInterface */ @@ -201,25 +201,33 @@ class Pdo implements AuthorizationCodeInterface, return $user['password'] == sha1($password); } - public function getUser($user_id) + public function getUser($username) { - $stmt = $this->db->prepare($sql = sprintf('SELECT * from %s where user_id=:user_id', $this->config['user_table'])); - $stmt->execute(array('user_id' => $user_id)); - return $stmt->fetch(); + $stmt = $this->db->prepare($sql = sprintf('SELECT * from %s where username=:username', $this->config['user_table'])); + $stmt->execute(array('username' => $username)); + + if (!$userInfo = $stmt->fetch()) { + return false; + } + + // the default behavior is to use "username" as the user_id + return array_merge(array( + 'user_id' => $username + ), $userInfo); } - public function setUser($user_id, $password, $firstName = null, $lastName = null) + public function setUser($username, $password, $firstName = null, $lastName = null) { // do not store in plaintext $password = sha1($password); // if it exists, update it. - if ($this->getUser($user_id)) { - $stmt = $this->db->prepare($sql = sprintf('UPDATE %s SET password=:password, first_name=:firstName, last_name=:lastName where user_id=:user_id', $this->config['user_table'])); + if ($this->getUser($username)) { + $stmt = $this->db->prepare($sql = sprintf('UPDATE %s SET password=:password, first_name=:firstName, last_name=:lastName where username=:username', $this->config['user_table'])); } else { - $stmt = $this->db->prepare(sprintf('INSERT INTO %s (user_id, password, first_name, last_name) VALUES (:user_id, :password, :firstName, :lastName)', $this->config['user_table'])); + $stmt = $this->db->prepare(sprintf('INSERT INTO %s (username, password, first_name, last_name) VALUES (:username, :password, :firstName, :lastName)', $this->config['user_table'])); } - return $stmt->execute(compact('user_id', 'password', 'firstName', 'lastName')); + return $stmt->execute(compact('username', 'password', 'firstName', 'lastName')); } /* OAuth2_Storage_JWTBearerInterface */ diff --git a/src/OAuth2/Storage/Redis.php b/src/OAuth2/Storage/Redis.php index 979fb32..440c3a2 100644 --- a/src/OAuth2/Storage/Redis.php +++ b/src/OAuth2/Storage/Redis.php @@ -108,14 +108,21 @@ class Redis implements AuthorizationCodeInterface, public function getUser($username) { - return $this->getValue($this->config['user_key'] . $username); + if (!$userInfo = $this->getValue($this->config['user_key'] . $username)) { + return false; + } + + // the default behavior is to use "username" as the user_id + return array_merge(array( + 'user_id' => $username, + ), $userInfo); } - public function setUser($user_id, $password, $first_name = null, $last_name = null) + public function setUser($username, $password, $first_name = null, $last_name = null) { return $this->setValue( - $this->config['user_key'] . $user_id, - compact('user_id', 'password', 'first_name', 'last_name') + $this->config['user_key'] . $username, + compact('username', 'password', 'first_name', 'last_name') ); } diff --git a/src/OAuth2/Storage/UserCredentialsInterface.php b/src/OAuth2/Storage/UserCredentialsInterface.php index 2a0f058..6e0fd7b 100644 --- a/src/OAuth2/Storage/UserCredentialsInterface.php +++ b/src/OAuth2/Storage/UserCredentialsInterface.php @@ -38,13 +38,13 @@ interface UserCredentialsInterface /** * @return - * ARRAY the associated "scope" or "user_id" values if applicable + * ARRAY the associated "user_id" and optional "scope" values * This function MUST return FALSE if the requested user does not exist or is * invalid. "scope" is a space-separated list of restricted scopes. * @code * return array( - * "scope" => SCOPE // OPTIONAL space-separated list of restricted scopes - * "user_id" => USER_ID, // REQUIRED user id to be stored with the authorization code or access token + * "user_id" => USER_ID, // REQUIRED user_id to be stored with the authorization code or access token + * "scope" => SCOPE // OPTIONAL space-separated list of restricted scopes * ); * @endcode */ diff --git a/test/lib/OAuth2/Storage/Bootstrap.php b/test/lib/OAuth2/Storage/Bootstrap.php index 00d75df..9b0839f 100644 --- a/test/lib/OAuth2/Storage/Bootstrap.php +++ b/test/lib/OAuth2/Storage/Bootstrap.php @@ -74,14 +74,14 @@ class Bootstrap $pdo->exec('CREATE TABLE oauth_clients (client_id TEXT, client_secret TEXT, redirect_uri TEXT, grant_types TEXT)'); $pdo->exec('CREATE TABLE oauth_access_tokens (access_token TEXT, client_id TEXT, user_id TEXT, expires TIMESTAMP, scope TEXT)'); $pdo->exec('CREATE TABLE oauth_authorization_codes (authorization_code TEXT, client_id TEXT, user_id TEXT, redirect_uri TEXT, expires TIMESTAMP, scope TEXT)'); - $pdo->exec('CREATE TABLE oauth_users (user_id TEXT, password TEXT, first_name TEXT, last_name TEXT)'); + $pdo->exec('CREATE TABLE oauth_users (username TEXT, password TEXT, first_name TEXT, last_name TEXT)'); $pdo->exec('CREATE TABLE oauth_refresh_tokens (refresh_token TEXT, client_id TEXT, user_id TEXT, expires TIMESTAMP, scope TEXT)'); // test data $pdo->exec('INSERT INTO oauth_clients (client_id, client_secret, grant_types) VALUES ("oauth_test_client", "testpass", "implicit password")'); $pdo->exec('INSERT INTO oauth_access_tokens (access_token, client_id) VALUES ("testtoken", "Some Client")'); $pdo->exec('INSERT INTO oauth_authorization_codes (authorization_code, client_id) VALUES ("testcode", "Some Client")'); - $pdo->exec('INSERT INTO oauth_users (user_id, password) VALUES ("testuser", "password")'); + $pdo->exec('INSERT INTO oauth_users (username, password) VALUES ("testuser", "password")'); } private function removeSqliteDb() @@ -98,14 +98,14 @@ class Bootstrap $pdo->exec('CREATE TABLE oauth_clients (client_id TEXT, client_secret TEXT, redirect_uri TEXT, grant_types TEXT)'); $pdo->exec('CREATE TABLE oauth_access_tokens (access_token TEXT, client_id TEXT, user_id TEXT, expires DATETIME, scope TEXT)'); $pdo->exec('CREATE TABLE oauth_authorization_codes (authorization_code TEXT, client_id TEXT, user_id TEXT, redirect_uri TEXT, expires DATETIME, scope TEXT)'); - $pdo->exec('CREATE TABLE oauth_users (user_id TEXT, password TEXT, first_name TEXT, last_name TEXT)'); + $pdo->exec('CREATE TABLE oauth_users (username TEXT, password TEXT, first_name TEXT, last_name TEXT)'); $pdo->exec('CREATE TABLE oauth_refresh_tokens (refresh_token TEXT, client_id TEXT, user_id TEXT, expires DATETIME, scope TEXT)'); // test data $pdo->exec('INSERT INTO oauth_clients (client_id, client_secret, grant_types) VALUES ("oauth_test_client", "testpass", "implicit password")'); $pdo->exec('INSERT INTO oauth_access_tokens (access_token, client_id) VALUES ("testtoken", "Some Client")'); $pdo->exec('INSERT INTO oauth_authorization_codes (authorization_code, client_id) VALUES ("testcode", "Some Client")'); - $pdo->exec('INSERT INTO oauth_users (user_id, password) VALUES ("testuser", "password")'); + $pdo->exec('INSERT INTO oauth_users (username, password) VALUES ("testuser", "password")'); } public function removeMysqlDb(\PDO $pdo) @@ -123,7 +123,7 @@ class Bootstrap $db->oauth_clients->insert(array('client_id' => "oauth_test_client", 'client_secret' => "testpass", 'redirect_uri' => "http://example.com", 'grant_types' => 'implicit password')); $db->oauth_access_tokens->insert(array('access_token' => "testtoken", 'client_id' => "Some Client")); $db->oauth_authorization_codes->insert(array('authorization_code' => "testcode", 'client_id' => "Some Client")); - $db->oauth_users->insert(array('user_id' => "testuser", 'password' => "password")); + $db->oauth_users->insert(array('username' => "testuser", 'password' => "password")); } public function removeMongoDb(\MongoDB $db) |
