Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Adding ability to re-generate refresh token if expired
Browse files Browse the repository at this point in the history
  • Loading branch information
ghecquet committed Jun 27, 2017
1 parent 7f89442 commit b48b0b5
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions core/src/plugins/core.access/src/Stream/OAuthStream.php
Expand Up @@ -21,18 +21,25 @@
use Pydio\Core\Services\CacheService; use Pydio\Core\Services\CacheService;
use Pydio\Core\Utils\FileHelper; use Pydio\Core\Utils\FileHelper;


use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Event\RequestEvents;
use GuzzleHttp\Event\SubscriberInterface;
use GuzzleHttp\Message\Response;



/** /**
* Class OAuthStream * Class OAuthStream
* @package Pydio\Access\Core\Stream * @package Pydio\Access\Core\Stream
*/ */
class OAuthStream implements StreamInterface class OAuthStream implements StreamInterface, SubscriberInterface
{ {
use StreamDecoratorTrait; use StreamDecoratorTrait;


/** @var ContextInterface Context */ /** @var ContextInterface Context */
private $context; private $context;




/** /**
* OAuthStream constructor. * OAuthStream constructor.
* @param StreamInterface $stream * @param StreamInterface $stream
Expand Down Expand Up @@ -135,12 +142,24 @@ public function __construct(


Stream::addContextOption($this->context, [ Stream::addContextOption($this->context, [
"auth" => "oauth2", "auth" => "oauth2",
"subscribers" => [$oauth2] "subscribers" => [$oauth2, $this]
]); ]);


$this->stream = $stream; $this->stream = $stream;
} }


/**
* Get the list of events this subscriber is being triggered on
*
* @return array Events
*/
public function getEvents()
{
return [
'error' => ['onError', RequestEvents::EARLY],
];
}

/** /**
* @param ContextInterface $ctx * @param ContextInterface $ctx
* @return array * @return array
Expand Down Expand Up @@ -227,4 +246,26 @@ private function setTokens($accessToken, $refreshToken)
public function getContents() { public function getContents() {
return $this->stream->getContents(); return $this->stream->getContents();
} }

/**
* Handle the before trigger
*
* @param ErrorEvent $event
* @internal param ErrorEvent $e
*/
public function onError(ErrorEvent $event)
{
$response = $event->getResponse();

if ($response && 401 == $response->getStatusCode()) {
$request = $event->getRequest();

if ($request->getConfig()->get('auth') == 'oauth2' && $request->getConfig()->get('retried')) {
$this->setTokens("", "");

//$request->getConfig()->set('retried', false);
//$event->intercept($event->getClient()->send($request));
}
}
}
} }

0 comments on commit b48b0b5

Please sign in to comment.