Completed
Push — master ( 80eaf4...3b3c51 )
by Дмитрий
03:29
created

getAccessTokenByRequestParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\OpenID;
8
9
use SocialConnect\Auth\Exception\InvalidAccessToken;
10
use SocialConnect\Auth\Provider\AbstractBaseProvider;
11
use SocialConnect\Common\Entity\User;
12
use SocialConnect\Common\Http\Client\Client;
13
14
abstract class AbstractProvider extends AbstractBaseProvider
15
{
16
    /**
17
     * @return array
18
     */
19
    public function getAuthUrlParameters()
20
    {
21
        return array(
22
            'client_id' => $this->consumer->getKey(),
23
            'redirect_uri' => $this->getRedirectUrl()
24
        );
25
    }
26
27
    /**
28
     * @return string
29
     */
30
    public function makeAuthUrl()
31
    {
32
        $urlParameters = $this->getAuthUrlParameters();
33
34
        if (count($this->scope) > 0) {
35
            $urlParameters['scope'] = $this->getScopeInline();
36
        }
37
38
        return $this->getAuthorizeUri() . '?' . http_build_query($urlParameters);
39
    }
40
41
    /**
42
     * Parse access token from response's $body
43
     *
44
     * @param $body
45
     * @return AccessToken
46
     * @throws InvalidAccessToken
47
     */
48
    public function parseToken($body)
49
    {
50
        parse_str($body, $token);
51
52
        if (!is_array($token) || !isset($token['access_token'])) {
53
            throw new InvalidAccessToken('Provider API returned an unexpected response');
54
        }
55
56
        return new AccessToken($token['access_token']);
57
    }
58
59
    /**
60
     * @param string $code
61
     * @return AccessToken
62
     */
63
    public function getAccessToken($code)
64
    {
65
        if (!is_string($code)) {
66
            throw new \InvalidArgumentException('Parameter $code must be a string');
67
        }
68
69
        $parameters = array(
70
            'code' => $code,
71
            'grant_type' => 'authorization_code',
72
            'redirect_uri' => $this->getRedirectUrl()
73
        );
74
75
        $response = $this->service->getHttpClient()->request(
76
            $this->getRequestTokenUri(),
77
            $parameters,
78
            Client::POST,
79
            array(
80
                'Authorization' => 'Basic '
81
            )
82
        );
83
84
        $body = $response->getBody();
85
86
        return $this->parseToken($body);
87
    }
88
89
90
    /**
91
     * @param array $parameters
92
     * @return AccessToken
93
     */
94
    public function getAccessTokenByRequestParameters(array $parameters)
95
    {
96
        return $this->getAccessToken($parameters['code']);
97
    }
98
99
    /**
100
     * Get current user identity from social network by $accessToken
101
     *
102
     * @param AccessToken $accessToken
103
     * @return User
104
     */
105
    abstract public function getIdentity(AccessToken $accessToken);
106
}
107