Skip to content

Commit 3d36d7e

Browse files
committed
Merge pull request #33 from CakeDC/master
Avoid displaying approval screen if the client was already approved
2 parents 8895702 + 31e8042 commit 3d36d7e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Diff for: src/Controller/OAuthController.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Cake\Core\Configure;
66
use Cake\Event\Event;
77
use Cake\Event\EventManager;
8+
use Cake\I18n\Time;
89
use League\OAuth2\Server\Exception\AccessDeniedException;
910
use League\OAuth2\Server\Exception\OAuthException;
1011
use League\OAuth2\Server\Util\RedirectUri;
@@ -78,6 +79,9 @@ public function authorize()
7879
return;
7980
}
8081

82+
$ownerModel = $this->request->query('owner_model') ?: 'Users';
83+
$ownerId = $this->request->query('owner_id') ?: $this->Auth->user('id');
84+
$clientId = $this->request->query('client_id');
8185
if (!$this->Auth->user()) {
8286
$query = $this->request->query;
8387
$query['redir'] = 'oauth';
@@ -90,6 +94,18 @@ public function authorize()
9094
'?' => $query
9195
]
9296
);
97+
} else {
98+
$currentTokens = $this->loadModel('OAuthServer.AccessTokens')
99+
->find()
100+
->where(['expires > ' => Time::now()->getTimestamp()])
101+
->matching('Sessions', function ($q) use ($ownerModel, $ownerId, $clientId) {
102+
return $q->where([
103+
'owner_model' => $ownerModel,
104+
'owner_id' => $ownerId,
105+
'client_id' => $clientId
106+
]);
107+
})
108+
->count();
93109
}
94110

95111
$event = new Event('OAuthServer.beforeAuthorize', $this);
@@ -101,9 +117,10 @@ public function authorize()
101117
$serializeKeys = array_keys($event->result);
102118
}
103119

104-
if ($this->request->is('post') && $this->request->data['authorization'] === 'Approve') {
105-
$ownerModel = isset($this->request->data['owner_model']) ? $this->request->data['owner_model'] : 'Users';
106-
$ownerId = isset($this->request->data['owner_id']) ? $this->request->data['owner_id'] : $this->Auth->user('id');
120+
121+
if ($currentTokens > 0 || ($this->request->is('post') && $this->request->data('authorization') === 'Approve')) {
122+
$ownerModel = $this->request->data('owner_model') ?: $ownerModel;
123+
$ownerId = $this->request->data('owner_id') ?: $ownerId;
107124
$redirectUri = $this->OAuth->Server->getGrantType('authorization_code')->newAuthorizeRequest($ownerModel, $ownerId, $authParams);
108125
$event = new Event('OAuthServer.afterAuthorize', $this);
109126
EventManager::instance()->dispatch($event);

0 commit comments

Comments
 (0)