1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Form\User\RegistrationForm; |
6
|
|
|
use App\OAuth\SelfSignedProvider; |
7
|
|
|
use Bone\Mvc\Controller; |
8
|
|
|
use Bone\Mvc\Registry; |
9
|
|
|
use GuzzleHttp\Client; |
10
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
11
|
|
|
|
12
|
|
|
class OfficialWebAppController extends Controller |
13
|
|
|
{ |
14
|
|
|
public function indexAction() |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function registerAction() |
20
|
|
|
{ |
21
|
|
|
$form = new RegistrationForm('register'); |
22
|
|
|
$this->view->form = $form; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sample page using client_credentials grant to connect to the API |
27
|
|
|
* |
28
|
|
|
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
29
|
|
|
*/ |
30
|
1 |
|
public function clientCredentialsExampleAction() |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
// This code fetches your access token |
34
|
|
|
// The self signed provider is for dev use only! |
35
|
1 |
|
$apiKeys = Registry::ahoy()->get('apiKeys'); |
36
|
1 |
|
$keys = $apiKeys['clientCredentials']; |
37
|
|
|
|
38
|
1 |
|
$provider = new SelfSignedProvider([ |
39
|
1 |
|
'clientId' => $keys['clientId'], |
40
|
1 |
|
'clientSecret' => $keys['clientSecret'], |
41
|
1 |
|
'redirectUri' => '', |
42
|
1 |
|
'urlAuthorize' => 'http://not-used-with-this-grant', |
43
|
1 |
|
'urlAccessToken' => $keys['urlAccessToken'], |
44
|
1 |
|
'urlResourceOwnerDetails' => $keys['urlResourceOwnerDetails'], |
45
|
|
|
'verify' => false, |
46
|
|
|
]); |
47
|
|
|
|
48
|
1 |
|
$accessToken = $provider->getAccessToken('client_credentials', ['scope' => ['admin']]); |
49
|
|
|
|
50
|
|
|
// From here on we start calling the API |
51
|
|
|
$client = new Client(['verify' => false]); |
52
|
|
|
$response = $client->get('https://apache/client', [ |
53
|
|
|
'headers' => [ |
54
|
|
|
'Authorization' => 'Bearer ' . $accessToken->getToken(), |
55
|
|
|
], |
56
|
|
|
]); |
57
|
|
|
$data = \json_decode($response->getBody()->getContents()); |
58
|
|
|
$response = new JsonResponse($data); |
59
|
|
|
|
60
|
|
|
return $response; // usually the data would be sent to a view for display, but that's outwith the scope |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|