Skip to content

Commit 4e678a3

Browse files
committed
Restriction on middleware to reject if allowed_country_code is set and the requested country code is out of the allowed country_codes
1 parent aa13042 commit 4e678a3

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

app/Http/Middleware/ApiAuthMiddleware.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public function handle($request, Closure $next)
4343
return response()->json(['error' => 'Application is not allowed to access this API version'], 403);
4444
}
4545

46+
$canAccessOrganisation = $this->canAccessOrganisation($request->path(), (array) $application->rules);
47+
48+
if (!$canAccessOrganisation) {
49+
return response()->json(['error' => 'Application is not allowed to access this organisation'], 403);
50+
}
51+
4652
$usageLog = new UsageLog;
4753
$usageLog->application_id = $application->id;
4854
$usageLog->method = $request->method();
@@ -71,4 +77,21 @@ private function canAccessRequestedVersion(string $path, array $rules): bool
7177

7278
return true;
7379
}
80+
81+
private function canAccessOrganisation(string $path, array $rules): bool
82+
{
83+
// Check if accessing org/{code}/whatnow endpoint
84+
if (preg_match('#org/([^/]+)/whatnow#', $path, $matches)) {
85+
$orgCode = $matches[1];
86+
87+
// If allowed_country_code is defined in rules, check if this org is restricted
88+
if (isset($rules['allowed_country_code']) && is_array($rules['allowed_country_code'])) {
89+
if (!in_array($orgCode, $rules['allowed_country_code'])) {
90+
return false;
91+
}
92+
}
93+
}
94+
95+
return true;
96+
}
7497
}

0 commit comments

Comments
 (0)