Skip to content

Commit

Permalink
Merge pull request #21 from hiqdev/master
Browse files Browse the repository at this point in the history
improved migration to work on non-MySQL (fixes #19) and other
  • Loading branch information
filsh committed Apr 10, 2015
2 parents 2489013 + 4cfaa8f commit 4670680
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
13 changes: 9 additions & 4 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Module extends \yii\base\Module
public $i18n;

private $_server;

private $_request;

private $_models = [];

Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -109,7 +114,7 @@ public function getResponse()
{
return new \OAuth2\Response();
}

/**
* Create storages
* @return type
Expand Down Expand Up @@ -191,4 +196,4 @@ protected function getDefaultModelClasses()
'Scopes' => 'filsh\yii2\oauth2server\models\OauthScopes',
];
}
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
],
Expand Down
70 changes: 48 additions & 22 deletions migrations/m140501_075311_add_oauth2_server.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,39 @@

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;
if ($this->db->driverName === 'mysql') {
$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}}', [
Expand All @@ -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';
Expand All @@ -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();
Expand Down

0 comments on commit 4670680

Please sign in to comment.