Debian Codegraph @

classes/Auth.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2020-2025 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains Auth class.
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg;
33
34 use Liuch\DmarcSrg\ErrorCodes;
35 use Liuch\DmarcSrg\Users\UserList;
36 use Liuch\DmarcSrg\Exception\SoftException;
37 use Liuch\DmarcSrg\Exception\AuthException;
38 use Liuch\DmarcSrg\Exception\ForbiddenException;
39
40 /**
41 * Class for working with authentication data.
42 */
43 class Auth
44 {
45 /** @var Core */
46 private $core = null;
47
48 /**
49 * The constructor
50 *
51 * @param Core $core
52 */
53 public function __construct(object $core)
54 {
55 $this->core = $core;
56 }
57
58 /**
59 * Checks if authentication is enabled.
60 *
61 * The method examines the key `password` in $admin array from the config file.
62 * It must exist and not be null.
63 *
64 * @return bool
65 */
66 public function isEnabled(): bool
67 {
68 return $this->core->config('admin/password') !== null;
69 }
70
71 /**
72 * The authetication with a username and password.
73 *
74 * This method checks the password passed in $password and creates a user session.
75 * This method throws an exception if the passed password is wrong.
76 * The password with an empty string is always wrong!
77 *
78 * @param string $username - User name.
79 * @param string $password - Password. Must not be an empty string.
80 *
81 * @return array Array with `error_code` and `message` fields.
82 */
83 public function login(string $username, string $password): array
84 {
85 $atype = $this->authenticationType();
86 if ($atype === 'password-only') {
87 $username = empty($username) ? 'admin' : '';
88 }
89 if (!empty($username)) {
90 try {
91 $user = UserList::getUserByName($username, $this->core);
92 if ($user->verifyPassword($password)) {
93 if (!$user->isEnabled()) {
94 throw new AuthException('The user is disabled. Contact the administrator');
95 }
96 $this->core->setCurrentUser($user);
97 return [
98 'error_code' => 0,
99 'message' => 'Authentication succeeded'
100 ];
101 }
102 } catch (AuthException $e) {
103 throw $e;
104 } catch (SoftException $e) {
105 }
106 }
107 throw new AuthException('Authentication failed. Try again');
108 }
109
110 /**
111 * Removes the current user's session.
112 *
113 * @return array Array with `error_code` and `message` fields.
114 */
115 public function logout(): array
116 {
117 $this->core->setCurrentUser(null);
118 return [
119 'error_code' => 0,
120 'message' => 'Logged out successfully'
121 ];
122 }
123
124 /**
125 * Checks if the user session exists.
126 *
127 * This method throws an exception if the authentication or authorization check fails.
128 *
129 * @param int $level Minimum access level required
130 *
131 * @return void
132 */
133 public function isAllowed(int $level): void
134 {
135 if ($this->isEnabled()) {
136 $user = $this->core->getCurrentUser();
137 if (!$user) {
138 throw new AuthException('Authentication needed', ErrorCodes::AUTH_NEEDED, $this->authenticationType());
139 }
140 if ($user->level() < $level) {
141 throw new ForbiddenException('Forbidden');
142 }
143 }
144 }
145
146 /**
147 * Returns the type of authentication
148 *
149 * @return string `base` - User name and password
150 * `password-only` - Password only
151 * `none` - Authentication is disabled
152 */
153 public function authenticationType(): string
154 {
155 if ($this->isEnabled()) {
156 return $this->core->config('users/user_management', false) ? 'base' : 'password-only';
157 }
158 return 'none';
159 }
160 }