Skip to content

Commit

Permalink
Oauth module v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
filsh committed Jun 18, 2015
1 parent 0562871 commit ae4853f
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 72 deletions.
114 changes: 51 additions & 63 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,91 +48,51 @@ class Module extends \yii\base\Module
*/
public $storageMap = [];




public $options = [];

public $grantTypes = [];

private $_server;

private $_request;
public $tokenParamName;
public $tokenAccessLifetime;

/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->registerComponents();
$this->registerTranslations();
}

/**
* Get oauth2 server instance
* @param type $force
* @return \OAuth2\Server
* Translate module message
*
* @param string $category
* @param string $message
* @param array $params
* @param string $language
* @return string
*/
public function getServer($force = false)
public static function t($category, $message, $params = [], $language = null)
{
if($this->_server === null || $force === true) {
$storages = [];
foreach($this->storageMap as $name => $value) {
$storages[$name] = \Yii::$container->get($name);
}
$server = new \OAuth2\Server($storages, $this->options);

foreach($this->grantTypes as $name => $options) {
if(!isset($storages[$name]) || empty($options['class'])) {
throw new \yii\base\InvalidConfigException('Invalid grant types configuration.');
}

$class = $options['class'];
unset($options['class']);

$reflection = new \ReflectionClass($class);
$config = array_merge([0 => $storages[$name]], [$options]);

$instance = $reflection->newInstanceArgs($config);
$server->addGrantType($instance);
}

$this->_server = $server;
}
return $this->_server;
return Yii::t('modules/oauth2/' . $category, $message, $params, $language);
}

/**
* Get oauth2 request instance from global variables
* @return \OAuth2\Request
*/
public function getRequest($force = false)
protected function registerComponents()
{
if ($this->_request === null || $force) {
$this->_request = \OAuth2\Request::createFromGlobals();
};
return $this->_request;
$this->setComponents([
'server' => $this->createServer(),
'request' => Request::createFromGlobals(),
'response' => new Response()
]);
}

/**
* Get oauth2 response instance
* @return \OAuth2\Response
*/
public function getResponse()
{
return new \OAuth2\Response();
}






/**
* Register translations for this module
*
* @return array
*/
public function registerTranslations()
protected function registerTranslations()
{
if(!isset(Yii::$app->get('i18n')->translations['modules/oauth2/*'])) {
Yii::$app->get('i18n')->translations['modules/oauth2/*'] = [
Expand All @@ -142,8 +102,36 @@ public function registerTranslations()
}
}

public static function t($category, $message, $params = [], $language = null)
protected function createServer()
{
return Yii::t('modules/oauth2/' . $category, $message, $params, $language);
$storages = [];
foreach(array_keys($this->storageMap) as $name) {
$storages[$name] = \Yii::$container->get($name);
}
$server = \Yii::$container->get(Server::className(), [
$storages,
[
'token_param_name' => $this->tokenParamName,
'access_lifetime' => $this->tokenAccessLifetime,
/** add more ... */
]
]);

foreach($this->grantTypes as $name => $options) {
if(!isset($storages[$name]) || empty($options['class'])) {
throw new \yii\base\InvalidConfigException('Invalid grant types configuration.');
}

$class = $options['class'];
unset($options['class']);

$reflection = new \ReflectionClass($class);
$config = array_merge([0 => $storages[$name]], [$options]);

$instance = $reflection->newInstanceArgs($config);
$server->addGrantType($instance);
}

return $server;
}
}
8 changes: 8 additions & 0 deletions Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace filsh\yii2\oauth2server;

class Request extends \OAuth2\Request
{
use traits\ClassNamespace;
}
8 changes: 8 additions & 0 deletions Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace filsh\yii2\oauth2server;

class Response extends \OAuth2\Response
{
use traits\ClassNamespace;
}
35 changes: 35 additions & 0 deletions Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace filsh\yii2\oauth2server;

class Server extends \OAuth2\Server
{
use traits\ClassNamespace;

/**
* @var \filsh\yii2\oauth2server\Module
*/
protected $module;

public function __construct(Module $module, $storage = array(), array $config = array(), array $grantTypes = array(), array $responseTypes = array(), \OAuth2\TokenType\TokenTypeInterface $tokenType = null, \OAuth2\ScopeInterface $scopeUtil = null, \OAuth2\ClientAssertionType\ClientAssertionTypeInterface $clientAssertionType = null)
{
$this->module = $module;
parent::__construct($storage, $config, $grantTypes, $responseTypes, $tokenType, $scopeUtil, $clientAssertionType);
}

public function handleTokenRequest(\OAuth2\RequestInterface $request = null, \OAuth2\ResponseInterface $response = null)
{
if($request === null) {
$request = $this->module->get('request');
}
return parent::handleTokenRequest($request, $response);
}

public function verifyResourceRequest(\OAuth2\RequestInterface $request = null, \OAuth2\ResponseInterface $response = null, $scope = null)
{
if($request === null) {
$request = $this->module->get('request');
}
parent::verifyResourceRequest($request, $response, $scope);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"yiisoft/yii2": "*",
"bshaffer/oauth2-server-php": "~1.7.1"
"bshaffer/oauth2-server-php": "~1.7"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 1 addition & 4 deletions controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public function behaviors()

public function actionToken()
{
$server = $this->module->getServer();
$request = $this->module->getRequest();
$response = $server->handleTokenRequest($request);

$response = $this->module->get('server')->handleTokenRequest();
return $response->getParameters();
}
}
2 changes: 1 addition & 1 deletion filters/ErrorToExceptionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function events()
*/
public function afterAction($event)
{
$response = Yii::$app->getModule('oauth2')->getServer()->getResponse();
$response = Yii::$app->getModule('oauth2')->get('response');

$isValid = true;
if($response !== null) {
Expand Down
5 changes: 2 additions & 3 deletions filters/auth/CompositeAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ class CompositeAuth extends \yii\filters\auth\CompositeAuth
*/
public function beforeAction($action)
{
$oauthServer = Yii::$app->getModule('oauth2')->getServer();
$oauthRequest = Yii::$app->getModule('oauth2')->getRequest();
$oauthServer->verifyResourceRequest($oauthRequest);
$server = Yii::$app->getModule('oauth2')->getServer();
$server->verifyResourceRequest();

return parent::beforeAction($action);
}
Expand Down
11 changes: 11 additions & 0 deletions traits/ClassNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace filsh\yii2\oauth2server\traits;

trait ClassNamespace
{
public static function className()
{
return get_called_class();
}
}

0 comments on commit ae4853f

Please sign in to comment.