Skip to content

Commit a60c57d

Browse files
authored
Merge pull request #580 from Lusitanian/v2.0
V2.0
2 parents f992e39 + 9edfb1d commit a60c57d

19 files changed

Lines changed: 1609 additions & 814 deletions

File tree

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@
3030
]
3131
},
3232
"require": {
33-
"php": "^7.2"
33+
"php": "^7.2 || 8.0.*"
3434
},
3535
"require-dev": {
3636
"ext-dom": "*",
3737
"ext-curl": "*",
3838
"ext-json": "*",
3939
"symfony/http-foundation": "~2.1",
4040
"predis/predis": "0.8.*@dev",
41-
"phpunit/phpunit": "8.5",
4241
"squizlabs/php_codesniffer": "^3.5",
43-
"friendsofphp/php-cs-fixer": "^2.16",
4442
"symfony/var-dumper": "^5.1",
45-
"symfony/finder": "^5.1"
43+
"symfony/finder": "^5.1",
44+
"phpunit/phpunit": "9.5.8",
45+
"friendsofphp/php-cs-fixer": "^3.0"
4646
},
4747
"suggest": {
4848
"symfony/http-foundation": "Allows using the Symfony Session storage backend.",

composer.lock

Lines changed: 1423 additions & 612 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/bootstrap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<?php
2-
require_once __DIR__ . './../vendor/autoload.php';
2+
3+
require_once __DIR__ . './../vendor/autoload.php';

examples/index.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,23 @@
2828
$status = $result ? 'passed' : 'failed';
2929
echo "<li>{$label} ... <span class='{$status}'>{$status}</span></li>";
3030
}
31-
echo '</ul>';
32-
33-
?>
31+
echo '</ul>'; ?>
3432
<h2>Select provider for check</h2>
3533
<ul>
3634
<?php
37-
/** @var SplFileInfo $files */
35+
/** @var SplFileInfo $files */
3836
foreach ($helper->getFinder() as $files) {
3937
$basename = $files->getBasename();
40-
echo '<li><a href="/provider/'.$basename.'">'.$basename.'</a></li>';
41-
}
42-
?>
38+
echo '<li><a href="/provider/' . $basename . '">' . $basename . '</a></li>';
39+
} ?>
4340
</ul>
4441

4542
<?php
4643
} else {
47-
echo 'Requirement check:' . PHP_EOL;
48-
foreach ($requirements as $label => $result) {
49-
$status = $result ? '32m passed' : '31m failed';
50-
echo "{$label} ... \033[{$status}\033[0m" . PHP_EOL;
51-
}
52-
}
44+
echo 'Requirement check:' . PHP_EOL;
45+
foreach ($requirements as $label => $result) {
46+
$status = $result ? '32m passed' : '31m failed';
47+
echo "{$label} ... \033[{$status}\033[0m" . PHP_EOL;
48+
}
49+
}
5350
?>

examples/provider/amazon.php

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,38 @@
1010
*/
1111

1212
use OAuth\Common\Consumer\Credentials;
13+
use OAuth\Common\Http\Client\CurlClient;
14+
use OAuth\Common\Http\Exception\TokenResponseException;
1315
use OAuth\Common\Storage\Session;
16+
use OAuth\Helper\Example;
1417
use OAuth\OAuth2\Service\Amazon;
1518

16-
/**
17-
* Bootstrap the example.
18-
*/
19-
require_once __DIR__ . '/bootstrap.php';
19+
require_once __DIR__ . '/../bootstrap.php';
2020

21-
// Session storage
21+
$helper = new Example();
2222
$storage = new Session();
23-
24-
// Setup the credentials for the requests
25-
$credentials = new Credentials(
26-
$servicesCredentials['amazon']['key'],
27-
$servicesCredentials['amazon']['secret'],
28-
$currentUri->getAbsoluteUri()
29-
);
30-
31-
// Instantiate the Amazon service using the credentials, http client, storage mechanism for the token and profile scope
32-
/** @var Amazon $amazonService */
33-
$amazonService = $serviceFactory->createService('amazon', $credentials, $storage, ['profile']);
34-
35-
if (!empty($_GET['code'])) {
36-
// This was a callback request from Amazon, get the token
37-
$token = $amazonService->requestAccessToken($_GET['code']);
38-
39-
// Send a request with it
40-
$result = json_decode($amazonService->request('/user/profile'), true);
41-
42-
// Show some of the resultant data
43-
echo 'Your unique Amazon user id is: ' . $result['user_id'] . ' and your name is ' . $result['name'];
44-
} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
45-
$url = $amazonService->getAuthorizationUri();
46-
header('Location: ' . $url);
47-
} else {
48-
$url = $currentUri->getRelativeUri() . '?go=go';
49-
echo "<a href='$url'>Login with Amazon!</a>";
23+
$client = new CurlClient();
24+
25+
$helper->setTitle('Amazon');
26+
if (empty($_GET)) {
27+
echo $helper->getContent();
28+
} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') {
29+
$credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
30+
$service = new Amazon($credentials, $client, $storage);
31+
echo $helper->getHeader();
32+
echo '<a href="' . $service->getAuthorizationUri() . '">get access token</a>';
33+
echo $helper->getFooter();
34+
} elseif (!empty($_GET['code'])) {
35+
$credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
36+
$service = new Amazon($credentials, $client, $storage);
37+
38+
echo $helper->getHeader();
39+
40+
try {
41+
$token = $service->requestAccessToken($_GET['code']);
42+
echo 'access token: ' . $token->getAccessToken();
43+
} catch (TokenResponseException $exception) {
44+
$helper->getErrorMessage($exception);
45+
}
46+
echo $helper->getFooter();
5047
}

examples/provider/google.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
use OAuth\Helper\Example;
88
use OAuth\OAuth2\Service\Google;
99

10-
require_once __DIR__.'/../bootstrap.php';
10+
require_once __DIR__ . '/../bootstrap.php';
1111

1212
$helper = new Example();
1313
$storage = new Session();
1414
$client = new CurlClient();
15+
$helper->setTitle('Google');
1516

1617
if (empty($_GET)) {
1718
echo $helper->getContent();
1819
} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') {
1920
echo $helper->getHeader();
21+
2022
try {
2123
$credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
2224
$google = new Google($credentials, $client, $storage, ['email']);
23-
echo '<a href="'.$google->getAuthorizationUri().'">get access token</a>';
25+
echo '<a href="' . $google->getAuthorizationUri() . '">get access token</a>';
2426
} catch (\Exception $exception) {
2527
$helper->getErrorMessage($exception);
2628
}
@@ -30,6 +32,7 @@
3032
$google = new Google($credentials, $client, $storage);
3133

3234
echo $helper->getHeader();
35+
3336
try {
3437
$token = $google->requestAccessToken($_GET['code']);
3538
echo 'access token: ' . $token->getAccessToken();

examples/provider/instagram.php

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,39 @@
11
<?php
22

3-
/**
4-
* Example of retrieving an authentication token of the Instagram service.
5-
*
6-
* PHP version 5.4
7-
*
8-
* @author David Desberg <david@daviddesberg.com>
9-
* @author Pieter Hordijk <info@pieterhordijk.com>
10-
* @author Hannes Van De Vreken <vandevreken.hannes@gmail.com>
11-
* @license http://www.opensource.org/licenses/mit-license.html MIT License
12-
*/
13-
143
use OAuth\Common\Consumer\Credentials;
4+
use OAuth\Common\Http\Client\CurlClient;
5+
use OAuth\Common\Http\Exception\TokenResponseException;
156
use OAuth\Common\Storage\Session;
7+
use OAuth\Helper\Example;
168
use OAuth\OAuth2\Service\Instagram;
179

18-
/**
19-
* Bootstrap the example.
20-
*/
21-
require_once __DIR__ . '/bootstrap.php';
10+
require_once __DIR__ . '/../bootstrap.php';
2211

23-
// Session storage
12+
$helper = new Example();
2413
$storage = new Session();
25-
26-
// Setup the credentials for the requests
27-
$credentials = new Credentials(
28-
$servicesCredentials['instagram']['key'],
29-
$servicesCredentials['instagram']['secret'],
30-
$currentUri->getAbsoluteUri()
31-
);
32-
33-
$scopes = ['basic', 'comments', 'relationships', 'likes'];
34-
35-
// Instantiate the Instagram service using the credentials, http client and storage mechanism for the token
36-
/** @var Instagram $instagramService */
37-
$instagramService = $serviceFactory->createService('instagram', $credentials, $storage, $scopes);
38-
39-
if (!empty($_GET['code'])) {
40-
// retrieve the CSRF state parameter
41-
$state = $_GET['state'] ?? null;
42-
43-
// This was a callback request from Instagram, get the token
44-
$instagramService->requestAccessToken($_GET['code'], $state);
45-
46-
// Send a request with it
47-
$result = json_decode($instagramService->request('users/self'), true);
48-
49-
// Show some of the resultant data
50-
echo 'Your unique instagram user id is: ' . $result['data']['id'] . ' and your name is ' . $result['data']['full_name'];
51-
} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
52-
$url = $instagramService->getAuthorizationUri();
53-
header('Location: ' . $url);
54-
} else {
55-
$url = $currentUri->getRelativeUri() . '?go=go';
56-
echo "<a href='$url'>Login with Instagram!</a>";
14+
$client = new CurlClient();
15+
16+
$helper->setTitle('Instagram');
17+
18+
if (empty($_GET)) {
19+
echo $helper->getContent();
20+
} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') {
21+
$credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
22+
$service = new Instagram($credentials, $client, $storage);
23+
echo $helper->getHeader();
24+
echo '<a href="' . $service->getAuthorizationUri() . '">get access token</a>';
25+
echo $helper->getFooter();
26+
} elseif (!empty($_GET['code'])) {
27+
$credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
28+
$service = new Instagram($credentials, $client, $storage);
29+
30+
echo $helper->getHeader();
31+
32+
try {
33+
$token = $service->requestAccessToken($_GET['code']);
34+
echo 'access token: ' . $token->getAccessToken();
35+
} catch (TokenResponseException $exception) {
36+
$helper->getErrorMessage($exception);
37+
}
38+
echo $helper->getFooter();
5739
}

examples/provider/ustream.php

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,39 @@
1010
*/
1111

1212
use OAuth\Common\Consumer\Credentials;
13+
use OAuth\Common\Http\Client\CurlClient;
14+
use OAuth\Common\Http\Exception\TokenResponseException;
1315
use OAuth\Common\Storage\Session;
16+
use OAuth\Helper\Example;
1417
use OAuth\OAuth2\Service\Ustream;
1518

16-
/**
17-
* Bootstrap the example.
18-
*/
19-
require_once __DIR__ . '/bootstrap.php';
19+
require_once __DIR__ . '/../bootstrap.php';
2020

21-
// Session storage
21+
$helper = new Example();
2222
$storage = new Session();
23-
24-
// Setup the credentials for the requests
25-
$credentials = new Credentials(
26-
$servicesCredentials['ustream']['key'],
27-
$servicesCredentials['ustream']['secret'],
28-
$currentUri->getAbsoluteUri()
29-
);
30-
31-
// Instantiate the Ustream service using the credentials, http client and storage mechanism for the token
32-
/** @var Ustream $ustream */
33-
$ustream = $serviceFactory->createService('Ustream', $credentials, $storage, ['identity']);
34-
35-
if (!empty($_GET['code'])) {
36-
// retrieve the CSRF state parameter
37-
$state = $_GET['state'] ?? null;
38-
39-
// This was a callback request from Ustream, get the token
40-
$ustream->requestAccessToken($_GET['code'], $state);
41-
42-
$result = json_decode($ustream->request('users/self.json'), true);
43-
44-
echo 'Your unique Ustream user id is: ' . $result['id'] . ' and your username is ' . $result['username'];
45-
} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
46-
$url = $ustream->getAuthorizationUri();
47-
header('Location: ' . $url);
48-
} else {
49-
$url = $currentUri->getRelativeUri() . '?go=go';
50-
echo "<a href='$url'>Login with Ustream!</a>";
23+
$client = new CurlClient();
24+
25+
$helper->setTitle('Instagram');
26+
27+
if (empty($_GET)) {
28+
echo $helper->getContent();
29+
} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') {
30+
$credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
31+
$service = new Ustream($credentials, $client, $storage);
32+
echo $helper->getHeader();
33+
echo '<a href="' . $service->getAuthorizationUri() . '">get access token</a>';
34+
echo $helper->getFooter();
35+
} elseif (!empty($_GET['code'])) {
36+
$credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
37+
$service = new Ustream($credentials, $client, $storage);
38+
39+
echo $helper->getHeader();
40+
41+
try {
42+
$token = $service->requestAccessToken($_GET['code']);
43+
echo 'access token: ' . $token->getAccessToken();
44+
} catch (TokenResponseException $exception) {
45+
$helper->getErrorMessage($exception);
46+
}
47+
echo $helper->getFooter();
5148
}

examples/provider/vkontakte.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
use OAuth\Common\Storage\Session;
77
use OAuth\Helper\Example;
88
use OAuth\OAuth2\Service\Vkontakte;
9-
use OAuth\OAuth2\Token\StdOAuth2Token;
109

11-
require_once __DIR__.'/../bootstrap.php';
10+
require_once __DIR__ . '/../bootstrap.php';
1211

1312
$helper = new Example();
1413
$storage = new Session();
1514
$client = new CurlClient();
16-
15+
$helper->setTitle('Vkontakte');
1716
if (empty($_GET)) {
1817
echo $helper->getContent();
1918
} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') {
2019
$credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
2120
$vkService = new Vkontakte($credentials, $client, $storage);
2221
echo $helper->getHeader();
23-
echo '<a href="'.$vkService->getAuthorizationUri().'">get access token</a>';
22+
echo '<a href="' . $vkService->getAuthorizationUri() . '">get access token</a>';
2423
echo $helper->getFooter();
2524
} elseif (!empty($_GET['code'])) {
2625
$credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
2726
$vkService = new Vkontakte($credentials, $client, $storage);
2827

2928
echo $helper->getHeader();
29+
3030
try {
3131
$token = $vkService->requestAccessToken($_GET['code']);
3232
echo 'access token: ' . $token->getAccessToken();

src/OAuth/Common/Http/Client/AbstractClient.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@ public function setTimeout($timeout)
5959
/**
6060
* @param array $headers
6161
*/
62-
public function normalizeHeaders(&$headers): void
62+
public function normalizeHeaders($headers): array
6363
{
64-
// Normalize headers
65-
array_walk(
66-
$headers,
67-
function (&$val, &$key): void {
68-
$key = ucfirst(strtolower($key));
69-
$val = ucfirst(strtolower($key)) . ': ' . $val;
70-
}
71-
);
64+
$normalizeHeaders = [];
65+
foreach ($headers as $key => $val) {
66+
$val = ucfirst(strtolower($key)) . ': ' . $val;
67+
$normalizeHeaders[$key] = $val;
68+
}
69+
70+
return $normalizeHeaders;
7271
}
7372
}

0 commit comments

Comments
 (0)