Skip to content

Commit

Permalink
chore: Move config to service class
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Jul 4, 2024
1 parent 1a8f868 commit e12723a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ Both the server and the Nextcloud instance must be accessible from the same netw
On the Nextcloud side, the server must be configured through:

```bash
occ config:app:set whiteboard websocket_server_url --value="ws://websocket-server:3002"
occ config:system:set whiteboard jwt_secret_key --value="some-random"
occ config:app:set whiteboard collabBackendUrl --value="nextcloud.local:3003"
occ config:app:set whiteboard jwt_secret_key --value="some-random"
```

#### Local node

Expand Down Expand Up @@ -71,3 +72,19 @@ services:

```
### Reverse proxy
#### Nginx
```
location /whiteboard/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:3002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
```
14 changes: 6 additions & 8 deletions lib/Controller/JWTController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

use Firebase\JWT\JWT;
use OC\User\NoUserException;
use OCA\Whiteboard\Service\ConfigService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;

Expand All @@ -28,15 +28,13 @@
final class JWTController extends Controller {
private const EXPIRATION_TIME = 15 * 60;

private const JWT_CONFIG_KEY = 'jwt_secret_key';

private const JWT_ALGORITHM = 'HS256';
public const JWT_ALGORITHM = 'HS256';

public function __construct(
IRequest $request,
IRequest $request,
private IUserSession $userSession,
private IConfig $config,
private IRootFolder $rootFolder
private ConfigService $configService,
private IRootFolder $rootFolder
) {
parent::__construct('whiteboard', $request);
}
Expand Down Expand Up @@ -89,7 +87,7 @@ public function getJWT(int $fileId): DataResponse {
return new DataResponse(['message' => 'File not found'], Http::STATUS_NOT_FOUND);
}

$key = $this->config->getSystemValueString(self::JWT_CONFIG_KEY, 'secret');
$key = $this->configService->getJwtSecretKey();
$issuedAt = time();
$expirationTime = $issuedAt + self::EXPIRATION_TIME;
$payload = [
Expand Down
8 changes: 4 additions & 4 deletions lib/Controller/WhiteboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use OC\User\NoUserException;
use OCA\Whiteboard\Service\ConfigService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
Expand All @@ -19,7 +20,6 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;

Expand All @@ -33,7 +33,7 @@ public function __construct(
IRequest $request,
private IUserSession $userSession,
private IRootFolder $rootFolder,
private IConfig $config
private ConfigService $configService
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -86,8 +86,8 @@ public function show(int $fileId): DataResponse {
}

try {
$key = $this->config->getSystemValueString('jwt_secret_key', 'secret');
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
$key = $this->configService->getJwtSecretKey();
$decoded = JWT::decode($jwt, new Key($key, JWTController::JWT_ALGORITHM));
$userId = $decoded->userid;
} catch (\Exception $e) {
return new DataResponse(['message' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
Expand Down
7 changes: 3 additions & 4 deletions lib/Listener/LoadViewerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@
namespace OCA\Whiteboard\Listener;

use OCA\Viewer\Event\LoadViewer;
use OCA\Whiteboard\AppInfo\Application;
use OCA\Whiteboard\Service\ConfigService;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\Util;

/** @template-implements IEventListener<LoadViewer|Event> */
class LoadViewerListener implements IEventListener {
public function __construct(
private IInitialState $initialState,
private IConfig $config,
private ConfigService $configService,
) {
}

Expand All @@ -36,7 +35,7 @@ public function handle(Event $event): void {

$this->initialState->provideInitialState(
'collabBackendUrl',
$this->config->getAppValue(Application::APP_ID, 'collabBackendUrl', '')
$this->configService->getCollabBackendUrl()
);
}
}

0 comments on commit e12723a

Please sign in to comment.