Skip to content

Commit 86568ca

Browse files
committed
fix: ResourceServer::isOwn checking
Change-Id: I9ee576f0fa8fc705af6fd6176f27c784fdecc8b0
1 parent 0f2b58e commit 86568ca

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

app/Models/OAuth2/ResourceServer.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use App\Models\Utils\BaseEntity;
1515
use Doctrine\Common\Collections\ArrayCollection;
1616
use Doctrine\ORM\Mapping AS ORM;
17+
use Illuminate\Support\Facades\Log;
18+
1719
/**
1820
* @package Models\OAuth2
1921
*/
@@ -63,9 +65,22 @@ class ResourceServer extends BaseEntity
6365
* @return bool
6466
*/
6567
public function isOwn($ip)
66-
{
67-
$ips = explode(',', $this->ips);
68-
return in_array($ip, $ips);
68+
{ $provided_ips = array_map('trim', explode(',', $ip));
69+
$own_ips = array_map('trim', explode(',', $this->ips));
70+
Log::debug
71+
(
72+
sprintf
73+
(
74+
"ResourceServer::isOwn resource server %s checking if %s is in %s",
75+
$this->id,
76+
$ip,
77+
$this->ips
78+
)
79+
);
80+
foreach ($provided_ips as $provided_ip){
81+
if(in_array($provided_ip, $own_ips)) return true;
82+
}
83+
return false;
6984
}
7085

7186
/**

app/Repositories/DoctrineResourceServerRepository.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,20 @@ public function getByHost(string $host):?ResourceServer
5959
public function getByIp(string $ip):?ResourceServer
6060
{
6161
Log::debug(sprintf("DoctrineResourceServerRepository::getByIp ip %s", $ip));
62-
return $this->getEntityManager()
63-
->createQueryBuilder()
64-
->select("r")
65-
->from($this->getBaseEntity(), "r")
66-
->where("r.ips like :ip")
67-
->setParameter("ip", '%'.trim($ip).'%')
68-
->setMaxResults(1)
69-
->getQuery()
70-
->getOneOrNullResult();
62+
$provided_ips = array_map('trim', explode(',', $ip));
63+
foreach ($provided_ips as $provided_ip) {
64+
$res = $this->getEntityManager()
65+
->createQueryBuilder()
66+
->select("r")
67+
->from($this->getBaseEntity(), "r")
68+
->where("r.ips like :ip")
69+
->setParameter("ip", '%' . trim($provided_ip) . '%')
70+
->setMaxResults(1)
71+
->getQuery()
72+
->getOneOrNullResult();
73+
if ($res instanceof ResourceServer) return $res;
74+
}
75+
return null;
7176
}
7277

7378
/**
@@ -104,26 +109,37 @@ public function getByAudienceAndIpAndActive(array $audience, string $ip):?Resour
104109
)
105110
);
106111

112+
107113
$query = <<<SQL
108-
SELECT r.* FROM oauth2_resource_server r
109-
WHERE FIND_IN_SET('{$ip}', r.ips) AND r.active = 1
114+
SELECT r.* FROM oauth2_resource_server r WHERE r.active = 1
110115
SQL;
116+
$ips_query = "";
117+
$provided_ips = array_map('trim', explode(',', $ip));
118+
foreach ($provided_ips as $index => $provided_ip) {
119+
if ($index > 0) {
120+
$ips_query .= " OR ";
121+
}
122+
$ips_query.= sprintf(" FIND_IN_SET('%s',r.ips) ", $provided_ip);
123+
}
111124

112-
$hosts_query = "";
125+
Log::debug(sprintf("DoctrineResourceServerRepository::getByAudienceAndIpAndActive ips_query %s", $ips_query));
113126

127+
$hosts_query = "";
114128
foreach ($audience as $index => $audience_item) {
115129
if ($index > 0) {
116130
$hosts_query .= " OR ";
117131
}
118132
$hosts_query.= sprintf(" FIND_IN_SET('%s',r.host) ", $audience_item);
119133
}
120134

121-
if(!empty($hosts_query))
122-
$hosts_query = " AND (". $hosts_query .")";
123135

124136
Log::debug(sprintf("DoctrineResourceServerRepository::getByAudienceAndIpAndActive hosts_query %s", $hosts_query));
137+
if(!empty($ips_query))
138+
$query = $query . " AND (" . $ips_query . ")";
139+
if(!empty($hosts_query))
140+
$query = $query . " AND (" . $hosts_query . ")";
125141

126-
$query = $query . $hosts_query. " LIMIT 1;";
142+
$query = $query . " LIMIT 1;";
127143

128144
Log::debug(sprintf("DoctrineResourceServerRepository::getByAudienceAndIpAndActive query %s", $query));
129145

0 commit comments

Comments
 (0)