Skip to content

Commit 7190429

Browse files
committed
SAML: allow domain by regex matching
1 parent e611e27 commit 7190429

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

app/Models/Saml2Tenant.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,38 @@
22

33
namespace App\Models;
44

5+
use Illuminate\Support\Facades\Log;
6+
use Illuminate\Support\Str;
57
use Slides\Saml2\Models\Tenant;
68

79
class Saml2Tenant extends Tenant
810
{
911
public static function firstFromDomain(string $domain)
1012
{
11-
return self::query()
12-
->where('domain', '=', $domain)
13-
->orWhere('alt_domain1', '=', $domain)
14-
->first();
13+
return self::all()
14+
->filter(function (Saml2Tenant $saml2Tenant) use ($domain) {
15+
$allowedDomains = [
16+
$saml2Tenant->domain,
17+
$saml2Tenant->alt_domain1,
18+
];
19+
20+
foreach ($allowedDomains as $allowedDomain) {
21+
// If SAML Tenant domain starts with ~, it is a regex
22+
if (Str::startsWith($allowedDomain, '~')) {
23+
if (1 === preg_match($allowedDomain, $domain)) {
24+
Log::debug("[SAML2 Authentication] $domain matches on domain regex $allowedDomain");
25+
return true;
26+
}
27+
} else {
28+
if ($domain === $allowedDomain) {
29+
Log::debug("[SAML2 Authentication] $domain is equal to domain $allowedDomain");
30+
return true;
31+
}
32+
}
33+
}
34+
35+
return false;
36+
})->first();
1537
}
1638

1739
public function getTenantId()

0 commit comments

Comments
 (0)