Public Pages

A lot of apps in Nextcloud want to expose public pages in some form. This can be to share files, a calendar or anything else. To ensure all those pages benefit from the security enhancements we add simplified controllers were created for app developers to use.

Concept

A public page is there to display a resource that a user wants to expose via a public link in an app.

A public page is identified by some token and can be protected by a password.

If a public page is password protected by default we will show the normal authentication page to enter the password.

A public page can also call other endpoints. These endpoints operate very similarly with the difference that if the user is not properly authenticated it will throw a 404.

It is required that you have a parameter (probaby in your url) with the token. As an example your routes.php could look like:

  1. <?php
  2. return [
  3. 'routes' => [
  4. [ 'name' => 'PublicAPI#get', 'url' => '/api/{token}', 'verb' => 'GET' ],
  5. [ 'name' => 'PublicDisplay#get', 'url' => '/display/{token}', 'verb' => 'GET' ],
  6. ]
  7. ];

Implementing an API called from a public share page

As said the PublicShareController is a very basic controller. You need to implement 3 functions. isPasswordProtected, isValidToken and getPasswordHash.

  1. <?php
  2. namespace OCA\Share_Test\Controller;
  3. use OCP\AppFramework\PublicShareController;
  4. class PublicAPIController extends PublicShareController {
  5. /**
  6. * Return the hash of the password for this share.
  7. * This function is of course only called when isPasswordProtected is true
  8. */
  9. protected function getPasswordHash(): string {
  10. return md5('secretpassword');
  11. }
  12. /**
  13. * Validate the token of this share. If the token is invalid this controller
  14. * will return a 404.
  15. */
  16. public function isValidToken(): bool {
  17. return $this->getToken() === 'secretToken';
  18. }
  19. /**
  20. * Allows you to specify if this share is password protected
  21. */
  22. protected function isPasswordProtected(): bool {
  23. return true;
  24. }
  25. /**
  26. * Your normal controller function
  27. */
  28. public function get() {
  29. // Work your magic
  30. }
  31. }

You can also chose to overwrite the shareNotFound function. That is called when the token is not valid. You can do additional logging here for example.

Implementing an authenticated public page

On some pages password authentication might be required (just like the when you share a file via public link with a password). In this case you need to extend a different provider.

The AuthPublicShareController requiers in addition to the PublicShareController that you also implement the verifyPassword and showShare functions.

Additionally you can overwrite the showAuthenticate and showAuthFailed functions if you do now want to use the default authentication pages.

The authFailed and authSucceeded functions can be overwritten as well and are called depending on the if authentication passed or not. You can handle additional logging there for example.