1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FMCSSOClient\OAuth; |
4
|
|
|
|
5
|
|
|
class RoutesManager |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Use https. |
10
|
|
|
* |
11
|
|
|
* @var bool |
12
|
|
|
*/ |
13
|
|
|
protected bool $ssl = true; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The auth domain. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected string $domain = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The authorize path. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected string $authorizePath = '/'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The token path. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected string $tokenPath = '/'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The user path. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected string $userPath = '/'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The separating character for the requested scopes. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected string $scopeSeparator = ' '; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The type of the encoding in the query. |
52
|
|
|
* |
53
|
|
|
* @var int Can be either PHP_QUERY_RFC3986 or PHP_QUERY_RFC1738. |
54
|
|
|
*/ |
55
|
|
|
protected int $encodingType = PHP_QUERY_RFC1738; |
56
|
|
|
|
57
|
|
|
|
58
|
23 |
|
public function __construct( |
59
|
|
|
string $domain, |
60
|
|
|
string $authorizePath = '/oauth/authorize', |
61
|
|
|
string $tokenPath = '/oauth/token', |
62
|
|
|
string $userPath = '/json/profile', |
63
|
|
|
string $scopeSeparator = ' ', |
64
|
|
|
bool $ssl = true |
65
|
|
|
) { |
66
|
23 |
|
$this->domain = $domain; |
67
|
23 |
|
$this->authorizePath = $authorizePath; |
68
|
23 |
|
$this->tokenPath = $tokenPath; |
69
|
23 |
|
$this->userPath = $userPath; |
70
|
23 |
|
$this->scopeSeparator = $scopeSeparator; |
71
|
23 |
|
$this->ssl = $ssl; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get "authorize" url |
76
|
|
|
* |
77
|
|
|
* @param array $codeFields |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
4 |
|
public function getAuthUrl(array $codeFields): string |
82
|
|
|
{ |
83
|
4 |
|
return $this->buildAuthUrlFromBase($this->buildUrlFromPath($this->authorizePath), $codeFields); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get "token" url |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
9 |
|
public function getTokenUrl(): string |
92
|
|
|
{ |
93
|
9 |
|
return $this->buildUrlFromPath($this->tokenPath); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get "user information" url |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
4 |
|
public function getUserUrl(): string |
102
|
|
|
{ |
103
|
4 |
|
return $this->buildUrlFromPath($this->userPath); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Build url from path value. |
108
|
|
|
* |
109
|
|
|
* @param string $path |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
13 |
|
public function buildUrlFromPath(string $path): string |
114
|
|
|
{ |
115
|
13 |
|
return ($this->ssl ? 'https' : 'http') . '://' . rtrim($this->domain, '/') . '/' . ltrim($path, '/'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Build the authentication URL for the provider from the given base URL. |
120
|
|
|
* |
121
|
|
|
* @param string $url |
122
|
|
|
* @param array $codeFields |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
4 |
|
protected function buildAuthUrlFromBase(string $url, array $codeFields): string |
127
|
|
|
{ |
128
|
4 |
|
return $url . '?' . http_build_query($codeFields, '', '&', $this->encodingType); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Prepare scopes for request. |
133
|
|
|
* |
134
|
|
|
* @param array $scopes |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
5 |
|
public function prepareScopes(array $scopes): string |
139
|
|
|
{ |
140
|
5 |
|
return $this->formatScopes($scopes, $this->scopeSeparator); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Format the given scopes. |
145
|
|
|
* |
146
|
|
|
* @param array $scopes |
147
|
|
|
* @param string $scopeSeparator |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
5 |
|
protected function formatScopes(array $scopes, string $scopeSeparator = ' '): string |
152
|
|
|
{ |
153
|
5 |
|
return implode($scopeSeparator, $scopes); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|