Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate private with preliminary public api #338

Merged
merged 23 commits into from
May 10, 2017

Conversation

marcelklehr
Copy link
Member

@marcelklehr marcelklehr commented Feb 10, 2017

This basically changes access to the private bookmarks and tags API to everyone without CSRF tokens and with CORS support, removes the public controller, and adjusts the public controller tests to work with the bookmark controller.

This is a work-in-progress and a follow-up on #322

Fixes #330, #328, #2

@marcelklehr marcelklehr changed the title Consolidate private and preliminary public api Consolidate private with preliminary public api Feb 10, 2017
@marcelklehr
Copy link
Member Author

marcelklehr commented Feb 10, 2017

I merged public_controller#return_as_json() into bookmarkcontroller#getBookmarks. As a result there's some duplicate functionality / redundant parameters now. This is to keep compatibility with the client-side code. If you want, I can adjust the client-side code to use the new parameters and remove the old ones.

Nonetheless, all tests are passing now and I also added docs for the API to the README :)

array('name' => 'public#return_as_json', 'url' => '/public/rest/v1/bookmark', 'verb' => 'GET'),
array('name' => 'public#new_bookmark', 'url' => '/public/rest/v1/bookmark', 'verb' => 'POST'),
array('name' => 'public#edit_bookmark', 'url' => '/public/rest/v1/bookmark/{id}', 'verb' => 'PUT'),
array('name' => 'public#delete_bookmark', 'url' => '/public/rest/v1/bookmark/{id}', 'verb' => 'DELETE'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally removing them would kill compatibility. Better if we could deprecate them over a couple releases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to keep the newly added CRUD methods, though, right? They haven't made it into any release yet, afaik

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, what wasn't there in 0.9.1 is vogelfrei.

$sortby = "",
$search = array()
) {
if ($user == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always use === as equal comparison parameter

//Legacy Routes
array('name' => 'public#return_as_json', 'url' => '/public/rest/v1/bookmark', 'verb' => 'GET'),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ok?

@blizzz
Copy link
Member

blizzz commented Apr 29, 2017

I rebased this locally. It seems something is wrong (perhaps with the annotated checks), because I am being logged out when access the bookmarks page normally via web interface.

@blizzz
Copy link
Member

blizzz commented Apr 29, 2017

there lies the issue i think

{
  …"message":"Undefined index: PHP_AUTH_USER at \/srv\/http\/nextcloud\/master\/lib\/private\/AppFramework\/Middleware\/Security\/CORSMiddleware.php#88"
…

not the index, but that we are logged out and have no provided parameters for logging in.

@marcelklehr
Copy link
Member Author

So, the problem I presume is that the client doesn't send auth data? Shouldn't there be a session cookie being sent along?

@blizzz
Copy link
Member

blizzz commented Apr 29, 2017

The CORS annotation makes sure that sessions are not being used, therefore the logout happens.

@marcelklehr
Copy link
Member Author

So, how do I realize an API that should be able to be used from within nextcloud as well as from other sites? Do we need to create a proxy Controller without CORS?

to fix app ui logging out onload
@blizzz
Copy link
Member

blizzz commented Apr 29, 2017

Perhaps taking away CORS? But then, it was added for a reason. Or not?

Do we need to create a proxy Controller without CORS?

Seems so, at least my thoughts go into the same direction.

@marcelklehr
Copy link
Member Author

marcelklehr commented Apr 30, 2017

Without CORS it works, so there's some mistake in how I added CORS, I guess.

Update: The tutorial doesn't mention anything else, though: https://docs.nextcloud.com/server/12/developer_manual/app/tutorial.html#adding-a-restful-api-optional

@marcelklehr
Copy link
Member Author

@marcelklehr
Copy link
Member Author

BernhardPosselt on IRC pointed out to me as well that CORS drops sessions, so we do need two controllers. The question is if this pr makes sense at all in this case. The whole idea was to have only one API controller. In this PR I basically merged the two, but now we're exposing the same interface at two different routes... Might still be better than having two entirely different interfaces on the two endpoints... What do you say?

@blizzz
Copy link
Member

blizzz commented May 1, 2017

Sidenote: just came across the added @NoCSRFRequired annotations. Those would make it vulnerable to CSFR attacks. For pure API functionality this is necessary to drop indeed, but not within our interface, because sessions are active.

If there is no use case for CORS (i am not aware of an request?), we can leave that out. The Passman middlewere reference would be about it?

Two different routes are OK imho, however I do not see them duplicated?

Actually, now I am thinking whether we should not keep a dedicated apicontroller, which would just inherit the internal one and just change the annotations, and call the parent. Probably there is a hook, though.

@marcelklehr
Copy link
Member Author

marcelklehr commented May 1, 2017

I agree on the CSRF issue.

CORS is not important to me, either.

I agree on the approach and have started working on this.

@marcelklehr
Copy link
Member Author

I think @cors imight be necessary for security, though, since NoCSRFRequired is added to API endpoints and all...?

@blizzz
Copy link
Member

blizzz commented May 5, 2017

I think @cors imight be necessary for security, though, since NoCSRFRequired is added to API endpoints and all...?

Not really for security. While it disables sessions (client would also need to reauthenticate), it also allows to be used across origins, i.e. from other websites/webapps. Sounds OK to me to include it nevertheless.

@blizzz
Copy link
Member

blizzz commented May 5, 2017

The changes seem to work so far from the UI. However, when returning bookmarks, only a piece of the data is returned back.

Expected:

spectacle hz7378

Returned:

spectacle yv7378

You'll see it as the date in the list is represented as "Invalid date" in the UI.

@marcelklehr
Copy link
Member Author

marcelklehr commented May 5, 2017

This is a symptom of merging the two APIs, it can be easily fixed by either modifying the client-side to include a parameter that tells the API all attributes it needs, or by modifying the API to deliver all those by default. Your call :)

@blizzz
Copy link
Member

blizzz commented May 6, 2017

Just return all of them. It keeps things simple, and the only extra cost comes from the tags. This is fine, and nothing can go wrong by chance (e.g
updating a bookmark).

Copy link
Member

@blizzz blizzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now and I went through the formalities. Bigger things: an undeclared method is used and a typo in a variable name. Other comments are basically about style/form.

$tags = array(),
$conjunction = "or",
$sortby = "",
$search = array()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpdoc (@param) annotation is missing for $user, $tags, $conjunction, $sortby, $search (yes, it was missing before already).

}else {
$publicOnly = true;
if ($this->userManager->userExists($user) == false) {
return $this->newJsonErrorMessage("User could not be identified");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newJsonErrorMessage() does not exist here

@@ -218,7 +276,7 @@ public function deleteBookmark($id = -1) {
}

/**
@NoAdminRequired
@NoAdminRequired
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • missing

@@ -243,7 +301,7 @@ public function clickBookmark($url = "") {
}

/**
@NoAdminRequired
@NoAdminRequired
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • missing

@@ -271,7 +329,7 @@ public function importBookmark() {
}

/**
@NoAdminRequired
@NoAdminRequired
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • missing


use \OCA\Bookmarks\Controller\Lib\Bookmarks;
use \OCP\AppFramework\Http\JSONResponse;
use \OCP\AppFramework\Http;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not in use

use \OCP\AppFramework\Http\JSONResponse;
use \OCP\AppFramework\Http;
use \OCP\AppFramework\ApiController;
use OCA\Bookmarks\Controller\Rest\InternalTagsController;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not in use

@@ -19,91 +19,111 @@ class Test_PublicController_Bookmarks extends TestCase {
private $db;
private $userManager;
/** @var PublicController */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should become BookmarkController

private $publicController;

protected function setUp() {
parent::setUp();

$this->userid = "testuser";
$this->otherUser = "otheruser";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declaration missing

@@ -19,91 +19,111 @@ class Test_PublicController_Bookmarks extends TestCase {
private $db;
private $userManager;
/** @var PublicController */
private $controller;
private $publicController;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would need the /** @var BookmarkController */ typehint, too


public function __construct($appName, IRequest $request, $userId, IDBConnection $db, IL10N $l10n, Bookmarks $bookmarks, Manager $userManager) {
parent::__construct($appName, $request);
$this->publicController = new BookmarkController($appname, $request, $userId, $db, $l10n, $bookmarks, $userManager);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, missed this before. Typo in $appname, the n must be upper case.

@@ -57,27 +60,81 @@ public function legacyGetBookmarks($type = "bookmark", $tag = '', $page = 0, $so
* @param string $tag
* @param int $page
* @param string $sort
* @param string user
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, but the $'s are missing in the var names

* @param string $tag
* @param int $page
* @param string $sort
* @param string user
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too 😇

@blizzz
Copy link
Member

blizzz commented May 10, 2017

I leave the two cosmetics for a quick additional PR. And use the moment to merge this. Good work, thanks a lot @marcelklehr :) 🚀 🎸

@blizzz blizzz merged commit 8d68ecb into nextcloud:master May 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants