Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
🚿 bullshit
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Aug 1, 2023
1 parent 228024c commit 421ab3c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 40 deletions.
19 changes: 6 additions & 13 deletions src/Core/OAuth1Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@

namespace chillerlan\OAuth\Core;

use chillerlan\HTTP\Utils\{MessageUtil, QueryUtil, UriUtil};
use chillerlan\HTTP\Utils\{MessageUtil, QueryUtil};
use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface};
use function array_merge, base64_encode, hash_hmac, implode, in_array,
ltrim, random_bytes, sodium_bin2hex, sprintf, strtoupper, time;
use function array_merge, base64_encode, hash_hmac, implode, in_array, random_bytes, sodium_bin2hex, strtoupper, time;

/**
* Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface.
Expand Down Expand Up @@ -123,20 +122,14 @@ protected function nonce():string{
* @throws \chillerlan\OAuth\Core\ProviderException
*/
protected function getSignature(string $url, array $params, string $method, string $accessTokenSecret = null):string{
$parsed = UriUtil::parseUrl($url);
$parsed = $this->uriFactory->createUri($url);

if(!isset($parsed['host']) || !isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)){
if($parsed->getHost() == '' || $parsed->getScheme() === '' || !in_array($parsed->getScheme(), ['http', 'https'])){
throw new ProviderException('getSignature: invalid url');
}

$url = sprintf('%s://%s', $parsed['scheme'], $parsed['host']);
$path = ltrim(($parsed['path'] ?? ''), '/');

if(!empty($path)){
$url = sprintf('%s/%s', $url, $path);
}

$signatureParams = array_merge(QueryUtil::parse(($parsed['query'] ?? '')), $params);
$signatureParams = array_merge(QueryUtil::parse($parsed->getQuery()), $params);
$url = (string)$parsed->withQuery('')->withFragment('');

unset($signatureParams['oauth_signature']);

Expand Down
32 changes: 14 additions & 18 deletions src/Core/OAuthProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace chillerlan\OAuth\Core;

use chillerlan\HTTP\Utils\{UriUtil, QueryUtil};
use chillerlan\HTTP\Utils\QueryUtil;
use chillerlan\HTTP\Psr17\{RequestFactory, StreamFactory, UriFactory};
use chillerlan\OAuth\OAuthOptions;
use chillerlan\OAuth\Storage\{MemoryStorage, OAuthStorageInterface};
Expand Down Expand Up @@ -340,35 +340,31 @@ protected function cleanBodyParams(iterable $params):array{
* @throws \chillerlan\OAuth\Core\ProviderException
*/
protected function getRequestTarget(string $uri):string{
$parsedURL = UriUtil::parseUrl($uri);

if(!isset($parsedURL['path'])){
throw new ProviderException('invalid path');
}
$parsedURL = $this->uriFactory->createUri($uri);
$parsedHost = $parsedURL->getHost();
$api = $this->uriFactory->createUri($this->apiURL);

// for some reason we were given a host name
if(isset($parsedURL['host'])){
$api = UriUtil::parseUrl($this->apiURL);
$host = ($api['host'] ?? null);
if($parsedHost !== ''){
$apiHost = $api->getHost();

// back out if it doesn't match
if($parsedURL['host'] !== $host){
throw new ProviderException(sprintf('given host (%s) does not match provider (%s)', $parsedURL['host'] , $host));
if($parsedHost !== $apiHost){
throw new ProviderException(sprintf('given host (%s) does not match provider (%s)', $parsedHost , $apiHost));
}

// we explicitly ignore any existing parameters here
return sprintf('https://%s/%s', $parsedURL['host'], ltrim($parsedURL['path'], '/'));
return (string)$parsedURL->withQuery('')->withFragment('');
}

// $apiURL may already include a part of the path
$api = rtrim($this->apiURL, '/');
$path = ltrim($parsedURL['path'], '/');
$parsedPath = $parsedURL->getPath();
$apiURL = rtrim((string)$api, '/');

if(empty($path)){
return $api;
if($parsedPath === ''){
return $apiURL;
}

return sprintf('%s/%s', $api, $path);
return sprintf('%s/%s', $apiURL, ltrim($parsedPath, '/'));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Providers/OAuthProviderTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ public function testTokenInvalidate():void{
public static function requestTargetProvider():array{
return [
'empty' => ['', 'https://localhost/api'],
'slash' => ['/', 'https://localhost/api'],
'slash' => ['/', 'https://localhost/api/'],
'no slashes' => ['a', 'https://localhost/api/a'],
'leading slash' => ['/b', 'https://localhost/api/b'],
'trailing slash' => ['c/', 'https://localhost/api/c/'],
'full url given' => ['https://localhost/other/path/d', 'https://localhost/other/path/d'],
'ignore params' => ['https://localhost/api/e/?with=param', 'https://localhost/api/e/'],
'ignore params' => ['https://localhost/api/e/?with=param#foo', 'https://localhost/api/e/'],
];
}

Expand Down
7 changes: 0 additions & 7 deletions tests/Providers/RequestTest/OAuthProviderRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ public function testRequestBody():void{
$this::assertSame('data=payload', (string)$r->getBody());
}

public function testRequestInvalidPathException():void{
$this::expectException(ProviderException::class);
$this::expectExceptionMessage('invalid path');

$this->provider->request('?query');
}

public function testRequestHostMismatchException():void{
$this::expectException(ProviderException::class);
$this::expectExceptionMessage('given host (notlocalhost) does not match provider (localhost)');
Expand Down

0 comments on commit 421ab3c

Please sign in to comment.