Naver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 108
rs 10
c 1
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A openid() 0 4 1
A getRedirectUrl() 0 13 2
A call() 0 13 1
A userInfo() 0 15 2
A getUserInfo() 0 16 4
A parseToken() 0 7 2
1
<?php
2
/**
3
 * naver开放平台  https://developers.naver.com/
4
 * api接口文档
5
 *      https://developers.naver.com/docs/common/openapiguide/
6
 * 申请的时候请选择
7
 */
8
namespace tinymeng\OAuth2\Gateways;
9
10
use tinymeng\OAuth2\Connector\Gateway;
11
use tinymeng\OAuth2\Exception\OAuthException;
12
use tinymeng\OAuth2\Helper\ConstCode;
13
use tinymeng\OAuth2\Helper\Str;
14
15
/**
16
 * Class Naver
17
 * @package tinymeng\OAuth2\Gateways
18
 * @Author: TinyMeng <[email protected]>
19
 * @Created: 2021/03/01
20
 */
21
class Naver extends Gateway
22
{
23
    const API_BASE            = 'https://openapi.naver.com/v1/';
24
    protected $AuthorizeURL   = 'https://nid.naver.com/oauth2.0/authorize';
25
    protected $AccessTokenURL = 'https://nid.naver.com/oauth2.0/token';
26
27
    /**
28
     * 得到跳转地址
29
     */
30
    public function getRedirectUrl()
31
    {
32
        //存储state
33
        $this->saveState();
34
        //登录参数
35
        $params = [
36
            'response_type' => $this->config['response_type'],
37
            'client_id'     => $this->config['app_id'],
38
            'redirect_uri'  => $this->config['callback'],
39
            'scope'         => $this->config['scope'],
40
            'state'         => $this->config['state'] ?: Str::random(),
41
        ];
42
        return $this->AuthorizeURL . '?' . http_build_query($params);
43
    }
44
45
    /**
46
     * 获取当前授权用户的openid标识
47
     */
48
    public function openid()
49
    {
50
        $result = $this->getUserInfo();
51
        return $result['userId'];
52
    }
53
54
    /**
55
     * 获取格式化后的用户信息
56
     */
57
    public function userInfo()
58
    {
59
        $result = $this->getUserInfo();
60
61
        $userInfo = array(
62
            'open_id'  => $this->token['access_token'],
63
            'union_id'  => $result['id'],
64
            'channel' => ConstCode::TYPE_NAVER,
65
            'email'=> $result['email'] ?? '',
66
            'nickname'=> $result['nickname'] ?? '',
67
            'gender'  => isset($result['gender']) ? $this->getGender($result['gender']) : ConstCode::GENDER,
68
            'avatar'  => $result['profile_image'] ?? '',
69
            'native'   => $result,
70
        );
71
        return $userInfo;
72
    }
73
74
    /**
75
     * 获取原始接口返回的用户信息
76
     */
77
    public function getUserInfo()
78
    {
79
        if($this->type == 'app'){//App登录
80
            if(!isset($_REQUEST['access_token']) ){
81
                throw new OAuthException("Naver APP登录 需要传输access_token参数! ");
82
            }
83
            $this->token['token_type'] = 'Bearer';
84
            $this->token['access_token'] = $_REQUEST['access_token'];
85
        }else {
86
            $this->getToken();
87
        }
88
        $data = $this->call('nid/me', $this->token, 'GET');
89
        if (isset($data['error'])) {
90
            throw new OAuthException($data['error_description']);
91
        }
92
        return $data['response'];
93
    }
94
95
    /**
96
     * 发起请求
97
     *
98
     * @param string $api
99
     * @param array $params
100
     * @param string $method
101
     * @return array
102
     */
103
    private function call($api, $params = [], $method = 'GET')
104
    {
105
        $method  = strtoupper($method);
106
        $request = [
107
            'method' => $method,
108
            'uri'    => self::API_BASE . $api,
109
        ];
110
111
        $headers = ['Authorization' => $this->token['token_type'] . ' ' . $this->token['access_token']];
112
113
        $data = $this->$method($request['uri'], $params, $headers);
114
115
        return json_decode($data, true);
116
    }
117
118
    /**
119
     * 解析access_token方法请求后的返回值
120
     * @param string $token 获取access_token的方法的返回值
121
     */
122
    protected function parseToken($token)
123
    {
124
        $token = json_decode($token, true);
125
        if (isset($token['error'])) {
126
            throw new OAuthException($token['error_description']);
127
        }
128
        return $token;
129
    }
130
131
}
132