Completed
Pull Request — master (#498)
by Dragonqos
04:00
created

Microsoft::parseAccessTokenResponse()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 4
nop 1
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
class Microsoft extends AbstractService
10
{
11
    const SCOPE_BASIC = 'wl.basic';
12
    const SCOPE_OFFLINE = 'wl.offline_access';
13
    const SCOPE_SIGNIN = 'wl.signin';
14
    const SCOPE_BIRTHDAY = 'wl.birthday';
15
    const SCOPE_CALENDARS = 'wl.calendars';
16
    const SCOPE_CALENDARS_UPDATE = 'wl.calendars_update';
17
    const SCOPE_CONTACTS_BIRTHDAY = 'wl.contacts_birthday';
18
    const SCOPE_CONTACTS_CREATE = 'wl.contacts_create';
19
    const SCOPE_CONTACTS_CALENDARS = 'wl.contacts_calendars';
20
    const SCOPE_CONTACTS_PHOTOS = 'wl.contacts_photos';
21
    const SCOPE_CONTACTS_SKYDRIVE = 'wl.contacts_skydrive';
22
    const SCOPE_EMAILS = 'wl.emails';
23
    const SCOPE_EVENTS_CREATE = 'wl.events_create';
24
    const SCOPE_MESSENGER = 'wl.messenger';
25
    const SCOPE_PHONE_NUMBERS = 'wl.phone_numbers';
26
    const SCOPE_PHOTOS = 'wl.photos';
27
    const SCOPE_POSTAL_ADDRESSES = 'wl.postal_addresses';
28
    const SCOPE_SHARE = 'wl.share';
29
    const SCOPE_SKYDRIVE = 'wl.skydrive';
30
    const SCOPE_SKYDRIVE_UPDATE = 'wl.skydrive_update';
31
    const SCOPE_WORK_PROFILE = 'wl.work_profile';
32
    const SCOPE_APPLICATIONS = 'wl.applications';
33
    const SCOPE_APPLICATIONS_CREATE = 'wl.applications_create';
34
    const SCOPE_IMAP = 'wl.imap';
35
36
    /**
37
     * MS uses some magical not officialy supported scope to get even moar info like full emailaddresses.
38
     * They agree that giving 3rd party apps access to 3rd party emailaddresses is a pretty lame thing to do so in all
39
     * their wisdom they added this scope because fuck you that's why.
40
     *
41
     * https://github.com/Lusitanian/PHPoAuthLib/issues/214
42
     * http://social.msdn.microsoft.com/Forums/live/en-US/c6dcb9ab-aed4-400a-99fb-5650c393a95d/how-retrieve-users-
43
     *                                  contacts-email-address?forum=messengerconnect
44
     *
45
     * Considering this scope is not officially supported: use with care
46
     */
47
    const SCOPE_CONTACTS_EMAILS = 'wl.contacts_emails';
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function init()
53
    {
54
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
55
            $this->baseApiUri = new Uri('https://apis.live.net/v5.0/');
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getAuthorizationEndpoint()
63
    {
64
        return new Uri('https://login.live.com/oauth20_authorize.srf');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getAccessTokenEndpoint()
71
    {
72
        return new Uri('https://login.live.com/oauth20_token.srf');
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getAuthorizationMethod()
79
    {
80
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function parseAccessTokenResponse($responseBody)
87
    {
88
        $data = json_decode($responseBody, true);
89
90
        if (null === $data || !is_array($data)) {
91
            throw new TokenResponseException('Unable to parse response.');
92
        } elseif (isset($data['error'])) {
93
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
94
        }
95
96
        $token = new StdOAuth2Token();
97
        $token->setAccessToken($data['access_token']);
98
        $token->setLifetime($data['expires_in']);
99
100
        if (isset($data['refresh_token'])) {
101
            $token->setRefreshToken($data['refresh_token']);
102
            unset($data['refresh_token']);
103
        }
104
105
        unset($data['access_token']);
106
        unset($data['expires_in']);
107
108
        $token->setExtraParams($data);
109
110
        return $token;
111
    }
112
}
113