Linkedin::getAuthorizationMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
/**
14
 * Linkedin service.
15
 *
16
 * @author Antoine Corcy <[email protected]>
17
 *
18
 * @see http://developer.linkedin.com/documents/authentication
19
 */
20
class Linkedin extends AbstractService
21
{
22
    /**
23
     * Defined scopes.
24
     *
25
     * @see https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context
26
     */
27
    const SCOPE_R_LITEPROFILE = 'r_liteprofile';
28
    const SCOPE_R_FULLPROFILE = 'r_fullprofile';
29
    const SCOPE_R_EMAILADDRESS = 'r_emailaddress';
30
    const SCOPE_R_NETWORK = 'r_network';
31
    const SCOPE_R_CONTACTINFO = 'r_contactinfo';
32
    const SCOPE_RW_NUS = 'rw_nus';
33
    const SCOPE_RW_COMPANY_ADMIN = 'rw_company_admin';
34
    const SCOPE_RW_GROUPS = 'rw_groups';
35
    const SCOPE_W_MESSAGES = 'w_messages';
36
    const SCOPE_W_MEMBER_SOCIAL = 'w_member_social';
37
38
    public function __construct(
39
        CredentialsInterface $credentials,
40
        ClientInterface $httpClient,
41
        TokenStorageInterface $storage,
42
        $scopes = [],
43
        ?UriInterface $baseApiUri = null
44
    ) {
45
        if (count($scopes) === 0) {
46
            $scopes = [self::SCOPE_R_LITEPROFILE, self::SCOPE_R_EMAILADDRESS];
47
        }
48
49
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
50
51
        if (null === $baseApiUri) {
52
            $this->baseApiUri = new Uri('https://api.linkedin.com/v2/');
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getAuthorizationEndpoint()
60
    {
61
        return new Uri('https://www.linkedin.com/oauth/v2/authorization');
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getAccessTokenEndpoint()
68
    {
69
        return new Uri('https://www.linkedin.com/oauth/v2/accessToken');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function getAuthorizationMethod()
76
    {
77
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function parseAccessTokenResponse($responseBody)
84
    {
85
        $data = json_decode($responseBody, true);
86
87
        if (null === $data || !is_array($data)) {
88
            throw new TokenResponseException('Unable to parse response.');
89
        } elseif (isset($data['error'])) {
90
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
91
        }
92
93
        $token = new StdOAuth2Token();
94
        $token->setAccessToken($data['access_token']);
95
        $token->setLifeTime($data['expires_in']);
96
97
        if (isset($data['refresh_token'])) {
98
            $token->setRefreshToken($data['refresh_token']);
99
            unset($data['refresh_token']);
100
        }
101
102
        unset($data['access_token'], $data['expires_in']);
103
104
        $token->setExtraParams($data);
105
106
        return $token;
107
    }
108
}
109