Completed
Push — main ( 95452b...550e9c )
by huang
03:50
created

OAuth2Manager::getAccessTokenInfo()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

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 16
cts 16
cp 1
crap 3
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 3
    public function __construct(Auth $auth, Config $config = null)
16
    {
17 3
        $this->auth = $auth;
18 3
        if ($config == null) {
19 3
            $this->config = new Config();
20
        } else {
21 3
            $this->config = $config;
22
        }
23 3
    }
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 $access_token 访问令牌
107
     * @return array
108
     *
109
     * @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
110
     */
111 1
    public function getAccessTokenInfo($access_token)
112
    {
113
        $params = array(
114 1
            'client_id' => $this->auth->getAppKey(),
115 1
            'client_secret' => $this->auth->getAppSecret(),
116 1
            'device_id' => $this->auth->getdeviceID(),
117 1
            'access_token' => $access_token
118
        );
119 1
        $data = http_build_query($params);
120
121 1
        $scheme = "http://";
122 1
        if ($this->config->useHTTPS === true) {
123 1
            $scheme = "https://";
124
        }
125 1
        $url = $scheme . Config::API_HOST . '/oauth2/get_token_info?' . $data;
126
127 1
        $headers = array();
128 1
        $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
129 1
        $response = Client::get($url, $headers);
130
131 1
        if (!$response->ok()) {
132 1
            return array(null, new Error($url, $response));
133
        }
134 1
        return array($response->json(), null);
135
    }
136
}
137