Skip to content

Commit 687f040

Browse files
committed
First implementation
0 parents  commit 687f040

File tree

12 files changed

+698
-0
lines changed

12 files changed

+698
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
namespace Flownative\OAuth2\Client\Controller;
3+
4+
use Flownative\OAuth2\Client\OAuthClient;
5+
use Flownative\OAuth2\Client\OAuthClientException;
6+
use Neos\Flow\Annotations\CompileStatic;
7+
use Neos\Flow\Mvc\Controller\ActionController;
8+
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
9+
use Neos\Flow\Reflection\ReflectionService;
10+
11+
final class OAuthController extends ActionController
12+
{
13+
14+
/**
15+
* @var array
16+
*/
17+
private $services;
18+
19+
public function initializeObject()
20+
{
21+
$this->services = self::detectServices($this->objectManager);
22+
}
23+
24+
/**
25+
* Start OAuth2 authorization
26+
*
27+
* @param string $consumerKey
28+
* @param string $consumerSecret
29+
* @param string $returnToUri
30+
* @param string $serviceName
31+
* @throws OAuthClientException
32+
*/
33+
public function startAuthorizationAction(string $consumerKey, string $consumerSecret, string $returnToUri, string $serviceName)
34+
{
35+
if (!isset($this->services[$serviceName])) {
36+
throw new OAuthClientException('Unknown client service.', 1511187873921);
37+
}
38+
39+
/** @var $client OAuthClient **/
40+
$client = new $this->services[$serviceName];
41+
$authorizeUri = $client->startAuthorization($consumerKey, $consumerSecret, $returnToUri);
42+
$this->redirectToUri($authorizeUri);
43+
}
44+
45+
/**
46+
* Finish OAuth2 authorization
47+
*
48+
* @param string $code
49+
* @param string $state
50+
* @param string $serviceName
51+
* @throws OAuthClientException
52+
*/
53+
public function finishAuthorizationAction(string $code, string $state, string $serviceName)
54+
{
55+
if (!isset($this->services[$serviceName])) {
56+
throw new OAuthClientException('Unknown client service.', 1511193117184);
57+
}
58+
59+
/** @var $client OAuthClient **/
60+
$client = new $this->services[$serviceName];
61+
$returnToUri = $client->finishAuthorization($code, $state);
62+
$this->redirectToUri($returnToUri);
63+
}
64+
65+
/**
66+
* Refresh OAuth2 authorization
67+
*
68+
* @param string $consumerKey
69+
* @param string $returnToUri
70+
* @param string $serviceName
71+
* @throws OAuthClientException
72+
*/
73+
public function refreshAuthorizationAction(string $consumerKey, string $returnToUri, string $serviceName)
74+
{
75+
if (!isset($this->services[$serviceName])) {
76+
throw new OAuthClientException('Unknown client service.', 1511193121713);
77+
}
78+
79+
/** @var $client OAuthClient **/
80+
$client = new $this->services[$serviceName];
81+
$authorizeUri = $client->refreshAuthorization($consumerKey, $returnToUri);
82+
$this->redirectToUri($authorizeUri);
83+
}
84+
85+
/**
86+
* Detects and collects all existing OAuth2 Client Services
87+
*
88+
* @param ObjectManagerInterface $objectManager
89+
* @return array
90+
* @CompileStatic
91+
*/
92+
protected static function detectServices(ObjectManagerInterface $objectManager): array
93+
{
94+
$services = [];
95+
/** @var ReflectionService $reflectionService */
96+
$reflectionService = $objectManager->get(ReflectionService::class);
97+
foreach ($reflectionService->getAllSubClassNamesForClass(OAuthClient::class) as $serviceClassName) {
98+
if ($reflectionService->isClassAbstract($serviceClassName)) {
99+
continue;
100+
}
101+
$services[call_user_func_array([$serviceClassName, 'getServiceName'], [])] = $serviceClassName;
102+
}
103+
return $services;
104+
}
105+
}

0 commit comments

Comments
 (0)