Skip to content

Commit

Permalink
feat: implement Clerk provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignacio José Canó Cabral committed Feb 17, 2024
1 parent 5b04054 commit 7e868e8
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 0 deletions.
1 change: 1 addition & 0 deletions monorepo-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ parameters:
src/CampaignMonitor: '[email protected]:SocialiteProviders/CampaignMonitor.git'
src/Cheddar: '[email protected]:SocialiteProviders/Cheddar.git'
src/ClaveUnica: '[email protected]:SocialiteProviders/ClaveUnica.git'
src/Clerk: '[email protected]:SocialiteProviders/Clerk.git'
src/Cognito: '[email protected]:SocialiteProviders/Cognito.git'
src/Coinbase: '[email protected]:SocialiteProviders/Coinbase.git'
src/ConstantContact: '[email protected]:SocialiteProviders/ConstantContact.git'
Expand Down
18 changes: 18 additions & 0 deletions src/Clerk/ClerkExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SocialiteProviders\Clerk;

use SocialiteProviders\Manager\SocialiteWasCalled;

class ClerkExtendSocialite
{
/**
* Register the provider.
*
* @param \SocialiteProviders\Manager\SocialiteWasCalled $socialiteWasCalled
*/
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite('clerk', Provider::class);
}
}
93 changes: 93 additions & 0 deletions src/Clerk/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace SocialiteProviders\Clerk;

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

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

/**
* {@inheritdoc}
*/
protected $scopes = ['profile','email'];

/**
* {@inheritdoc}
*/
protected $scopeSeparator = ' ';

/**
* {@inheritdoc}
*/
protected function getBaseUrl()
{
return $this->getConfig('base_url');
}

/**
* {@inheritdoc}
*/
public static function additionalConfigKeys()
{
return ['base_url'];
}

/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase("{$this->getBaseUrl()}/oauth/authorize", $state);
}

/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return "{$this->getBaseUrl()}/oauth/token";
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get("{$this->getBaseUrl()}/oauth/userinfo", [
'headers' => [
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody(), true);
}

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

/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code'
]);
}
}
63 changes: 63 additions & 0 deletions src/Clerk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Clerk

This is a provider for [Clerk](https://clerk.com/)

```bash
composer require socialiteproviders/clerk
```

## 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
'clerk' => [
'client_id' => env('CLERK_CLIENT_ID'),
'client_secret' => env('CLERK_CLIENT_SECRET'),
'redirect' => env('CLERK_CALLBACK_URL'),
'base_url' => env('CLERK_BASE_URL'),
],
```

### Add base URL to `.env`

Clerk provides a customer URL for your different projects for this reason you need to provide a base_url

```bash
CLERK_BASE_URL=https://example.clerk.accounts.dev
```

### 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\Clerk\ClerkExtendSocialite::class.'@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('clerk')->redirect();
```

@TODO

### Returned User fields

- `id`
- `nickname`
- `name`
- `email`
- `avatar`
21 changes: 21 additions & 0 deletions src/Clerk/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "socialiteproviders/clerk",
"description": "Clerk OAuth2 Provider for Laravel Socialite",
"license": "MIT",
"authors": [
{
"name": "Ignacio Cano",
"email": "[email protected]"
}
],
"require": {
"php": "^8.0",
"ext-json": "*",
"socialiteproviders/manager": "^4.0"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\Clerk\\": ""
}
}
}

0 comments on commit 7e868e8

Please sign in to comment.