-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #422 from patrickcarlohickman/add-client-credentia…
…ls-with-basic-auth Add client credentials grant with basic auth
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/Http/OAuth2/GetClientCredentialsTokenBasicAuthRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Http\OAuth2; | ||
|
||
use Saloon\Enums\Method; | ||
use Saloon\Http\Request; | ||
use Saloon\Contracts\Body\HasBody; | ||
use Saloon\Contracts\Authenticator; | ||
use Saloon\Traits\Body\HasFormBody; | ||
use Saloon\Helpers\OAuth2\OAuthConfig; | ||
use Saloon\Traits\Plugins\AcceptsJson; | ||
use Saloon\Http\Auth\BasicAuthenticator; | ||
|
||
class GetClientCredentialsTokenBasicAuthRequest extends Request implements HasBody | ||
{ | ||
use HasFormBody; | ||
use AcceptsJson; | ||
|
||
/** | ||
* Define the method that the request will use. | ||
*/ | ||
protected Method $method = Method::POST; | ||
|
||
/** | ||
* Define the endpoint for the request. | ||
*/ | ||
public function resolveEndpoint(): string | ||
{ | ||
return $this->oauthConfig->getTokenEndpoint(); | ||
} | ||
|
||
/** | ||
* Requires the authorization code and OAuth 2 config. | ||
* | ||
* @param array<string> $scopes | ||
*/ | ||
public function __construct(protected OAuthConfig $oauthConfig, protected array $scopes = [], protected string $scopeSeparator = ' ') | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Register the default data. | ||
* | ||
* @return array{ | ||
* grant_type: string, | ||
* scope: string, | ||
* } | ||
*/ | ||
public function defaultBody(): array | ||
{ | ||
return [ | ||
'grant_type' => 'client_credentials', | ||
'scope' => implode($this->scopeSeparator, array_merge($this->oauthConfig->getDefaultScopes(), $this->scopes)), | ||
]; | ||
} | ||
|
||
/** | ||
* Default authenticator used. | ||
*/ | ||
protected function defaultAuth(): ?Authenticator | ||
{ | ||
return new BasicAuthenticator($this->oauthConfig->getClientId(), $this->oauthConfig->getClientSecret()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Traits\OAuth2; | ||
|
||
use Saloon\Http\Request; | ||
use Saloon\Helpers\OAuth2\OAuthConfig; | ||
use Saloon\Http\OAuth2\GetClientCredentialsTokenBasicAuthRequest; | ||
|
||
trait ClientCredentialsBasicAuthGrant | ||
{ | ||
use ClientCredentialsGrant; | ||
|
||
/** | ||
* Resolve the access token request | ||
*/ | ||
protected function resolveAccessTokenRequest(OAuthConfig $oauthConfig, array $scopes = [], string $scopeSeparator = ' '): Request | ||
{ | ||
return new GetClientCredentialsTokenBasicAuthRequest($oauthConfig, $scopes, $scopeSeparator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
tests/Fixtures/Connectors/ClientCredentialsBasicAuthConnector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Tests\Fixtures\Connectors; | ||
|
||
use Saloon\Http\Connector; | ||
use Saloon\Helpers\OAuth2\OAuthConfig; | ||
use Saloon\Traits\OAuth2\ClientCredentialsBasicAuthGrant; | ||
|
||
class ClientCredentialsBasicAuthConnector extends Connector | ||
{ | ||
use ClientCredentialsBasicAuthGrant; | ||
|
||
/** | ||
* Define the base URL. | ||
*/ | ||
public function resolveBaseUrl(): string | ||
{ | ||
return 'https://oauth.saloon.dev'; | ||
} | ||
|
||
/** | ||
* Define default Oauth config. | ||
*/ | ||
protected function defaultOauthConfig(): OAuthConfig | ||
{ | ||
return OAuthConfig::make() | ||
->setClientId('client-id') | ||
->setClientSecret('client-secret'); | ||
} | ||
} |