refactor: Update Wopi::checkFileInfo() so it can authenticate users from access token.

This commit is contained in:
Pol Dellaiera 2021-09-29 16:11:46 +02:00
parent a2df3c8144
commit fb8b8b354f

View File

@ -14,6 +14,8 @@ use ChampsLibres\WopiLib\Contract\Service\WopiInterface;
use loophp\psr17\Psr17Interface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
final class ChillWopi implements WopiInterface
{
@ -21,15 +23,19 @@ final class ChillWopi implements WopiInterface
private Psr17Interface $psr17;
private UserProviderInterface $userProvider;
private WopiInterface $wopi;
public function __construct(
DocumentManagerInterface $documentManager,
Psr17Interface $psr17,
UserProviderInterface $userProvider,
WopiInterface $wopi
) {
$this->documentManager = $documentManager;
$this->psr17 = $psr17;
$this->userProvider = $userProvider;
$this->wopi = $wopi;
}
@ -38,6 +44,52 @@ final class ChillWopi implements WopiInterface
?string $accessToken,
RequestInterface $request
): ResponseInterface {
try {
$user = $this->userProvider->loadUserByUsername($accessToken);
} catch (UsernameNotFoundException $e) {
return $this
->psr17
->createResponse(401);
}
// @ TODO : Replace this with a call to decorated object once authentication is done.
$document = $this->documentManager->findByDocumentId($fileId);
$userIdentifier = $user->getUsername();
$userCacheKey = sprintf('wopi_putUserInfo_%s', $userIdentifier);
return $this
->psr17
->createResponse()
->withHeader('Content-Type', 'application/json')
->withBody($this->psr17->createStream((string) json_encode(
[
'BaseFileName' => $this->documentManager->getBasename($document),
'OwnerId' => 'Symfony',
'Size' => $this->documentManager->getSize($document),
'UserId' => $userIdentifier,
'ReadOnly' => false,
'UserCanAttend' => true,
'UserCanPresent' => true,
'UserCanRename' => true,
'UserCanWrite' => true,
'UserCanNotWriteRelative' => false,
'SupportsUserInfo' => true,
'SupportsDeleteFile' => true,
'SupportsLocks' => true,
'SupportsGetLock' => true,
'SupportsExtendedLockLength' => true,
'UserFriendlyName' => $userIdentifier,
'SupportsUpdate' => true,
'SupportsRename' => true,
'DisablePrint' => false,
'AllowExternalMarketplace' => true,
'SupportedShareUrlTypes' => [
'ReadOnly',
],
'SHA256' => $this->documentManager->getSha256($document),
'UserInfo' => (string) $this->cache->getItem($userCacheKey)->get(),
]
)));
$response = $this->wopi->checkFileInfo($fileId, $accessToken, $request);
$document = $this->documentManager->findByDocumentId($fileId);