1 | <?php |
||
26 | class Server |
||
27 | { |
||
28 | /** |
||
29 | * @var GrantExtensionInterface[] |
||
30 | */ |
||
31 | protected $grantExtensions; |
||
32 | |||
33 | /** |
||
34 | * @var ApplicationLoaderInterface |
||
35 | */ |
||
36 | protected $applicationLoader; |
||
37 | |||
38 | /** |
||
39 | * @var EventDispatcherInterface |
||
40 | */ |
||
41 | protected $eventDispatcher; |
||
42 | |||
43 | /** |
||
44 | * @var RandomTokenGenerator |
||
45 | */ |
||
46 | protected $randomTokenGenerator; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $accessTokenTtl; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $accessTokenClassName; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $refreshTokenTtl; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $refreshTokenClassName; |
||
67 | |||
68 | /** |
||
69 | * Construct. |
||
70 | * |
||
71 | * @param EventDispatcherInterface $eventDispatcher |
||
72 | * @param ApplicationLoaderInterface $applicationLoader |
||
73 | * @param int $accessTokenTtl |
||
74 | * @param string $accessTokenClassName |
||
75 | * @param int $refreshTokenTtl |
||
76 | * @param string $refreshTokenClassName |
||
77 | * @param RandomTokenGenerator $randomTokenGenerator |
||
78 | * @param array $grantExtensions |
||
79 | */ |
||
80 | public function __construct( |
||
105 | |||
106 | /** |
||
107 | * Register an extension under given grant type. |
||
108 | * |
||
109 | * @param string $grantType |
||
110 | * @param GrantExtensionInterface $extension |
||
111 | */ |
||
112 | public function registerGrantExtension($grantType, GrantExtensionInterface $extension) |
||
116 | |||
117 | /** |
||
118 | * Validate given request parameters and build a |
||
119 | * LoginAttempt object with it. |
||
120 | * |
||
121 | * @param array $data |
||
122 | * @param array $headers |
||
123 | * @param array $query |
||
124 | * |
||
125 | * @return LoginAttempt |
||
126 | */ |
||
127 | protected function createLoginAttempt(array $data, array $headers, array $query) |
||
128 | { |
||
129 | // validate grant_type manually (needed to guess specialized option resolver) |
||
130 | if (empty($data['grant_type'])) { |
||
131 | throw new \InvalidArgumentException('Any grant_type given.'); |
||
132 | } |
||
133 | $grantType = $data['grant_type']; |
||
134 | if (!isset($this->grantExtensions[$grantType])) { |
||
135 | throw new \InvalidArgumentException('Given grant_type is invalid.'); |
||
136 | } |
||
137 | |||
138 | // create option resolver |
||
139 | $requestResolver = new OptionsResolver(); |
||
140 | $requestResolver->setRequired(array( |
||
141 | 'client_secret', |
||
142 | 'client_api_key', |
||
143 | 'grant_type', |
||
144 | )); |
||
145 | $this->grantExtensions[$grantType]->configureRequestParameters( |
||
146 | $requestResolver |
||
147 | ); |
||
148 | |||
149 | return new LoginAttempt( |
||
150 | $query, |
||
151 | $requestResolver->resolve($data), |
||
152 | $headers |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Loads application for given login attempt. |
||
158 | * |
||
159 | * @param LoginAttempt $loginAttempt |
||
160 | * |
||
161 | * @return ApplicationInterface |
||
162 | * |
||
163 | * @throws InvalidGrantException |
||
164 | */ |
||
165 | protected function loadApplication(LoginAttempt $loginAttempt) |
||
166 | { |
||
167 | // retrieve Application |
||
168 | if (!$application = $this->applicationLoader->retrieveByApiKeyAndSecret( |
||
169 | $loginAttempt->getData('client_api_key'), |
||
170 | $loginAttempt->getData('client_secret') |
||
171 | )) { |
||
172 | throw new InvalidGrantException( |
||
173 | $loginAttempt, |
||
174 | 'Any application found for given api_key / secret.' |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | return $application; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Runs grant extension to load accounts. |
||
183 | * |
||
184 | * @param ApplicationInterface $application |
||
185 | * @param LoginAttempt $loginAttempt |
||
186 | * |
||
187 | * @return AccountInterface |
||
188 | * |
||
189 | * @throws \InvalidArgumentException |
||
190 | * @throws UnknownGrantTypeException |
||
191 | */ |
||
192 | protected function loadAccount( |
||
202 | |||
203 | /** |
||
204 | * Grant given credentials, or throws an exception if invalid |
||
205 | * credentials for application or account. |
||
206 | * |
||
207 | * @param array $data login request data |
||
208 | * @param array $headers optionnal login request headers |
||
209 | * @param array $query optionnal login request query |
||
210 | * |
||
211 | * @return AccessTokenInterface |
||
212 | */ |
||
213 | public function grant(array $data, array $headers = array(), array $query = array()) |
||
257 | } |
||
258 |