bashofmann

added magic method for additional parameters which may be returned for different…

… scopes or services (e.g. user_id)
@@ -141,6 +141,10 @@ class OAuth2_Service @@ -141,6 +141,10 @@ class OAuth2_Service
141 'code' => $code, 141 'code' => $code,
142 ); 142 );
143 143
  144 + if ($this->_scope) {
  145 + $parameters['scope'] = $this->_scope;
  146 + }
  147 +
144 $http = new OAuth2_HttpClient($this->_configuration->getAccessTokenEndpoint(), 'POST', http_build_query($parameters)); 148 $http = new OAuth2_HttpClient($this->_configuration->getAccessTokenEndpoint(), 'POST', http_build_query($parameters));
145 $http->execute(); 149 $http->execute();
146 150
@@ -204,6 +208,15 @@ class OAuth2_Service @@ -204,6 +208,15 @@ class OAuth2_Service
204 isset($response['refresh_token']) ? $response['refresh_token'] : null, 208 isset($response['refresh_token']) ? $response['refresh_token'] : null,
205 isset($response['expires_in']) ? $response['expires_in'] : null); 209 isset($response['expires_in']) ? $response['expires_in'] : null);
206 210
  211 + unset($response['access_token']);
  212 + unset($response['refresh_token']);
  213 + unset($response['expires_in']);
  214 +
  215 + // add additional parameters which may be returned depending on service and scope
  216 + foreach ($response as $key => $value) {
  217 + $token->{'set' . $key}($value);
  218 + }
  219 +
207 $this->_dataStore->storeAccessToken($token); 220 $this->_dataStore->storeAccessToken($token);
208 } 221 }
209 222
@@ -269,6 +282,10 @@ class OAuth2_Token @@ -269,6 +282,10 @@ class OAuth2_Token
269 private $_lifeTime; 282 private $_lifeTime;
270 283
271 /** 284 /**
  285 + * @var array
  286 + */
  287 + private $_additionalParams = array();
  288 + /**
272 * 289 *
273 * @param string $accessToken 290 * @param string $accessToken
274 * @param string $refreshToken 291 * @param string $refreshToken
@@ -283,6 +300,39 @@ class OAuth2_Token @@ -283,6 +300,39 @@ class OAuth2_Token
283 } 300 }
284 301
285 /** 302 /**
  303 + * magic method for setting and getting additional parameters returned from
  304 + * service
  305 + *
  306 + * e.g. user_id parameter with scope openid
  307 + *
  308 + * @param string $name
  309 + * @param array $arguments
  310 + * @return mixed
  311 + */
  312 + public function __call($name, $arguments) {
  313 + if (strlen($name) < 4) {
  314 + throw new OAuth2_Exception('undefined magic method called');
  315 + }
  316 + $method = substr($name, 0, 3);
  317 + $param = substr($name, 3);
  318 + switch ($method) {
  319 + case 'get':
  320 + if (! isset($this->_additionalParams[$param])) {
  321 + throw new OAuth2_Exception($param . ' was not returned by service');
  322 + }
  323 + return $this->_additionalParams[$param];
  324 + case 'set':
  325 + if (! isset($arguments[0])) {
  326 + throw new OAuth2_Exception('magic setter has no argument');
  327 + }
  328 + $this->_additionalParams[$param] = $arguments[0];
  329 + break;
  330 + default:
  331 + throw new OAuth2_Exception('undefined magic method called');
  332 + }
  333 + }
  334 +
  335 + /**
286 * @return string 336 * @return string
287 */ 337 */
288 public function getAccessToken() { 338 public function getAccessToken() {