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