|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Alexandre |
|
5
|
|
|
* Date: 10/03/2018 |
|
6
|
|
|
* Time: 15:55 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OAuth2; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use OAuth2\ClientAuthentication\ClientAuthenticationMethodManager; |
|
13
|
|
|
use OAuth2\Endpoints\AuthorizationEndpoint; |
|
14
|
|
|
use OAuth2\Endpoints\TokenEndpoint; |
|
15
|
|
|
use OAuth2\Flows\AuthorizationCodeFlow; |
|
16
|
|
|
use OAuth2\Flows\FlowManager; |
|
17
|
|
|
use OAuth2\Flows\ImplicitFlow; |
|
18
|
|
|
use OAuth2\GrantTypes\GrantTypeManager; |
|
19
|
|
|
use OAuth2\ResponseModes\ResponseModeManager; |
|
20
|
|
|
use OAuth2\ResponseTypes\ResponseTypeManager; |
|
21
|
|
|
use OAuth2\Roles\ResourceOwnerInterface; |
|
22
|
|
|
use OAuth2\ScopePolicy\ScopePolicyManager; |
|
23
|
|
|
use OAuth2\Storages\StorageManager; |
|
24
|
|
|
|
|
25
|
|
|
class Server |
|
26
|
|
|
{ |
|
27
|
|
|
protected $authorizationEndpoint; |
|
28
|
|
|
protected $tokenEndpoint; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(Config $config, |
|
31
|
|
|
StorageManager $storageManager, |
|
32
|
|
|
ResourceOwnerInterface $resourceOwner) |
|
33
|
|
|
{ |
|
34
|
|
|
$responseTypeManager = new ResponseTypeManager(); |
|
35
|
|
|
$responseModeManager = new ResponseModeManager(); |
|
36
|
|
|
$scopePolicyManager = new ScopePolicyManager($config); |
|
37
|
|
|
$grantTypeManager = new GrantTypeManager(); |
|
38
|
|
|
$clientAuthenticationMethodManager = new ClientAuthenticationMethodManager($storageManager->getClientStorage()); |
|
39
|
|
|
|
|
40
|
|
|
$authorizationCodeFlow = new AuthorizationCodeFlow( |
|
41
|
|
|
$storageManager->getAuthorizationCodeStorage(), |
|
42
|
|
|
$storageManager->getAccessTokenStorage(), |
|
43
|
|
|
$storageManager->getRefreshTokenStorage()); |
|
44
|
|
|
|
|
45
|
|
|
$implicitFlow = new ImplicitFlow($storageManager->getAccessTokenStorage()); |
|
46
|
|
|
|
|
47
|
|
|
$flowManager = new FlowManager($responseTypeManager, $grantTypeManager); |
|
48
|
|
|
$flowManager->addFlow($authorizationCodeFlow); |
|
49
|
|
|
$flowManager->addFlow($implicitFlow); |
|
50
|
|
|
|
|
51
|
|
|
$this->authorizationEndpoint = new AuthorizationEndpoint( |
|
52
|
|
|
$responseTypeManager, |
|
53
|
|
|
$responseModeManager, |
|
54
|
|
|
$scopePolicyManager, |
|
55
|
|
|
$resourceOwner, |
|
56
|
|
|
$storageManager->getClientStorage()); |
|
57
|
|
|
|
|
58
|
|
|
$this->tokenEndpoint = new TokenEndpoint( |
|
59
|
|
|
$storageManager->getClientStorage(), |
|
60
|
|
|
$grantTypeManager, |
|
61
|
|
|
$clientAuthenticationMethodManager); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return AuthorizationEndpoint |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getAuthorizationEndpoint(): AuthorizationEndpoint |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->authorizationEndpoint; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return TokenEndpoint |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getTokenEndpoint(): TokenEndpoint |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->tokenEndpoint; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return bool |
|
82
|
|
|
* |
|
83
|
|
|
* @see https://tools.ietf.org/html/rfc6749#section-3.1.2.1 |
|
84
|
|
|
* |
|
85
|
|
|
* Endpoint Request Confidentiality |
|
86
|
|
|
* |
|
87
|
|
|
* The redirection endpoint SHOULD require the use of TLS as described |
|
88
|
|
|
* in Section 1.6 when the requested response type is "code" or "token", |
|
89
|
|
|
* or when the redirection request will result in the transmission of |
|
90
|
|
|
* sensitive credentials over an open network. This specification does |
|
91
|
|
|
* not mandate the use of TLS because at the time of this writing, |
|
92
|
|
|
* requiring clients to deploy TLS is a significant hurdle for many |
|
93
|
|
|
* client developers. If TLS is not available, the authorization server |
|
94
|
|
|
* SHOULD warn the resource owner about the insecure endpoint prior to |
|
95
|
|
|
* redirection (e.g., display a message during the authorization |
|
96
|
|
|
* request). |
|
97
|
|
|
* |
|
98
|
|
|
* Lack of transport-layer security can have a severe impact on the |
|
99
|
|
|
* security of the client and the protected resources it is authorized |
|
100
|
|
|
* to access. The use of transport-layer security is particularly |
|
101
|
|
|
* critical when the authorization process is used as a form of |
|
102
|
|
|
* delegated end-user authentication by the client (e.g., third-party |
|
103
|
|
|
* sign-in service). |
|
104
|
|
|
*/ |
|
105
|
|
|
public function isSecure() |
|
106
|
|
|
{ |
|
107
|
|
|
return isset($_SERVER['HTTPS']) && ('on' == strtolower($_SERVER['HTTPS']) || 1 == $_SERVER['HTTPS']); |
|
108
|
|
|
} |
|
109
|
|
|
} |