2023-03-14 14:33:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use GuzzleHttp\Client;
|
2023-03-15 13:42:40 +00:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2023-03-14 14:33:06 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2023-03-15 13:42:40 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Http\Request;
|
2023-03-14 14:33:06 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2023-03-15 13:42:40 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-03-14 14:33:06 +00:00
|
|
|
|
|
|
|
class AuthController extends Controller
|
|
|
|
{
|
2023-03-15 13:42:40 +00:00
|
|
|
public function redirect(Request $request): RedirectResponse
|
2023-03-14 14:33:06 +00:00
|
|
|
{
|
|
|
|
$request->session()->put('state', $state = Str::random(40));
|
|
|
|
|
|
|
|
$query = http_build_query([
|
|
|
|
'client_id' => config('oauth.client_id'),
|
|
|
|
'redirect_uri' => config('oauth.callback_uri'),
|
|
|
|
'response_type' => 'code',
|
2023-05-14 10:17:59 +00:00
|
|
|
'scope' => 'user realname',
|
2023-03-14 14:33:06 +00:00
|
|
|
'state' => $state,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return redirect()->to(config('oauth.oauth_auth_url') . '?' . $query);
|
|
|
|
}
|
|
|
|
|
2023-03-15 13:42:40 +00:00
|
|
|
public function callback(Request $request): RedirectResponse
|
2023-03-14 14:33:06 +00:00
|
|
|
{
|
2023-03-15 13:42:40 +00:00
|
|
|
// $state = $request->session()->pull('state');
|
2023-03-14 14:33:06 +00:00
|
|
|
|
|
|
|
// if (strlen($state) > 0 && $state === $request->state) {
|
|
|
|
// abort(403, 'Invalid state');
|
|
|
|
// }
|
|
|
|
|
|
|
|
$http = new Client;
|
|
|
|
|
2023-03-15 13:42:40 +00:00
|
|
|
try {
|
|
|
|
$authorize = $http->post(config('oauth.oauth_token_url'), [
|
|
|
|
'form_params' => [
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => config('oauth.client_id'),
|
|
|
|
'client_secret' => config('oauth.client_secret'),
|
|
|
|
'redirect_uri' => config('oauth.callback_uri'),
|
|
|
|
'code' => $request->input('code'),
|
|
|
|
],
|
2023-07-22 00:47:03 +00:00
|
|
|
// 'verify' => false,
|
2023-03-15 13:42:40 +00:00
|
|
|
])->getBody();
|
|
|
|
} catch (GuzzleException $e) {
|
|
|
|
}
|
2023-03-14 14:33:06 +00:00
|
|
|
$authorize = json_decode($authorize);
|
|
|
|
|
2023-03-15 13:42:40 +00:00
|
|
|
try {
|
|
|
|
$oauth_user = $http->get(config('oauth.oauth_user_url'), [
|
|
|
|
'headers' => [
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
'Authorization' => 'Bearer ' . $authorize->access_token,
|
|
|
|
],
|
2023-07-22 00:47:03 +00:00
|
|
|
// 'verify' => false
|
2023-03-15 13:42:40 +00:00
|
|
|
])->getBody();
|
|
|
|
} catch (GuzzleException $e) {
|
|
|
|
}
|
2023-03-14 14:33:06 +00:00
|
|
|
$oauth_user = json_decode($oauth_user);
|
|
|
|
|
2023-03-15 13:42:40 +00:00
|
|
|
$user_sql = (new User)->where('email', $oauth_user->email);
|
2023-03-14 14:33:06 +00:00
|
|
|
$user = $user_sql->first();
|
|
|
|
|
2023-03-15 13:42:40 +00:00
|
|
|
// $api_token = null;
|
2023-03-14 14:33:06 +00:00
|
|
|
if (is_null($user)) {
|
|
|
|
$name = $oauth_user->name;
|
|
|
|
$email = $oauth_user->email;
|
|
|
|
$password = Hash::make(Str::random(40));
|
2023-03-15 13:42:40 +00:00
|
|
|
$user = (new User)->create(compact('name', 'email', 'password'));
|
2023-03-14 14:33:06 +00:00
|
|
|
$request->session()->put('auth.password_confirmed_at', time());
|
|
|
|
} else {
|
|
|
|
if ($user->name != $oauth_user->name) {
|
2023-03-15 13:42:40 +00:00
|
|
|
(new User)->where('email', $oauth_user->email)->update([
|
2023-03-14 14:33:06 +00:00
|
|
|
'name' => $oauth_user->name
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 10:17:59 +00:00
|
|
|
if (!is_null($oauth_user->real_name_verified_at)) {
|
|
|
|
$user_sql->update([
|
|
|
|
'realnamed' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Auth::guard('web')->loginUsingId($user->id, true);
|
2023-03-14 14:33:06 +00:00
|
|
|
|
|
|
|
return redirect()->route('index');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-15 13:42:40 +00:00
|
|
|
public function confirm_password(Request $request): JsonResponse|RedirectResponse
|
2023-03-14 14:33:06 +00:00
|
|
|
{
|
|
|
|
$request->validate($this->password_rules());
|
|
|
|
|
|
|
|
$request->session()->put('auth.password_confirmed_at', time());
|
|
|
|
|
|
|
|
return $request->wantsJson()
|
|
|
|
? new JsonResponse([], 204)
|
|
|
|
: redirect()->intended();
|
|
|
|
}
|
|
|
|
|
2023-03-15 13:42:40 +00:00
|
|
|
protected function password_rules(): array
|
2023-03-14 14:33:06 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'password' => 'required|password',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-03-15 13:42:40 +00:00
|
|
|
public function logout(): RedirectResponse
|
2023-03-14 14:33:06 +00:00
|
|
|
{
|
2023-05-14 10:17:59 +00:00
|
|
|
Auth::guard('web')->logout();
|
2023-03-14 14:33:06 +00:00
|
|
|
return redirect()->route('index');
|
|
|
|
}
|
|
|
|
}
|