Completed
Push — master ( fc6f01...c001b0 )
by Alexandre
01:58
created

getResponseModeManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 11/06/2018
6
 * Time: 22:25
7
 */
8
9
namespace OAuth2\Roles\AuthorizationServer;
10
11
12
use OAuth2\AuthorizationEndpointResponseTypes\ResponseTypeManager;
13
use OAuth2\AuthorizationGrantTypes\Flows\AuthorizationCodeFlow;
14
use OAuth2\AuthorizationGrantTypes\Flows\ClientCredentialsFlow;
15
use OAuth2\AuthorizationGrantTypes\Flows\FlowManager;
16
use OAuth2\AuthorizationGrantTypes\Flows\ImplicitFlow;
17
use OAuth2\AuthorizationGrantTypes\Flows\ResourceOwnerPasswordCredentialsFlow;
18
use OAuth2\AuthorizationGrantTypes\GrantTypeManager;
19
use OAuth2\AuthorizationGrantTypes\RefreshTokenGrantType;
20
use OAuth2\ClientAuthentication\ClientAuthenticationMethodManager;
21
use OAuth2\ClientAuthentication\ClientSecretBasicAuthenticationMethod;
22
use OAuth2\ClientAuthentication\ClientSecretPostAuthenticationMethod;
23
use OAuth2\Config;
24
use OAuth2\Endpoints\Authorization\AuthorizationRequestBuilder;
25
use OAuth2\Endpoints\AuthorizationEndpoint;
26
use OAuth2\Endpoints\TokenEndpoint;
27
use OAuth2\Endpoints\TokenRevocationEndpoint;
28
use OAuth2\ResponseModes\FragmentResponseMode;
29
use OAuth2\ResponseModes\QueryResponseMode;
30
use OAuth2\ResponseModes\ResponseModeManager;
31
use OAuth2\ScopePolicy\ScopePolicyManager;
32
use OAuth2\Storages\StorageManager;
33
34
class AuthorizationServerBuilder
35
{
36
    /**
37
     * @var Config
38
     */
39
    private $config;
40
    /**
41
     * @var StorageManager
42
     */
43
    private $storageManager;
44
    /**
45
     * @var ScopePolicyManager
46
     */
47
    private $scopePolicyManager;
48
    /**
49
     * @var EndUserInterface
50
     */
51
    private $endUser;
52
53
    private $clientAuthenticationMethodManager;
54
    private $responseTypeManager;
55
    private $grantTypeManager;
56
    private $responseModeManager;
57
    private $flowManager;
58
59
    private $authorizationRequestBuilder;
60
61
    public function __construct(Config $config,
62
                                StorageManager $storageManager,
63
                                ScopePolicyManager $scopePolicyManager,
64
                                EndUserInterface $endUser
65
    )
66
    {
67
        $this->config = $config;
68
        $this->storageManager = $storageManager;
69
        $this->scopePolicyManager = $scopePolicyManager;
70
        $this->endUser = $endUser;
71
72
        $this->responseTypeManager = new ResponseTypeManager();
73
74
        $this->grantTypeManager = new GrantTypeManager();
75
        $this->grantTypeManager->setGrantType('refresh_token', new RefreshTokenGrantType(
76
            $this->storageManager->getAccessTokenStorage(),
77
            $this->storageManager->getRefreshTokenStorage(),
78
            $config,
79
            $this->scopePolicyManager
80
        ));
81
82
        $this->responseModeManager = new ResponseModeManager();
83
        $this->responseModeManager
84
            ->setResponseMode('query', new QueryResponseMode())
85
            ->setResponseMode('fragment', new FragmentResponseMode());
86
87
        $this->flowManager = new FlowManager($this->responseTypeManager, $this->grantTypeManager);
88
        $this->flowManager
89
            ->addFlow('authorization_code', new AuthorizationCodeFlow(
90
                $config,
91
                $this->storageManager->getAuthorizationCodeStorage(),
92
                $this->storageManager->getAccessTokenStorage(),
93
                $this->storageManager->getRefreshTokenStorage()
94
            ))
95
            ->addFlow('implicit', new ImplicitFlow(
96
                $this->storageManager->getAccessTokenStorage(),
97
                $this->storageManager->getRefreshTokenStorage()
98
            ))
99
            ->addFlow('resource_owner_password_credentials', new ResourceOwnerPasswordCredentialsFlow(
100
                $this->scopePolicyManager,
101
                $this->storageManager->getResourceOwnerStorage(),
102
                $this->storageManager->getAccessTokenStorage(),
103
                $this->storageManager->getRefreshTokenStorage()))
104
            ->addFlow('client_credentials', new ClientCredentialsFlow(
105
                $this->scopePolicyManager,
106
                $this->storageManager->getAccessTokenStorage(),
107
                $this->storageManager->getRefreshTokenStorage()
108
            ));
109
110
        $this->clientAuthenticationMethodManager = new ClientAuthenticationMethodManager($storageManager->getClientStorage());
111
        $this->clientAuthenticationMethodManager
112
            ->setClientAuthenticationMethod('client_secret_basic', new ClientSecretBasicAuthenticationMethod(
113
                $this->storageManager->getClientStorage()
114
            ))
115
            ->setClientAuthenticationMethod('client_secret_post', new ClientSecretPostAuthenticationMethod(
116
                $this->storageManager->getClientStorage()
117
            ));
118
119
        $this->authorizationRequestBuilder = new AuthorizationRequestBuilder(
120
            $this->storageManager->getClientStorage(),
121
            $this->responseTypeManager,
122
            $this->responseModeManager,
123
            $this->scopePolicyManager
124
        );
125
    }
126
127
    public function build(): AuthorizationServer
128
    {
129
        $authorizationEndpoint = new AuthorizationEndpoint($this->authorizationRequestBuilder, $this->endUser);
130
131
        $tokenEndpoint = new TokenEndpoint(
132
            $this->grantTypeManager,
133
            $this->clientAuthenticationMethodManager);
134
135
        $tokenRevocationEndpoint = new TokenRevocationEndpoint(
136
            $this->clientAuthenticationMethodManager,
137
            $this->storageManager);
138
139
        return new AuthorizationServer($authorizationEndpoint, $tokenEndpoint, $tokenRevocationEndpoint);
140
    }
141
142
    /**
143
     * @return ClientAuthenticationMethodManager
144
     */
145
    public function getClientAuthenticationMethodManager(): ClientAuthenticationMethodManager
146
    {
147
        return $this->clientAuthenticationMethodManager;
148
    }
149
150
    /**
151
     * @return ResponseModeManager
152
     */
153
    public function getResponseModeManager(): ResponseModeManager
154
    {
155
        return $this->responseModeManager;
156
    }
157
158
    /**
159
     * @param AuthorizationRequestBuilder $authorizationRequestBuilder
160
     * @return AuthorizationServerBuilder
161
     */
162
    public function setAuthorizationRequestBuilder(AuthorizationRequestBuilder $authorizationRequestBuilder): self
163
    {
164
        $this->authorizationRequestBuilder = $authorizationRequestBuilder;
165
        return $this;
166
    }
167
168
    /**
169
     * @return FlowManager
170
     */
171
    public function getFlowManager(): FlowManager
172
    {
173
        return $this->flowManager;
174
    }
175
176
    /**
177
     * @return StorageManager
178
     */
179
    public function getStorageManager(): StorageManager
180
    {
181
        return $this->storageManager;
182
    }
183
184
    /**
185
     * @return ScopePolicyManager
186
     */
187
    public function getScopePolicyManager(): ScopePolicyManager
188
    {
189
        return $this->scopePolicyManager;
190
    }
191
192
    /**
193
     * @return Config
194
     */
195
    public function getConfig(): Config
196
    {
197
        return $this->config;
198
    }
199
200
    /**
201
     * @return EndUserInterface
202
     */
203
    public function getEndUser(): EndUserInterface
204
    {
205
        return $this->endUser;
206
    }
207
208
    /**
209
     * @return ResponseTypeManager
210
     */
211
    public function getResponseTypeManager(): ResponseTypeManager
212
    {
213
        return $this->responseTypeManager;
214
    }
215
216
    /**
217
     * @return GrantTypeManager
218
     */
219
    public function getGrantTypeManager(): GrantTypeManager
220
    {
221
        return $this->grantTypeManager;
222
    }
223
224
    /**
225
     * @return AuthorizationRequestBuilder
226
     */
227
    public function getAuthorizationRequestBuilder(): AuthorizationRequestBuilder
228
    {
229
        return $this->authorizationRequestBuilder;
230
    }
231
}