1*a9ed3890SAndreas Gohr<?php 2*a9ed3890SAndreas Gohr 3*a9ed3890SAndreas Gohrnamespace dokuwiki\plugin\oauth\Service; 4*a9ed3890SAndreas Gohr 5*a9ed3890SAndreas Gohruse OAuth\Common\Http\Exception\TokenResponseException; 6*a9ed3890SAndreas Gohruse OAuth\OAuth2\Service\AbstractService; 7*a9ed3890SAndreas Gohruse OAuth\OAuth2\Token\StdOAuth2Token; 8*a9ed3890SAndreas Gohr 9*a9ed3890SAndreas Gohr/** 10*a9ed3890SAndreas Gohr * Implements the parseAccessTokenResponse like most services do it. Can be used as a base 11*a9ed3890SAndreas Gohr * for custom services. 12*a9ed3890SAndreas Gohr */ 13*a9ed3890SAndreas Gohrabstract class AbstractOAuth2Base extends AbstractService 14*a9ed3890SAndreas Gohr{ 15*a9ed3890SAndreas Gohr 16*a9ed3890SAndreas Gohr /** @inheritdoc */ 17*a9ed3890SAndreas Gohr protected function parseAccessTokenResponse($responseBody) 18*a9ed3890SAndreas Gohr { 19*a9ed3890SAndreas Gohr $data = json_decode($responseBody, true); 20*a9ed3890SAndreas Gohr 21*a9ed3890SAndreas Gohr if (null === $data || !is_array($data)) { 22*a9ed3890SAndreas Gohr throw new TokenResponseException('Unable to parse response.'); 23*a9ed3890SAndreas Gohr } elseif (isset($data['error'])) { 24*a9ed3890SAndreas Gohr throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); 25*a9ed3890SAndreas Gohr } 26*a9ed3890SAndreas Gohr 27*a9ed3890SAndreas Gohr $token = new StdOAuth2Token(); 28*a9ed3890SAndreas Gohr $token->setAccessToken($data['access_token']); 29*a9ed3890SAndreas Gohr unset($data['access_token']); 30*a9ed3890SAndreas Gohr 31*a9ed3890SAndreas Gohr if (isset($data['expires'])) { 32*a9ed3890SAndreas Gohr $token->setLifeTime($data['expires']); 33*a9ed3890SAndreas Gohr unset($data['expires']); 34*a9ed3890SAndreas Gohr } else { 35*a9ed3890SAndreas Gohr $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); 36*a9ed3890SAndreas Gohr } 37*a9ed3890SAndreas Gohr 38*a9ed3890SAndreas Gohr if (isset($data['refresh_token'])) { 39*a9ed3890SAndreas Gohr $token->setRefreshToken($data['refresh_token']); 40*a9ed3890SAndreas Gohr unset($data['refresh_token']); 41*a9ed3890SAndreas Gohr } 42*a9ed3890SAndreas Gohr 43*a9ed3890SAndreas Gohr $token->setExtraParams($data); 44*a9ed3890SAndreas Gohr 45*a9ed3890SAndreas Gohr return $token; 46*a9ed3890SAndreas Gohr } 47*a9ed3890SAndreas Gohr} 48