OpenAuth::authorize()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 2
nop 3
dl 0
loc 8
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace OpenOauth;
3
4
use OpenOauth\Core\Core;
5
use OpenOauth\Core\Http\Http;
6
use OpenOauth\Core\CacheDriver\BaseDriver as CacheBaseDriver;
7
use OpenOauth\Core\DatabaseDriver\BaseDriver as DatabaseBaseDriver;
8
9
/**
10
 * 微信Auth相关接口.
11
 *
12
 * @author Tian.
13
 */
14
class OpenAuth extends Core
15
{
16
    const API_URL       = 'https://open.weixin.qq.com/connect/oauth2/authorize';
17
    const CODE_GET_USER = 'https://api.weixin.qq.com/sns/oauth2/component/access_token';
18
    const API_USER_INFO = 'https://api.weixin.qq.com/sns/userinfo';
19
20
    protected $authorizedUser;
21
    private   $authorized_app_id;
22
23
    /**
24
     * AuthApi constructor.
25
     *
26
     * @param $authorized_app_id
27
     */
28
    public function __construct($authorized_app_id)
29
    {
30
        parent::__construct();
31
32
        $this->authorized_app_id = $authorized_app_id;
33
    }
34
35
    /**
36
     * 获取当前URL
37
     *
38
     * @return string
39
     */
40 View Code Duplication
    public static function current()
0 ignored issues
show
Coding Style introduced by
current uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $protocol = (!empty($_SERVER['HTTPS'])
43
            && $_SERVER['HTTPS'] !== 'off'
44
            || $_SERVER['SERVER_PORT'] === 443) ? 'https://' : 'http://';
45
46
        if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
47
            $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
48
        } else {
49
            $host = $_SERVER['HTTP_HOST'];
50
        }
51
52
        return $protocol . $host . $_SERVER['REQUEST_URI'];
53
    }
54
55
    /**
56
     * 生成outh URL
57
     *
58
     * @param string $to
59
     * @param string $scope
60
     * @param string $state
61
     *
62
     * @return string
63
     */
64
    public function url($to = null, $scope = 'snsapi_userinfo', $state = 'STATE')
65
    {
66
        $to !== null || $to = $this->current();
67
68
        $queryStr = [
69
            'appid'           => $this->authorized_app_id,
70
            'redirect_uri'    => $to,
71
            'response_type'   => 'code',
72
            'scope'           => $scope,
73
            'state'           => $state,
74
            'component_appid' => $this->configs->component_app_id,
75
        ];
76
77
        return self::API_URL . '?' . http_build_query($queryStr) . '#wechat_redirect';
78
    }
79
80
    /**
81
     * 直接跳转
82
     *
83
     * @param string $to
84
     * @param string $scope
85
     * @param string $state
86
     */
87
    public function redirect($to = null, $scope = 'snsapi_userinfo', $state = 'STATE')
88
    {
89
        header('Location:' . $this->url($to, $scope, $state));
90
91
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
92
    }
93
94
    /**
95
     * 获取已授权用户
96
     *
97
     * @return array $user
98
     */
99
    public function user()
0 ignored issues
show
Coding Style introduced by
user uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
100
    {
101
        if ($this->authorizedUser || !isset($_GET['state']) || (!$code = isset($_GET['code']) ? $_GET['code'] : false) && isset($_GET['state'])) {
102
            return $this->authorizedUser;
103
        }
104
105
        $user = $this->getUser($code);
106
107
        return $this->authorizedUser = $user;
108
    }
109
110
    /**
111
     * 通过授权获取用户
112
     *
113
     * @param null   $to
114
     * @param string $scope
115
     * @param string $state
116
     *
117
     * @return array
118
     */
119
    public function authorize($to = null, $scope = 'snsapi_userinfo', $state = 'STATE')
0 ignored issues
show
Coding Style introduced by
authorize uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
120
    {
121
        if (!isset($_GET['state']) && (!$code = isset($_GET['code']) ? $_GET['code'] : false)) {
122
            $this->redirect($to, $scope, $state);
123
        }
124
125
        return $this->user();
126
    }
127
128
    /**
129
     * 获取用户信息
130
     *
131
     * @param string $code
132
     *
133
     * @return array
134
     */
135
    public function getUser($code)
136
    {
137
138
        $queryStr = [
139
            'appid'                  => $this->authorized_app_id,
140
            'grant_type'             => 'authorization_code',
141
            'component_appid'        => $this->configs->component_app_id,
142
            'code'                   => $code,
143
            'component_access_token' => $this->getComponentAccessToken(),
144
        ];
145
146
        $query_data = http_build_query($queryStr);
147
148
        $res = Http::_get(self::CODE_GET_USER . '?' . $query_data);
149
150
        return $res;
151
    }
152
153
    /**
154
     * 获取大授权 用户信息
155
     *
156
     * @param        $access_token
157
     * @param        $openid
158
     * @param string $lang
159
     *
160
     * @return array
161
     */
162
    public function getUserInfo($access_token, $openid, $lang = 'zh_CN')
163
    {
164
        $queryStr = [
165
            'access_token' => $access_token,
166
            'openid'       => $openid,
167
            'lang'         => $lang,
168
        ];
169
170
        $query_data = http_build_query($queryStr);
171
172
        $res = Http::_get(self::API_USER_INFO . '?' . $query_data);
173
174
        return $res;
175
    }
176
}
177