public function __construct(RemoteStorage $remoteStorage, ApprovalManagementStorage $approvalManagementStorage, TemplateManagerInterface $templateManager, ClientStorageInterface $clientStorage, ResourceServerStorageInterface $resourceServerStorage, ApprovalStorageInterface $approvalStorage, AuthorizationCodeStorageInterface $authorizationCodeStorage, AccessTokenStorageInterface $accessTokenStorage, array $options = array(), IO $io = null) { $this->remoteStorage = $remoteStorage; $this->approvalManagementStorage = $approvalManagementStorage; parent::__construct($templateManager, $clientStorage, $resourceServerStorage, $approvalStorage, $authorizationCodeStorage, $accessTokenStorage, $options, $io); $this->get('/_account', function (Request $request, UserInfoInterface $userInfo) { $approvalList = $this->approvalManagementStorage->getApprovalList($userInfo->getUserId()); return $this->templateManager->render('getAccountPage', array('approval_list' => $approvalList, 'host' => $request->getHeader('Host'), 'user_id' => $userInfo->getUserId(), 'disk_usage' => $this->remoteStorage->getFolderSize(new Path('/' . $userInfo->getUserId() . '/')), 'request_url' => $request->getUrl()->toString(), 'show_account_icon' => true)); }, array('fkooman\\Rest\\Plugin\\Authentication\\AuthenticationPlugin' => array('activate' => array('user')))); $this->delete('/_approvals', function (Request $request, UserInfoInterface $userInfo) { $deleteApprovalRequest = RequestValidation::validateDeleteApprovalRequest($request); $approval = new Approval($userInfo->getUserId(), $deleteApprovalRequest['client_id'], $deleteApprovalRequest['response_type'], $deleteApprovalRequest['scope']); $this->approvalManagementStorage->deleteApproval($approval); return new RedirectResponse($request->getUrl()->getRootUrl() . '_account', 302); }, array('fkooman\\Rest\\Plugin\\Authentication\\AuthenticationPlugin' => array('activate' => array('user')))); $this->get('/.well-known/webfinger', function (Request $request) { $resource = $request->getUrl()->getQueryParameter('resource'); if (null === $resource) { throw new BadRequestException('resource parameter missing'); } if (0 !== strpos($resource, 'acct:')) { throw new BadRequestException('unsupported resource type'); } $userAddress = substr($resource, 5); $atPos = strpos($userAddress, '@'); if (false === $atPos) { throw new BadRequestException('invalid user address'); } $user = substr($userAddress, 0, $atPos); $host = substr($userAddress, $atPos + 1); //if($host !== $request->getUrl()->getHost()) { // throw new BadRequestException(sprintf('host of webfinger resource does not match host of request %s', $host)); //} $webFingerData = array('links' => array(array('href' => sprintf('%s%s', $request->getUrl()->getRootUrl(), $user), 'properties' => array('http://remotestorage.io/spec/version' => 'draft-dejong-remotestorage-05', 'http://remotestorage.io/spec/web-authoring' => null, 'http://tools.ietf.org/html/rfc6749#section-4.2' => sprintf('%s_oauth/authorize?login_hint=%s', $request->getUrl()->getRootUrl(), $user), 'http://tools.ietf.org/html/rfc6750#section-2.3' => null, 'http://tools.ietf.org/html/rfc7233' => 'development' !== $this->options['server_mode'] ? 'GET' : null), 'rel' => 'http://tools.ietf.org/id/draft-dejong-remotestorage'), array('href' => sprintf('%s%s', $request->getUrl()->getRootUrl(), $user), 'properties' => array('http://remotestorage.io/spec/version' => 'draft-dejong-remotestorage-03', 'http://tools.ietf.org/html/rfc2616#section-14.16' => 'development' !== $this->options['server_mode'] ? 'GET' : false, 'http://tools.ietf.org/html/rfc6749#section-4.2' => sprintf('%s_oauth/authorize?login_hint=%s', $request->getUrl()->getRootUrl(), $user), 'http://tools.ietf.org/html/rfc6750#section-2.3' => false), 'rel' => 'remotestorage'))); $response = new Response(200, 'application/jrd+json'); $response->setBody(Json::encode($webFingerData)); return $response; }, array('fkooman\\Rest\\Plugin\\Authentication\\AuthenticationPlugin' => array('enabled' => false))); $this->get('/', function (Request $request, UserInfoInterface $userInfo = null) { return $this->templateManager->render('indexPage', array('user_id' => null !== $userInfo ? $userInfo->getUserId() : null, 'show_account_icon' => true)); }, array('fkooman\\Rest\\Plugin\\Authentication\\AuthenticationPlugin' => array('activate' => array('user'), 'require' => false))); $this->addRoute(['GET', 'HEAD'], '*', function (Request $request, TokenInfo $tokenInfo = null) { return $this->getObject($request, $tokenInfo); }, array('fkooman\\Rest\\Plugin\\Authentication\\AuthenticationPlugin' => array('activate' => array('api'), 'require' => false))); // put a document $this->put('*', function (Request $request, TokenInfo $tokenInfo) { return $this->putDocument($request, $tokenInfo); }, array('fkooman\\Rest\\Plugin\\Authentication\\AuthenticationPlugin' => array('activate' => array('api')), 'fkooman\\Rest\\Plugin\\ReferrerCheck\\ReferrerCheckPlugin' => array('enabled' => false))); // delete a document $this->delete('*', function (Request $request, TokenInfo $tokenInfo) { return $this->deleteDocument($request, $tokenInfo); }, array('fkooman\\Rest\\Plugin\\Authentication\\AuthenticationPlugin' => array('activate' => array('api')), 'fkooman\\Rest\\Plugin\\ReferrerCheck\\ReferrerCheckPlugin' => array('enabled' => false))); // options request $this->options('*', function (Request $request) { return $this->optionsRequest($request); }, array('fkooman\\Rest\\Plugin\\Authentication\\AuthenticationPlugin' => array('enabled' => false))); }
public function testGetFolderWithFolder() { $p1 = new Path('/admin/messages/foo/baz.txt'); $p2 = new Path('/admin/messages/foo/foobar/bar.txt'); $p3 = new Path('/admin/messages/foo/'); $this->r->putDocument($p1, 'text/plain', 'Hello Baz!'); $this->r->putDocument($p2, 'text/plain', 'Hello Bar!'); $this->r->putDocument($p2, 'text/plain', 'Hello Updated Bar!'); $j = new Json(); $folderData = $j->decode($this->r->getFolder($p3)); $this->assertEquals(2, count($folderData)); $this->assertEquals(2, count($folderData['items'])); $this->assertEquals('http://remotestorage.io/spec/folder-description', $folderData['@context']); $this->assertRegexp('/2:[a-z0-9]+/i', $folderData['items']['foobar/']['ETag']); $this->assertRegexp('/1:[a-z0-9]+/i', $folderData['items']['baz.txt']['ETag']); $this->assertEquals('text/plain', $folderData['items']['baz.txt']['Content-Type']); $this->assertEquals(10, $folderData['items']['baz.txt']['Content-Length']); $this->assertRegexp('/3:[a-z0-9]+/i', $this->r->getVersion($p3)); }
public function getFolder(Path $p, array $ifNoneMatch = null) { if (null !== $ifNoneMatch && in_array($this->md->getVersion($p), $ifNoneMatch)) { throw new RemoteStorageException('folder not modified'); } $f = array('@context' => 'http://remotestorage.io/spec/folder-description', 'items' => $this->d->getFolder($p)); foreach ($f['items'] as $name => $meta) { $f['items'][$name]['ETag'] = $this->md->getVersion(new Path($p->getFolderPath() . $name)); // if item is a folder we don't want Content-Type if (strrpos($name, '/') !== strlen($name) - 1) { $f['items'][$name]['Content-Type'] = $this->md->getContentType(new Path($p->getFolderPath() . $name)); } } return Json::encode($f, JSON_FORCE_OBJECT); }
public function retrieveAuthorizationCode($authorizationCode) { $data = Json::decode(Base64Url::decode($authorizationCode)); return new AuthorizationCode($data['client_id'], $data['user_id'], $data['issued_at'], $data['redirect_uri'], $data['scope']); }
public function retrieveAccessToken($accessToken) { $data = Json::decode(Base64Url::decode($accessToken)); return new AccessToken($data['client_id'], $data['user_id'], $data['issued_at'], $data['scope']); }
public function testForceObject() { $this->assertEquals('{}', Json::encode(array(), JSON_FORCE_OBJECT)); }
<?php require_once dirname(__DIR__) . '/vendor/autoload.php'; use fkooman\IndieOAuth\MyOAuthService; use fkooman\Ini\IniReader; use fkooman\Json\Json; use fkooman\OAuth\OAuthServer; use fkooman\OAuth\Storage\JsonResourceServerStorage; use fkooman\OAuth\Storage\JsonClientStorage; use fkooman\OAuth\Storage\PdoCodeTokenStorage; use fkooman\Rest\Plugin\Authentication\Basic\BasicAuthentication; use fkooman\Rest\Plugin\Authentication\IndieAuth\IndieAuthAuthentication; use fkooman\Tpl\Twig\TwigTemplateManager; // CONFIG $iniReader = IniReader::fromFile(dirname(__DIR__) . '/config/server.ini'); // USER AUTH #$userAuthentication = new IndieAuthAuthentication(); #$userAuthentication->setUnauthorizedRedirectUri('/identify'); $userAuthentication = new BasicAuthentication(function ($userId) { // read users file $r = Json::decodeFile(dirname(__DIR__) . '/config/users.json'); return $r[$userId]['secret']; }, array('realm' => 'OAuth')); $db = new PDO($iniReader->v('Db', 'dsn'), $iniReader->v('Db', 'username', false), $iniReader->v('Db', 'password', false)); $pdoCodeTokenStorage = new PdoCodeTokenStorage($db); $t = new TwigTemplateManager(array(dirname(__DIR__) . '/views', dirname(__DIR__) . '/config/views')); $o = new OAuthServer($t, new JsonClientStorage(dirname(__DIR__) . '/config/clients.json'), new JsonResourceServerStorage(dirname(__DIR__) . '/config/resource_servers.json'), $pdoCodeTokenStorage, $pdoCodeTokenStorage); $service = new MyOAuthService($o, $userAuthentication); $service->run()->send();