Skip to content

Implemented helper for password reset #1043

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

Merged
merged 7 commits into from
Apr 28, 2025
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
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ RUN apt-get update \
&& apt-get -y install git iproute2 procps lsb-release \
&& apt-get -y install mariadb-client \
&& apt-get -y install libpng-dev \
&& apt-get -y install ssmtp \
\
# Install extensions (optional)
&& docker-php-ext-install pdo_mysql gd \
Expand Down
2 changes: 1 addition & 1 deletion doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
## Bugfixes

- Fixed a bug where creating a new preprocessor would copy the configured limit command over the configured skip command

- Implemented sending emails inside docker container

# v0.14.2 -> v0.14.3

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
restart: always
volumes:
- hashtopolis:/usr/local/share/hashtopolis:Z
# - ./ssmtp.conf:/etc/ssmtp/ssmtp.conf
environment:
HASHTOPOLIS_DB_USER: $MYSQL_USER
HASHTOPOLIS_DB_PASS: $MYSQL_PASSWORD
Expand Down
3 changes: 2 additions & 1 deletion src/api/v2/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function get($key): string {
include(dirname(__FILE__) . '/../../inc/confv2.php');
return new JwtAuthentication([
"path" => "/",
"ignore" => ["/api/v2/auth/token", "/api/v2/openapi.json"],
"ignore" => ["/api/v2/auth/token", "/api/v2/helper/resetUserPassword", "/api/v2/openapi.json"],
"secret" => $PEPPER[0],
"attribute" => false,
"secure" => false,
Expand Down Expand Up @@ -279,6 +279,7 @@ public function process(Request $request, RequestHandler $handler): Response {
require __DIR__ . "/../../inc/apiv2/helper/purgeTask.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/recountFileLines.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/resetChunk.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/resetUserPassword.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/setUserPassword.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/unassignAgent.routes.php";

Expand Down
39 changes: 39 additions & 0 deletions src/inc/apiv2/helper/resetUserPassword.routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use DBA\User;
use \Psr\Http\Message\ServerRequestInterface;

require_once(dirname(__FILE__) . "/../common/AbstractHelperAPI.class.php");

class ResetUserPasswordHelperAPI extends AbstractHelperAPI {
public static function getBaseUri(): string {
return "/api/v2/helper/resetUserPassword";
}

public static function getAvailableMethods(): array {
return ['POST'];
}

public function getRequiredPermissions(string $method): array {
return [];
}

public function preCommon(ServerRequestInterface $request): void {
// nothing, there is no user for this request as it is an unauthenticated request
}

public function getFormFields(): array {
return [
User::EMAIL => ["type" => "str"],
User::USERNAME => ["type" => "str"],
];
}

public function actionPost($data): array|null {
UserUtils::userForgotPassword($data[User::USERNAME], $data[User::EMAIL]);

return ["reset" => "success"];
}
}

ResetUserPasswordHelperAPI::register($app);
51 changes: 12 additions & 39 deletions src/inc/handlers/ForgotHandler.class.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,24 @@
<?php

use DBA\QueryFilter;
use DBA\User;
use DBA\Factory;

class ForgotHandler implements Handler {
public function __construct($configId = null) {
//we need nothing to load
}

public function handle($action) {
switch ($action) {
case DForgotAction::RESET:
$this->forgot($_POST['username'], $_POST['email']);
break;
default:
UI::addMessage(UI::ERROR, "Invalid action!");
break;
}
}

private function forgot($username, $email) {
$username = htmlentities($username, ENT_QUOTES, "UTF-8");
$qF = new QueryFilter(User::USERNAME, $username, "=");
$res = Factory::getUserFactory()->filter([Factory::FILTER => $qF]);
if ($res == null || sizeof($res) == 0) {
UI::addMessage(UI::ERROR, "No such user!");
return;
}
$user = $res[0];
if ($user->getEmail() != $email) {
UI::addMessage(UI::ERROR, "No such user!");
return;
}
$newSalt = Util::randomString(20);
$newPass = Util::randomString(10);
$newHash = Encryption::passwordHash($newPass, $newSalt);

$tmpl = new Template("email/forgot");
$tmplPlain = new Template("email/forgot.plain");
$obj = array('username' => $user->getUsername(), 'password' => $newPass);
if (Util::sendMail($user->getEmail(), "Password reset", $tmpl->render($obj), $tmplPlain->render($obj))) {
Factory::getUserFactory()->mset($user, [User::PASSWORD_HASH => $newHash, User::PASSWORD_SALT => $newSalt, User::IS_COMPUTED_PASSWORD => 1]);
UI::addMessage(UI::SUCCESS, "Password reset! You should receive an email soon.");
try {
switch ($action) {
case DForgotAction::RESET:
UserUtils::userForgotPassword($_POST['username'], $_POST['email']);
UI::addMessage(UI::SUCCESS, "Password reset! You should receive an email soon.");
break;
default:
UI::addMessage(UI::ERROR, "Invalid action!");
break;
}
}
else {
UI::addMessage(UI::ERROR, "Password reset failed because of an error when sending the email! Please check if PHP is able to send emails.");
catch (HTException $e) {
UI::addMessage(UI::ERROR, $e->getMessage());
}
}
}
26 changes: 26 additions & 0 deletions src/inc/utils/UserUtils.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ public static function deleteUser($userId, $adminUser) {
Factory::getUserFactory()->delete($user);
}

public static function userForgotPassword($username, $email) {
$username = htmlentities($username, ENT_QUOTES, "UTF-8");
$qF = new QueryFilter(User::USERNAME, $username, "=");
$res = Factory::getUserFactory()->filter([Factory::FILTER => $qF]);
if ($res == null || sizeof($res) == 0) {
throw new HTException("No such user!");
}
$user = $res[0];
if ($user->getEmail() != $email) {
throw new HTException("No such user!");
}
$newSalt = Util::randomString(20);
$newPass = Util::randomString(10);
$newHash = Encryption::passwordHash($newPass, $newSalt);

$tmpl = new Template("email/forgot");
$tmplPlain = new Template("email/forgot.plain");
$obj = array('username' => $user->getUsername(), 'password' => $newPass);
if (Util::sendMail($user->getEmail(), "Password reset", $tmpl->render($obj), $tmplPlain->render($obj))) {
Factory::getUserFactory()->mset($user, [User::PASSWORD_HASH => $newHash, User::PASSWORD_SALT => $newSalt, User::IS_COMPUTED_PASSWORD => 1]);
}
else {
throw new HTException("Password reset failed because of an error when sending the email! Please check if PHP is able to send emails.");
}
}

/**
* @param int $userId
* @throws HTException
Expand Down
20 changes: 20 additions & 0 deletions ssmtp.conf.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# The user that gets all the mails (UID < 1000, usually the admin)
[email protected]

# The mail server (where the mail is sent to)
mailhub=smtp.domain.com:465

# The address where the mail appears to come from for user authentication.
rewriteDomain=domain.com

# Use implicit TLS (port 465). When using port 587, change UseSTARTTLS=Yes
UseTLS=Yes
UseSTARTTLS=No

# Username/Password
AuthUser=username
AuthPass=password
AuthMethod=PLAIN

# Email 'From header's can override the default domain?
FromLineOverride=yes