OAuth2::getAccessToken()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Foundation\Http;
13
14
use Jitamin\Foundation\Base;
15
16
/**
17
 * OAuth2 Client.
18
 */
19
class OAuth2 extends Base
20
{
21
    protected $clientId;
22
    protected $secret;
23
    protected $callbackUrl;
24
    protected $authUrl;
25
    protected $tokenUrl;
26
    protected $scopes;
27
    protected $tokenType;
28
    protected $accessToken;
29
30
    /**
31
     * Create OAuth2 service.
32
     *
33
     * @param string $clientId
34
     * @param string $secret
35
     * @param string $callbackUrl
36
     * @param string $authUrl
37
     * @param string $tokenUrl
38
     * @param array  $scopes
39
     *
40
     * @return OAuth2
41
     */
42
    public function createService($clientId, $secret, $callbackUrl, $authUrl, $tokenUrl, array $scopes)
43
    {
44
        $this->clientId = $clientId;
45
        $this->secret = $secret;
46
        $this->callbackUrl = $callbackUrl;
47
        $this->authUrl = $authUrl;
48
        $this->tokenUrl = $tokenUrl;
49
        $this->scopes = $scopes;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Generate OAuth2 state and return the token value.
56
     *
57
     * @return string
58
     */
59
    public function getState()
60
    {
61
        if (!isset($this->sessionStorage->oauthState) || empty($this->sessionStorage->oauthState)) {
0 ignored issues
show
Documentation introduced by
The property sessionStorage does not exist on object<Jitamin\Foundation\Http\OAuth2>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
62
            $this->sessionStorage->oauthState = $this->token->getToken();
0 ignored issues
show
Documentation introduced by
The property sessionStorage does not exist on object<Jitamin\Foundation\Http\OAuth2>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property token does not exist on object<Jitamin\Foundation\Http\OAuth2>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
        }
64
65
        return $this->sessionStorage->oauthState;
0 ignored issues
show
Documentation introduced by
The property sessionStorage does not exist on object<Jitamin\Foundation\Http\OAuth2>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
    }
67
68
    /**
69
     * Check the validity of the state (CSRF token).
70
     *
71
     * @param string $state
72
     *
73
     * @return bool
74
     */
75
    public function isValidateState($state)
76
    {
77
        return $state === $this->getState();
78
    }
79
80
    /**
81
     * Get authorization url.
82
     *
83
     * @return string
84
     */
85
    public function getAuthorizationUrl()
86
    {
87
        $params = [
88
            'response_type' => 'code',
89
            'client_id'     => $this->clientId,
90
            'redirect_uri'  => $this->callbackUrl,
91
            'scope'         => implode(' ', $this->scopes),
92
            'state'         => $this->getState(),
93
        ];
94
95
        return $this->authUrl.'?'.http_build_query($params);
96
    }
97
98
    /**
99
     * Get authorization header.
100
     *
101
     * @return string
102
     */
103
    public function getAuthorizationHeader()
104
    {
105
        if (strtolower($this->tokenType) === 'bearer') {
106
            return 'Authorization: Bearer '.$this->accessToken;
107
        }
108
109
        return '';
110
    }
111
112
    /**
113
     * Get access token.
114
     *
115
     * @param string $code
116
     *
117
     * @return string
118
     */
119
    public function getAccessToken($code)
120
    {
121
        if (empty($this->accessToken) && !empty($code)) {
122
            $params = [
123
                'code'          => $code,
124
                'client_id'     => $this->clientId,
125
                'client_secret' => $this->secret,
126
                'redirect_uri'  => $this->callbackUrl,
127
                'grant_type'    => 'authorization_code',
128
                'state'         => $this->getState(),
129
            ];
130
131
            $response = json_decode($this->httpClient->postForm($this->tokenUrl, $params, ['Accept: application/json']), true);
0 ignored issues
show
Documentation introduced by
The property httpClient does not exist on object<Jitamin\Foundation\Http\OAuth2>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
132
133
            $this->tokenType = isset($response['token_type']) ? $response['token_type'] : '';
134
            $this->accessToken = isset($response['access_token']) ? $response['access_token'] : '';
135
        }
136
137
        return $this->accessToken;
138
    }
139
140
    /**
141
     * Set access token.
142
     *
143
     * @param string $token
144
     * @param string $type
145
     *
146
     * @return string
147
     */
148
    public function setAccessToken($token, $type = 'bearer')
149
    {
150
        $this->accessToken = $token;
151
        $this->tokenType = $type;
152
    }
153
}
154