Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send additional form data when requesting access_token with OAuth2 #450

Open
77media-creations opened this issue Aug 28, 2024 · 1 comment

Comments

@77media-creations
Copy link

Hi there,

Great package first of all. I have recently started to implement in my laravel package.

In this particular project, I'm trying to integrate external APIs (Machine to Machine) with OAuth2 authentication.

This service requires username: xxx and password: xxx along with client_id: xxx and 'client_secret: xxx**passed in the request body** when requesting anaccess_token`.

I'm not sure where to pass these additional variables within the connector class.

So far, I have done the following:

class ServiceConnector extends Connector
{
    use ClientCredentialsGrant;

    public function __construct(public array $connectionData)
    {
    }

    /**
     * The Base URL of the API.
     */
    public function resolveBaseUrl(): string
    {
        return $this->connectionData['base_url'];
    }

    /**
     * The OAuth2 configuration
     */
    protected function defaultOauthConfig(): OAuthConfig
    {
        return OAuthConfig::make()
            ->setClientId($this->connectionData['client_id'])
            ->setClientSecret($this->connectionData['client_secret'])
            ->setRedirectUri($this->connectionData['base_url'])
            ->setDefaultScopes($this->connectionData['scopes'])
            ->setTokenEndpoint($this->connectionData['token_url'])
            ->setRequestModifier(function (Request $request) {
                if ($request instanceof GetClientCredentialsTokenRequest) {
                    $request->body()->set([
                        'username' => $this->connectionData['username'],
                        'password' => $this->connectionData['password'],
                        'grant_type' => 'password',
                    ]);
                }
            });
    }

}

It seems like the variables passed via the setRequestModifier() method are not correctly going with the request as form body.

Am I missing something somewhere or this type of authentication has a different approach?

Any help is appreciated.

@use-the-fork
Copy link

use-the-fork commented Oct 10, 2024

@77media-creations In case you are still stuck on this the solution for me was to modify the code like this:

  ->setRequestModifier(function (Request $request) {
      if ($request instanceof GetClientCredentialsTokenRequest) {
          $request->body()->set([
                                    'username' => $this->connectionData['username'],
                                    'password' => $this->connectionData['password'],
                                    'client_id' => $this->connectionData['clientId'],
                                    'client_secret' => $this->connectionData['client_secret'],
                                    'grant_type' => 'password',
                                ]);
      }
  });

The client_id and client_secret needed to be added and everything worked from there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants