Completed
Push — master ( 3a54e6...f3ad06 )
by Дмитрий
03:25
created

AbstractProvider::getAccessToken()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Auth\Provider\OAuth2;
8
9
use InvalidArgumentException;
10
use SocialConnect\Auth\Exception\InvalidAccessToken;
11
use SocialConnect\Auth\Provider\AbstractBaseProvider;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Http\Client\Client;
14
15
abstract class AbstractProvider extends AbstractBaseProvider
16
{
17
    /**
18
     * HTTP method for access token request
19
     *
20
     * @var string
21
     */
22
    protected $requestHttpMethod = Client::POST;
23
24
    /**
25
     * @return array
26
     */
27
    public function getAuthUrlParameters()
28
    {
29
        return array(
30
            'client_id' => $this->consumer->getKey(),
31
            'redirect_uri' => $this->getRedirectUrl()
32
        );
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function makeAuthUrl()
39
    {
40
        $urlParameters = $this->getAuthUrlParameters();
41
42
        if (count($this->scope) > 0) {
43
            $urlParameters['scope'] = $this->getScopeInline();
44
        }
45
46
        if (count($this->fields) > 0) {
47
            $urlParameters['fields'] = $this->getFieldsInline();
48
        }
49
50
        return $this->getAuthorizeUri() . '?' . http_build_query($urlParameters);
51
    }
52
53
    /**
54
     * Parse access token from response's $body
55
     *
56
     * @param $body
57
     * @return AccessToken
58
     * @throws InvalidAccessToken
59
     */
60
    public function parseToken($body)
61
    {
62
        if (empty($body)) {
63
            throw new InvalidAccessToken('Provider response with empty body');
64
        }
65
66
        parse_str($body, $token);
67
68
        if (!is_array($token) || !isset($token['access_token'])) {
69
            throw new InvalidAccessToken('Provider API returned an unexpected response');
70
        }
71
72
        return new AccessToken($token['access_token']);
73
    }
74
75
    /**
76
     * @param string $code
77
     * @return AccessToken
78
     */
79
    public function getAccessToken($code)
80
    {
81
        if (!is_string($code)) {
82
            throw new InvalidArgumentException('Parameter $code must be a string');
83
        }
84
85
        $parameters = array(
86
            'client_id' => $this->consumer->getKey(),
87
            'client_secret' => $this->consumer->getSecret(),
88
            'code' => $code,
89
            'grant_type' => 'authorization_code',
90
            'redirect_uri' => $this->getRedirectUrl()
91
        );
92
93
        $response = $this->service->getHttpClient()->request(
94
            $this->getRequestTokenUri(),
95
            $parameters,
96
            $this->requestHttpMethod,
97
            [
98
                'Content-Type' => 'application/x-www-form-urlencoded'
99
            ]
100
        );
101
        $body = $response->getBody();
102
103
        return $this->parseToken($body);
104
    }
105
106
107
    /**
108
     * @param array $parameters
109
     * @return AccessToken
110
     */
111
    public function getAccessTokenByRequestParameters(array $parameters)
112
    {
113
        return $this->getAccessToken($parameters['code']);
114
    }
115
116
    /**
117
     * Get current user identity from social network by $accessToken
118
     *
119
     * @param AccessToken $accessToken
120
     * @return User
121
     *
122
     * @throws \SocialConnect\Auth\Exception\InvalidResponse
123
     */
124
    abstract public function getIdentity(AccessToken $accessToken);
125
}
126