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 19, 2015
1 parent ae4853f commit 9d69443
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 126 deletions.
114 changes: 68 additions & 46 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Module extends \yii\base\Module
*/
public $storageMap = [];

/**
* @var array GrantTypes map
*/
public $grantTypes = [];

public $tokenParamName;
Expand All @@ -60,39 +63,77 @@ class Module extends \yii\base\Module
public function init()
{
parent::init();
$this->registerComponents();
$this->registerTranslations();
}

/**
* Translate module message
* Gets Oauth2 Server
*
* @param string $category
* @param string $message
* @param array $params
* @param string $language
* @return string
* @return \filsh\yii2\oauth2server\Server
* @throws \yii\base\InvalidConfigException
*/
public static function t($category, $message, $params = [], $language = null)
public function getServer()
{
return Yii::t('modules/oauth2/' . $category, $message, $params, $language);
if(!$this->has('server')) {
$storages = [];
foreach(array_keys($this->storageMap) as $name) {
$storages[$name] = \Yii::$container->get($name);
}

$grantTypes = [];
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);
$grantTypes[$name] = $instance;
}

$server = \Yii::$container->get(Server::className(), [
$this,
$storages,
[
'token_param_name' => $this->tokenParamName,
'access_lifetime' => $this->tokenAccessLifetime,
/** add more ... */
],
$grantTypes
]);

$this->set('server', $server);
}
return $this->get('server');
}

protected function registerComponents()
public function getRequest()
{
$this->setComponents([
'server' => $this->createServer(),
'request' => Request::createFromGlobals(),
'response' => new Response()
]);
if(!$this->has('request')) {
$this->set('request', Request::createFromGlobals());
}
return $this->get('request');
}

public function getResponse()
{
if(!$this->has('response')) {
$this->set('response', new Response());
}
return $this->get('response');
}

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

protected function createServer()
/**
* Translate module message
*
* @param string $category
* @param string $message
* @param array $params
* @param string $language
* @return string
*/
public static function t($category, $message, $params = [], $language = null)
{
$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;
return Yii::t('modules/oauth2/' . $category, $message, $params, $language);
}
}
12 changes: 6 additions & 6 deletions Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public function __construct(Module $module, $storage = array(), array $config =
parent::__construct($storage, $config, $grantTypes, $responseTypes, $tokenType, $scopeUtil, $clientAssertionType);
}

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

public function verifyResourceRequest(\OAuth2\RequestInterface $request = null, \OAuth2\ResponseInterface $response = null, $scope = null)
public function handleTokenRequest(\OAuth2\RequestInterface $request = null, \OAuth2\ResponseInterface $response = null)
{
if($request === null) {
$request = $this->module->get('request');
$request = $this->module->getRequest();
}
parent::verifyResourceRequest($request, $response, $scope);
return parent::handleTokenRequest($request, $response);
}
}
2 changes: 1 addition & 1 deletion controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function behaviors()

public function actionToken()
{
$response = $this->module->get('server')->handleTokenRequest();
$response = $this->module->getServer()->handleTokenRequest();
return $response->getParameters();
}
}
25 changes: 25 additions & 0 deletions exceptions/HttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace filsh\yii2\oauth2server\exceptions;

class HttpException extends \yii\web\HttpException
{
/**
* @var string Uri for details of exception
*/
public $errorUri;

/**
* Constructor.
* @param integer $status HTTP status code, such as 404, 500, etc.
* @param string $message error message
* @param string $errorUri error uri
* @param integer $code error code
* @param \Exception $previous The previous exception used for the exception chaining.
*/
public function __construct($status, $message = null, $errorUri = null, $code = 0, \Exception $previous = null)
{
$this->errorUri = $errorUri;
parent::__construct($status, $message, $code, $previous);
}
}
23 changes: 15 additions & 8 deletions filters/ErrorToExceptionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
use Yii;
use yii\base\Controller;
use filsh\yii2\oauth2server\Module;
use filsh\yii2\oauth2server\exceptions\HttpException;

class ErrorToExceptionFilter extends \yii\base\Behavior
{
/**
* @inheritdoc
*/
public function events()
{
return [Controller::EVENT_AFTER_ACTION => 'afterAction'];
Expand All @@ -20,20 +24,23 @@ public function events()
*/
public function afterAction($event)
{
$response = Yii::$app->getModule('oauth2')->get('response');
$response = Yii::$app->getModule('oauth2')->getServer()->getResponse();

$isValid = true;
if($response !== null) {
$isValid = $response->isInformational() || $response->isSuccessful() || $response->isRedirection();
}
if(!$isValid) {
$status = $response->getStatusCode();
// TODO: необходимо также пробрасывать error_uri
$message = Module::t('common', $response->getParameter('error_description'));
if($message === null) {
$message = Module::t('common', 'An internal server error occurred.');
}
throw new \yii\web\HttpException($status, $message);
throw new HttpException($response->getStatusCode(), $this->getErrorMessage($response), $response->getParameter('error_uri'));
}
}

protected function getErrorMessage(\OAuth2\Response $response)
{
$message = Module::t('common', $response->getParameter('error_description'));
if($message === null) {
$message = Module::t('common', 'An internal server error occurred.');
}
return $message;
}
}
65 changes: 0 additions & 65 deletions grants/UserAuthCredentials.php

This file was deleted.

0 comments on commit 9d69443

Please sign in to comment.