Skip to content

Commit 7f74c25

Browse files
author
mamluki
committed
initial commit after copying files from Filsh/yii2-oauth2-server
0 parents  commit 7f74c25

28 files changed

+1584
-0
lines changed

.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Folders to ignore
2+
vendor
3+
4+
# OS or Editor files and folders
5+
.buildpath
6+
.cache
7+
.DS_Store
8+
.idea
9+
.project
10+
.settings
11+
.tmproj
12+
desktop.ini
13+
nbproject
14+
Thumbs.db
15+
*.sublime-project
16+
*.sublime-workspace

Bootstrap.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace filsh\yii2\oauth2server;
4+
5+
/**
6+
* Instead use bootstrap module
7+
* should be removed in v2.1 version
8+
*
9+
* @deprecated v2.0.1
10+
*/
11+
class Bootstrap implements \yii\base\BootstrapInterface
12+
{
13+
use BootstrapTrait;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function bootstrap($app)
19+
{
20+
/** @var $module Module */
21+
if ($app->hasModule('oauth2') && ($module = $app->getModule('oauth2')) instanceof Module) {
22+
$this->initModule($module);
23+
24+
if ($app instanceof \yii\console\Application) {
25+
$module->controllerNamespace = 'filsh\yii2\oauth2server\commands';
26+
}
27+
}
28+
}
29+
}

BootstrapTrait.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace filsh\yii2\oauth2server;
4+
5+
trait BootstrapTrait
6+
{
7+
/**
8+
* @var array Model's map
9+
*/
10+
private $_modelMap = [
11+
'OauthClients' => 'filsh\yii2\oauth2server\models\OauthClients',
12+
'OauthAccessTokens' => 'filsh\yii2\oauth2server\models\OauthAccessTokens',
13+
'OauthAuthorizationCodes' => 'filsh\yii2\oauth2server\models\OauthAuthorizationCodes',
14+
'OauthRefreshTokens' => 'filsh\yii2\oauth2server\models\OauthRefreshTokens',
15+
'OauthScopes' => 'filsh\yii2\oauth2server\models\OauthScopes',
16+
];
17+
18+
/**
19+
* @var array Storage's map
20+
*/
21+
private $_storageMap = [
22+
'access_token' => 'filsh\yii2\oauth2server\storage\Pdo',
23+
'authorization_code' => 'filsh\yii2\oauth2server\storage\Pdo',
24+
'client_credentials' => 'filsh\yii2\oauth2server\storage\Pdo',
25+
'client' => 'filsh\yii2\oauth2server\storage\Pdo',
26+
'refresh_token' => 'filsh\yii2\oauth2server\storage\Pdo',
27+
'user_credentials' => 'filsh\yii2\oauth2server\storage\Pdo',
28+
'public_key' => 'filsh\yii2\oauth2server\storage\Pdo',
29+
'jwt_bearer' => 'filsh\yii2\oauth2server\storage\Pdo',
30+
'scope' => 'filsh\yii2\oauth2server\storage\Pdo',
31+
];
32+
33+
protected function initModule(Module $module)
34+
{
35+
$this->_modelMap = array_merge($this->_modelMap, $module->modelMap);
36+
foreach ($this->_modelMap as $name => $definition) {
37+
\Yii::$container->set("filsh\\yii2\\oauth2server\\models\\" . $name, $definition);
38+
$module->modelMap[$name] = is_array($definition) ? $definition['class'] : $definition;
39+
}
40+
41+
$this->_storageMap = array_merge($this->_storageMap, $module->storageMap);
42+
foreach ($this->_storageMap as $name => $definition) {
43+
\Yii::$container->set($name, $definition);
44+
$module->storageMap[$name] = is_array($definition) ? $definition['class'] : $definition;
45+
}
46+
}
47+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Igor Maliy
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Module.php

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
3+
namespace filsh\yii2\oauth2server;
4+
5+
use \Yii;
6+
use yii\i18n\PhpMessageSource;
7+
use \array_key_exists;
8+
9+
/**
10+
* For example,
11+
*
12+
* ```php
13+
* 'oauth2' => [
14+
* 'class' => 'filsh\yii2\oauth2server\Module',
15+
* 'tokenParamName' => 'accessToken',
16+
* 'tokenAccessLifetime' => 3600 * 24,
17+
* 'storageMap' => [
18+
* 'user_credentials' => 'common\models\User',
19+
* ],
20+
* 'grantTypes' => [
21+
* 'user_credentials' => [
22+
* 'class' => 'OAuth2\GrantType\UserCredentials',
23+
* ],
24+
* 'refresh_token' => [
25+
* 'class' => 'OAuth2\GrantType\RefreshToken',
26+
* 'always_issue_new_refresh_token' => true
27+
* ]
28+
* ]
29+
* ]
30+
* ```
31+
*/
32+
class Module extends \yii\base\Module
33+
{
34+
const VERSION = '2.0.0';
35+
36+
/**
37+
* @var array Model's map
38+
*/
39+
public $modelMap = [];
40+
41+
/**
42+
* @var array Storage's map
43+
*/
44+
public $storageMap = [];
45+
46+
/**
47+
* @var array GrantTypes collection
48+
*/
49+
public $grantTypes = [];
50+
51+
/**
52+
* @var array server options
53+
*/
54+
public $options = [];
55+
56+
/**
57+
* @var string name of access token parameter
58+
*/
59+
public $tokenParamName;
60+
61+
/**
62+
* @var type max access lifetime
63+
*/
64+
public $tokenAccessLifetime;
65+
/**
66+
* @var whether to use JWT tokens
67+
*/
68+
public $useJwtToken = false;//ADDED
69+
70+
/**
71+
* @inheritdoc
72+
*/
73+
public function init()
74+
{
75+
parent::init();
76+
$this->registerTranslations();
77+
}
78+
79+
/**
80+
* Gets Oauth2 Server
81+
*
82+
* @return \filsh\yii2\oauth2server\Server
83+
* @throws \yii\base\InvalidConfigException
84+
*/
85+
public function getServer()
86+
{
87+
if(!$this->has('server')) {
88+
$storages = [];
89+
90+
if($this->useJwtToken)
91+
{
92+
if(!array_key_exists('access_token', $this->storageMap) || !array_key_exists('public_key', $this->storageMap)) {
93+
throw new \yii\base\InvalidConfigException('access_token and public_key must be set or set useJwtToken to false');
94+
}
95+
//define dependencies when JWT is used instead of normal token
96+
\Yii::$container->clear('public_key'); //remove old definition
97+
\Yii::$container->set('public_key', $this->storageMap['public_key']);
98+
\Yii::$container->set('OAuth2\Storage\PublicKeyInterface', $this->storageMap['public_key']);
99+
100+
\Yii::$container->clear('access_token'); //remove old definition
101+
\Yii::$container->set('access_token', $this->storageMap['access_token']);
102+
}
103+
104+
foreach(array_keys($this->storageMap) as $name) {
105+
$storages[$name] = \Yii::$container->get($name);
106+
}
107+
108+
$grantTypes = [];
109+
foreach($this->grantTypes as $name => $options) {
110+
if(!isset($storages[$name]) || empty($options['class'])) {
111+
throw new \yii\base\InvalidConfigException('Invalid grant types configuration.');
112+
}
113+
114+
$class = $options['class'];
115+
unset($options['class']);
116+
117+
$reflection = new \ReflectionClass($class);
118+
$config = array_merge([0 => $storages[$name]], [$options]);
119+
120+
$instance = $reflection->newInstanceArgs($config);
121+
$grantTypes[$name] = $instance;
122+
}
123+
124+
$server = \Yii::$container->get(Server::className(), [
125+
$this,
126+
$storages,
127+
array_merge(array_filter([
128+
'use_jwt_access_tokens' => $this->useJwtToken,//ADDED
129+
'token_param_name' => $this->tokenParamName,
130+
'access_lifetime' => $this->tokenAccessLifetime,
131+
/** add more ... */
132+
]), $this->options),
133+
$grantTypes
134+
]);
135+
136+
$this->set('server', $server);
137+
}
138+
return $this->get('server');
139+
}
140+
141+
public function getRequest()
142+
{
143+
if(!$this->has('request')) {
144+
$this->set('request', Request::createFromGlobals());
145+
}
146+
return $this->get('request');
147+
}
148+
149+
public function getResponse()
150+
{
151+
if(!$this->has('response')) {
152+
$this->set('response', new Response());
153+
}
154+
return $this->get('response');
155+
}
156+
157+
/**
158+
* @param $response
159+
*/
160+
public function setResponse($response)
161+
{
162+
Yii::$app->response->setStatusCode($response->getStatusCode());
163+
$headers = Yii::$app->response->getHeaders();
164+
foreach ($response->getHttpHeaders() as $name => $value)
165+
$headers->set($name, $value);
166+
}
167+
/**
168+
* @param $is_authorized
169+
* @param $user_id
170+
* @return \OAuth2\ResponseInterface
171+
* @throws \yii\base\InvalidConfigException
172+
*/
173+
public function handleAuthorizeRequest($is_authorized, $user_id)
174+
{
175+
$response = $this->getServer()->handleAuthorizeRequest(
176+
$this->getRequest(),
177+
$this->getResponse(),
178+
$is_authorized,
179+
$user_id
180+
);
181+
$this->setResponse($response);
182+
return $response;
183+
}
184+
185+
/**
186+
* Register translations for this module
187+
*
188+
* @return array
189+
*/
190+
public function registerTranslations()
191+
{
192+
if(!isset(Yii::$app->get('i18n')->translations['modules/oauth2/*'])) {
193+
Yii::$app->get('i18n')->translations['modules/oauth2/*'] = [
194+
'class' => PhpMessageSource::className(),
195+
'basePath' => __DIR__ . '/messages',
196+
];
197+
}
198+
}
199+
200+
/**
201+
* Translate module message
202+
*
203+
* @param string $category
204+
* @param string $message
205+
* @param array $params
206+
* @param string $language
207+
* @return string
208+
*/
209+
public static function t($category, $message, $params = [], $language = null)
210+
{
211+
return Yii::t('modules/oauth2/' . $category, $message, $params, $language);
212+
}
213+
}

0 commit comments

Comments
 (0)