Danlog's avatar
Level 3

Issue token from specific client Passport

Hey guys, struggling a bit with creating a token from a client id. Any ideas on what I'm doing wrong?

0 likes
5 replies
LaryAI's avatar
Level 58

To issue a token from a specific client using Laravel Passport, you need to ensure that the client credentials are correct and that the request to the /oauth/token endpoint is properly formatted. Here are a few things to check and a solution to help you troubleshoot the issue:

  1. Ensure Client Credentials are Correct: Double-check that the client_id and client_secret are correct and match the ones stored in your oauth_clients table.

  2. Environment Configuration: Make sure that APP_URL in your .env file is correctly set to the base URL of your application.

  3. Guzzle HTTP Client: Ensure that the Guzzle HTTP client is properly installed and configured in your Laravel application.

  4. Error Handling: Improve error handling to get more detailed information about what might be going wrong.

Here's a refined version of your code with some improvements:

Key Points:

  • Error Handling: The code now includes more detailed error handling for Guzzle exceptions, which can help you diagnose issues with the HTTP request.
  • Environment Variables: Ensure that APP_URL is correctly set in your .env file.
  • Client Credentials: Double-check the client_id and client_secret values.

By following these steps and using the improved error handling, you should be able to identify and resolve the issue with generating a token from a specific client.

martinbean's avatar

Any ideas on what I'm doing wrong?

@danlog Yes: you’re using Passport completely wrong.

Why are you manually creating and returning tokens from your own endpoint? Passport is an OAuth server. You’re meant to obtaining tokens using the built-in /oauth/token endpoint. Not weirdly proxying the endpoint with your own endpoint.

martinbean's avatar

@Danlog A user needs to log in to able to approve the OAuth token request. Otherwise any user could issue a token for any other user.

Danlog's avatar
Level 3

@martinbean Yes. You're absolutely right.

I thought, though, that there was a way to work it with the api Middleware, but Laravel forces it on the web one. That's the issue with me. Hence why sessions are quite essential for such process.

I tried workarounds, but it always tries to send me to the web routes. I managed to somehow make it work all the way up to the authorization page, but that redirdct is always then trying to send me to the login page for web again.

I am contemplating to use Sanctum for the project since it is easier to setup, has something similar to scopes, and offers stateful sessions, which I can use for passkeys.

This "auth service" is just aimed at first-party apps.

Please or to participate in this conversation.