|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\V1Module\Presenters; |
| 4 | + |
| 5 | +use App\Exceptions\ForbiddenRequestException; |
| 6 | +use App\Exceptions\BadRequestException; |
| 7 | +use App\Model\Repository\Instances; |
| 8 | +use App\Model\Repository\Users; |
| 9 | +use App\Model\View\UserViewFactory; |
| 10 | +use App\Helpers\Extensions; |
| 11 | +use App\Security\AccessManager; |
| 12 | +use App\Security\TokenScope; |
| 13 | + |
| 14 | +/** |
| 15 | + * Endpoints handling 3rd party extensions communication. |
| 16 | + */ |
| 17 | +class ExtensionsPresenter extends BasePresenter |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var Extensions |
| 21 | + * @inject |
| 22 | + */ |
| 23 | + public $extensions; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Instances |
| 27 | + * @inject |
| 28 | + */ |
| 29 | + public $instances; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var Users |
| 33 | + * @inject |
| 34 | + */ |
| 35 | + public $users; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var AccessManager |
| 39 | + * @inject |
| 40 | + */ |
| 41 | + public $accessManager; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var UserViewFactory |
| 45 | + * @inject |
| 46 | + */ |
| 47 | + public $userViewFactory; |
| 48 | + |
| 49 | + public function checkUrl(string $extId, string $instanceId) |
| 50 | + { |
| 51 | + $user = $this->getCurrentUser(); |
| 52 | + $extension = $this->extensions->getExtension($extId); |
| 53 | + $instance = $this->instances->findOrThrow($instanceId); |
| 54 | + if (!$extension || !$extension->isAccessible($instance, $user)) { |
| 55 | + throw new ForbiddenRequestException(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Return URL refering to the extension with properly injected temporary JWT token. |
| 61 | + * @GET |
| 62 | + * @Param(type="query", name="locale", required=false, validation="string:2") |
| 63 | + */ |
| 64 | + public function actionUrl(string $extId, string $instanceId, ?string $locale) |
| 65 | + { |
| 66 | + $user = $this->getCurrentUser(); |
| 67 | + $extension = $this->extensions->getExtension($extId); |
| 68 | + |
| 69 | + $token = $this->accessManager->issueToken( |
| 70 | + $user, |
| 71 | + null, |
| 72 | + [TokenScope::EXTENSIONS], |
| 73 | + $extension->getUrlTokenExpiration(), |
| 74 | + ["instance" => $instanceId, "extension" => $extId] |
| 75 | + ); |
| 76 | + |
| 77 | + if (!$locale) { |
| 78 | + $locale = $this->getCurrentUserLocale(); |
| 79 | + } |
| 80 | + |
| 81 | + $this->sendSuccessResponse($extension->getUrl($token, $locale)); |
| 82 | + } |
| 83 | + |
| 84 | + public function checkToken(string $extId) |
| 85 | + { |
| 86 | + /* |
| 87 | + * This checker does not employ traditional ACLs for permission checks since it is trvial and it is better |
| 88 | + * to keep everything here (in one place). However, this may change in the future should the presenter get |
| 89 | + * more complex. |
| 90 | + * This action expects to be authenticated by temporary token generated in 'url' action. |
| 91 | + */ |
| 92 | + |
| 93 | + // All users within this scope are allowed the operation... |
| 94 | + $this->isInScope(TokenScope::EXTENSIONS); |
| 95 | + |
| 96 | + // ...but the token must be also valid... |
| 97 | + $token = $this->getAccessToken(); |
| 98 | + $instanceId = $token->getPayload('instance'); |
| 99 | + if ($token->getPayload('extension') !== $extId || !$instanceId) { |
| 100 | + throw new BadRequestException(); |
| 101 | + } |
| 102 | + |
| 103 | + // ...and the extension must be accessible by the user. |
| 104 | + $user = $this->getCurrentUser(); |
| 105 | + $extension = $this->extensions->getExtension($extId); |
| 106 | + $instance = $this->instances->findOrThrow($instanceId); |
| 107 | + if (!$extension || !$extension->isAccessible($instance, $user)) { |
| 108 | + throw new ForbiddenRequestException(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * This endpoint is used by a backend of an extension to get a proper access token |
| 114 | + * (from a temp token passed via URL). It also returns details about authenticated user. |
| 115 | + * @POST |
| 116 | + */ |
| 117 | + public function actionToken(string $extId) |
| 118 | + { |
| 119 | + $user = $this->getCurrentUser(); |
| 120 | + $extension = $this->extensions->getExtension($extId); |
| 121 | + $authUser = $extension->getTokenUserId() ? $this->users->findOrThrow($extension->getTokenUserId()) : $user; |
| 122 | + |
| 123 | + $token = $this->accessManager->issueToken( |
| 124 | + $authUser, |
| 125 | + null, |
| 126 | + $extension->getTokenScopes(), |
| 127 | + $extension->getTokenExpiration(), |
| 128 | + ); |
| 129 | + |
| 130 | + $this->sendSuccessResponse([ |
| 131 | + "accessToken" => $token, |
| 132 | + "user" => $this->userViewFactory->getFullUser($user, false /* do not show really everything */), |
| 133 | + ]); |
| 134 | + } |
| 135 | +} |
0 commit comments