From 4cfaa8f4d47442f0e007072f4d011694856f9e36 Mon Sep 17 00:00:00 2001 From: sol Date: Wed, 8 Apr 2015 20:52:20 +0000 Subject: [PATCH] improved migration to work on non-MySQL improved Module::getRequest to store like getServer minor fix at README --- Module.php | 13 ++-- README.md | 6 +- .../m140501_075311_add_oauth2_server.php | 70 +++++++++++++------ 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/Module.php b/Module.php index 56e2b11..496b276 100644 --- a/Module.php +++ b/Module.php @@ -48,6 +48,8 @@ class Module extends \yii\base\Module public $i18n; private $_server; + + private $_request; private $_models = []; @@ -96,9 +98,12 @@ public function getServer($force = false) * Get oauth2 request instance from global variables * @return \OAuth2\Request */ - public function getRequest() + public function getRequest($force = false) { - return \OAuth2\Request::createFromGlobals(); + if ($this->_request === null || $force) { + $this->_request = \OAuth2\Request::createFromGlobals(); + }; + return $this->_request; } /** @@ -109,7 +114,7 @@ public function getResponse() { return new \OAuth2\Response(); } - + /** * Create storages * @return type @@ -191,4 +196,4 @@ protected function getDefaultModelClasses() 'Scopes' => 'filsh\yii2\oauth2server\models\OauthScopes', ]; } -} \ No newline at end of file +} diff --git a/README.md b/README.md index e7bd7bd..bdb0f44 100644 --- a/README.md +++ b/README.md @@ -36,14 +36,14 @@ To use this extension, simply add the following code in your application config ], 'grantTypes' => [ 'client_credentials' => [ - 'class' => '\OAuth2\GrantType\ClientCredentials', + 'class' => 'OAuth2\GrantType\ClientCredentials', 'allow_public_clients' => false ], 'user_credentials' => [ - 'class' => '\OAuth2\GrantType\UserCredentials' + 'class' => 'OAuth2\GrantType\UserCredentials' ], 'refresh_token' => [ - 'class' => '\OAuth2\GrantType\RefreshToken', + 'class' => 'OAuth2\GrantType\RefreshToken', 'always_issue_new_refresh_token' => true ] ], diff --git a/migrations/m140501_075311_add_oauth2_server.php b/migrations/m140501_075311_add_oauth2_server.php index bc78e84..9a12be1 100644 --- a/migrations/m140501_075311_add_oauth2_server.php +++ b/migrations/m140501_075311_add_oauth2_server.php @@ -4,6 +4,29 @@ class m140501_075311_add_oauth2_server extends \yii\db\Migration { + + public function mysql($yes,$no='') { + return $this->db->driverName === 'mysql' ? $yes : $no; + } + + public function primaryKey($columns) { + return 'PRIMARY KEY (' . $this->db->getQueryBuilder()->buildColumns($columns) . ')'; + } + + public function foreignKey($columns,$refTable,$refColumns,$onDelete = null,$onUpdate = null) { + $builder = $this->db->getQueryBuilder(); + $sql = ' FOREIGN KEY (' . $builder->buildColumns($columns) . ')' + . ' REFERENCES ' . $this->db->quoteTableName($refTable) + . ' (' . $builder->buildColumns($refColumns) . ')'; + if ($onDelete !== null) { + $sql .= ' ON DELETE ' . $onDelete; + } + if ($onUpdate !== null) { + $sql .= ' ON UPDATE ' . $onUpdate; + } + return $sql; + } + public function up() { $tableOptions = null; @@ -11,6 +34,9 @@ public function up() $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; } + $now = $this->mysql('CURRENT_TIMESTAMP',"'now'"); + $on_update_now = $this->mysql("ON UPDATE $now"); + $transaction = $this->db->beginTransaction(); try { $this->createTable('{{%oauth_clients}}', [ @@ -20,72 +46,72 @@ public function up() 'grant_types' => Schema::TYPE_STRING . '(100) NOT NULL', 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', - 'PRIMARY KEY (`client_id`)' + $this->primaryKey('client_id'), ], $tableOptions); - + $this->createTable('{{%oauth_access_tokens}}', [ 'access_token' => Schema::TYPE_STRING . '(40) NOT NULL', 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', - 'expires' => Schema::TYPE_TIMESTAMP . ' NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', + 'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now", 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - 'PRIMARY KEY (`access_token`)', - 'FOREIGN KEY (`client_id`) REFERENCES {{%oauth_clients}} (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE', + $this->primaryKey('access_token'), + $this->foreignKey('client_id','{{%oauth_clients}}','client_id','CASCADE','CASCADE'), ], $tableOptions); - + $this->createTable('{{%oauth_refresh_tokens}}', [ 'refresh_token' => Schema::TYPE_STRING . '(40) NOT NULL', 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', - 'expires' => Schema::TYPE_TIMESTAMP . ' NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', + 'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now", 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - 'PRIMARY KEY (`refresh_token`)', - 'FOREIGN KEY (`client_id`) REFERENCES {{%oauth_clients}} (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE', + $this->primaryKey('refresh_token'), + $this->foreignKey('client_id','{{%oauth_clients}}','client_id','CASCADE','CASCADE'), ], $tableOptions); - + $this->createTable('{{%oauth_authorization_codes}}', [ 'authorization_code' => Schema::TYPE_STRING . '(40) NOT NULL', 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', 'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL', - 'expires' => Schema::TYPE_TIMESTAMP . ' NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', + 'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now", 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - 'PRIMARY KEY (`authorization_code`)', - 'FOREIGN KEY (`client_id`) REFERENCES {{%oauth_clients}} (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE', + $this->primaryKey('authorization_code'), + $this->foreignKey('client_id','{{%oauth_clients}}','client_id','CASCADE','CASCADE'), ], $tableOptions); - + $this->createTable('{{%oauth_scopes}}', [ 'scope' => Schema::TYPE_STRING . '(2000) NOT NULL', 'is_default' => Schema::TYPE_BOOLEAN . ' NOT NULL', ], $tableOptions); - + $this->createTable('{{%oauth_jwt}}', [ 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', 'subject' => Schema::TYPE_STRING . '(80) DEFAULT NULL', 'public_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - 'PRIMARY KEY (`client_id`)', + $this->primaryKey('client_id'), ], $tableOptions); - + $this->createTable('{{%oauth_users}}', [ 'username' => Schema::TYPE_STRING . '(255) NOT NULL', 'password' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', 'first_name' => Schema::TYPE_STRING . '(255) DEFAULT NULL', 'last_name' => Schema::TYPE_STRING . '(255) DEFAULT NULL', - 'PRIMARY KEY (`username`)', + $this->primaryKey('username'), ], $tableOptions); - + $this->createTable('{{%oauth_public_keys}}', [ 'client_id' => Schema::TYPE_STRING . '(255) NOT NULL', 'public_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', 'private_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', 'encryption_algorithm' => Schema::TYPE_STRING . '(100) DEFAULT \'RS256\'', ], $tableOptions); - + // insert client data $this->batchInsert('{{%oauth_clients}}', ['client_id', 'client_secret', 'redirect_uri', 'grant_types'], [ ['testclient', 'testpass', 'http://fake/', 'client_credentials authorization_code password implicit'], ]); - + $transaction->commit(); } catch (Exception $e) { echo 'Exception: ' . $e->getMessage() . '\n'; @@ -108,7 +134,7 @@ public function down() $this->dropTable('{{%oauth_refresh_tokens}}'); $this->dropTable('{{%oauth_access_tokens}}'); $this->dropTable('{{%oauth_clients}}'); - + $transaction->commit(); } catch (Exception $e) { $transaction->rollback();