1 | <?php |
||
25 | class AuthorizationServer implements EmitterAwareInterface |
||
26 | { |
||
27 | use EmitterAwareTrait; |
||
28 | |||
29 | /** |
||
30 | * @var GrantTypeInterface[] |
||
31 | */ |
||
32 | protected $enabledGrantTypes = []; |
||
33 | |||
34 | /** |
||
35 | * @var \DateInterval[] |
||
36 | */ |
||
37 | protected $grantTypeAccessTokenTTL = []; |
||
38 | |||
39 | /** |
||
40 | * @var CryptKey |
||
41 | */ |
||
42 | protected $privateKey; |
||
43 | |||
44 | /** |
||
45 | * @var CryptKey |
||
46 | */ |
||
47 | protected $publicKey; |
||
48 | |||
49 | /** |
||
50 | * @var null|ResponseTypeInterface |
||
51 | */ |
||
52 | protected $responseType; |
||
53 | |||
54 | /** |
||
55 | * @var ClientRepositoryInterface |
||
56 | */ |
||
57 | private $clientRepository; |
||
58 | |||
59 | /** |
||
60 | * @var AccessTokenRepositoryInterface |
||
61 | */ |
||
62 | private $accessTokenRepository; |
||
63 | |||
64 | /** |
||
65 | * @var ScopeRepositoryInterface |
||
66 | */ |
||
67 | private $scopeRepository; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | private $encryptionKey; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | private $defaultScope = ''; |
||
78 | |||
79 | /** |
||
80 | * New server instance. |
||
81 | * |
||
82 | * @param ClientRepositoryInterface $clientRepository |
||
83 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
84 | * @param ScopeRepositoryInterface $scopeRepository |
||
85 | * @param CryptKey|string $privateKey |
||
86 | * @param string $encryptionKey |
||
87 | * @param null|ResponseTypeInterface $responseType |
||
88 | */ |
||
89 | public function __construct( |
||
90 | ClientRepositoryInterface $clientRepository, |
||
91 | AccessTokenRepositoryInterface $accessTokenRepository, |
||
92 | ScopeRepositoryInterface $scopeRepository, |
||
93 | $privateKey, |
||
94 | $encryptionKey, |
||
95 | ResponseTypeInterface $responseType = null |
||
96 | ) { |
||
97 | $this->clientRepository = $clientRepository; |
||
98 | $this->accessTokenRepository = $accessTokenRepository; |
||
99 | $this->scopeRepository = $scopeRepository; |
||
100 | |||
101 | if ($privateKey instanceof CryptKey === false) { |
||
102 | $privateKey = new CryptKey($privateKey); |
||
103 | } |
||
104 | $this->privateKey = $privateKey; |
||
105 | $this->encryptionKey = $encryptionKey; |
||
106 | $this->responseType = $responseType; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Enable a grant type on the server. |
||
111 | * |
||
112 | * @param GrantTypeInterface $grantType |
||
113 | * @param null|\DateInterval $accessTokenTTL |
||
114 | */ |
||
115 | public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null) |
||
116 | { |
||
117 | if ($accessTokenTTL instanceof \DateInterval === false) { |
||
118 | $accessTokenTTL = new \DateInterval('PT1H'); |
||
119 | } |
||
120 | |||
121 | $grantType->setAccessTokenRepository($this->accessTokenRepository); |
||
122 | $grantType->setClientRepository($this->clientRepository); |
||
123 | $grantType->setScopeRepository($this->scopeRepository); |
||
124 | $grantType->setDefaultScope($this->defaultScope); |
||
125 | $grantType->setPrivateKey($this->privateKey); |
||
126 | $grantType->setEmitter($this->getEmitter()); |
||
127 | $grantType->setEncryptionKey($this->encryptionKey); |
||
128 | |||
129 | $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType; |
||
130 | $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Validate an authorization request |
||
135 | * |
||
136 | * @param ServerRequestInterface $request |
||
137 | * |
||
138 | * @throws OAuthServerException |
||
139 | * |
||
140 | * @return AuthorizationRequest |
||
141 | */ |
||
142 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
143 | { |
||
144 | foreach ($this->enabledGrantTypes as $grantType) { |
||
145 | if ($grantType->canRespondToAuthorizationRequest($request)) { |
||
146 | return $grantType->validateAuthorizationRequest($request); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | throw OAuthServerException::unsupportedGrantType(); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Complete an authorization request |
||
155 | * |
||
156 | * @param AuthorizationRequest $authRequest |
||
157 | * @param ResponseInterface $response |
||
158 | * |
||
159 | * @return ResponseInterface |
||
160 | */ |
||
161 | public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response) |
||
167 | |||
168 | /** |
||
169 | * Return an access token response. |
||
170 | * |
||
171 | * @param ServerRequestInterface $request |
||
172 | * @param ResponseInterface $response |
||
173 | * |
||
174 | * @throws OAuthServerException |
||
175 | * |
||
176 | * @return ResponseInterface |
||
177 | */ |
||
178 | public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response) |
||
196 | |||
197 | /** |
||
198 | * Get the token type that grants will return in the HTTP response. |
||
199 | * |
||
200 | * @return ResponseTypeInterface |
||
201 | */ |
||
202 | protected function getResponseType() |
||
213 | |||
214 | /** |
||
215 | * Set the default scope for the authorization server. |
||
216 | * |
||
217 | * @param string $defaultScope |
||
218 | */ |
||
219 | public function setDefaultScope($defaultScope) |
||
223 | } |
||
224 |