Aliyun   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 41
c 1
b 0
f 0
dl 0
loc 118
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A userInfo() 0 15 3
A getToken() 0 13 2
A openid() 0 3 1
A parseToken() 0 7 2
A getUserInfo() 0 8 1
A getRedirectUrl() 0 13 1
1
<?php
2
/**
3
 * 阿里云
4
 * api接口文档
5
 *      https://help.aliyun.com/zh/ram/user-guide/overview-of-oauth-applications
6
*/
7
namespace tinymeng\OAuth2\Gateways;
8
use tinymeng\OAuth2\Connector\Gateway;
9
use tinymeng\OAuth2\Exception\OAuthException;
10
use tinymeng\OAuth2\Helper\ConstCode;
11
12
/**
13
 * Class Aliyun
14
 * @package tinymeng\OAuth2\Gateways
15
 * @Author: TinyMeng <[email protected]>
16
 * @Created: 2023/07/09
17
 */
18
class Aliyun extends Gateway
19
{
20
    const API_BASE            = 'https://signin.aliyun.com/';
21
    protected $AuthorizeURL   = 'https://signin.aliyun.com/oauth2/v1/auth';
22
    protected $AccessTokenURL = 'https://oauth.aliyun.com/v1/token';
23
    protected $UserInfoURL = 'https://oauth.aliyun.com/v1/userinfo';
24
25
    /**
26
     * Description:  得到跳转地址
27
     * @author: JiaMeng <[email protected]>
28
     * Updater:
29
     * @return string
30
     */
31
    public function getRedirectUrl()
32
    {
33
        //存储state
34
        $this->saveState();
35
        //登录参数
36
        $params = [
37
            'response_type' => $this->config['response_type'],
38
            'client_id'     => $this->config['app_id'],
39
            'redirect_uri'  => $this->config['callback'],
40
            'state'         => $this->config['state'],
41
            'scope'         => $this->config['scope'],
42
        ];
43
        return $this->AuthorizeURL . '?' . http_build_query($params);
44
    }
45
46
    /**
47
     * Description:  获取格式化后的用户信息
48
     * @return array
49
     * @throws OAuthException
50
     * @author: JiaMeng <[email protected]>
51
     * Updater:
52
     */
53
    public function userInfo()
54
    {
55
        $result = $this->getUserInfo();
56
        $userInfo = [
57
            'open_id' => isset($result['uid']) ? $result['uid'] : '',
58
            'union_id'=> isset($result['aid']) ? $result['aid'] : '',
59
            'channel' => ConstCode::TYPE_ALIYUN,
60
            'nickname'=> $result['login_name'],
61
            'gender'  => ConstCode::GENDER,
62
            'avatar'  => '',
63
            'birthday'=> '',
64
            'access_token'=> $this->token['access_token'] ?? '',
65
            'native'=> $result,
66
        ];
67
        return $userInfo;
68
    }
69
70
    /**
71
     * Description:  获取原始接口返回的用户信息
72
     * @return array
73
     * @throws OAuthException
74
     * @author: JiaMeng <[email protected]>
75
     * Updater:
76
     */
77
    public function getUserInfo()
78
    {
79
        /** 获取用户信息 */
80
        $this->openid();
81
82
        $headers = ['Authorization: Bearer '.$this->token['access_token']];
83
        $data = $this->get($this->UserInfoURL, [],$headers);
84
        return json_decode($data, true);
85
    }
86
87
    /**
88
     * Description:  获取当前授权用户的openid标识
89
     * @author: JiaMeng <[email protected]>
90
     * Updater:
91
     * @return mixed
92
     * @throws OAuthException
93
     */
94
    public function openid()
95
    {
96
        $this->getToken();
97
    }
98
99
100
    /**
101
     * Description:  获取AccessToken
102
     * @author: JiaMeng <[email protected]>
103
     * Updater:
104
     */
105
    protected function getToken(){
106
        if (empty($this->token)) {
107
            /** 验证state参数 */
108
            $this->CheckState();
109
110
            /** 获取参数 */
111
            $params = $this->accessTokenParams();
112
113
            /** 获取access_token */
114
            $this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params);
115
            $token =  $this->post($this->AccessTokenURL);
116
            /** 解析token值(子类实现此方法) */
117
            $this->token = $this->parseToken($token);
118
        }
119
    }
120
121
    /**
122
     * Description:  解析access_token方法请求后的返回值
123
     * @author: JiaMeng <[email protected]>
124
     * Updater:
125
     * @param $token
126
     * @return mixed
127
     * @throws OAuthException
128
     */
129
    protected function parseToken($token)
130
    {
131
        $data = json_decode($token, true);
132
        if (isset($data['access_token'])) {
133
            return $data;
134
        } else {
135
            throw new OAuthException("获取Aliyun ACCESS_TOKEN出错:{$data['error']}");
136
        }
137
    }
138
139
}
140