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

Commit 96c7a75

Browse files
committed
🚿
1 parent 475ac16 commit 96c7a75

13 files changed

+95
-46
lines changed

examples/oauth-example-common.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
use chillerlan\OAuth\OAuthOptions;
1111
use chillerlan\DotEnv\DotEnv;
12-
use chillerlan\OAuthTest\{OAuthTestHttpClient, OAuthTestSessionStorage, OAuthTestLogger};
12+
use chillerlan\OAuthTest\{OAuthTestHttpClient, OAuthTestMemoryStorage, OAuthTestLogger};
1313

1414
\ini_set('date.timezone', 'Europe/Amsterdam');
1515

1616
// these vars are supposed to be set before this file is included
1717
/** @var string $ENVVAR - name prefix for the environment variable */
1818
/** @var string $CFGDIR - the directory where configuration is stored (.env, cacert, tokens) */
19-
19+
$ENVVAR = $ENVVAR ?? '';
20+
$CFGDIR = $CFGDIR ?? __DIR__.'/../config';
2021
$LOGLEVEL = $LOGLEVEL ?? 'info';
2122

2223
$env = (new DotEnv($CFGDIR, '.env', false))->load();
@@ -31,22 +32,19 @@
3132
// HTTPOptions
3233
'ca_info' => $CFGDIR.'/cacert.pem',
3334
'userAgent' => 'chillerlanPhpOAuth/3.0.0 +https://github.com/codemasher/php-oauth',
34-
35-
// testHTTPClient
3635
'sleep' => 0.25,
3736
];
3837

3938
/** @var \chillerlan\Settings\SettingsContainerInterface $options */
40-
$options = new class($options_arr) extends OAuthOptions{
41-
protected $sleep; // testHTTPClient
42-
};
39+
$options = new OAuthOptions($options_arr);
4340

41+
/** @var \Psr\Log\LoggerInterface $logger */
4442
$logger = new OAuthTestLogger($LOGLEVEL);
4543

46-
/** @var \chillerlan\HTTP\Psr18\HTTPClientInterface $http */
44+
/** @var \Psr\Http\Client\ClientInterface $http */
4745
$http = new OAuthTestHttpClient($options);
4846
#$http->setLogger($logger);
4947

5048
/** @var \chillerlan\OAuth\Storage\OAuthStorageInterface $storage */
51-
$storage = new OAuthTestSessionStorage($options, $CFGDIR);
49+
$storage = new OAuthTestMemoryStorage($options, $CFGDIR);
5250
#$storage->setLogger($logger);

src/Core/OAuth1Provider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ protected function parseTokenResponse(ResponseInterface $response, bool $checkCa
9090
throw new ProviderException('invalid token');
9191
}
9292

93-
if($checkCallbackConfirmed && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')){
93+
if(
94+
$checkCallbackConfirmed
95+
&& (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')
96+
){
9497
throw new ProviderException('oauth callback unconfirmed');
9598
}
9699

@@ -119,6 +122,7 @@ protected function nonce():string{
119122
$nonce = random_bytes(32);
120123

121124
// use the sodium extension if available
125+
/** @noinspection PhpComposerExtensionStubsInspection */
122126
return function_exists('sodium_bin2hex')
123127
? \sodium_bin2hex($nonce)
124128
: bin2hex($nonce);

src/Core/OAuth2Provider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ public function refreshAccessToken(AccessToken $token = null):AccessToken{
225225
$refreshToken = $token->refreshToken;
226226

227227
if(empty($refreshToken)){
228-
throw new ProviderException(sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires)));
228+
throw new ProviderException(
229+
sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires))
230+
);
229231
}
230232

231233
$body = [

src/Core/OAuthInterface.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ public function getRequestAuthorization(RequestInterface $request, AccessToken $
6767
* @return \Psr\Http\Message\ResponseInterface
6868
* @throws \chillerlan\OAuth\Core\ProviderException
6969
*/
70-
public function request(string $path, array $params = null, string $method = null, $body = null, array $headers = null):ResponseInterface;
70+
public function request(
71+
string $path,
72+
array $params = null,
73+
string $method = null,
74+
$body = null,
75+
array $headers = null
76+
):ResponseInterface;
7177

7278
/**
7379
* Sets an optional OAuthStorageInterface

src/Core/OAuthProvider.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ abstract class OAuthProvider implements OAuthInterface{
146146
*
147147
* @throws \chillerlan\HTTP\MagicAPI\ApiClientException
148148
*/
149-
public function __construct(ClientInterface $http, OAuthStorageInterface $storage, SettingsContainerInterface $options, LoggerInterface $logger = null){
149+
public function __construct(
150+
ClientInterface $http,
151+
OAuthStorageInterface $storage,
152+
SettingsContainerInterface $options,
153+
LoggerInterface $logger = null
154+
){
150155
$this->http = $http;
151156
$this->storage = $storage;
152157
$this->options = $options;
@@ -307,7 +312,13 @@ protected function cleanBodyParams(array $params):array{
307312
/**
308313
* @inheritDoc
309314
*/
310-
public function request(string $path, array $params = null, string $method = null, $body = null, array $headers = null):ResponseInterface{
315+
public function request(
316+
string $path,
317+
array $params = null,
318+
string $method = null,
319+
$body = null,
320+
array $headers = null
321+
):ResponseInterface{
311322

312323
$request = $this->requestFactory
313324
->createRequest($method ?? 'GET', merge_query($this->apiURL.$path, $params ?? []));
@@ -349,7 +360,11 @@ public function sendRequest(RequestInterface $request):ResponseInterface{
349360
$token = $this->storage->getAccessToken($this->serviceName);
350361

351362
// attempt to refresh an expired token
352-
if($this instanceof TokenRefresh && $this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)){
363+
if(
364+
$this instanceof TokenRefresh
365+
&& $this->options->tokenAutoRefresh
366+
&& ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)
367+
){
353368
$token = $this->refreshAccessToken($token);
354369
}
355370

src/OAuthOptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
* @property string $ca_info
3535
* @property bool $ssl_verifypeer
3636
* @property string $curlHandle
37+
* @property int $windowSize
38+
* @property int|float $sleep
39+
* @property int $timeout
40+
* @property int $retries
41+
* @property array $curl_multi_options
3742
*/
3843
class OAuthOptions extends SettingsContainerAbstract{
3944
use OAuthOptionsTrait, HTTPOptionsTrait;

tests/API/APITestAbstract.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
namespace chillerlan\OAuthTest\API;
1414

1515
use chillerlan\DotEnv\DotEnv;
16-
use chillerlan\Settings\SettingsContainerInterface;
1716
use chillerlan\OAuth\{Core\OAuthInterface, OAuthOptions};
1817
use chillerlan\OAuthTest\{OAuthTestHttpClient, OAuthTestLogger, OAuthTestMemoryStorage};
18+
use chillerlan\Settings\SettingsContainerInterface;
1919
use PHPUnit\Framework\TestCase;
2020
use Psr\Http\Client\ClientInterface;
2121
use Psr\Http\Message\ResponseInterface;
2222
use Psr\Log\LoggerInterface;
2323

2424
use function chillerlan\HTTP\Psr7\{get_json, get_xml};
25+
use function file_exists, ini_set;
2526

2627
abstract class APITestAbstract extends TestCase{
2728

@@ -83,9 +84,9 @@ abstract class APITestAbstract extends TestCase{
8384
protected $http;
8485

8586
protected function setUp():void{
86-
\ini_set('date.timezone', 'Europe/Amsterdam');
87+
ini_set('date.timezone', 'Europe/Amsterdam');
8788

88-
$file = \file_exists($this->CFG.'/.env') ? '.env' : '.env_travis';
89+
$file = file_exists($this->CFG.'/.env') ? '.env' : '.env_travis';
8990
$this->dotEnv = (new DotEnv($this->CFG, $file))->load();
9091

9192
if($this->dotEnv->get('IS_CI') === 'TRUE'){
@@ -96,20 +97,15 @@ protected function setUp():void{
9697

9798
$this->testuser = $this->dotEnv->get($this->ENV.'_TESTUSER');
9899

99-
$options = [
100+
$this->options = new OAuthOptions([
100101
'key' => $this->dotEnv->get($this->ENV.'_KEY'),
101102
'secret' => $this->dotEnv->get($this->ENV.'_SECRET'),
102103
'tokenAutoRefresh' => true,
103104
// HTTPOptionsTrait
104105
'ca_info' => $this->CFG.'/cacert.pem',
105106
'userAgent' => 'chillerlanPhpOAuth/3.0.0 +https://github.com/chillerlan/php-oauth',
106-
// testHTTPClient
107107
'sleep' => $this->requestDelay,
108-
];
109-
110-
$this->options = new class($options) extends OAuthOptions{
111-
protected $sleep;
112-
};
108+
]);
113109

114110
$this->logger = new OAuthTestLogger('debug');
115111
$this->storage = new OAuthTestMemoryStorage($this->options, $this->CFG);

tests/API/OAuth2APITestAbstract.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use chillerlan\OAuth\Core\{AccessToken, ClientCredentials, OAuth2Interface};
1616
use chillerlan\OAuth\Storage\MemoryStorage;
1717

18+
use function time;
19+
1820
/**
1921
* @property \chillerlan\OAuth\Core\OAuth2Interface $provider
2022
*/
@@ -42,7 +44,7 @@ public function testRequestCredentialsToken(){
4244
$this->assertIsString($token->accessToken);
4345

4446
if($token->expires !== AccessToken::EOL_NEVER_EXPIRES){
45-
$this->assertGreaterThan(\time(), $token->expires);
47+
$this->assertGreaterThan(time(), $token->expires);
4648
}
4749

4850
$this->logger->debug('OAuth2ClientCredentials', $token->toArray());

tests/Core/AccessTokenTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use chillerlan\OAuth\Core\AccessToken;
1515
use PHPUnit\Framework\TestCase;
1616

17+
use function sleep, time;
18+
1719
class AccessTokenTest extends TestCase{
1820

1921
/**
@@ -95,30 +97,30 @@ public function testIsExpired($expires, $isExpired){
9597

9698
public function testIsExpiredVariable(){
9799

98-
$now = \time();
100+
$now = time();
99101
$expiry1 = $now + 3600;
100102
$this->token->setExpiry($expiry1);
101103
$this->assertSame($expiry1, $this->token->expires);
102104
$this->assertFalse($this->token->isExpired());
103105

104-
$now = \time();
106+
$now = time();
105107
$expiry2 = 3600;
106108
$this->token->setExpiry($expiry2);
107109
$this->assertSame($now+$expiry2, $this->token->expires);
108110
$this->assertFalse($this->token->isExpired());
109111

110-
$now = \time();
112+
$now = time();
111113
$expiry3 = 2;
112114
$this->token->setExpiry($expiry3);
113115
$this->assertSame($now+$expiry3, $this->token->expires);
114-
\sleep(3);
116+
sleep(3);
115117
$this->assertTrue($this->token->isExpired());
116118

117-
$now = \time();
119+
$now = time();
118120
$expiry4 = $now + 2;
119121
$this->token->setExpiry($expiry4);
120122
$this->assertSame($expiry4, $this->token->expires);
121-
\sleep(3);
123+
sleep(3);
122124
$this->assertTrue($this->token->isExpired());
123125
}
124126

tests/OAuthTestHttpClient.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ class OAuthTestHttpClient implements ClientInterface, LoggerAwareInterface{
3939
* @param \Psr\Http\Client\ClientInterface|null $http
4040
* @param \Psr\Log\LoggerInterface|null $logger
4141
*/
42-
public function __construct(SettingsContainerInterface $options, ClientInterface $http = null, LoggerInterface $logger = null){
42+
public function __construct(
43+
SettingsContainerInterface $options,
44+
ClientInterface $http = null,
45+
LoggerInterface $logger = null
46+
){
4347
$this->options = $options;
4448
$this->client = new LoggingClient(
4549
$http ?? new CurlClient($this->options),

0 commit comments

Comments
 (0)