0
votes

I'm trying to use the authorization code grant to log third-party clients in passport Oauth server. when i use password grant its work properly and passport give me token and every thing is okey. in third party app i add this code:

Route::get('/redirect', function (Request $request) {
    $query = http_build_query([
        'client_id' => 3,
        'redirect_uri' => 'http://127.0.0.1:8001/callback',
        'response_type' => 'code',
        'scope' => '',
        'state' => \Str::random(40),
    ]);

    return redirect('http://127.0.0.1:8000/oauth/authorize?' . $query);
});

Route::get('/callback', function (Request $request) {
    $response = (new GuzzleHttp\Client)->post('http://127.0.0.1:8000/oauth/token', [
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => 3, 
            'client_secret' => 'Ux6p201tRFrPQ5lLZI1RMce1GYD6qTJ9Qvj7nM2Z',
            'redirect_uri' => 'http://127.0.0.1:8001/callback',
            'code' => $request->code,
        ]
    ]);
    session()->put('token', json_decode((string)$response->getBody(), true));

    return redirect('/todos');
});

Route::get('/todos', function () {
    $response = (new GuzzleHttp\Client)->get('http://127.0.0.1:8000/api/todos', [
        'headers' => [
            'Authorization' => 'Bearer ' . session()->get('token.access_token')
        ]
    ]);

    return json_decode((string)$response->getBody(), true);
});

in oauth server i create client with id:3 secret:Ux6p201tRFrPQ5lLZI1RMce1GYD6qTJ9Qvj7nM2Z callback:'http://127.0.0.1:8001/callback'

but when i create request to /redirect from third party app i get: unauthenticated 401 and redirect to login also i try add

  Passport::$ignoreCsrfToken = true;

in AuthServiceProvider. but yet still not work. anybody can help me please?

1
try AuthServiceProvider file add Passport::$ignoreCsrfToken = true;Sehdev
hi @Sehdev and tanx for answer. i added this but steal i get redirect to login and unauthenticated responsevahid
Are you still getting the error?Sehdev
yes. and redirect to loginvahid
when i use postman to make request to above route and add bearer token its solve.but i want use for third party app (wordpress sso)vahid

1 Answers

0
votes

Similar issue is already listed on Laravel Passport github repository

To resolve this error add Passport::$ignoreCsrfToken = true; in AuthServiceProvider file

https://github.com/laravel/passport/issues/839