Skip to content

Commit d6bba7d

Browse files
author
Bui Sy Nguyen
committed
[WIP] First runnable version
1 parent 7dca04c commit d6bba7d

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

fproject/authclient/AuthAction.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ public function run()
5151
if (!$collection->hasClient($clientId)) {
5252
throw new NotFoundHttpException("Unknown auth client '{$clientId}'");
5353
}
54+
55+
/** @var OAuth2 $client */
5456
$client = $collection->getClient($clientId);
5557

58+
if (!empty($_GET['sid']))
59+
$client->sessionId = $_GET['sid'];
60+
5661
return $this->auth($client);
5762
}
5863
}
@@ -70,6 +75,7 @@ public function onAuthSuccess($client)
7075
$identity = new UserIdentity($attributes);
7176
if(Yii::$app->user->login($identity, $client->getAccessToken()->getExpireDuration()))
7277
{
78+
$identity->sid = $client->sessionId;
7379
$identity->saveToSession();
7480
}
7581
}

fproject/authclient/OAuth2.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
use Firebase\JWT\JWK;
2323
use Firebase\JWT\JWT;
24+
use fproject\models\UserIdentity;
2425
use yii\helpers\Json;
2526
use 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
}

fproject/models/UserIdentity.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
*/
3232
class UserIdentity implements IdentityInterface
3333
{
34+
/** @var string $sid Session ID */
35+
public $sid;
36+
3437
/**
3538
* @var string $sub
3639
* User ID register in pk-auth

0 commit comments

Comments
 (0)