|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Slides\Saml2\Helpers; |
|
6
|
|
|
|
|
7
|
|
|
use Slides\Saml2\Models\Tenant; |
|
8
|
|
|
use Illuminate\Support\Facades\URL; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* TenantWrapper wraps a Tenant and adds business logic. |
|
12
|
|
|
* |
|
13
|
|
|
* Done this way to the business logic can be unit tested, on mock Tenant, |
|
14
|
|
|
* since Eloquent models themselves are virtually impossible to unit test. |
|
15
|
|
|
* |
|
16
|
|
|
* @package App\Helpers |
|
17
|
|
|
*/ |
|
18
|
|
|
class TenantWrapper |
|
19
|
|
|
{ |
|
20
|
|
|
private Tenant $tenant; |
|
21
|
|
|
|
|
22
|
|
|
final public function __construct(Tenant $tenant) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->tenant = $tenant; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Factory method for fluent calls, |
|
29
|
|
|
* e.g. TenantWrapper::with($tenant)->blah_blah |
|
30
|
|
|
* |
|
31
|
|
|
* @param Tenant $tenant |
|
32
|
|
|
* @return TenantWrapper |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function with(Tenant $tenant): self |
|
35
|
|
|
{ |
|
36
|
|
|
return new TenantWrapper($tenant); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Use this to determine the SP Entity ID for a tenant. |
|
41
|
|
|
* |
|
42
|
|
|
* 1. Returns default SP entity ID (metadata URL) *UNLESS* id_app_url_override is filled in (non-empty string), |
|
43
|
|
|
* 2. ... in which case it will return the SP entity ID path appended to that value instead of |
|
44
|
|
|
* what URL::route uses (APP_URL at command line, request HOST during a request) |
|
45
|
|
|
* |
|
46
|
|
|
* Q: Why not just make this Tenant::getSpEntityIdAttribute? |
|
47
|
|
|
* A: Because Eloquent models hate unit testing |
|
48
|
|
|
* |
|
49
|
|
|
* @return string SP Entity ID |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getSpEntityId(): string |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->getUrlWithDomainOverrideIfConfigured('saml.metadata'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Use this to determine the ACS URL for a tenant. |
|
58
|
|
|
* |
|
59
|
|
|
* 1. Returns default ACS URL *UNLESS* id_app_url_override is filled in (non-empty string), |
|
60
|
|
|
* 2. ... in which case it will return the ACS path appended to that value instead of |
|
61
|
|
|
* what URL::route uses (APP_URL at command line, request HOST during a request) |
|
62
|
|
|
* |
|
63
|
|
|
* Q: Why not just make this Tenant::getAcsUrlAttribute? |
|
64
|
|
|
* A: Because Eloquent models hate unit testing |
|
65
|
|
|
* |
|
66
|
|
|
* @return string ACS URL (full path) |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getAcsUrl(): string |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->getUrlWithDomainOverrideIfConfigured('saml.acs'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Use this to determine the Single Logout Service URL for a tenant. |
|
75
|
|
|
* |
|
76
|
|
|
* 1. Returns default SLS URL *UNLESS* id_app_url_override is filled in (non-empty string), |
|
77
|
|
|
* 2. ... in which case it will return the SLS path appended to that value instead of |
|
78
|
|
|
* what URL::route uses (APP_URL at command line, request HOST during a request) |
|
79
|
|
|
* |
|
80
|
|
|
* @return string SLS URL (full path) |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getSlsUrl(): string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->getUrlWithDomainOverrideIfConfigured('saml.sls'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* By default (id_app_url_override not configured), returns default route URL |
|
89
|
|
|
* (which, for URL::route, is a /-relative path, not an absolute including domain) |
|
90
|
|
|
* |
|
91
|
|
|
* When id_app_url_override is configured, the route *path* appended to that id_app_url_override value |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $routeName Valid route — intended to be one of the saml.* routes |
|
94
|
|
|
* @return string path or full URL of that route |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getUrlWithDomainOverrideIfConfigured(string $routeName): string |
|
97
|
|
|
{ |
|
98
|
|
|
$routeParams = [ 'uuid' => $this->tenant->uuid ]; |
|
99
|
|
|
|
|
100
|
|
|
if ($this->tenant->id_app_url_override) { |
|
101
|
|
|
// Override the APP_URL with "manual" host: |
|
102
|
|
|
$manualBaseUrl = rtrim($this->tenant->id_app_url_override, '/'); |
|
103
|
|
|
$absolute = false; |
|
104
|
|
|
return $manualBaseUrl . URL::route($routeName, $routeParams, $absolute); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// Normal case, default base URL (route() default $absolute to true, so 3rd param omitted) |
|
108
|
|
|
return URL::route($routeName, $routeParams); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|