Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved SQL and removed validate_user().

Validating the user can now easily be handled by the auth package so no need to worry about it in the model.
  • Loading branch information...
commit a04b225a65abd47d00945669e598e4234cb6ec9c 1 parent e5bade7
@philsturgeon philsturgeon authored
View
43 classes/model/server/db.php
@@ -10,23 +10,23 @@
namespace OAuth2;
-class Model_Server_Db extends Model_Server
+class Model_Server_DB extends Model_Server
{
- const TABLE_CLIENT = 'applications';
- const TABLE_SESSIONS = 'oauth2_sessions';
+ const TABLE_CLIENT = 'oauth_clients';
+ const TABLE_SESSIONS = 'oauth_sessions';
const TABLE_SESSION_SCOPES = 'oauth_session_scopes';
- const TABLE_SCOPES = 'scopes';
+ const TABLE_SCOPES = 'oauth_scopes';
public function get_client(array $where)
{
- $client = \DB::select('name', 'client_id', 'auto_approve')
+ $clients = \DB::select('name', 'client_id', 'auto_approve')
->from(static::TABLE_CLIENT)
->where($where)
->limit(1)
- ->execute()
- ->as_array();
+ ->as_object()
+ ->execute();
- return $client ? current($client) : false;
+ return isset($clients[0]) ? $clients[0] : false;
}
public function get_session(array $where)
@@ -35,10 +35,10 @@ public function get_session(array $where)
->from(static::TABLE_SESSIONS)
->where($where)
->limit(1)
- ->execute()
- ->as_array();
+ ->as_object()
+ ->execute();
- return $session ? current($session) : false;
+ return isset($session[0]) ? $session[0] : false;
}
public function get_token_from_session($session_id)
@@ -64,15 +64,15 @@ public function has_user_authenicated_client($client_id, $user_id)
->where('access_token', '!=', null)
->from(static::TABLE_SESSIONS)
->limit(1)
- ->as_array()
+ ->as_object()
->execute();
- return $tokens ? current($tokens->access_token) : false;
+ return isset($tokens[0]) ? $tokens[0]->access_token : false;
}
public function has_scope($access_token, $scope)
{
- $has_any = \Db::select('id')
+ $has_any = \DB::select('id')
->where('access_token', $access_token)
->where('scope', $scope)
->from(static::TABLE_SESSION_SCOPES)
@@ -85,7 +85,7 @@ public function has_scope($access_token, $scope)
public function new_session(array $values)
{
// Set the session values
- $result = \Db::delete(static::TABLE_SESSION)
+ $result = \DB::insert(static::TABLE_SESSIONS)
->set($values)
->execute();
@@ -97,7 +97,7 @@ public function new_session(array $values)
{
if (trim($scope) !== "")
{
- \Db::insert(static::TABLE_SESSION_SCOPES)
+ \DB::insert(static::TABLE_SESSION_SCOPES)
->set(array(
'session_id' => $session_id,
'scope' => $scope
@@ -108,7 +108,7 @@ public function new_session(array $values)
public function update_session(array $where, array $values)
{
- return \Db::update(static::TABLE_SESSION)
+ return \DB::update(static::TABLE_SESSION)
->set($values)
->where($where)
->execute();
@@ -128,7 +128,7 @@ public function create_access_token($session_id)
'stage' => 'granted'
));
- \Db::set('access_token', $access_token)
+ \DB::set('access_token', $access_token)
->where('session_id', $session_id)
->update(static::TABLE_SESSION_SCOPES)
->execute();
@@ -138,16 +138,11 @@ public function create_access_token($session_id)
public function delete_session(array $where)
{
- return \Db::delete(static::TABLE_SESSION)
+ return \DB::delete(static::TABLE_SESSIONS)
->where($where)
->execute();
}
- public function validate_user($username = "", $password = "")
- {
- exit('DO ME! '.__FUNCTION__.' L:'.__FILE__);
- }
-
// Scopes
public function get_scope($scope)
View
22 classes/server.php
@@ -235,7 +235,7 @@ public function scope_exists($scope)
return (bool) $this->model->get_scope($scope);
}
- public function get_scope($scopes)
+ public function get_scope($scope)
{
return $this->model->get_scope($scope);
}
@@ -255,28 +255,12 @@ public function redirect_uri($redirect_uri = '', $params = array(), $query_delim
{
if (strstr($redirect_uri, $query_delimeter))
{
- $redirect_uri = $redirect_uri . http_build_query($params);
+ return $redirect_uri . http_build_query($params);
}
else
{
- $redirect_uri = $redirect_uri . $query_delimeter . http_build_query($params);
+ return $redirect_uri . $query_delimeter . http_build_query($params);
}
-
- return $redirect_uri;
- }
-
-
- /**
- * Sign the user into your application.
- *
- * Edit this function to suit your needs. It must return a user's id as a string
- * or FALSE if the sign in was incorrect
- *
- * @return string|bool
- */
- public function validate_user($username = "", $password = "")
- {
- $this->model->validate_user($username, $password);
}
}
View
31 schema/mysql.sql → schema/server-mysql.sql
@@ -1,7 +1,7 @@
-DROP TABLE IF EXISTS oauth2_session_scopes, oauth2_sessions, oauth2_applications, oauth2_scopes;
+DROP TABLE IF EXISTS oauth_session_scopes, oauth_sessions, oauth_clients, oauth_scopes;
-CREATE TABLE `oauth2_applications` (
+CREATE TABLE `oauth_clients` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL DEFAULT '',
`client_id` varchar(32) NOT NULL DEFAULT '',
@@ -16,17 +16,8 @@ CREATE TABLE `oauth2_applications` (
UNIQUE KEY `client_id` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-CREATE TABLE `oauth2_scopes` (
- `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
- `scope` varchar(64) NOT NULL DEFAULT '',
- `name` varchar(64) NOT NULL DEFAULT '',
- `description` varchar(100) DEFAULT '',
- PRIMARY KEY (`id`),
- UNIQUE KEY `scope` (`scope`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
-CREATE TABLE `oauth2_sessions` (
+CREATE TABLE `oauth_sessions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`client_id` varchar(32) NOT NULL DEFAULT '',
`redirect_uri` varchar(250) NOT NULL DEFAULT '',
@@ -40,11 +31,19 @@ CREATE TABLE `oauth2_sessions` (
`limited_access` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Used for user agent flows',
PRIMARY KEY (`id`),
KEY `client_id` (`client_id`),
- CONSTRAINT `oauth_sessions_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `applications` (`client_id`) ON DELETE CASCADE
+ CONSTRAINT `oauth_sessions_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `oauth_clients` (`client_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+CREATE TABLE `oauth_scopes` (
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+ `scope` varchar(64) NOT NULL DEFAULT '',
+ `name` varchar(64) NOT NULL DEFAULT '',
+ `description` varchar(100) DEFAULT '',
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `scope` (`scope`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-CREATE TABLE `oauth2_session_scopes` (
+CREATE TABLE `oauth_session_scopes` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`session_id` int(11) unsigned NOT NULL,
`access_token` varchar(50) NOT NULL DEFAULT '',
@@ -53,6 +52,6 @@ CREATE TABLE `oauth2_session_scopes` (
KEY `session_id` (`session_id`),
KEY `scope` (`scope`),
KEY `access_token` (`access_token`),
- CONSTRAINT `oauth_session_scopes_ibfk_1` FOREIGN KEY (`scope`) REFERENCES `scopes` (`scope`),
- CONSTRAINT `oauth_session_scopes_ibfk_2` FOREIGN KEY (`session_id`) REFERENCES `oauth2_sessions` (`id`) ON DELETE CASCADE
+ CONSTRAINT `oauth_session_scopes_ibfk_1` FOREIGN KEY (`scope`) REFERENCES `oauth_scopes` (`scope`),
+ CONSTRAINT `oauth_session_scopes_ibfk_2` FOREIGN KEY (`session_id`) REFERENCES `oauth_sessions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Please sign in to comment.
Something went wrong with that request. Please try again.