Skip to content

Commit b3cd722

Browse files
authored
Instrospection tweaks (#17)
performance fixes for introspection endpoint Signed-off-by: smarcet@gmail.com <smarcet@gmail.com> Change-Id: I7928c53fe0885c83571ee5b5821a988f1752b1fb Signed-off-by: smarcet@gmail.com <smarcet@gmail.com>
1 parent 0d6b8cc commit b3cd722

12 files changed

Lines changed: 85 additions & 10 deletions

app/Models/OAuth2/AccessToken.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/**
2121
* @ORM\Entity(repositoryClass="App\Repositories\DoctrineAccessTokenRepository")
2222
* @ORM\Table(name="oauth2_access_token")
23+
* @ORM\Cache("NONSTRICT_READ_WRITE")
2324
* Class AccessToken
2425
* @package Models\OAuth2
2526
*/

app/Models/OAuth2/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
/**
3838
* @ORM\Entity(repositoryClass="App\Repositories\DoctrineOAuth2ClientRepository")
3939
* @ORM\Table(name="oauth2_client")
40+
* @ORM\Cache("NONSTRICT_READ_WRITE")
4041
* Class Client
4142
* @package Models\OAuth2
4243
*/

app/Models/OAuth2/RefreshToken.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/**
2222
* @ORM\Entity(repositoryClass="App\Repositories\DoctrineRefreshTokenRepository")
2323
* @ORM\Table(name="oauth2_refresh_token")
24+
* @ORM\Cache("NONSTRICT_READ_WRITE")
2425
* Class RefreshToken
2526
* Refresh Token Entity
2627
*/

app/Repositories/DoctrineAccessTokenRepository.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Doctrine\ORM\QueryBuilder;
1616
use Models\OAuth2\AccessToken;
17+
use Models\OAuth2\RefreshToken;
1718
use OAuth2\Repositories\IAccessTokenRepository;
1819
/**
1920
* Class DoctrineAccessTokenRepository
@@ -41,6 +42,24 @@ function getByValue(string $hashed_value):?AccessToken
4142
return $this->findOneBy(['value' => $hashed_value]);
4243
}
4344

45+
/**
46+
* @param string $hashed_value
47+
* @return AccessToken|null
48+
*/
49+
function getByValueCacheable(string $hashed_value):?AccessToken
50+
{
51+
return $this->getEntityManager()
52+
->createQueryBuilder()
53+
->select("e")
54+
->from($this->getBaseEntity(), "e")
55+
->where("e.value = (:value)")
56+
->setParameter("value", trim($hashed_value))
57+
->setMaxResults(1)
58+
->getQuery()
59+
->setCacheable(true)
60+
->getOneOrNullResult();
61+
}
62+
4463
/**
4564
* @param string $hashed_value
4665
* @return AccessToken|null

app/Repositories/DoctrineOAuth2ClientRepository.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ public function getClientById(string $client_id):?Client
9898
->getOneOrNullResult();
9999
}
100100

101+
/**
102+
* @param string $client_id
103+
* @return Client|null
104+
* @throws \Doctrine\ORM\NonUniqueResultException
105+
*/
106+
public function getClientByIdCacheable(string $client_id):?Client
107+
{
108+
return $this->getEntityManager()
109+
->createQueryBuilder()
110+
->select("c")
111+
->from($this->getBaseEntity(), "c")
112+
->where("c.client_id = (:client_id)")
113+
->setParameter("client_id", trim($client_id))
114+
->setMaxResults(1)
115+
->getQuery()
116+
->setCacheable(true)
117+
->getOneOrNullResult();
118+
}
119+
101120
/**
102121
* @param int $id
103122
* @return Client|null

app/Repositories/DoctrineRefreshTokenRepository.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,22 @@ function getByValue(string $hashed_value):?RefreshToken
6262
{
6363
return $this->findOneBy(['value' => $hashed_value]);
6464
}
65+
66+
/**
67+
* @param string $hashed_value
68+
* @return RefreshToken|null
69+
*/
70+
function getByValueCacheable(string $hashed_value):?RefreshToken
71+
{
72+
return $this->getEntityManager()
73+
->createQueryBuilder()
74+
->select("e")
75+
->from($this->getBaseEntity(), "e")
76+
->where("e.value = (:value)")
77+
->setParameter("value", trim($hashed_value))
78+
->setMaxResults(1)
79+
->getQuery()
80+
->setCacheable(true)
81+
->getOneOrNullResult();
82+
}
6583
}

app/Services/OAuth2/TokenService.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ public function getAccessToken($value, $is_hashed = false)
770770
if (!$this->cache_service->exists($hashed_value)) {
771771
$this->lock_manager_service->lock('lock.get.accesstoken.' . $hashed_value, function () use ($value, $hashed_value) {
772772
// check on DB...
773-
$access_token_db = $this->access_token_repository->getByValue($hashed_value);
773+
$access_token_db = $this->access_token_repository->getByValueCacheable($hashed_value);
774774
if (is_null($access_token_db)) {
775775
if ($this->isAccessTokenRevoked($hashed_value)) {
776776
throw new RevokedAccessTokenException(sprintf('Access token %s is revoked!', $value));
@@ -830,8 +830,9 @@ public function getAccessToken($value, $is_hashed = false)
830830
$access_token->setRefreshToken($refresh_token);
831831
}
832832
} catch (UnacquiredLockException $ex1) {
833-
throw new InvalidAccessTokenException(sprintf("access token %s ", $value));
833+
throw new InvalidAccessTokenException(sprintf("Access token %s. ", $value));
834834
}
835+
835836
return $access_token;
836837
});
837838
}
@@ -920,19 +921,19 @@ public function createRefreshToken(AccessToken &$access_token, $refresh_cache =
920921
}
921922

922923
/**
923-
* @param \oauth2\services\refresh $value
924-
* @param bool $is_hashed
924+
* @param string $value
925+
* @param false $is_hashed
925926
* @return RefreshToken
926927
* @throws InvalidGrantTypeException
927-
* @throws ReplayAttackException
928+
* @throws ReplayAttackRefreshTokenException
928929
* @throws RevokedRefreshTokenException
929930
*/
930931
public function getRefreshToken($value, $is_hashed = false)
931932
{
932933
//hash the given value, bc tokens values are stored hashed on DB
933934
$hashed_value = !$is_hashed ? Hash::compute('sha256', $value) : $value;
934935

935-
$refresh_token_db = $this->refresh_token_repository->getByValue($hashed_value);
936+
$refresh_token_db = $this->refresh_token_repository->getByValueCacheable($hashed_value);
936937

937938
if (is_null($refresh_token_db)) {
938939
if ($this->isRefreshTokenRevoked($hashed_value))

app/libs/OAuth2/GrantTypes/AbstractGrantType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public function completeFlow(OAuth2Request $request)
9393
// get client credentials from request..
9494
$this->client_auth_context = $this->client_service->getCurrentClientAuthInfo();
9595

96-
// retrieve client from storage..
97-
$this->current_client = $this->client_repository->getClientById($this->client_auth_context->getId());
96+
// retrieve client from storage ...
97+
$this->current_client = $this->client_repository->getClientByIdCacheable($this->client_auth_context->getId());
9898

9999
if (is_null($this->current_client))
100100
throw new InvalidClientException

app/libs/OAuth2/GrantTypes/ValidateBearerTokenGrantType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ public function completeFlow(OAuth2Request $request)
185185

186186
$strategy->validate($access_token, $this->current_client);
187187

188-
$issued_client = $this->client_repository->getClientById($access_token->getClientId());
188+
$issued_client = $this->client_repository->getClientByIdCacheable($access_token->getClientId());
189189

190190
if (is_null($issued_client))
191191
{
192192
throw new BearerTokenDisclosureAttemptException
193193
(
194194
sprintf
195195
(
196-
'access token %s does not belongs to client id %s',
196+
'Access token %s does not belongs to client id %s.',
197197
$token_value,
198198
$access_token->getClientId()
199199
)

app/libs/OAuth2/Repositories/IAccessTokenRepository.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ function getAllValidByUserId(int $user_id,PagingInfo $paging_info):PagingRespons
5656
*/
5757
function getByValue(string $hashed_value):?AccessToken;
5858

59+
/**
60+
* @param string $hashed_value
61+
* @return AccessToken|null
62+
*/
63+
function getByValueCacheable(string $hashed_value):?AccessToken;
64+
5965
/**
6066
* @param string $hashed_value
6167
* @return AccessToken

0 commit comments

Comments
 (0)