2121
2222use Firebase \JWT \JWK ;
2323use Firebase \JWT \JWT ;
24+ use fproject \models \UserIdentity ;
2425use yii \helpers \Json ;
2526use Yii ;
2627
@@ -38,6 +39,8 @@ class OAuth2 extends \yii\authclient\OAuth2
3839 /** @var string $contextData the context data that will send together with auth's URL params */
3940 public $ contextData ;
4041
42+ /** @var string $sessionId the session ID issued by OAuth provider */
43+ public $ sessionId ;
4144 /**
4245 * @var array list of attribute names, which should be requested from API to initialize user attributes.
4346 */
@@ -47,6 +50,8 @@ class OAuth2 extends \yii\authclient\OAuth2
4750 'email ' ,
4851 ];
4952
53+ /** The expire duration for pubic key */
54+ const PUBLIC_KEY_EXPIRE_DURATION = 86400 ;
5055 /**
5156 * @inheritdoc
5257 */
@@ -123,20 +128,29 @@ public function getCurlOptions()
123128
124129 public function getPublicKey ()
125130 {
126- if (! isset ($ this ->_publicKey ))
131+ if (empty ($ this ->_publicKey ) && ! empty ( $ this -> jwkUrl ))
127132 {
128- $ this ->_publicKey = $ this ->sendRequest ('GET ' , $ this ->jwkUrl );
133+ if (Yii::$ app ->cache )
134+ {
135+ $ this ->_publicKey = Yii::$ app ->cache ->get ($ this ->jwkUrl );
136+ if ($ this ->_publicKey === false )
137+ {
138+ $ this ->_publicKey = $ this ->sendRequest ('GET ' , $ this ->jwkUrl );
139+ Yii::$ app ->cache ->set ($ this ->jwkUrl , $ this ->_publicKey , self ::PUBLIC_KEY_EXPIRE_DURATION );
140+ }
141+ }
129142 }
130143 return $ this ->_publicKey ;
131144 }
132145
133146 public function logout ()
134147 {
135- $ sid = Yii::$ app ->user ->getId ();
136- if ($ sid )
148+ /** @var UserIdentity $identity */
149+ $ identity = Yii::$ app ->user ->getIdentity ();
150+ if ($ identity != null && !empty ($ identity ->sid ))
137151 {
138152 $ headers = ['Authorization ' => "Bearer " . $ this ->getAccessToken ()->token ];
139- $ params = ['sid ' => $ sid ];
153+ $ params = ['sid ' => $ identity -> sid ];
140154 $ this ->sendRequest ("GET " , $ this ->logoutUrl , $ params , $ headers );
141155 }
142156 }
0 commit comments