|
10 | 10 |
|
11 | 11 | use OC\Authentication\Token\IToken; |
12 | 12 | use OCA\GlobalSiteSelector\AppInfo\Application; |
| 13 | +use OCA\GlobalSiteSelector\Exceptions\LocalFederatedShareException; |
13 | 14 | use OCA\GlobalSiteSelector\Exceptions\MasterUrlException; |
| 15 | +use OCA\GlobalSiteSelector\Exceptions\SharedFileException; |
14 | 16 | use OCA\GlobalSiteSelector\GlobalSiteSelector; |
| 17 | +use OCA\GlobalSiteSelector\Model\LocalFile; |
| 18 | +use OCA\GlobalSiteSelector\Service\GlobalScaleService; |
| 19 | +use OCA\GlobalSiteSelector\Service\GlobalShareService; |
15 | 20 | use OCA\GlobalSiteSelector\Service\SlaveService; |
16 | 21 | use OCA\GlobalSiteSelector\Slave; |
17 | 22 | use OCA\GlobalSiteSelector\TokenHandler; |
|
41 | 46 | * @package OCA\GlobalSiteSelector\Controller |
42 | 47 | */ |
43 | 48 | class SlaveController extends OCSController { |
44 | | - |
45 | 49 | public function __construct( |
46 | 50 | $appName, |
47 | 51 | IRequest $request, |
48 | | - private GlobalSiteSelector $gss, |
49 | | - private IUserSession $userSession, |
50 | | - private IURLGenerator $urlGenerator, |
51 | | - private ICrypto $crypto, |
52 | | - private TokenHandler $tokenHandler, |
53 | | - private IUserManager $userManager, |
54 | | - private UserBackend $userBackend, |
55 | | - private ISession $session, |
56 | | - private SlaveService $slaveService, |
57 | | - private IConfig $config, |
58 | | - private LoggerInterface $logger, |
| 52 | + private readonly GlobalSiteSelector $gss, |
| 53 | + private readonly IUserSession $userSession, |
| 54 | + private readonly IURLGenerator $urlGenerator, |
| 55 | + private readonly ICrypto $crypto, |
| 56 | + private readonly TokenHandler $tokenHandler, |
| 57 | + private readonly IUserManager $userManager, |
| 58 | + private readonly UserBackend $userBackend, |
| 59 | + private readonly ISession $session, |
| 60 | + private readonly SlaveService $slaveService, |
| 61 | + private readonly GlobalScaleService $globalScaleService, |
| 62 | + private readonly GlobalShareService $globalShareService, |
| 63 | + private readonly IConfig $config, |
| 64 | + private readonly LoggerInterface $logger, |
59 | 65 | ) { |
60 | 66 | parent::__construct($appName, $request); |
61 | 67 | } |
62 | 68 |
|
| 69 | + |
| 70 | + /** |
| 71 | + * @PublicPage |
| 72 | + * @NoCSRFRequired |
| 73 | + */ |
| 74 | + public function discovery(): DataResponse { |
| 75 | + // public data, reachable from outside. |
| 76 | + return new DataResponse(['token' => $this->globalScaleService->getLocalToken()]); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * return sharing details about a file. |
| 81 | + * request must contain encoded jwt. |
| 82 | + * |
| 83 | + * @PublicPage |
| 84 | + * @NoCSRFRequired |
| 85 | + */ |
| 86 | + public function sharedFile(?string $jwt): DataResponse { |
| 87 | + if ($jwt === null) { |
| 88 | + return new DataResponse(['message' => 'missing jwt'], Http::STATUS_NOT_FOUND); |
| 89 | + } |
| 90 | + $key = $this->gss->getJwtKey(); |
| 91 | + $decoded = (array)JWT::decode($jwt, new Key($key, Application::JWT_ALGORITHM)); |
| 92 | + // JWT store data as stdClass, not array |
| 93 | + $decoded = json_decode(json_encode($decoded), true); |
| 94 | + |
| 95 | + $this->logger->debug('decoded request', ['data' => $decoded]); |
| 96 | + |
| 97 | + $fileId = (int)($decoded['fileId'] ?? 0); |
| 98 | + $shareId = (int)($decoded['shareId'] ?? 0); |
| 99 | + $instance = $decoded['instance'] ?? ''; |
| 100 | + |
| 101 | + $target = new LocalFile(); |
| 102 | + $target->import($decoded['target'] ?? []); |
| 103 | + |
| 104 | + try { |
| 105 | + // the file is local and returns shares related to it |
| 106 | + return new DataResponse($this->globalShareService->getSharedFiles($fileId, $shareId, $instance, $target)); |
| 107 | + } catch (SharedFileException $e) { |
| 108 | + // file not found |
| 109 | + return new DataResponse(['message' => $e->getMessage()], Http::STATUS_NOT_FOUND); |
| 110 | + } catch (LocalFederatedShareException $e) { |
| 111 | + // the file is not local and returns the shared folder and the path to the file |
| 112 | + return new DataResponse($e->getFederatedShare(), Http::STATUS_MOVED_PERMANENTLY); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + |
63 | 117 | /** |
64 | 118 | * @PublicPage |
65 | 119 | * @NoCSRFRequired |
|
0 commit comments