1 | <?php |
||
27 | class Server |
||
28 | { |
||
29 | /** |
||
30 | * @var EntityCollection |
||
31 | */ |
||
32 | protected $grantExtensions; |
||
33 | |||
34 | /** |
||
35 | * @var ApplicationLoaderInterface |
||
36 | */ |
||
37 | protected $applicationLoader; |
||
38 | |||
39 | /** |
||
40 | * @var AccessTokenLoaderInterface |
||
41 | */ |
||
42 | protected $accessTokenLoader; |
||
43 | |||
44 | /** |
||
45 | * @var EventDispatcherInterface |
||
46 | */ |
||
47 | protected $eventDispatcher; |
||
48 | |||
49 | /** |
||
50 | * @var RandomTokenGenerator |
||
51 | */ |
||
52 | protected $randomTokenGenerator; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $tokenOptions; |
||
58 | |||
59 | /** |
||
60 | * Construct. |
||
61 | * |
||
62 | * @param EventDispatcherInterface $eventDispatcher |
||
63 | * @param ApplicationLoaderInterface $applicationLoader |
||
64 | * @param AccessTokenLoaderInterface $accessTokenLoader |
||
65 | * @param RandomTokenGenerator $randomTokenGenerator |
||
66 | * @param array $tokenOptions |
||
67 | * @param array $grantExtensions |
||
68 | */ |
||
69 | 18 | public function __construct( |
|
70 | EventDispatcherInterface $eventDispatcher, |
||
71 | ApplicationLoaderInterface $applicationLoader, |
||
72 | AccessTokenLoaderInterface $accessTokenLoader, |
||
73 | RandomTokenGenerator $randomTokenGenerator, |
||
74 | array $tokenOptions, |
||
75 | array $grantExtensions = array() |
||
76 | ) { |
||
77 | 18 | $this->applicationLoader = $applicationLoader; |
|
78 | 18 | $this->accessTokenLoader = $accessTokenLoader; |
|
79 | 18 | $this->eventDispatcher = $eventDispatcher; |
|
80 | 18 | $this->randomTokenGenerator = $randomTokenGenerator; |
|
81 | |||
82 | 18 | $tokenOptionsResolver = new OptionsResolver(); |
|
83 | 18 | $tokenOptionsResolver->setDefaults(array( |
|
84 | 18 | 'access_token_class' => AccessToken::class, |
|
85 | 18 | 'access_token_ttl' => AccessTokenInterface::DEFAULT_TTL, |
|
86 | 18 | 'refresh_token_class' => RefreshToken::class, |
|
87 | 18 | 'refresh_token_ttl' => RefreshTokenInterface::DEFAULT_TTL, |
|
88 | 18 | )); |
|
89 | |||
90 | 18 | $this->tokenOptions = $tokenOptionsResolver->resolve($tokenOptions); |
|
91 | |||
92 | 18 | $this->grantExtensions = new EntityCollection(); |
|
93 | 18 | foreach ($grantExtensions as $grantType => $extension) { |
|
94 | 18 | $this->registerGrantExtension($grantType, $extension); |
|
95 | 18 | } |
|
96 | 18 | } |
|
97 | |||
98 | /** |
||
99 | * Register an extension under given grant type. |
||
100 | * |
||
101 | * @param string $grantType |
||
102 | * @param GrantExtensionInterface $extension |
||
103 | */ |
||
104 | 18 | public function registerGrantExtension($grantType, GrantExtensionInterface $extension) |
|
108 | |||
109 | /** |
||
110 | * Validate given request parameters and build a |
||
111 | * LoginAttempt object with it. |
||
112 | * |
||
113 | * @param array $data |
||
114 | * @param array $headers |
||
115 | * @param array $query |
||
116 | * |
||
117 | * @return LoginAttempt |
||
118 | */ |
||
119 | 18 | protected function createLoginAttempt(array $data, array $headers, array $query) |
|
147 | |||
148 | /** |
||
149 | * Loads application for given login attempt. |
||
150 | * |
||
151 | * @param LoginAttempt $loginAttempt |
||
152 | * |
||
153 | * @return ApplicationInterface |
||
154 | * |
||
155 | * @throws InvalidGrantException |
||
156 | */ |
||
157 | 14 | protected function loadApplication(LoginAttempt $loginAttempt) |
|
158 | { |
||
159 | // retrieve Application |
||
160 | 14 | if (!$application = $this->applicationLoader->retrieveByApiKeyAndSecret( |
|
161 | 14 | $loginAttempt->getData('client_api_key'), |
|
162 | 14 | $loginAttempt->getData('client_secret') |
|
163 | 14 | )) { |
|
164 | 2 | throw new InvalidGrantException( |
|
165 | 2 | $loginAttempt, |
|
166 | 'Any application found for given api_key / secret.' |
||
167 | 2 | ); |
|
168 | } |
||
169 | |||
170 | 12 | return $application; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * Runs grant extension to load accounts. |
||
175 | * |
||
176 | * @param ApplicationInterface $application |
||
177 | * @param LoginAttempt $loginAttempt |
||
178 | * |
||
179 | * @return AccountInterface |
||
180 | * |
||
181 | * @throws \InvalidArgumentException |
||
182 | * @throws UnknownGrantTypeException |
||
183 | */ |
||
184 | 12 | protected function loadAccount( |
|
194 | |||
195 | /** |
||
196 | * Grant given credentials, or throws an exception if invalid |
||
197 | * credentials for application or account. |
||
198 | * |
||
199 | * @param array $data login request data |
||
200 | * @param array $headers optionnal login request headers |
||
201 | * @param array $query optionnal login request query |
||
202 | * |
||
203 | * @return AccessTokenInterface |
||
204 | */ |
||
205 | 18 | public function grant(array $data, array $headers = array(), array $query = array()) |
|
206 | { |
||
207 | // create and validate login attempt from given data |
||
208 | 18 | $loginAttempt = $this->createLoginAttempt( |
|
209 | 18 | $data, $headers, $query |
|
210 | 18 | ); |
|
211 | |||
212 | // load application / account |
||
213 | 14 | $account = $this->loadAccount( |
|
214 | 14 | $application = $this->loadApplication($loginAttempt), |
|
215 | $loginAttempt |
||
216 | 12 | ); |
|
217 | |||
218 | // event call |
||
219 | 12 | $this->eventDispatcher->dispatch( |
|
220 | 12 | AccessTokenEvents::MAJORA_ACCESS_TOKEN_CREATED, |
|
221 | 12 | new AccessTokenEvent( |
|
222 | |||
223 | // access token generation |
||
224 | 12 | $accessToken = new $this->tokenOptions['access_token_class']( |
|
225 | 12 | $application, |
|
226 | 12 | $account, |
|
227 | 12 | $this->tokenOptions['access_token_ttl'], |
|
228 | 12 | null, // for now, we let the expiration date calculate itself |
|
229 | 12 | $this->randomTokenGenerator->generate('access_token'), |
|
230 | |||
231 | // refresh token generation only if necessary |
||
232 | 12 | in_array('refresh_token', $application->getAllowedGrantTypes()) |
|
233 | 12 | && $this->grantExtensions->containsKey('refresh_token') ? |
|
234 | 12 | new $this->tokenOptions['refresh_token_class']( |
|
235 | 6 | $application, |
|
236 | 6 | $account, |
|
237 | 6 | $this->tokenOptions['refresh_token_ttl'], |
|
238 | 6 | null, // same |
|
239 | 6 | $this->randomTokenGenerator->generate('refresh_token') |
|
240 | 6 | ) : |
|
241 | null |
||
242 | 12 | ) |
|
243 | 12 | ) |
|
244 | 12 | ); |
|
245 | |||
246 | 12 | return $accessToken; |
|
247 | } |
||
248 | |||
249 | /** |
||
250 | * Tests if given hash match a valid AccessToken. |
||
251 | * |
||
252 | * @param string $hash |
||
253 | * |
||
254 | * @return AccessToken |
||
255 | * |
||
256 | * @throws InvalidAccessTokenException |
||
257 | */ |
||
258 | public function check($hash) |
||
266 | } |
||
267 |