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