Completed
Push — master ( 2a258c...683c35 )
by Alexandre
02:27
created

AuthorizationServer::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 49
nc 1
nop 4
dl 0
loc 77
rs 8.9342
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 10/03/2018
6
 * Time: 15:55
7
 */
8
9
namespace OAuth2\Roles\AuthorizationServer;
10
11
12
use OAuth2\ClientAuthentication\ClientAuthenticationMethodManager;
13
use OAuth2\ClientAuthentication\ClientSecretBasicAuthenticationMethod;
14
use OAuth2\ClientAuthentication\ClientSecretPostAuthenticationMethod;
15
use OAuth2\Config;
16
use OAuth2\Endpoints\AuthorizationEndpoint;
17
use OAuth2\Endpoints\AuthorizationRequestBuilder;
18
use OAuth2\Endpoints\EndpointInterface;
19
use OAuth2\Endpoints\TokenEndpoint;
20
use OAuth2\AuthorizationGrantTypes\Flows\AuthorizationCodeFlow;
21
use OAuth2\AuthorizationGrantTypes\Flows\ClientCredentialsFlow;
22
use OAuth2\AuthorizationGrantTypes\Flows\FlowManager;
23
use OAuth2\AuthorizationGrantTypes\Flows\ImplicitFlow;
24
use OAuth2\AuthorizationGrantTypes\Flows\ResourceOwnerPasswordCredentialsFlow;
25
use OAuth2\AuthorizationGrantTypes\GrantTypeManager;
26
use OAuth2\AuthorizationGrantTypes\RefreshTokenGrantType;
27
use OAuth2\ResponseModes\FragmentResponseMode;
28
use OAuth2\ResponseModes\QueryResponseMode;
29
use OAuth2\ResponseModes\ResponseModeManager;
30
use OAuth2\AuthorizationEndpointResponseTypes\ResponseTypeManager;
31
use OAuth2\Roles\AuthorizationServerInterface;
32
use OAuth2\ScopePolicy\ScopePolicyManager;
33
use OAuth2\Storages\StorageManager;
34
35
36
class AuthorizationServer implements AuthorizationServerInterface
37
{
38
    protected $authorizationEndpoint;
39
    protected $tokenEndpoint;
40
    protected $responseTypeManager;
41
    protected $scopePolicyManager;
42
    protected $grantTypeManager;
43
    protected $clientAuthenticationMethodManager;
44
    protected $responseModeManager;
45
    protected $flowManager;
46
47
    public function __construct(Config $config,
48
                                StorageManager $storageManager,
49
                                ScopePolicyManager $scopePolicyManager,
50
                                EndUserInterface $authorizationServerEndUser)
51
    {
52
        $this->responseTypeManager = new ResponseTypeManager();
53
        $this->scopePolicyManager = $scopePolicyManager;
54
        $this->grantTypeManager = new GrantTypeManager();
55
56
        $this->clientAuthenticationMethodManager = new ClientAuthenticationMethodManager($storageManager->getClientStorage());
57
        $this->clientAuthenticationMethodManager->addClientAuthenticationMethod('client_secret_basic',
58
            new ClientSecretBasicAuthenticationMethod($storageManager->getClientStorage()));
59
        $this->clientAuthenticationMethodManager->addClientAuthenticationMethod('client_secret_post',
60
            new ClientSecretPostAuthenticationMethod($storageManager->getClientStorage()));
61
62
//        $queryResponseMode = new QueryResponseMode();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
        $this->responseModeManager = new ResponseModeManager();
64
        $this->responseModeManager->addResponseMode('query', new QueryResponseMode());
65
        $this->responseModeManager->addResponseMode('fragment', new FragmentResponseMode());
66
67
        // response_type : code
68
        // grant_type : authorization_code
69
        $authorizationCodeFlow = new AuthorizationCodeFlow(
70
            $config,
71
            $storageManager->getAuthorizationCodeStorage(),
72
            $storageManager->getAccessTokenStorage(),
73
            $storageManager->getRefreshTokenStorage()
74
        );
75
76
        // response_type : token
77
        $implicitFlow = new ImplicitFlow(
78
            $storageManager->getAccessTokenStorage(),
79
            $storageManager->getRefreshTokenStorage()
80
        );
81
82
        // grant_type : password
83
        $resourceOwnerPasswordCredentialsFlow = new ResourceOwnerPasswordCredentialsFlow(
84
            $this->scopePolicyManager,
85
            $storageManager->getResourceOwnerStorage(),
86
            $storageManager->getAccessTokenStorage(),
87
            $storageManager->getRefreshTokenStorage());
88
89
        // grant_type : client_credentials
90
        $clientCredentialsFlow = new ClientCredentialsFlow(
91
            $this->scopePolicyManager,
92
            $storageManager->getAccessTokenStorage(),
93
            $storageManager->getRefreshTokenStorage()
94
        );
95
96
        // grant_type : refresh_token
97
        $refreshTokenGrantType = new RefreshTokenGrantType(
98
            $storageManager->getAccessTokenStorage(),
99
            $storageManager->getRefreshTokenStorage(),
100
            $config,
101
            $this->scopePolicyManager
102
        );
103
104
        $this->flowManager = new FlowManager($this->responseTypeManager, $this->grantTypeManager);
105
        $this->flowManager->addFlow($authorizationCodeFlow);
106
        $this->flowManager->addFlow($implicitFlow);
107
        $this->flowManager->addFlow($resourceOwnerPasswordCredentialsFlow);
108
        $this->flowManager->addFlow($clientCredentialsFlow);
109
110
        $this->grantTypeManager->addGrantType('refresh_token', $refreshTokenGrantType);
111
112
        $authorizationRequestBuilder = new AuthorizationRequestBuilder(
113
            $storageManager->getClientStorage(),
114
            $this->responseTypeManager,
115
            $this->responseModeManager,
116
            $this->scopePolicyManager
117
        );
118
        $this->authorizationEndpoint = new AuthorizationEndpoint($authorizationRequestBuilder, $authorizationServerEndUser);
119
120
        $this->tokenEndpoint = new TokenEndpoint(
121
            $storageManager->getClientStorage(),
122
            $this->grantTypeManager,
123
            $this->clientAuthenticationMethodManager);
124
    }
125
126
    /**
127
     * @return AuthorizationEndpoint
128
     */
129
    public function getAuthorizationEndpoint(): EndpointInterface
130
    {
131
        return $this->authorizationEndpoint;
132
    }
133
134
    /**
135
     * @return TokenEndpoint
136
     */
137
    public function getTokenEndpoint(): EndpointInterface
138
    {
139
        return $this->tokenEndpoint;
140
    }
141
142
    /**
143
     * @return bool
144
     *
145
     * @see https://tools.ietf.org/html/rfc6749#section-3.1.2.1
146
     *
147
     *  Endpoint Request Confidentiality
148
     *
149
     *     The redirection endpoint SHOULD require the use of TLS as described
150
     * in Section 1.6 when the requested response type is "code" or "token",
151
     * or when the redirection request will result in the transmission of
152
     * sensitive credentials over an open network.  This specification does
153
     * not mandate the use of TLS because at the time of this writing,
154
     * requiring clients to deploy TLS is a significant hurdle for many
155
     * client developers.  If TLS is not available, the authorization server
156
     * SHOULD warn the resource owner about the insecure endpoint prior to
157
     * redirection (e.g., display a message during the authorization
158
     * request).
159
     *
160
     * Lack of transport-layer security can have a severe impact on the
161
     * security of the client and the protected resources it is authorized
162
     * to access.  The use of transport-layer security is particularly
163
     * critical when the authorization process is used as a form of
164
     * delegated end-user authentication by the client (e.g., third-party
165
     * sign-in service).
166
     * @deprecated
167
     */
168
    public function isSecure()
169
    {
170
        return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443;
171
    }
172
}