Passed
Push — master ( 9a636e...af59f2 )
by Alexandre
03:34
created

Server::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 44
nc 1
nop 3
dl 0
loc 67
ccs 0
cts 10
cp 0
crap 2
rs 9.2815
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;
10
11
12
use OAuth2\ClientAuthentication\ClientAuthenticationMethodManager;
13
use OAuth2\ClientAuthentication\ClientSecretBasicAuthenticationMethod;
14
use OAuth2\ClientAuthentication\ClientSecretPostAuthenticationMethod;
15
use OAuth2\Endpoints\AuthorizationEndpoint;
16
use OAuth2\Endpoints\TokenEndpoint;
17
use OAuth2\Flows\AuthorizationCodeFlow;
18
use OAuth2\Flows\ClientCredentialsFlow;
19
use OAuth2\Flows\FlowManager;
20
use OAuth2\Flows\ImplicitFlow;
21
use OAuth2\Flows\ResourceOwnerPasswordCredentialsFlow;
22
use OAuth2\GrantTypes\GrantTypeManager;
23
use OAuth2\GrantTypes\RefreshTokenGrantType;
24
use OAuth2\ResponseModes\FragmentResponseMode;
25
use OAuth2\ResponseModes\QueryResponseMode;
26
use OAuth2\ResponseModes\ResponseModeManager;
27
use OAuth2\ResponseTypes\ResponseTypeManager;
28
use OAuth2\Roles\ResourceOwnerInterface;
29
use OAuth2\ScopePolicy\ScopePolicyManager;
30
use OAuth2\Storages\StorageManager;
31
32
class Server
33
{
34
    protected $authorizationEndpoint;
35
    protected $tokenEndpoint;
36
37
    public function __construct(Config $config,
38
                                StorageManager $storageManager,
39
                                ResourceOwnerInterface $resourceOwner)
40
    {
41
        $responseTypeManager = new ResponseTypeManager();
42
        $scopePolicyManager = new ScopePolicyManager($config);
43
        $grantTypeManager = new GrantTypeManager();
44
45
        $clientAuthenticationMethodManager = new ClientAuthenticationMethodManager($storageManager->getClientStorage());
46
        $clientAuthenticationMethodManager->addClientAuthenticationMethod('client_secret_basic',
47
            new ClientSecretBasicAuthenticationMethod($storageManager->getClientStorage()));
48
        $clientAuthenticationMethodManager->addClientAuthenticationMethod('client_secret_post',
49
            new ClientSecretPostAuthenticationMethod($storageManager->getClientStorage()));
50
51
        $responseModeManager = new ResponseModeManager();
52
        $responseModeManager->addResponseMode('query', new QueryResponseMode());
53
        $responseModeManager->addResponseMode('fragment', new FragmentResponseMode());
54
55
        // response_type : code
56
        // grant_type : authorization_code
57
        $authorizationCodeFlow = new AuthorizationCodeFlow(
58
            $storageManager->getAuthorizationCodeStorage(),
59
            $storageManager->getAccessTokenStorage(),
60
            $storageManager->getRefreshTokenStorage()
61
        );
62
63
        // response_type : token
64
        $implicitFlow = new ImplicitFlow($storageManager->getAccessTokenStorage());
65
66
        // grant_type : password
67
        $resourceOwnerPasswordCredentialsFlow = new ResourceOwnerPasswordCredentialsFlow(
68
            $scopePolicyManager,
69
            $storageManager->getResourceOwnerStorage(),
70
            $storageManager->getAccessTokenStorage(),
71
            $storageManager->getRefreshTokenStorage());
72
73
        // grant_type : client_credentials
74
        $clientCredentialsFlow = new ClientCredentialsFlow(
75
            $scopePolicyManager,
76
            $storageManager->getAccessTokenStorage(),
77
            $storageManager->getRefreshTokenStorage()
78
        );
79
80
        // grant_type : refresh_token
81
        $refreshTokenGrantType = new RefreshTokenGrantType(
82
            $storageManager->getAccessTokenStorage(),
83
            $storageManager->getRefreshTokenStorage());
84
85
        $flowManager = new FlowManager($responseTypeManager, $grantTypeManager);
86
        $flowManager->addFlow($authorizationCodeFlow);
87
        $flowManager->addFlow($implicitFlow);
88
        $flowManager->addFlow($resourceOwnerPasswordCredentialsFlow);
89
        $flowManager->addFlow($clientCredentialsFlow);
90
91
        $grantTypeManager->addGrantType('refresh_token', $refreshTokenGrantType);
92
93
        $this->authorizationEndpoint = new AuthorizationEndpoint(
94
            $responseTypeManager,
95
            $responseModeManager,
96
            $scopePolicyManager,
97
            $resourceOwner,
98
            $storageManager->getClientStorage());
99
100
        $this->tokenEndpoint = new TokenEndpoint(
101
            $storageManager->getClientStorage(),
102
            $grantTypeManager,
103
            $clientAuthenticationMethodManager);
104
    }
105
106
    /**
107
     * @return AuthorizationEndpoint
108
     */
109
    public function getAuthorizationEndpoint(): AuthorizationEndpoint
110
    {
111
        return $this->authorizationEndpoint;
112
    }
113
114
    /**
115
     * @return TokenEndpoint
116
     */
117
    public function getTokenEndpoint(): TokenEndpoint
118
    {
119
        return $this->tokenEndpoint;
120
    }
121
122
    /**
123
     * @return bool
124
     *
125
     * @see https://tools.ietf.org/html/rfc6749#section-3.1.2.1
126
     *
127
     *  Endpoint Request Confidentiality
128
     *
129
     *     The redirection endpoint SHOULD require the use of TLS as described
130
     * in Section 1.6 when the requested response type is "code" or "token",
131
     * or when the redirection request will result in the transmission of
132
     * sensitive credentials over an open network.  This specification does
133
     * not mandate the use of TLS because at the time of this writing,
134
     * requiring clients to deploy TLS is a significant hurdle for many
135 4
     * client developers.  If TLS is not available, the authorization server
136
     * SHOULD warn the resource owner about the insecure endpoint prior to
137 4
     * redirection (e.g., display a message during the authorization
138
     * request).
139
     *
140
     * Lack of transport-layer security can have a severe impact on the
141
     * security of the client and the protected resources it is authorized
142
     * to access.  The use of transport-layer security is particularly
143 4
     * critical when the authorization process is used as a form of
144
     * delegated end-user authentication by the client (e.g., third-party
145 4
     * sign-in service).
146
     */
147
    public function isSecure()
148
    {
149
        return isset($_SERVER['HTTPS']) && ('on' == strtolower($_SERVER['HTTPS']) || 1 == $_SERVER['HTTPS']);
150
    }
151
}