|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Puock\Theme\oauth; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | + |
| 7 | +class RainbowOAuth |
| 8 | +{ |
| 9 | + public $appid; |
| 10 | + |
| 11 | + public $appSecret; |
| 12 | + |
| 13 | + public $callbackUrl; |
| 14 | + |
| 15 | + public $state; |
| 16 | + |
| 17 | + public $result = []; |
| 18 | + |
| 19 | + public $accessToken; |
| 20 | + |
| 21 | + public $openid; |
| 22 | + |
| 23 | + |
| 24 | + public function __construct($appid = null, $appSecret = null, $callbackUrl = null) |
| 25 | + { |
| 26 | + $this->appid = trim((string)($appid ?? '')); |
| 27 | + $this->appSecret = trim((string)($appSecret ?? '')); |
| 28 | + $this->callbackUrl = trim((string)($callbackUrl ?? '')); |
| 29 | + } |
| 30 | + |
| 31 | + public function getAuthUrl($callbackUrl = null, $state = null, $scope = null) |
| 32 | + { |
| 33 | + $this->state = $state ?: md5(uniqid('', true)); |
| 34 | + |
| 35 | + $redirectUri = $callbackUrl ?: $this->callbackUrl; |
| 36 | + if (empty($redirectUri)) |
| 37 | + { |
| 38 | + throw new InvalidArgumentException('callbackUrl 不能为空'); |
| 39 | + } |
| 40 | + |
| 41 | + // Append state for CSRF validation |
| 42 | + $redirectUriWithState = add_query_arg('state', $this->state, $redirectUri); |
| 43 | + |
| 44 | + $socialType = $this->getSocialType(); |
| 45 | + |
| 46 | + $data = $this->request([ |
| 47 | + 'act' => 'login', |
| 48 | + 'appid' => $this->appid, |
| 49 | + 'appkey' => $this->appSecret, |
| 50 | + 'type' => $socialType, |
| 51 | + 'redirect_uri' => $redirectUriWithState, |
| 52 | + ]); |
| 53 | + |
| 54 | + $url = (string)($data['url'] ?? ''); |
| 55 | + if (empty($url)) |
| 56 | + { |
| 57 | + throw new InvalidArgumentException('彩虹聚合登录返回的跳转地址为空'); |
| 58 | + } |
| 59 | + |
| 60 | + return $url; |
| 61 | + } |
| 62 | + |
| 63 | + public function getAccessToken($storeState = '', $code = null, $state = null) |
| 64 | + { |
| 65 | + $state = $state ?? ($_GET['state'] ?? ''); |
| 66 | + if ((string)$storeState !== (string)$state) |
| 67 | + { |
| 68 | + throw new InvalidArgumentException('state验证失败'); |
| 69 | + } |
| 70 | + |
| 71 | + $code = $code ?? ($_GET['code'] ?? ''); |
| 72 | + if (empty($code)) |
| 73 | + { |
| 74 | + throw new InvalidArgumentException('缺少回调参数 code'); |
| 75 | + } |
| 76 | + |
| 77 | + $socialType = $this->getSocialType(); |
| 78 | + |
| 79 | + $data = $this->request([ |
| 80 | + 'act' => 'callback', |
| 81 | + 'appid' => $this->appid, |
| 82 | + 'appkey' => $this->appSecret, |
| 83 | + 'type' => $socialType, |
| 84 | + 'code' => $code, |
| 85 | + ]); |
| 86 | + |
| 87 | + $this->result = $data; |
| 88 | + $this->accessToken = (string)($data['access_token'] ?? ''); |
| 89 | + $this->openid = (string)($data['social_uid'] ?? ''); |
| 90 | + |
| 91 | + if (empty($this->openid)) |
| 92 | + { |
| 93 | + throw new InvalidArgumentException('彩虹聚合登录返回 social_uid 为空'); |
| 94 | + } |
| 95 | + |
| 96 | + return $this->accessToken; |
| 97 | + } |
| 98 | + |
| 99 | + public function getUserInfo($accessToken = null) |
| 100 | + { |
| 101 | + if (!empty($this->result)) |
| 102 | + { |
| 103 | + return $this->result; |
| 104 | + } |
| 105 | + |
| 106 | + if (empty($this->openid)) |
| 107 | + { |
| 108 | + throw new InvalidArgumentException('openid 为空,无法查询用户信息'); |
| 109 | + } |
| 110 | + |
| 111 | + $socialType = $this->getSocialType(); |
| 112 | + |
| 113 | + $data = $this->request([ |
| 114 | + 'act' => 'query', |
| 115 | + 'appid' => $this->appid, |
| 116 | + 'appkey' => $this->appSecret, |
| 117 | + 'type' => $socialType, |
| 118 | + 'social_uid' => $this->openid, |
| 119 | + ]); |
| 120 | + |
| 121 | + $this->result = $data; |
| 122 | + return $this->result; |
| 123 | + } |
| 124 | + |
| 125 | + private function getSocialType(): string |
| 126 | + { |
| 127 | + $allow = ['qq', 'wx', 'alipay', 'sina', 'baidu', 'huawei', 'xiaomi', 'douyin', 'bilibili', 'dingtalk']; |
| 128 | + |
| 129 | + $providerType = sanitize_key($_GET['type'] ?? ''); |
| 130 | + if (in_array($providerType, $allow, true)) { |
| 131 | + return $providerType; |
| 132 | + } |
| 133 | + |
| 134 | + if (str_starts_with($providerType, 'ccy_')) |
| 135 | + { |
| 136 | + $t = substr($providerType, strlen('ccy_')); |
| 137 | + if (in_array($t, $allow, true)) |
| 138 | + { |
| 139 | + return $t; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return 'qq'; |
| 144 | + } |
| 145 | + |
| 146 | + private function request(array $query): array |
| 147 | + { |
| 148 | + if (empty($this->appid) || empty($this->appSecret)) |
| 149 | + { |
| 150 | + throw new InvalidArgumentException('彩虹聚合登录 AppID/AppKey 未配置'); |
| 151 | + } |
| 152 | + |
| 153 | + $apiBase = (string)(\pk_get_option('oauth_ccy_api') ?: 'https://u.cccyun.cc'); |
| 154 | + $apiBase = rtrim($apiBase, '/'); |
| 155 | + if (!str_starts_with($apiBase, 'http://') && !str_starts_with($apiBase, 'https://')) { |
| 156 | + throw new InvalidArgumentException('接口地址格式不正确'); |
| 157 | + } |
| 158 | + |
| 159 | + $apiUrl = $apiBase . '/connect.php'; |
| 160 | + $url = add_query_arg($query, $apiUrl); |
| 161 | + |
| 162 | + $resp = wp_remote_get($url, [ |
| 163 | + 'timeout' => 15, |
| 164 | + 'redirection' => 0, |
| 165 | + ]); |
| 166 | + |
| 167 | + if (is_wp_error($resp)) |
| 168 | + { |
| 169 | + throw new InvalidArgumentException('请求彩虹聚合登录失败:' . $resp->get_error_message()); |
| 170 | + } |
| 171 | + |
| 172 | + $body = wp_remote_retrieve_body($resp); |
| 173 | + $data = json_decode($body, true); |
| 174 | + if (!is_array($data)) |
| 175 | + { |
| 176 | + throw new InvalidArgumentException('彩虹聚合登录返回数据解析失败'); |
| 177 | + } |
| 178 | + |
| 179 | + $code = (int)($data['code'] ?? -1); |
| 180 | + if ($code !== 0) |
| 181 | + { |
| 182 | + $msg = (string)($data['msg'] ?? '请求失败'); |
| 183 | + throw new InvalidArgumentException($msg); |
| 184 | + } |
| 185 | + |
| 186 | + return $data; |
| 187 | + } |
| 188 | +} |
0 commit comments