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 Zend\Diactoros\Response\JsonResponse; |
10
|
|
|
|
11
|
|
|
class OfficialWebAppController extends Controller |
12
|
|
|
{ |
13
|
|
|
public function indexAction() |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function registerAction() |
19
|
|
|
{ |
20
|
|
|
$form = new RegistrationForm('register'); |
21
|
|
|
$this->view->form = $form; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sample page using client_credentials grant to connect to the API |
26
|
|
|
* |
27
|
|
|
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
28
|
|
|
*/ |
29
|
|
|
public function clientCredentialsExampleAction() |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
|
|
// This code fetches your access token |
33
|
|
|
// The self signed provider is for dev use only! |
34
|
|
|
$apiKeys = Registry::ahoy()->get('apiKeys'); |
35
|
|
|
$options = $apiKeys['clientCredentials']; |
36
|
|
|
|
37
|
|
|
$provider = new SelfSignedProvider($options); |
38
|
|
|
|
39
|
|
|
$accessToken = $provider->getAccessToken('client_credentials', ['scope' => ['admin']]); |
40
|
|
|
$request = $provider->getAuthenticatedRequest('GET', $options['host'] . '/client', $accessToken); |
41
|
|
|
$response = $provider->getResponse($request); |
42
|
|
|
|
43
|
|
|
$data = \json_decode($response->getBody()->getContents()); |
44
|
|
|
$response = new JsonResponse($data); |
45
|
|
|
|
46
|
|
|
return $response; // usually the data would be sent to a view for display, but that's outwith the scope |
47
|
|
|
} catch (\Exception $e) { |
48
|
|
|
die($e->getCode() . $e->getMessage() . $e->getTraceAsString()); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.