Skip to content

Commit

Permalink
feat: add gohighlevel provider (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
atymic authored Jan 13, 2025
1 parent e5687f2 commit e8face1
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/GoHighLevel/GoHighLevelExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SocialiteProviders\GoHighLevel;

use SocialiteProviders\Manager\SocialiteWasCalled;

class GoHighLevelExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled): void
{
$socialiteWasCalled->extendSocialite('gohighlevel', Provider::class);
}
}
83 changes: 83 additions & 0 deletions src/GoHighLevel/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace SocialiteProviders\GoHighLevel;

use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

class Provider extends AbstractProvider
{
public const IDENTIFIER = 'GOHIGHLEVEL';

protected $scopeSeparator = ' ';

protected $scopes = ['users.readonly'];

protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase('https://marketplace.leadconnectorhq.com/oauth/chooselocation', $state);
}

protected function getTokenUrl(): string
{
return 'https://services.leadconnectorhq.com/oauth/token';
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$userId = $this->credentialsResponseBody['userId'] ?? null;

if (!$userId) {
return null;
}

$response = $this->getHttpClient()->get('https://services.leadconnectorhq.com/users/' . $userId);

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

/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'name' => Arr::get($user, 'name'),
'email' => Arr::get($user, 'email'),
'id' => Arr::get($user, 'id'),
'nickname' => null,
'avatar' => null,
]);
}

/**
* Acquire a new access token using the refresh token.
*
* @see https://highlevel.stoplight.io/docs/integrations/00d0c0ecaa369-get-access-token
*
* @param string $refreshToken
*
* @return array
*/
public function refreshToken($refreshToken)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => [
'Accept' => 'application/json',
],
RequestOptions::FORM_PARAMS => [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
],
]);

return json_decode((string) $response->getBody(), true);
}
}
64 changes: 64 additions & 0 deletions src/GoHighLevel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# GoHighLevel

```bash
composer require socialiteproviders/gohighlevel
```

## Installation & Basic Usage

Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below.

### Add configuration to `config/services.php`

```php
'gohighlevel' => [
'client_id' => env('GOHIGHLEVEL_CLIENT_ID'),
'client_secret' => env('GOHIGHLEVEL_CLIENT_SECRET'),
'redirect' => env('GOHIGHLEVEL_REDIRECT_URI')
],
```

### Add provider event listener

#### Laravel 11+

In Laravel 11, the default `EventServiceProvider` provider was removed. Instead, add the listener using the `listen` method on the `Event` facade, in your `AppServiceProvider` `boot` method.

* Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.

```php
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
$event->extendSocialite('gohighlevel', \SocialiteProviders\GoHighLevel\Provider::class);
});
```
<details>
<summary>
Laravel 10 or below
</summary>
Configure the package's listener to listen for `SocialiteWasCalled` events.

Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions.

```php
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
\SocialiteProviders\GoHighLevel\GoHighLevelExtendSocialite::class.'@handle',
],
];
```
</details>

### Usage

You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed):

```php
return Socialite::driver('gohighlevel')->redirect();
```

### Returned User fields

- ``id``
- ``name``
- ``email``
33 changes: 33 additions & 0 deletions src/GoHighLevel/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "socialiteproviders/gohighlevel",
"description": "GoHighLevel OAuth2 Provider for Laravel Socialite",
"license": "MIT",
"keywords": [
"gohighlevel",
"laravel",
"oauth",
"provider",
"socialite"
],
"authors": [
{
"name": "atymic",
"email": "[email protected]"
}
],
"support": {
"issues": "https://github.com/socialiteproviders/providers/issues",
"source": "https://github.com/socialiteproviders/providers",
"docs": "https://socialiteproviders.com/gohighlevel"
},
"require": {
"php": "^8.1",
"ext-json": "*",
"socialiteproviders/manager": "^4.8"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\GoHighLevel\\": ""
}
}
}

0 comments on commit e8face1

Please sign in to comment.