Completed
Push — main ( bbbde4...aab90d )
by huang
29:39 queued 28:15
created

OAuth2Manager::getAccessTokenInfo()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 3
eloc 16
c 3
b 1
f 1
nc 4
nop 1
dl 0
loc 24
ccs 15
cts 16
cp 0.9375
crap 3.0021
rs 9.7333
1
<?php
2
3
namespace Xmly\OAuth2;
4
5
use Xmly\Auth;
6
use Xmly\Config;
7
use Xmly\Http\Client;
8
use Xmly\Http\Error;
9
10
final class OAuth2Manager
11
{
12
    private $auth;
13
    private $config;
14
15 5
    public function __construct(Auth $auth, Config $config = null)
16
    {
17 5
        $this->auth = $auth;
18 5
        if ($config == null) {
19 5
            $this->config = new Config();
20
        } else {
21 5
            $this->config = $config;
22
        }
23 5
    }
24
25
    /**
26
     * 获取访问令牌
27
     *
28
     * @param string $code
29
     * @param string $redirect_uri
30
     * @param string $state
31
     * @param string $grant_type
32
     * @return array
33
     *
34
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=9&articleId=5#%E8%8E%B7%E5%8F%96%E8%AE%BF%E9%97%AE%E4%BB%A4%E7%89%8C
35
     */
36 1
    public function getAccessToken($code, $redirect_uri, $state, $grant_type = 'authorization_code')
37
    {
38
        $params = array(
39 1
            'client_id' => $this->auth->getAppKey(),
40 1
            'client_secret' => $this->auth->getAppSecret(),
41 1
            'device_id' => $this->auth->getdeviceID(),
42 1
            'code' => $code,
43 1
            'redirect_uri' => $redirect_uri,
44 1
            'state' => $state,
45 1
            'grant_type' => $grant_type
46
        );
47 1
        $data = http_build_query($params);
48
49 1
        $scheme = "http://";
50 1
        if ($this->config->useHTTPS === true) {
51 1
            $scheme = "https://";
52
        }
53 1
        $url = $scheme . Config::API_HOST . '/oauth2/v2/access_token';
54
55 1
        $headers = array();
56 1
        $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
57 1
        $response = Client::post($url, $data, $headers);
58
59 1
        if (!$response->ok()) {
60 1
            return array(null, new Error($url, $response));
61
        }
62
        return array($response->json(), null);
63
    }
64
65
    /**
66
     * 刷新访问令牌
67
     *
68
     * @param string $refresh_token 刷新令牌参数,从 /oauth2/v2/access_token 返回
69
     * @param string $redirect_uri OAuth2回调地址,即创建应用时填写的回调地址。使用时请对它的值进行URL编码处理,并最好提供https地址
70
     * @param string $grant_type 授权模式,固定值"refresh_token"
71
     * @return array
72
     *
73
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=9&articleId=5#%E5%88%B7%E6%96%B0%E8%AE%BF%E9%97%AE%E4%BB%A4%E7%89%8C
74
     */
75 1
    public function refreshAccessToken($refresh_token, $redirect_uri, $grant_type = 'refresh_token')
76
    {
77
        $params = array(
78 1
            'client_id' => $this->auth->getAppKey(),
79 1
            'client_secret' => $this->auth->getAppSecret(),
80 1
            'device_id' => $this->auth->getdeviceID(),
81 1
            'refresh_token' => $refresh_token,
82 1
            'redirect_uri' => $redirect_uri,
83 1
            'grant_type' => $grant_type
84
        );
85 1
        $data = http_build_query($params);
86
87 1
        $scheme = "http://";
88 1
        if ($this->config->useHTTPS === true) {
89 1
            $scheme = "https://";
90
        }
91 1
        $url = $scheme . Config::API_HOST . '/oauth2/refresh_token';
92
93 1
        $headers = array();
94 1
        $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
95 1
        $response = Client::post($url, $data, $headers);
96
97 1
        if (!$response->ok()) {
98 1
            return array(null, new Error($url, $response));
99
        }
100 1
        return array($response->json(), null);
101
    }
102
103
    /**
104
     * 回收访问令牌
105
     *
106
     * @param string $redirect_uri OAuth2回调地址,即创建应用时填写的回调地址。使用时请对它的值进行URL编码处理,并最好提供https地址
107
     * @param string $accessToken 访问令牌
108
     * @return array
109
     *
110
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=9&articleId=5#%E5%9B%9E%E6%94%B6%E8%AE%BF%E9%97%AE%E4%BB%A4%E7%89%8C
111
     */
112 1
    public function revokeAccessToken($redirect_uri, $accessToken)
113
    {
114
        $params = array(
115 1
            'client_id' => $this->auth->getAppKey(),
116 1
            'client_secret' => $this->auth->getAppSecret(),
117 1
            'device_id' => $this->auth->getdeviceID(),
118 1
            'redirect_uri' => $redirect_uri,
119 1
            'access_token' => $accessToken
120
        );
121 1
        $data = http_build_query($params);
122
123 1
        $scheme = "http://";
124 1
        if ($this->config->useHTTPS === true) {
125
            $scheme = "https://";
126
        }
127 1
        $url = $scheme . Config::API_HOST . '/oauth2/revoke_token?' . $data;
128
129 1
        $headers = array();
130 1
        $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
131 1
        $response = Client::post($url, null, $headers);
132
133 1
        if (!$response->ok()) {
134 1
            return array(null, new Error($url, $response));
135
        }
136
        return array($response->json(), null);
137
    }
138
139
    /**
140
     * 查询访问令牌
141
     *
142
     * @param string $access_token 访问令牌
143
     * @return array
144
     *
145
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=9&articleId=5#%E6%9F%A5%E8%AF%A2%E8%AE%BF%E9%97%AE%E4%BB%A4%E7%89%8C
146
     */
147 1
    public function getAccessTokenInfo($access_token)
148
    {
149
        $params = array(
150 1
            'client_id' => $this->auth->getAppKey(),
151 1
            'client_secret' => $this->auth->getAppSecret(),
152 1
            'device_id' => $this->auth->getdeviceID(),
153 1
            'access_token' => $access_token
154
        );
155 1
        $data = http_build_query($params);
156
157 1
        $scheme = "http://";
158 1
        if ($this->config->useHTTPS === true) {
159 1
            $scheme = "https://";
160
        }
161 1
        $url = $scheme . Config::API_HOST . '/oauth2/get_token_info?' . $data;
162
163 1
        $headers = array();
164 1
        $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
165 1
        $response = Client::get($url, $headers);
166
167 1
        if (!$response->ok()) {
168 1
            return array(null, new Error($url, $response));
169
        }
170
        return array($response->json(), null);
171
    }
172
173
    /**
174
     * 回收刷新访问令牌
175
     *
176
     * @param string $redirect_uri OAuth2回调地址,即创建应用时填写的回调地址。使用时请对它的值进行URL编码处理,并最好提供https地址
177
     * @param string $refresh_token 刷新访问令牌
178
     * @return array
179
     *
180
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=9&articleId=5#%E5%9B%9E%E6%94%B6%E5%88%B7%E6%96%B0%E8%AE%BF%E9%97%AE%E4%BB%A4%E7%89%8C
181
     */
182 1
    public function revokeRefreshToken($redirect_uri, $refresh_token)
183
    {
184
        $params = array(
185 1
            'client_id' => $this->auth->getAppKey(),
186 1
            'client_secret' => $this->auth->getAppSecret(),
187 1
            'device_id' => $this->auth->getdeviceID(),
188 1
            'redirect_uri' => $redirect_uri,
189 1
            'refresh_token' => $refresh_token
190
        );
191 1
        $data = http_build_query($params);
192
193 1
        $scheme = "http://";
194 1
        if ($this->config->useHTTPS === true) {
195
            $scheme = "https://";
196
        }
197 1
        $url = $scheme . Config::API_HOST . '/oauth2/revoke_refresh_token?' . $data;
198
199 1
        $headers = array();
200 1
        $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
201 1
        $response = Client::post($url, null, $headers);
202
203 1
        if (!$response->ok()) {
204 1
            return array(null, new Error($url, $response));
205
        }
206
        return array($response->json(), null);
207
    }
208
}
209