Skip to content

Commit

Permalink
add Clover OAuth 2 provider (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
macbookandrew authored Feb 28, 2024
0 parents commit 248741d
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
13 changes: 13 additions & 0 deletions CloverExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SocialiteProviders\Clover;

use SocialiteProviders\Manager\SocialiteWasCalled;

class CloverExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite(Provider::IDENTIFIER, Provider::class);
}
}
103 changes: 103 additions & 0 deletions Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace SocialiteProviders\Clover;

use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

class Provider extends AbstractProvider
{
/**
* Unique Provider Identifier.
*/
const IDENTIFIER = 'clover';

/**
* {@inheritdoc}
*/
protected $scopes = [];

/**
* Indicates if the session state should be utilized.
*
* @var bool
*/
protected $stateless = true;

public static function additionalConfigKeys()
{
return [
'environment',
];
}

protected function getApiDomain(): string
{
return match ($this->getConfig('environment')) {
'sandbox' => 'sandbox.dev.clover.com',
default => 'www.clover.com',
};
}

/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
sprintf('https://%s/oauth/v2/authorize', $this->getApiDomain()),
$state
);
}

/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
$domain = match ($this->getConfig('environment')) {
'sandbox' => 'apisandbox.dev.clover.com',
default => 'api.clover.com',
};

return sprintf('https://%s/oauth/token', $domain);
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$requestParams = str(request()->fullUrl())
->after('?')
->explode('&')
->mapWithKeys(fn (string $keyAndValue) => [str($keyAndValue)->before('=')->toString() => str($keyAndValue)->after('=')->toString()]);

$response = $this->getHttpClient()->get(sprintf(
'https://%s/v3/merchants/%s/employees/%s',
$this->getApiDomain(),
$requestParams['merchant_id'],
$requestParams['employee_id'],
), [
'headers' => [
'Authorization' => 'Bearer '.$token,
],
]);

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

/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['id'],
'nickname' => $user['name'],
'name' => $user['name'],
'email' => $user['email'],
'avatar' => null,
]);
}
}
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Clover

```bash
composer require socialiteproviders/clover
```

## Installation & Basic Usage

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

Ensure the app has permission to read employees.

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

```php
'clover' => [
'client_id' => env('CLOVER_CLIENT_ID'),
'client_secret' => env('CLOVER_CLIENT_SECRET'),
'redirect' => env('CLOVER_REDIRECT_URI')
'environment' => env('CLOVER_ENVIRONMENT', 'production'),
],
```

### Add provider event listener

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\\Clover\\CloverExtendSocialite@handle',
],
];
```

### 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('clover')->redirect();
```

Presumably you are using this OAuth provider in order to retrieve an API token for calling other API endpoints.

The user includes a `token` property that you can use to retrieve the API token like this:

```php
Route::get('clover/auth/callback', function () {
$user = Socialite::driver('clover')->user();

// Save this token somewhere for other use.
$token = $user->token;
});
```
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "socialiteproviders/clover",
"description": "Clover OAuth2 Provider for Laravel Socialite",
"license": "MIT",
"authors": [
{
"name": "macbookandrew",
"homepage": "https://andrewrminion.com"
}
],
"require": {
"php": "^8.1",
"ext-json": "*",
"socialiteproviders/manager": "^4.0"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\Clover\\": ""
}
}
}

0 comments on commit 248741d

Please sign in to comment.