Skip to content

Commit

Permalink
fix oauth user info response
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Apr 5, 2024
1 parent ff1b88a commit 8c3b05b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
43 changes: 26 additions & 17 deletions app/Http/Controllers/OauthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace App\Http\Controllers;

use App\Http\Resources\UserResource;
use App\Models\OauthClient;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
Expand All @@ -10,31 +11,35 @@

class OauthController extends Controller
{
private int $clientId = 3;
private int $clientId = 8;
private string $baseUri;

public function __construct()
{
$this->baseUri = getSchemeAndHttpHost();
}
public function Redirect(Request $request)
private ?OauthClient $client = null;

// public function __construct()
// {
// $this->baseUri = getSchemeAndHttpHost();
//
// $this->client = OauthClient::query()->find($this->clientId);
// }
public function redirect(Request $request)
{
$request->session()->put('state', $state = Str::random(40));
// $request->session()->put('state', $state = Str::random(40));

$query = http_build_query([
'client_id' => $this->clientId,
'redirect_uri' => $this->baseUri."/oauth/callback",
'client_id' => $this->client->id,
'redirect_uri' => $this->client->redirect,
'response_type' => 'code',
'scope' => '',
'state' => $state,
'prompt' => 'none', // "none", "consent", or "login"
// 'state' => $state,
// 'prompt' => 'none', // "none", "consent", or "login"
]);

return redirect($this->baseUri.'/oauth/authorize?'.$query);

}

public function Callback(Request $request)
public function callback(Request $request)
{
// $state = $request->session()->pull('state');
//
Expand All @@ -43,23 +48,27 @@ public function Callback(Request $request)
// \InvalidArgumentException::class
// );

$clientInfo = Client::query()->findOrFail($this->clientId);
$response = Http::asForm()->post($this->baseUri.'/oauth/token', [
'grant_type' => 'authorization_code',
'client_id' => $this->clientId,
'client_secret' => $clientInfo->secret,
'redirect_uri' => $this->baseUri.'/oauth/callback',
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
'redirect_uri' => $this->client->redirect,
'code' => $request->code,
]);

return $response->json();
}

public function debug(Request $request)
{
dd($request->all());
}


public function userInfo(): array
{
$user = Auth::user();
$resource = new UserResource($user);
return $resource->response()->getData(true);
return $resource->response()->getData(true)['data'];
}
}
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@

Route::group(['prefix' => 'oauth'], function () {
Route::get("user-info", [\App\Http\Controllers\OauthController::class, 'userInfo'])->name("oauth.user_info")->middleware('auth:api');
// Route::get('redirect', [\App\Http\Controllers\OauthController::class, 'Redirect']);
// Route::get('callback', [\App\Http\Controllers\OauthController::class, 'Callback']);
// Route::get('redirect', [\App\Http\Controllers\OauthController::class, 'redirect']);
// Route::get('callback', [\App\Http\Controllers\OauthController::class, 'callback']);
});

0 comments on commit 8c3b05b

Please sign in to comment.