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

Rework Authentication into separate services #1491

Merged
merged 5 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
- Source filters are stricter, they need to start and end with a `/`. ([#1423](https://github.com/fossar/selfoss/pull/1423))
- OPML importer has been merged into the React client. ([#1442](https://github.com/fossar/selfoss/pull/1442))
- Web requests will send `Accept-Encoding` header. ([#1482](https://github.com/fossar/selfoss/pull/1482))
- Authentication system has been rewritten to allow more methods in the future. ([#1491](https://github.com/fossar/selfoss/pull/1491))
- Authentication will now also log user out when the credentials in the config change. ([#1491](https://github.com/fossar/selfoss/pull/1491))
- Requests from loopback IP address now give full access to all operations, not just update. Additionally, IPv6 loopback address is recognized and proxies are ignored. ([#1491](https://github.com/fossar/selfoss/pull/1491))

#### For developers
- Back-end source code is now checked using [PHPStan](https://phpstan.org/). ([#1409](https://github.com/fossar/selfoss/pull/1409))
Expand Down
9 changes: 9 additions & 0 deletions src/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ function boot_error(string $message) {
->register(helpers\Authentication::class)
->setShared(true)
;

$container
->register(
helpers\Authentication\AuthenticationService::class,
[new Slince\Di\Reference(helpers\Authentication\AuthenticationFactory::class), 'create']
)
->setShared(true)
;

$container
->register(helpers\Session::class)
->setShared(true)
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/About.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function about(): void {
'htmlTitle' => trim($this->configuration->htmlTitle), // string
'allowPublicUpdate' => $this->configuration->allowPublicUpdateAccess, // bool
'publicMode' => $this->configuration->public, // bool
'authEnabled' => $this->authentication->enabled() === true, // bool
'authEnabled' => $this->authentication->enabled(), // bool
'readingSpeed' => $this->configuration->readingSpeedWpm > 0 ? $this->configuration->readingSpeedWpm : null, // ?int
'language' => $this->configuration->language === '0' ? null : $this->configuration->language, // ?string
'userCss' => file_exists(BASEDIR . '/user.css') ? filemtime(BASEDIR . '/user.css') : null, // ?int
Expand Down
23 changes: 9 additions & 14 deletions src/controllers/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace controllers;

use helpers;
use helpers\Authentication\AuthenticationService;
use helpers\View;

/**
* Controller for user related tasks
*/
class Authentication {
private helpers\Authentication $authentication;
private AuthenticationService $authenticationService;
private View $view;

public function __construct(helpers\Authentication $authentication, View $view) {
$this->authentication = $authentication;
public function __construct(AuthenticationService $authenticationService, View $view) {
$this->authenticationService = $authenticationService;
$this->view = $view;
}

Expand All @@ -26,17 +26,11 @@ public function __construct(helpers\Authentication $authentication, View $view)
public function login(): void {
$error = null;

if (isset($_REQUEST['username'])) {
$username = $_REQUEST['username'];
} else {
$username = '';
if (!isset($_REQUEST['username'])) {
$error = 'no username given';
}

if (isset($_REQUEST['password'])) {
$password = $_REQUEST['password'];
} else {
$password = '';
if (!isset($_REQUEST['password'])) {
$error = 'no password given';
}

Expand All @@ -47,7 +41,8 @@ public function login(): void {
]);
}

if ($this->authentication->login($username, $password)) {
// The function automatically checks the request for credentials.
if ($this->authenticationService->isPrivileged()) {
$this->view->jsonSuccess([
'success' => true,
]);
Expand All @@ -64,7 +59,7 @@ public function login(): void {
* json
*/
public function logout(): void {
$this->authentication->logout();
$this->authenticationService->destroy();
$this->view->jsonSuccess([
'success' => true,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Helpers/HashPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(Authentication $authentication, View $view) {
* json
*/
public function hash(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

if (!isset($_POST['password'])) {
$this->view->jsonError([
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function home(): void {
return;
}

$this->authentication->needsLoggedInOrPublicMode();
$this->authentication->ensureCanRead();

// load tags
$tags = $this->tagsDao->getWithUnread();
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
* @param ?string $itemId ID of item to mark as read
*/
public function mark(?string $itemId = null): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

$ids = null;
if ($itemId !== null) {
Expand Down Expand Up @@ -84,7 +84,7 @@ public function mark(?string $itemId = null): void {
* @param string $itemId id of an item to mark as unread
*/
public function unmark(string $itemId): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

try {
$itemId = Misc::forceId($itemId);
Expand All @@ -106,7 +106,7 @@ public function unmark(string $itemId): void {
* @param string $itemId id of an item to starr
*/
public function starr(string $itemId): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

try {
$itemId = Misc::forceId($itemId);
Expand All @@ -127,7 +127,7 @@ public function starr(string $itemId): void {
* @param string $itemId id of an item to unstarr
*/
public function unstarr(string $itemId): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

try {
$itemId = Misc::forceId($itemId);
Expand All @@ -146,7 +146,7 @@ public function unstarr(string $itemId): void {
* json
*/
public function listItems(): void {
$this->authentication->needsLoggedInOrPublicMode();
$this->authentication->ensureCanRead();

// parse params
$options = new ItemOptions($_GET);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Items/Stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(Authentication $authentication, \daos\Items $itemsDa
* json
*/
public function stats(): void {
$this->authentication->needsLoggedInOrPublicMode();
$this->authentication->ensureCanRead();

$stats = $this->itemsDao->stats();

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/Items/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(Authentication $authentication, Configuration $confi
* json
*/
public function sync(): void {
$this->authentication->needsLoggedInOrPublicMode();
$this->authentication->ensureCanRead();

if (isset($_GET['since'])) {
$params = $_GET;
Expand Down Expand Up @@ -113,7 +113,7 @@ public function sync(): void {
* Items statuses bulk update.
*/
public function updateStatuses(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

if (isset($_POST['updatedStatuses'])) {
$updatedStatuses = $_POST['updatedStatuses'];
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Opml/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function writeSource(array $source): void {
* @note Uses the selfoss namespace to store selfoss-specific information
*/
public function export(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

$this->logger->debug('start OPML export');
$this->writer->openMemory();
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Opml/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(Authentication $authentication, Logger $logger, \dao
* @note Borrows from controllers/Sources.php:write
*/
public function add(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

http_response_code(400);

Expand Down
6 changes: 3 additions & 3 deletions src/controllers/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function __construct(Authentication $authentication, Configuration $confi
* rss feed
*/
public function rss(): void {
$this->authentication->needsLoggedInOrPublicMode();
$this->authentication->ensureCanRead();

$this->feedWriter->setTitle($this->configuration->rssTitle);
$this->feedWriter->setChannelElement('description', '');
$this->feedWriter->setSelfLink($this->view->base . 'feed');
$this->feedWriter->setSelfLink($this->view->getBaseUrl() . 'feed');

$this->feedWriter->setLink($this->view->base);
$this->feedWriter->setLink($this->view->getBaseUrl());

// get sources
$lastSourceId = 0;
Expand Down
14 changes: 7 additions & 7 deletions src/controllers/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(Authentication $authentication, \daos\Sources $sourc
* json
*/
public function show(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

// get available spouts
$spouts = $this->spoutLoader->all();
Expand All @@ -61,7 +61,7 @@ public function show(): void {
* json
*/
public function add(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

$spouts = $this->spoutLoader->all();

Expand All @@ -75,7 +75,7 @@ public function add(): void {
* json
*/
public function params(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

if (!isset($_GET['spout'])) {
$this->view->error('no spout type given');
Expand All @@ -102,7 +102,7 @@ public function params(): void {
* @param string $id ID of source to remove
*/
public function remove(string $id): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

try {
$id = Misc::forceId($id);
Expand All @@ -126,7 +126,7 @@ public function remove(string $id): void {
* json
*/
public function listSources(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

// load sources
$sources = $this->sourcesDao->getWithIcon();
Expand All @@ -145,7 +145,7 @@ public function listSources(): void {
* json
*/
public function spouts(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

$spouts = $this->spoutLoader->all();
$this->view->jsonSuccess($spouts);
Expand All @@ -156,7 +156,7 @@ public function spouts(): void {
* json
*/
public function stats(): void {
$this->authentication->needsLoggedInOrPublicMode();
$this->authentication->ensureCanRead();

// load sources
$sources = $this->sourcesDao->getWithUnread();
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Sources/Write.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(
* @param ?string $id ID of source to update, or null to create a new one
*/
public function write(?string $id = null): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

$data = $this->request->getData();

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function tagsAddColors(array $itemTags, ?array $tags = null): StringKeyed
* set tag color
*/
public function color(): void {
$this->authentication->needsLoggedIn();
$this->authentication->ensureIsPrivileged();

$data = $this->request->getData();

Expand Down Expand Up @@ -101,7 +101,7 @@ public function color(): void {
* html
*/
public function listTags(): void {
$this->authentication->needsLoggedInOrPublicMode();
$this->authentication->ensureCanRead();

$tags = $this->tagsDao->getWithUnread();

Expand Down
Loading