delboy1978uk /
oauth2-api
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Controller; |
||||
| 4 | |||||
| 5 | use App\Form\User\LoginForm; |
||||
| 6 | use App\Form\User\RegistrationForm; |
||||
| 7 | use App\OAuth\SelfSignedProvider; |
||||
| 8 | use Bone\Mvc\Controller; |
||||
| 9 | use Bone\Mvc\Registry; |
||||
| 10 | use Del\Exception\EmailLinkException; |
||||
| 11 | use Del\Icon; |
||||
| 12 | use GuzzleHttp\Exception\ClientException; |
||||
| 13 | use GuzzleHttp\Psr7\MultipartStream; |
||||
| 14 | use Psr\Http\Message\RequestInterface; |
||||
| 15 | use Zend\Diactoros\Response; |
||||
| 16 | use Zend\Diactoros\Response\JsonResponse; |
||||
| 17 | use Zend\Diactoros\Response\RedirectResponse; |
||||
| 18 | use Zend\Diactoros\Stream; |
||||
| 19 | |||||
| 20 | class OfficialWebAppController extends Controller |
||||
| 21 | { |
||||
| 22 | /** @var SelfSignedProvider $oAuthClient */ |
||||
| 23 | private $oAuthClient; |
||||
| 24 | |||||
| 25 | /** @var string $host */ |
||||
| 26 | private $host; |
||||
| 27 | |||||
| 28 | /** @var string $locale */ |
||||
| 29 | private $locale; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
||||
| 33 | */ |
||||
| 34 | public function init() |
||||
| 35 | { |
||||
| 36 | $apiKeys = Registry::ahoy()->get('apiKeys'); |
||||
| 37 | $options = $apiKeys['clientCredentials']; |
||||
| 38 | |||||
| 39 | $this->host = $options['host']; |
||||
| 40 | $this->oAuthClient = new SelfSignedProvider($options); |
||||
| 41 | $this->locale = $this->getParam('locale', 'en_GB'); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | public function indexAction() |
||||
| 45 | { |
||||
| 46 | |||||
| 47 | } |
||||
| 48 | |||||
| 49 | public function thanksForRegisteringAction() |
||||
| 50 | { |
||||
| 51 | |||||
| 52 | } |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
||||
| 56 | */ |
||||
| 57 | public function activateUserAccountAction() |
||||
| 58 | { |
||||
| 59 | $email = $this->getParam('email'); |
||||
| 60 | $token = $this->getParam('token'); |
||||
| 61 | $url = '/' . $this->locale.'/user/activate/' . $email . '/' . $token; |
||||
| 62 | $request = $this->getAuthenticatedRequest($url); |
||||
| 63 | try { |
||||
| 64 | $this->oAuthClient->getResponse($request); |
||||
| 65 | $this->view->activated = true; |
||||
| 66 | $this->view->message = [Icon::CHECK . ' Email successfully validated.', 'success']; |
||||
| 67 | } catch (ClientException $e) { |
||||
| 68 | $data = \json_decode($e->getResponse()->getBody()->getContents(), true); |
||||
| 69 | $this->view->message = [Icon::WARNING . ' ' . $data['error'], 'danger']; |
||||
| 70 | $this->view->activated = false; |
||||
| 71 | if ($data['error'] == EmailLinkException::LINK_EXPIRED) { |
||||
| 72 | $this->view->resendLink = '/website/resend-activation/' . $email; |
||||
| 73 | } |
||||
| 74 | } |
||||
| 75 | } |
||||
| 76 | |||||
| 77 | /** |
||||
| 78 | * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
||||
| 79 | */ |
||||
| 80 | public function resendActivationAction() |
||||
| 81 | { |
||||
| 82 | $email = $this->getParam('email'); |
||||
| 83 | $url = '/' . $this->locale . '/user/activate/resend/' . $email; |
||||
| 84 | $request = $this->getAuthenticatedRequest($url); |
||||
| 85 | try { |
||||
| 86 | $this->oAuthClient->getResponse($request); |
||||
| 87 | $response = new Response(); |
||||
| 88 | $html = $this->viewEngine->render('official-web-app/thanks-for-registering'); |
||||
| 89 | $html = $this->viewEngine->render('layouts/layout', ['content' => $html]); |
||||
| 90 | $stream = $this->createStreamFromString($html); |
||||
| 91 | |||||
| 92 | return $response->withBody($stream); |
||||
| 93 | |||||
| 94 | } catch (ClientException $e) { |
||||
| 95 | $data = \json_decode($e->getResponse()->getBody()->getContents(), true); |
||||
| 96 | $this->view->message = [Icon::WARNING . ' ' . $data['error'], 'danger']; |
||||
| 97 | } |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | /** |
||||
| 101 | * @return RedirectResponse |
||||
| 102 | * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
||||
| 103 | */ |
||||
| 104 | public function registerAction() |
||||
| 105 | { |
||||
| 106 | $form = new RegistrationForm('register'); |
||||
| 107 | |||||
| 108 | if ($this->getRequest()->getMethod() == 'POST') { |
||||
| 109 | |||||
| 110 | $formData = $this->getRequest()->getParsedBody(); |
||||
| 111 | $form->populate($formData); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 112 | if ($form->isValid()) { |
||||
| 113 | $values = $form->getValues(); |
||||
| 114 | $request = $this->getAuthenticatedRequest('/en_GB/user/register', 'POST'); |
||||
| 115 | $request = $this->addMultipartFormData($request, [ |
||||
| 116 | 'email' => $values['email'], |
||||
| 117 | 'password' => $values['password'], |
||||
| 118 | 'confirm' => $values['confirm'], |
||||
| 119 | ]); |
||||
| 120 | |||||
| 121 | try { |
||||
| 122 | |||||
| 123 | $this->oAuthClient->getResponse($request); |
||||
| 124 | return new RedirectResponse('/website/thanks-for-registering'); |
||||
| 125 | |||||
| 126 | } catch (ClientException $e) { |
||||
| 127 | |||||
| 128 | $data = \json_decode($e->getResponse()->getBody()->getContents(), true); |
||||
| 129 | $this->view->message = [Icon::WARNING . ' ' . $data['message'], 'danger']; |
||||
| 130 | } |
||||
| 131 | } |
||||
| 132 | } |
||||
| 133 | |||||
| 134 | $this->view->form = $form; |
||||
| 135 | } |
||||
| 136 | |||||
| 137 | /** |
||||
| 138 | * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
||||
| 139 | */ |
||||
| 140 | public function loginAction() |
||||
| 141 | { |
||||
| 142 | $form = new LoginForm('login'); |
||||
| 143 | |||||
| 144 | if ($this->getRequest()->getMethod() == 'POST') { |
||||
| 145 | |||||
| 146 | $formData = $this->getRequest()->getParsedBody(); |
||||
| 147 | $form->populate($formData); |
||||
|
0 ignored issues
–
show
It seems like
$formData can also be of type null and object; however, parameter $data of Del\Form\AbstractForm::populate() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 148 | if ($form->isValid()) { |
||||
| 149 | $values = $form->getValues(); |
||||
| 150 | $this->view->email = $values['email']; |
||||
| 151 | $request = $this->getAuthenticatedRequest('/en_GB/user/login', 'POST'); |
||||
| 152 | $request = $this->addMultipartFormData($request, [ |
||||
| 153 | 'email' => $values['email'], |
||||
| 154 | 'password' => $values['password'], |
||||
| 155 | ]); |
||||
| 156 | |||||
| 157 | try { |
||||
| 158 | |||||
| 159 | $response = $this->oAuthClient->getResponse($request); |
||||
| 160 | die(var_dump($response)); |
||||
|
0 ignored issues
–
show
Are you sure the usage of
var_dump($response) is correct as it seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 161 | |||||
| 162 | } catch (ClientException $e) { |
||||
| 163 | |||||
| 164 | $data = \json_decode($e->getResponse()->getBody()->getContents(), true); |
||||
| 165 | $this->view->message = [Icon::WARNING . ' ' . $data['message'], 'danger']; |
||||
| 166 | } |
||||
| 167 | } |
||||
| 168 | } |
||||
| 169 | |||||
| 170 | $this->view->form = $form; |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | /** |
||||
| 174 | * Sample page using client_credentials grant to connect to the API |
||||
| 175 | * |
||||
| 176 | * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
||||
| 177 | */ |
||||
| 178 | public function clientCredentialsExampleAction() |
||||
| 179 | { |
||||
| 180 | $request = $this->getAuthenticatedRequest('/client'); |
||||
| 181 | $response = $this->oAuthClient->getResponse($request); |
||||
| 182 | |||||
| 183 | $data = \json_decode($response->getBody()->getContents()); |
||||
| 184 | $response = new JsonResponse($data); |
||||
| 185 | |||||
| 186 | return $response; // usually the data would be sent to a view for display, but that's outwith the scope |
||||
| 187 | } |
||||
| 188 | |||||
| 189 | |||||
| 190 | /** |
||||
| 191 | * @param $content |
||||
| 192 | * @return Stream |
||||
| 193 | */ |
||||
| 194 | public function createStreamFromString($content) |
||||
| 195 | { |
||||
| 196 | $stream = new Stream('php://memory', 'wb+'); |
||||
| 197 | $stream->write($content); |
||||
| 198 | $stream->rewind(); |
||||
| 199 | |||||
| 200 | return $stream; |
||||
| 201 | } |
||||
| 202 | |||||
| 203 | |||||
| 204 | /** |
||||
| 205 | * @param array $data |
||||
| 206 | * @return MultipartStream |
||||
| 207 | */ |
||||
| 208 | public function createMultipartStream(array $data) |
||||
| 209 | { |
||||
| 210 | $elements = []; |
||||
| 211 | foreach ($data as $key => $val) { |
||||
| 212 | $elements[] = [ |
||||
| 213 | 'name' => $key, |
||||
| 214 | 'contents' => $val, |
||||
| 215 | ]; |
||||
| 216 | } |
||||
| 217 | $stream = new MultipartStream($elements); |
||||
| 218 | |||||
| 219 | return $stream; |
||||
| 220 | } |
||||
| 221 | |||||
| 222 | /** |
||||
| 223 | * @param $url |
||||
| 224 | * @param string $method |
||||
| 225 | * @return RequestInterface |
||||
| 226 | * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
||||
| 227 | */ |
||||
| 228 | public function getAuthenticatedRequest($url, $method = 'GET') |
||||
| 229 | { |
||||
| 230 | $token = $this->getAccessToken(); |
||||
| 231 | $request = $this->oAuthClient->getAuthenticatedRequest($method, $this->host . $url, $token); |
||||
| 232 | |||||
| 233 | return $request; |
||||
| 234 | } |
||||
| 235 | |||||
| 236 | /** |
||||
| 237 | * @param RequestInterface $request |
||||
| 238 | * @param array $data |
||||
| 239 | * @return RequestInterface |
||||
| 240 | */ |
||||
| 241 | public function addMultipartFormData(RequestInterface $request, array $data) |
||||
| 242 | { |
||||
| 243 | return $request->withBody($this->createMultipartStream($data)); |
||||
| 244 | } |
||||
| 245 | |||||
| 246 | /** |
||||
| 247 | * @return \League\OAuth2\Client\Token\AccessTokenInterface |
||||
| 248 | * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
||||
| 249 | */ |
||||
| 250 | private function getAccessToken() |
||||
| 251 | { |
||||
| 252 | return $this->oAuthClient->getAccessToken('client_credentials', ['scope' => ['admin']]); |
||||
| 253 | } |
||||
| 254 | } |
||||
| 255 |