Skip to content

Feature | Add OAuth 1.0a Authenticator #469

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

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Http/Auth/OAuth1Authenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);

namespace Saloon\Http\Auth;

use Saloon\Contracts\Authenticator;
use Saloon\Enums\Method;
use Saloon\Http\PendingRequest;

class OAuth1Authenticator implements Authenticator
{
public function __construct(
private readonly string $consumerKey,
private readonly string $consumerSecret,
private readonly string $token,
private readonly string $tokenSecret) {}

/**
* Apply the authentication to the request.
*
*/
public function set(PendingRequest $pendingRequest): void
{
$oauthHeader = $this->generateOAuthHeader($pendingRequest);
$pendingRequest->headers()
->add('Authorization', $oauthHeader);
}

private function generateOAuthHeader(PendingRequest $pendingRequest): string
{
$method = $pendingRequest->getMethod();
$url = $pendingRequest->getUrl();
$params = array_merge($pendingRequest->query()?->all() ?? [], $pendingRequest->body()?->all() ?? []);

$oauthParams = [
'oauth_consumer_key' => $this->consumerKey,
'oauth_token' => $this->token,
'oauth_nonce' => $this->generateNonce(),
'oauth_timestamp' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_version' => '1.0',
];
$baseString = $this->generateBaseString($method, $url, array_merge($params, $oauthParams));
$signature = $this->generateSignature($baseString);
$oauthParams['oauth_signature'] = $signature;

return 'OAuth '.urldecode(http_build_query($oauthParams, '', ', '));

}

private function generateBaseString(Method $method, string $url, array $params): string
{
ksort($params);

$query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);

return strtoupper($method->value).'&'.rawurlencode($url).'&'.rawurlencode($query);
}

private function generateSignature(string $baseString): string
{
$key = rawurlencode($this->consumerSecret).'&'.rawurlencode($this->tokenSecret);

return base64_encode(hash_hmac('sha1', $baseString, $key, true));
}

private function generateNonce(): string
{
return bin2hex(random_bytes(16));
}
}
13 changes: 13 additions & 0 deletions tests/Unit/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Saloon\Http\Auth\OAuth1Authenticator;
use Saloon\Http\PendingRequest;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
Expand Down Expand Up @@ -66,6 +67,18 @@
connector()->send($request, $mockClient);
});

test('you can use the oauth1 authenticator', function () {
$request = new UserRequest();
$request->authenticate(new OAuth1Authenticator('consumer-key', 'consumer-secret', 'token', 'token-secret'));

$pendingRequest = connector()->createPendingRequest($request);
//
expect($pendingRequest->headers()->get('Authorization'))
->toContain('consumer-key')
->toContain('token')
->toContain('oauth_signature');
});

test('you can use your own authenticators', function () {
$request = new UserRequest();
$request->authenticate(new PizzaAuthenticator('Margherita', 'San Pellegrino'));
Expand Down