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