|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Admin; |
| 4 | + |
| 5 | + |
| 6 | +use App\Events\ThreeLogin; |
| 7 | +use Illuminate\Http\Request; |
| 8 | + |
| 9 | +use GuzzleHttp\Client; |
| 10 | +use Illuminate\Support\Facades\Cache; |
| 11 | +use Laravel\Socialite\Facades\Socialite; |
| 12 | + |
| 13 | +class OauthController extends Controller |
| 14 | +{ |
| 15 | + public $github_id = '52c2e3cc172fee5bccd7'; |
| 16 | + public $github_secret = 'fa5a77a1ddbea1728fc1c33bb53eef6e8b1e5757'; |
| 17 | + public $github_callback = 'http://localhost:8006/oauth/github/callback'; |
| 18 | + |
| 19 | + public $gitee_id = '9fb826c130d2c709f579a07ac75a0961055fdb756e8c2107202f50bb634deb88'; |
| 20 | + public $gitee_secret = 'bdaeab8f1427802f5f30b4e3b524ec513ea143648b1d5c54b5dae8caa7865489'; |
| 21 | + public $gitee_callback = "https://lv6.halian.net/api/admin/oauth/gitee"; |
| 22 | + /** |
| 23 | + * Display a listing of the resource. |
| 24 | + * |
| 25 | + * @return \Illuminate\Http\Response |
| 26 | + */ |
| 27 | + |
| 28 | + public $uuid = null; |
| 29 | + |
| 30 | + public function test1() |
| 31 | + { |
| 32 | + global $uuid; |
| 33 | + $uuid = request('uuid'); |
| 34 | + $uuid = $uuid; |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + public function test2() |
| 39 | + { |
| 40 | + dd($this->uuid); |
| 41 | + } |
| 42 | + |
| 43 | + public function redirectToProvider() |
| 44 | + { |
| 45 | + Cache::set('github_url', request('address')); |
| 46 | + return Socialite::driver('github')->stateless()->redirect(); |
| 47 | + } |
| 48 | + |
| 49 | + public function getUserInfoByGithub() |
| 50 | + { |
| 51 | + $result = Socialite::driver('github')->stateless()->user(); |
| 52 | + $token = $result->token; |
| 53 | + $user = $result->user; |
| 54 | + Cache::set($token, $user['id']); |
| 55 | + $url = Cache::get('github_url').'?token='.$token; |
| 56 | + Header("HTTP/1.1 303 See Other"); |
| 57 | + header("Location: ".$url); |
| 58 | + exit(); |
| 59 | + } |
| 60 | + |
| 61 | + public function redirectToGitee() |
| 62 | + { |
| 63 | + Cache::set('gitee_url', request('address')); |
| 64 | + $uuid = request('uuid'); |
| 65 | + $redirect = $this->gitee_callback."?uuid=$uuid"; |
| 66 | + $url = "https://gitee.com/oauth/authorize?client_id=".$this->gitee_id."&redirect_uri=".$redirect."&response_type=code"; |
| 67 | + Header("HTTP/1.1 303 See Other"); |
| 68 | + header("Location: $url"); |
| 69 | + exit(); |
| 70 | + } |
| 71 | + |
| 72 | + public function getUserInfoByGitee() |
| 73 | + { |
| 74 | + |
| 75 | + $code = request('code'); |
| 76 | + $uuid = request('uuid'); |
| 77 | +// $_SERVER['REDIRECT_QUERY_STRING'] = "code=$code"; |
| 78 | +// $_SERVER['QUERY_STRING'] = "code=$code"; |
| 79 | +// $_SERVER['REQUEST_URI'] = "/api/admin/oauth/gitee?code=$code"; |
| 80 | +// request()->except('uuid'); |
| 81 | + $client = new Client(); |
| 82 | + // 根据code取得令牌 |
| 83 | + $response = $client->request("POST", "https://gitee.com/oauth/token", [ |
| 84 | + "form_params" => [ |
| 85 | + 'grant_type' => 'authorization_code', |
| 86 | + 'code' => $code, |
| 87 | + 'client_id' => $this->gitee_id, |
| 88 | + 'redirect_uri' => $this->gitee_callback."?uuid=$uuid", |
| 89 | + 'client_secret' => $this->gitee_secret |
| 90 | + ]]); |
| 91 | + $result = json_decode($response->getBody()->getContents(), true); |
| 92 | + $access_token = $result['access_token']; |
| 93 | + // 根据令牌取得个人信息,并存储于缓存 |
| 94 | + $response = $client->request("GET", "https://gitee.com/api/v5/user?access_token=$access_token"); |
| 95 | + $result = json_decode($response->getBody()->getContents(), true); |
| 96 | + Cache::set("gitee_$access_token", $result['id']); |
| 97 | + Header("HTTP/1.1 303 See Other"); |
| 98 | + broadcast(new ThreeLogin('gitee', $uuid)); |
| 99 | + $url = Cache::get('gitee_url')."?token=$access_token"; |
| 100 | + header("Location: ".$url); |
| 101 | + exit(); |
| 102 | + } |
| 103 | + |
| 104 | + public function index() |
| 105 | + { |
| 106 | + // |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Store a newly created resource in storage. |
| 111 | + * |
| 112 | + * @param \Illuminate\Http\Request $request |
| 113 | + * @return \Illuminate\Http\Response |
| 114 | + */ |
| 115 | + public function store(Request $request) |
| 116 | + { |
| 117 | + // |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Display the specified resource. |
| 122 | + * |
| 123 | + * @param int $id |
| 124 | + * @return \Illuminate\Http\Response |
| 125 | + */ |
| 126 | + public function show($id) |
| 127 | + { |
| 128 | + // |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Update the specified resource in storage. |
| 133 | + * |
| 134 | + * @param \Illuminate\Http\Request $request |
| 135 | + * @param int $id |
| 136 | + * @return \Illuminate\Http\Response |
| 137 | + */ |
| 138 | + public function update(Request $request, $id) |
| 139 | + { |
| 140 | + // |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Remove the specified resource from storage. |
| 145 | + * |
| 146 | + * @param int $id |
| 147 | + * @return \Illuminate\Http\Response |
| 148 | + */ |
| 149 | + public function destroy($id) |
| 150 | + { |
| 151 | + // |
| 152 | + } |
| 153 | +} |
0 commit comments